Get the normals sorted out; enable lighting from multiple lights

This commit is contained in:
Eryn Wells 2018-11-23 08:15:19 -07:00
parent a72309ec09
commit e1ec7701ba
3 changed files with 50 additions and 36 deletions

View file

@ -74,7 +74,7 @@ kernel void updateGeometryNormals(constant packed_float3 *meshPositions [[buffer
/// *----*----* (2, 2)
///
/// Adding each vector divided by n would be better, but numerical stability
/// isn't low risk here since these vectors are all normalized.
/// is low risk here since these vectors are all normalized.
///
kernel void updateGeometryVertexNormals(constant packed_float3 *meshPositions [[buffer(GeneratorBufferIndexMeshPositions)]],
constant packed_float3 *faceNormals [[buffer(GeneratorBufferIndexFaceNormals)]],
@ -87,13 +87,13 @@ kernel void updateGeometryVertexNormals(constant packed_float3 *meshPositions [[
float3 normal = float3();
uint adjacent = 0;
if (tid.y > 0 && tid.x > 0) {
if (tid.x > 0 && tid.y > 0) {
uint aIndex = 2 * segmentIndex(uint2(tid.x - 1, tid.y - 1), segs) + 1;
normal += faceNormals[aIndex];
adjacent += 1;
}
if (tid.y > 0 && tid.x < segs.x) {
uint segment = segmentIndex(uint2(tid.x, tid.y - 1), segs);
if (tid.x > 0 && tid.y < segs.y) {
uint segment = segmentIndex(uint2(tid.x - 1, tid.y), segs);
uint bIndex = 2 * segment;
uint cIndex = 2 * segment + 1;
normal += faceNormals[bIndex] + faceNormals[cIndex];
@ -104,8 +104,8 @@ kernel void updateGeometryVertexNormals(constant packed_float3 *meshPositions [[
normal += faceNormals[dIndex];
adjacent += 1;
}
if (tid.x > 0 && tid.y < segs.y) {
uint segment = segmentIndex(uint2(tid.x - 1, tid.y), segs);
if (tid.x < segs.x && tid.y > 0) {
uint segment = segmentIndex(uint2(tid.x, tid.y - 1), segs);
uint eIndex = 2 * segment + 1;
uint fIndex = 2 * segment;
normal += faceNormals[eIndex] + faceNormals[fIndex];
@ -114,7 +114,7 @@ kernel void updateGeometryVertexNormals(constant packed_float3 *meshPositions [[
if (adjacent != 0) {
normal = normalize(normal / float(adjacent));
uint idx = segmentIndex(tid, segs);
uint idx = segmentIndex(tid, segs) + 1;
vertexNormals[idx] = normal;
}
}