Skip to content

Commit 7b8a5f4

Browse files
committed
Vox loading and ambient occlusion.
1 parent 3e6db29 commit 7b8a5f4

22 files changed

+439
-97
lines changed

classes/materials/blocksinformationfragmentshader.glsl

+3-11
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,7 @@ vec3 getPositionForTextureCoordinates(vec2 textureCoordinates) {
2626

2727
void main() {
2828
vec3 blockPosition = getPositionForTextureCoordinates(vUv);
29-
30-
vec3 neighborPosition = blockPosition;
31-
neighborPosition.y = mod(neighborPosition.y + 1.0, worldSize.y);
32-
33-
if (isValidPosition(neighborPosition)) {
34-
vec2 neighborBlockCoordinates = getTextureCoordinatesForPosition(neighborPosition);
35-
vec4 color = texture2D(blocksInformation, neighborBlockCoordinates);
36-
gl_FragColor = color;
37-
} else {
38-
gl_FragColor = vec4(0.0);
39-
}
29+
vec2 blockCoordinates = getTextureCoordinatesForPosition(blockPosition);
30+
vec4 color = texture2D(blocksInformation, blockCoordinates);
31+
gl_FragColor = color;
4032
}

classes/materials/voxelworld-common.glsl

+5
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ bool isValidPosition(vec3 position) {
1616
position.y >= 0.0 && position.y < worldSize.x &&
1717
position.z >= 0.0 && position.z < worldSize.x);
1818
}
19+
20+
int getBlockMaterialForPosition(vec3 position) {
21+
if (!isValidPosition(position)) return 0;
22+
return int(texture2D(blocksInformation, getTextureCoordinatesForPosition(position)).a * 255.0);
23+
}

classes/materials/voxelworld-discardinvisible-vertex.glsl

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ if (blockMaterial == 0) {
88
return;
99
}
1010

11-
// See if the neighbor towards the vertex is facing is empty.
11+
// See if the neighbor towards the vertex is facing is also full.
1212
vec3 neighborPosition = blockPosition + normal;
13-
if (isValidPosition(neighborPosition)) {
14-
int neighborBlockMaterial = int(texture2D(blocksInformation, getTextureCoordinatesForPosition(neighborPosition)).a * 255.0);
15-
if (neighborBlockMaterial > 0) {
16-
gl_Position = vec4(0);
17-
return;
18-
}
13+
int neighborBlockMaterial = getBlockMaterialForPosition(neighborPosition);
14+
if (neighborBlockMaterial > 0) {
15+
// No need to draw this face since it's inside the model.
16+
gl_Position = vec4(0);
17+
return;
1918
}

classes/materials/voxelworld-fragment.glsl

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
12
varying vec3 vMaterialDiffuse;
23
varying vec3 vMaterialEmmisive;
34

45
varying vec3 vLightFront;
56
varying vec3 vIndirectFront;
7+
varying vec3 vIndirectFactor;
68

79
#include <common>
810
#include <packing>
@@ -19,8 +21,9 @@ void main() {
1921

2022
reflectedLight.indirectDiffuse = getAmbientLightIrradiance(ambientLightColor);
2123
reflectedLight.indirectDiffuse += vIndirectFront;
22-
2324
reflectedLight.indirectDiffuse *= BRDF_Diffuse_Lambert(diffuseColor.rgb);
25+
reflectedLight.indirectDiffuse *= vIndirectFactor;
26+
2427
reflectedLight.directDiffuse = vLightFront;
2528
reflectedLight.directDiffuse *= BRDF_Diffuse_Lambert(diffuseColor.rgb) * getShadowMask();
2629

classes/materials/voxelworld-vertex.glsl

+16
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ uniform vec2 materialDataSize;
66

77
varying vec3 vMaterialDiffuse;
88
varying vec3 vMaterialEmmisive;
9+
910
varying vec3 vLightFront;
1011
varying vec3 vIndirectFront;
12+
varying vec3 vIndirectFactor;
1113

1214
#include <common>
1315
#include <lights_pars_begin>
@@ -31,4 +33,18 @@ void main() {
3133
vec2 blockMaterialColorCoordinates = vec2((float(blockMaterial) + 0.5) / materialDataSize.x, (float(paletteRow) + 0.5) / materialDataSize.y);
3234
vMaterialDiffuse = texture2D(materialData, blockMaterialColorCoordinates).rgb;
3335
vMaterialEmmisive = vec3(0.0);
36+
37+
// Calculate ambient occlusion.
38+
float shadowBlockCount = 0.0;
39+
vec3 shadowSamplePosition = position;
40+
41+
if (getBlockMaterialForPosition(shadowSamplePosition) > 0) {shadowBlockCount++;}
42+
shadowSamplePosition.x--;
43+
if (getBlockMaterialForPosition(shadowSamplePosition) > 0) {shadowBlockCount++;}
44+
shadowSamplePosition.z++;
45+
if (getBlockMaterialForPosition(shadowSamplePosition) > 0) {shadowBlockCount++;}
46+
shadowSamplePosition.x++;
47+
if (getBlockMaterialForPosition(shadowSamplePosition) > 0) {shadowBlockCount++;}
48+
49+
vIndirectFactor = vec3(1.0 - shadowBlockCount / 4.0);
3450
}

classes/projectgaia.coffee

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ProjectGaia
2929
directionalLight.shadow.mapSize.width = 4096
3030
directionalLight.shadow.mapSize.height = 4096
3131
directionalLight.shadow.bias = -0.001
32-
directionalLight.position.set 10, 50, 20
32+
directionalLight.position.set 30, 50, 40
3333
@scene.add directionalLight
3434

3535
# Create loading manager
@@ -46,21 +46,21 @@ class ProjectGaia
4646
# Load assets.
4747
ProjectGaia.Materials.VoxelWorld.load @loadingManager
4848
ProjectGaia.Materials.BlocksInformation.load @loadingManager
49+
ProjectGaia.VoxelWorld.load @loadingManager
4950

5051
initialize: ->
51-
sideLength = 32
52-
52+
model = ProjectGaia.VoxelWorld.environmentModel
53+
5354
@worldSize =
54-
width: sideLength
55-
height: sideLength
56-
depth: sideLength
55+
width: model.width
56+
height: model.height
57+
depth: model.depth
5758

5859
groundGeometry = new THREE.PlaneBufferGeometry(@worldSize.width + 100, @worldSize.depth + 100)
5960
groundMaterial = new THREE.MeshLambertMaterial color: 0x808080
6061

6162
ground = new THREE.Mesh groundGeometry, groundMaterial
6263
ground.receiveShadow = true
63-
ground.position.y = -@worldSize.height / 2
6464
ground.rotation.x = -Math.PI / 2
6565
@scene.add ground
6666

@@ -100,12 +100,12 @@ class ProjectGaia
100100
@voxelMesh.castShadow = true
101101
@voxelMesh.receiveShadow = true
102102
@voxelMesh.customDepthMaterial = @voxelWorldDepthMaterial
103-
@voxelMesh.position.set -@worldSize.width / 2, -@worldSize.height / 2, -@worldSize.depth / 2
103+
@voxelMesh.position.set -@worldSize.width / 2, 0, -@worldSize.depth / 2
104104
@scene.add @voxelMesh
105105

106106
# Create the camera.
107-
@camera = new THREE.PerspectiveCamera 60, window.innerWidth / window.innerHeight, 1, 400
108-
@camera.position.z = @worldSize.depth * 2
107+
@camera = new THREE.PerspectiveCamera 45, window.innerWidth / window.innerHeight, 1, 400
108+
@camera.position.set 0, @worldSize.height / 2, @worldSize.depth * 1.5
109109

110110
@controls = new THREE.OrbitControls @camera, @renderer.domElement
111111

classes/projectgaia.js

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/projectgaia.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/voxelmodel.coffee

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class ProjectGaia.VoxelModel
2+
constructor: (@options) ->
3+
@options.loadingManager.itemStart @options.url
4+
5+
parser = new vox.Parser()
6+
parser.parse(@options.url).then (data) =>
7+
console.log "loaded vox", data, @
8+
9+
@width = data.size.x
10+
@height = data.size.z
11+
@depth = data.size.y
12+
13+
@sizeVector = new THREE.Vector3().copy data.size
14+
15+
@colors = for color in data.palette
16+
new THREE.Color().setIntegerRGB color
17+
18+
@blocks = []
19+
20+
for x in [0...@width]
21+
@blocks[x] = []
22+
23+
for y in [0...@height]
24+
@blocks[x][y] = []
25+
26+
for z in [0...@depth]
27+
@blocks[x][y][z] = type: 0, material: 0
28+
29+
for voxel in data.voxels
30+
if materialIndex = ProjectGaia.VoxelWorld.getMaterialIndexForColor @colors[voxel.colorIndex]
31+
materialProperties = ProjectGaia.VoxelWorld.BlockMaterialProperties[materialIndex]
32+
33+
@blocks[voxel.x][voxel.z][@depth - 1 - voxel.y] =
34+
material: materialIndex
35+
type: materialProperties.blockType
36+
37+
else
38+
console.warn "No material assigned to color", data.palette[voxel.colorIndex]
39+
40+
@options.loadingManager.itemEnd @options.url

0 commit comments

Comments
 (0)