-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
2,223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// shaders | ||
var vertCode = ` | ||
attribute vec3 coords; | ||
attribute vec4 texCoord; | ||
attribute vec4 color; | ||
varying vec4 v_color; | ||
varying vec2 v_texCoord; | ||
varying float v_fog; | ||
varying vec2 v_light; | ||
uniform mat4 u_proj; | ||
uniform mat4 u_view; | ||
uniform mat4 u_model; | ||
uniform float u_timer; | ||
void main(void) { | ||
v_color = color*vec4(1.0,0.9,0.8,1.0); | ||
float t = 1.0 - v_color.a; | ||
v_color.a = 1.0; | ||
vec4 modelPos = u_model * vec4(coords, 1.0); | ||
modelPos.x += sin((u_timer + modelPos.x * 0.2)*2.0) * 0.2 * t; | ||
vec4 position = u_proj * u_view * modelPos; | ||
v_fog = min(1.0, position.z*0.005); | ||
v_texCoord = texCoord.xy; | ||
v_light = texCoord.ba; | ||
gl_Position = position; | ||
}`; | ||
|
||
var fragCode = ` | ||
varying mediump vec4 v_color; | ||
varying mediump vec2 v_texCoord; | ||
varying mediump float v_fog; | ||
varying mediump vec2 v_light; | ||
uniform sampler2D u_texture0; | ||
uniform highp float u_timer; | ||
uniform mediump float u_saturation; | ||
uniform mediump vec3 u_fogColor; | ||
void main(void) { | ||
mediump float t = v_fog; | ||
mediump vec4 texColor = texture2D(u_texture0, v_texCoord); | ||
if (texColor.a < 0.5) | ||
discard; | ||
mediump float s = 1.0;//sin(u_timer * 0.1) * 0.5 + 0.5; | ||
mediump vec4 skyLight = vec4(v_light.y*s,v_light.y*s,v_light.y*s,1.0); | ||
mediump vec4 color = v_color * (vec4(v_light.x,v_light.x*0.7,v_light.x*0.4, 1.0)+skyLight) * texColor * (1.0-t) + vec4(u_fogColor*t, v_color.a); | ||
mediump float mid = (color.r+color.g+color.b)*0.333333; | ||
mid *= 1.0-(1.0-pow(max(1.0, v_light.x+v_light.y*s),1.0)); | ||
color.r = color.r * u_saturation + mid * (1.0-u_saturation); | ||
color.g = color.g * u_saturation + mid * (1.0-u_saturation); | ||
color.b = color.b * u_saturation + mid * (1.0-u_saturation); | ||
gl_FragColor = color; | ||
}`; | ||
|
||
|
||
var atlas,noise_texture,blank_texture,border_texture; | ||
function initialize_assets() { | ||
// textures | ||
var res = 16; | ||
atlas = new Atlas(res, [ | ||
tex_noise_rgb(res,res, 0.05, 0.2), | ||
tex_noise_rgb(res,res, 0.5, 0.5), | ||
create_test_texture(res,res), | ||
create_test_texture(res,res), | ||
tex_grass(res,res), | ||
create_border_texture(res,res), | ||
create_leaves_texture(res,res, 0.05, 0.5, 0.33), | ||
]); | ||
noise_texture = atlas.texture;//new Texture(tex_grass(res,res), res,res, gl.RGBA);//new Texture(noise_rgb(res,res, 0.05, 0.2), res,res, gl.RGB); | ||
blank_texture = new Texture(create_test_texture(4,4), 4,4, gl.RGBA); | ||
border_texture = new Texture(create_border_texture(32,32), 32,32, gl.RGBA); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
function Chunk(x,y,z, w,h,d){ | ||
this.w = w; | ||
this.h = h; | ||
this.d = d; | ||
this.x = x; | ||
this.y = y; | ||
this.z = z; | ||
this.voxels = new Uint8Array(w*h*d); | ||
this.lightmap = new Lightmap(); | ||
this.modified = true; | ||
for (let y = 0; y < h; y++){ | ||
for (let z = 0; z < d; z++){ | ||
for (let x = 0; x < w; x++){ | ||
//if (y <= (noise_oct((x+this.x*w)*0.01, 2)+noise_oct((z+this.z*d)*0.01+345.3245, 2))*10+20) | ||
// this.voxels[(y*d+z)*w+x] = 1; | ||
let gx = x + this.x * w; | ||
let gy = y + this.y * h; | ||
let gz = z + this.z * d; | ||
|
||
let upper = (noise_oct((x+this.x*w)*0.025, 2)+noise_oct((gy+1)*0.08+8553.643, 2)+noise_oct((z+this.z*d)*0.025+343.5, 2))*((gy+1)*0.01 + 1.0); | ||
let noise = (noise_oct((x+this.x*w)*0.025, 2)+noise_oct(gy*0.08+8553.643, 2)+noise_oct((z+this.z*d)*0.025+343.5, 2))*(gy*0.01 + 1.0); | ||
|
||
let lx = (gx - 8*w) / (8*w); | ||
let lz = (gz - 8*d) / (8*d); | ||
let length = Math.sqrt(Math.sqrt(lx*lx + lz*lz)); | ||
noise -= length; | ||
upper -= length; | ||
if (y*0.05 < noise){ | ||
this.voxels[(y*d+z)*w+x] = 6; | ||
if (y*0.05 >= upper-0.1) | ||
this.voxels[(y*d+z)*w+x] = 1; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
Chunk.prototype.isBlocked = function(x,y,z, group){ | ||
let w = this.w; | ||
let h = this.h; | ||
let d = this.d; | ||
if (x < 0 || y < 0 || z < 0 || x >= w || y >= h || z >= d) | ||
return false; | ||
return bricks[this.voxels[(y*d+z)*w+x]].drawGroup == group; | ||
} | ||
|
||
Chunk.prototype.set = function(x,y,z, id){ | ||
let w = this.w; | ||
let h = this.h; | ||
let d = this.d; | ||
if (x < 0 || y < 0 || z < 0 || x == w || y == h || z == d) | ||
return; | ||
this.voxels[(y*d+z)*w+x] = id; | ||
this.modified = true; | ||
} | ||
|
||
Chunk.prototype.get = function(x,y,z){ | ||
if (x < 0 || y < 0 || z < 0 || x == this.w || y == this.h || z == this.d) | ||
return 0; | ||
return this.voxels[(y*this.d+z)*this.w+x]; | ||
} | ||
|
||
Chunk.prototype.getLight = function(x,y,z, channel){ | ||
if (x < 0 || y < 0 || z < 0 || x == this.w || y == this.h || z == this.d) | ||
return 0; | ||
return this.lightmap.get(x,y,z, channel); | ||
} | ||
|
||
function renderChunk(chunk, chunks, bricks, batch, dsBatch){ | ||
for (let y = 0; y < chunk.h; y++){ | ||
for (let z = 0; z < chunk.d; z++){ | ||
for (let x = 0; x < chunk.w; x++){ | ||
let gx = x + chunk.x * chunk.w; | ||
let gy = y + chunk.y * chunk.h; | ||
let gz = z + chunk.z * chunk.d; | ||
let voxel = chunk.voxels[(y*chunk.d+z)*chunk.w+x]; | ||
let brick = bricks[voxel]; | ||
let color = brick.color; | ||
let l = 1.0; | ||
/*for (let e = gy+1; e < chunk.h; e++){ | ||
if (chunks.get(gx,e,gz)){ | ||
l = 0.75; | ||
break; | ||
} | ||
}*/ | ||
if (voxel > 0){ | ||
if (brick.emission > 0) | ||
l *= 2.0; | ||
/*if (brick.emission > 0){ | ||
batch.box(x+0.5,y+0.5,z+0.5, 1,1,1, 1,1,1,1, | ||
chunks.isBlocked(gx+1,gy,gz), | ||
chunks.isBlocked(gx-1,gy,gz), | ||
chunks.isBlocked(gx,gy,gz+1), | ||
chunks.isBlocked(gx,gy,gz-1), | ||
chunks.isBlocked(gx,gy+1,gz), | ||
chunks.isBlocked(gx,gy-1,gz)); | ||
} else {*/ | ||
if (brick.type == 1) | ||
dsBatch.grass(gx,gy,gz, x+0.5,y+0.5,z+0.5, 1,1,1, color[0]*l,color[1]*l,color[2]*l,1,chunks, brick); | ||
else | ||
batch.block(gx,gy,gz, x+0.5,y+0.5,z+0.5, 1,1,1, color[0]*l,color[1]*l,color[2]*l,1,chunks, brick); | ||
//} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function Chunks(ox,oy, w,h){ | ||
this.w = w; | ||
this.h = h; | ||
this.ox = ox; | ||
this.oy = oy; | ||
this.chunks = new Array(w*h); | ||
this.chunks_second = new Array(w*h); | ||
this.meshes = new Array(w*h); | ||
this.meshes_second = new Array(w*h); | ||
for (let y = 0; y < h; y++){ | ||
for (let x = 0; x < w; x++){ | ||
this.chunks[y * w + x] = new Chunk(x+ox,0,y+oy, CW,CH,CD); | ||
} | ||
} | ||
this.meshes.fill(null); | ||
} | ||
|
||
Chunks.prototype.isBlocked = function(x,y,z, group){ | ||
let cx = Math.floor(x / CW); | ||
let cy = Math.floor(y / CH); | ||
let cz = Math.floor(z / CD); | ||
if (cx < this.ox || cy < 0 || cz < this.oy || cx >= this.ox+this.w || cy >= 1 || cz >= this.oy+this.d) | ||
return false; | ||
let chunk = this.chunks[cz * this.w + cx]; | ||
if (chunk == null) | ||
return false; | ||
return chunk.isBlocked(x - cx * CW, y - cy * CH, z - cz * CD, group); | ||
} | ||
|
||
Chunks.prototype.get = function(x,y,z){ | ||
let cx = Math.floor(x / CW); | ||
let cy = Math.floor(y / CH); | ||
let cz = Math.floor(z / CD); | ||
if (cx < this.ox || cy < 0 || cz < this.oy || cx >= this.ox+this.w || cy >= this.h || cz >= this.oy+this.d) | ||
return null; | ||
let chunk = this.chunks[cz * this.w + cx]; | ||
if (chunk == null) | ||
return null; | ||
return chunk.get(x - cx * CW, y - cy * CH, z - cz * CD); | ||
} | ||
|
||
Chunks.prototype.isSolid = function(x,y,z){ | ||
let id = this.get(x,y,z); | ||
if (id == null) | ||
return true; | ||
return bricks[id].solid; | ||
} | ||
|
||
Chunks.prototype.getLight = function(x,y,z,channel){ | ||
let cx = Math.floor(x / CW); | ||
let cy = Math.floor(y / CH); | ||
let cz = Math.floor(z / CD); | ||
if (cx < this.ox || cy < 0 || cz < this.oy || cx >= this.ox+this.w || cy >= this.h || cz >= this.oy+this.d) | ||
return 0; | ||
let chunk = this.chunks[cz * this.w + cx]; | ||
if (chunk == null) | ||
return 0; | ||
return chunk.lightmap.get(x - cx * CW, y - cy * CH, z - cz * CD, channel); | ||
} | ||
|
||
Chunks.prototype.set = function(x,y,z, id){ | ||
let cx = Math.floor(x / CW); | ||
let cy = Math.floor(y / CH); | ||
let cz = Math.floor(z / CD); | ||
if (cx < this.ox || cy < 0 || cz < this.oy || cx >= this.ox+this.w || cy >= this.h || cz >= this.oy+this.d) | ||
return; | ||
let chunk = this.chunks[cz * this.w + cx]; | ||
if (chunk == null) | ||
return; | ||
chunk.set(x - cx * CW, y - cy * CH, z - cz * CD, id); | ||
} | ||
|
||
Chunks.prototype.setLight = function(x,y,z, channel, value){ | ||
let cx = Math.floor(x / CW); | ||
let cy = Math.floor(y / CH); | ||
let cz = Math.floor(z / CD); | ||
if (cx < this.ox || cy < 0 || cz < this.oy || cx >= this.ox+this.w || cy >= this.h || cz >= this.oy+this.d) | ||
return; | ||
let chunk = this.chunks[cz * this.w + cx]; | ||
if (chunk == null) | ||
return; | ||
chunk.modified = true; | ||
chunk.lightmap.set(x - cx * CW, y - cy * CH, z - cz * CD, channel, value); | ||
} | ||
|
||
Chunks.prototype.setModified = function(x,y,z){ | ||
let cx = Math.floor(x / CW); | ||
let cy = Math.floor(y / CH); | ||
let cz = Math.floor(z / CD); | ||
if (cx < this.ox || cy < 0 || cz < this.oy || cx >= this.ox+this.w || cy >= this.h || cz >= this.oy+this.d) | ||
return; | ||
let chunk = this.chunks[cz * this.w + cx]; | ||
if (chunk == null) | ||
return; | ||
chunk.modified = true; | ||
} | ||
|
||
Chunks.prototype.raycast = function(sx,sy,sz, dx,dy,dz, step, steps){ | ||
let x = sx; | ||
let y = sy; | ||
let z = sz; | ||
let px = sx; | ||
let py = sy; | ||
let pz = sz; | ||
let length = 0.0; | ||
for (; length < steps*step; length += step){ | ||
x += step * dx; | ||
y += step * dy; | ||
z += step * dz; | ||
let ix = Math.floor(x); | ||
let iy = Math.floor(y); | ||
let iz = Math.floor(z); | ||
if (this.get(ix,iy,iz) > 0){ | ||
let ipx = Math.floor(px); | ||
let ipy = Math.floor(py); | ||
let ipz = Math.floor(pz); | ||
return {'px':px, 'py':py, 'pz':pz, | ||
'x':x, 'y':y, 'z':z, 'length':length, | ||
'ix':ix, 'iy':iy, 'iz':iz, | ||
'ipx':ipx, 'ipy':ipy, 'ipz':ipz} | ||
} | ||
px = x; | ||
py = y; | ||
pz = z; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function Atlas(res, textures){ | ||
let sqrt = Math.ceil(Math.sqrt(textures.length)); | ||
sqrt = 16; | ||
let size = sqrt * res; | ||
|
||
this.size = size; | ||
this.sqrt = sqrt; | ||
this.res = res; | ||
var data = new Uint8Array(size*size*4); | ||
for (let i = 0; i < textures.length; i++){ | ||
let sub = textures[i]; | ||
let x = i % sqrt; | ||
let y = Math.floor(i / sqrt); | ||
x *= res; | ||
y *= res; | ||
for (let y0 = 0; y0 < res; y0++){ | ||
for (let x0 = 0; x0 < res; x0++){ | ||
data[((y + y0) * size + (x + x0)) * 4 + 0] = sub[(y0*res+x0) * 4 + 0]; | ||
data[((y + y0) * size + (x + x0)) * 4 + 1] = sub[(y0*res+x0) * 4 + 1]; | ||
data[((y + y0) * size + (x + x0)) * 4 + 2] = sub[(y0*res+x0) * 4 + 2]; | ||
data[((y + y0) * size + (x + x0)) * 4 + 3] = sub[(y0*res+x0) * 4 + 3]; | ||
} | ||
} | ||
} | ||
this.texture = new Texture(data, size,size, gl.RGBA); | ||
} |
Oops, something went wrong.