Created
July 30, 2020 11:11
-
-
Save nshen/7086c0d33c7c901a54f71f669a517de9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
computeVertexNormals() { | |
const positionAttribute = this.attributes['position']; | |
if (positionAttribute !== undefined) { | |
this.addAttribute('normal', { size: 3, data: new Float32Array(positionAttribute.count * 3) }); | |
let normalAttribute = this.attributes['normal']; | |
const pA = new Vec3(), pB = new Vec3(), pC = new Vec3(); | |
const nA = new Vec3(), nB = new Vec3(), nC = new Vec3(); | |
const cb = new Vec3(), ab = new Vec3(); | |
// indexed elements | |
const index = this.attributes['index']; | |
if (index) { | |
let vA, vB, vC; | |
for (let i = 0, il = index.count; i < il; i += 3) { | |
vA = index.data[i]; | |
vB = index.data[i + 1]; | |
vC = index.data[i + 2]; | |
// pA.fromBufferAttribute(positionAttribute, vA); | |
// pB.fromBufferAttribute(positionAttribute, vB); | |
// pC.fromBufferAttribute(positionAttribute, vC); | |
pA.fromArray(positionAttribute.data, vA * positionAttribute.size); | |
pB.fromArray(positionAttribute.data, vB * positionAttribute.size); | |
pC.fromArray(positionAttribute.data, vC * positionAttribute.size); | |
// cb.subVectors(pC, pB); | |
// ab.subVectors(pA, pB); | |
cb.sub(pC, pB); | |
ab.sub(pA, pB); | |
// cb.cross(ab); | |
cb.cross(ab); | |
// nA.fromBufferAttribute(normalAttribute, vA); | |
// nB.fromBufferAttribute(normalAttribute, vB); | |
// nC.fromBufferAttribute(normalAttribute, vC); | |
nA.fromArray(normalAttribute.data, vA * normalAttribute.size); | |
nB.fromArray(normalAttribute.data, vB * normalAttribute.size); | |
nC.fromArray(normalAttribute.data, vC * normalAttribute.size); | |
// nA.add(cb); | |
// nB.add(cb); | |
// nC.add(cb); | |
nA.add(cb); | |
nB.add(cb); | |
nC.add(cb); | |
// normalAttribute.setXYZ(vA, nA.x, nA.y, nA.z); | |
// normalAttribute.setXYZ(vB, nB.x, nB.y, nB.z); | |
// normalAttribute.setXYZ(vC, nC.x, nC.y, nC.z); | |
vA *= normalAttribute.size; | |
normalAttribute.data[vA] = nA.x; | |
normalAttribute.data[vA + 1] = nA.y; | |
normalAttribute.data[vA + 2] = nA.z; | |
vB *= normalAttribute.size; | |
normalAttribute.data[vB] = nB.x; | |
normalAttribute.data[vB + 1] = nB.y; | |
normalAttribute.data[vB + 2] = nB.z; | |
vC *= normalAttribute.size; | |
normalAttribute.data[vC] = nC.x; | |
normalAttribute.data[vC + 1] = nC.y; | |
normalAttribute.data[vC + 2] = nC.z; | |
} | |
} else { | |
// non-indexed elements (unconnected triangle soup) | |
// for (let i = 0, il = positionAttribute.count; i < il; i += 3) { | |
// pA.fromBufferAttribute(positionAttribute, i + 0); | |
// pB.fromBufferAttribute(positionAttribute, i + 1); | |
// pC.fromBufferAttribute(positionAttribute, i + 2); | |
// cb.subVectors(pC, pB); | |
// ab.subVectors(pA, pB); | |
// cb.cross(ab); | |
// normalAttribute.setXYZ(i + 0, cb.x, cb.y, cb.z); | |
// normalAttribute.setXYZ(i + 1, cb.x, cb.y, cb.z); | |
// normalAttribute.setXYZ(i + 2, cb.x, cb.y, cb.z); | |
// } | |
} | |
this.normalizeNormals(); | |
normalAttribute.needsUpdate = true; | |
} | |
} | |
normalizeNormals() { | |
const normals = this.attributes.normal; | |
for (let i = 0, il = normals.count; i < il; i++) { | |
// _vector.fromBufferAttribute(normals, i); | |
tempVec3.fromArray(normals.data, i * normals.size); | |
tempVec3.normalize(); | |
// normals.setXYZ(i, _vector.x, _vector.y, _vector.z); | |
normals.data[i * normals.size] = tempVec3.x; | |
normals.data[i * normals.size + 1] = tempVec3.y; | |
normals.data[i * normals.size + 2] = tempVec3.z; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment