Permalink
Browse files

Merge pull request #7 from shama/ao

Compute vertex colors for ambient occlusion.
  • Loading branch information...
2 parents 36549f1 + 871e678 commit 198d2e8344efd7d8588b6e85bdfa7213737b5db4 @maxogden committed Mar 21, 2013
Showing with 19 additions and 2 deletions.
  1. +19 −2 index.js
View
@@ -30,18 +30,35 @@ function Mesh(data, mesher, scaleFactor, three) {
if (q.length === 5) {
var f = new this.THREE.Face4(q[0], q[1], q[2], q[3])
f.color = new this.THREE.Color(q[4])
- f.vertexColors = [f.color,f.color,f.color,f.color]
geometry.faces.push(f)
} else if (q.length == 4) {
var f = new this.THREE.Face3(q[0], q[1], q[2])
f.color = new this.THREE.Color(q[3])
- f.vertexColors = [f.color,f.color,f.color]
geometry.faces.push(f)
}
}
geometry.computeFaceNormals()
+ // compute vertex colors for ambient occlusion
+ var light = new THREE.Color(0xffffff)
+ var shadow = new THREE.Color(0x505050)
+ for (var i = 0; i < geometry.faces.length; ++i) {
+ var face = geometry.faces[i]
+ // facing up
+ if (face.normal.y === 1) face.vertexColors = [light, light, light, light]
+ // facing down
+ else if (face.normal.y === -1) face.vertexColors = [shadow, shadow, shadow, shadow]
+ // facing right
+ else if (face.normal.x === 1) face.vertexColors = [shadow, light, light, shadow]
+ // facing left
+ else if (face.normal.x === -1) face.vertexColors = [shadow, shadow, light, light]
+ // facing backward
+ else if (face.normal.z === 1) face.vertexColors = [shadow, shadow, light, light]
+ // facing forward
+ else face.vertexColors = [shadow, light, light, shadow]
+ }
+
geometry.verticesNeedUpdate = true
geometry.elementsNeedUpdate = true
geometry.normalsNeedUpdate = true

0 comments on commit 198d2e8

Please sign in to comment.