-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from RagnarokResearchLab/334-normals-debug-vi…
…sualization Enable visualizing surface normals with "pointy" triangles (via a new DebugDraw primitive)
- Loading branch information
Showing
14 changed files
with
157 additions
and
5 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
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
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,71 @@ | ||
local Mesh = require("Core.NativeClient.WebGPU.Mesh") | ||
|
||
local table_insert = table.insert | ||
|
||
local NormalsVisualization = { | ||
TRIANGLE_WIDTH = 0.1, | ||
TRIANGLE_LENGTH = 1, | ||
color = { | ||
red = 1, | ||
green = 0x7F / 255, | ||
blue = 1, | ||
}, | ||
} | ||
function NormalsVisualization:Construct(mesh) | ||
local visualizationMesh = Mesh(mesh.displayName .. "NormalsVisualization") | ||
|
||
for index = 1, #mesh.surfaceNormals, 3 do | ||
local vertex = { | ||
x = mesh.vertexPositions[index + 0], | ||
y = mesh.vertexPositions[index + 1], | ||
z = mesh.vertexPositions[index + 2], | ||
} | ||
local surfaceNormal = { | ||
x = mesh.surfaceNormals[index + 0], | ||
y = mesh.surfaceNormals[index + 1], | ||
z = mesh.surfaceNormals[index + 2], | ||
} | ||
|
||
local nextAvailableVertexID = #visualizationMesh.vertexPositions / 3 | ||
table_insert(visualizationMesh.vertexPositions, vertex.x) | ||
table_insert(visualizationMesh.vertexPositions, vertex.y) | ||
table_insert(visualizationMesh.vertexPositions, vertex.z) | ||
table_insert(visualizationMesh.vertexPositions, vertex.x + surfaceNormal.x * self.TRIANGLE_LENGTH) | ||
-- There's no ceilings, so all flat surfaces must be facing up (normal will have a positive Y component) | ||
table_insert(visualizationMesh.vertexPositions, vertex.y + surfaceNormal.y * self.TRIANGLE_LENGTH) | ||
table_insert(visualizationMesh.vertexPositions, vertex.z + surfaceNormal.z * self.TRIANGLE_LENGTH) | ||
table_insert(visualizationMesh.vertexPositions, vertex.x - self.TRIANGLE_WIDTH) | ||
table_insert(visualizationMesh.vertexPositions, vertex.y - self.TRIANGLE_WIDTH) | ||
table_insert(visualizationMesh.vertexPositions, vertex.z - self.TRIANGLE_WIDTH) | ||
|
||
for i = 1, 3, 1 do | ||
table_insert(visualizationMesh.vertexColors, self.color.red) | ||
table_insert(visualizationMesh.vertexColors, self.color.green) | ||
table_insert(visualizationMesh.vertexColors, self.color.blue) | ||
end | ||
|
||
table_insert(visualizationMesh.triangleConnections, nextAvailableVertexID) | ||
table_insert(visualizationMesh.triangleConnections, nextAvailableVertexID + 1) | ||
table_insert(visualizationMesh.triangleConnections, nextAvailableVertexID + 2) | ||
|
||
for i = 1, 3, 1 do | ||
table_insert(visualizationMesh.diffuseTextureCoords, 0) | ||
table_insert(visualizationMesh.diffuseTextureCoords, 0) | ||
end | ||
|
||
for i = 1, 3, 1 do | ||
-- Not used (material is unlit) | ||
table_insert(visualizationMesh.surfaceNormals, 0) | ||
table_insert(visualizationMesh.surfaceNormals, 0) | ||
table_insert(visualizationMesh.surfaceNormals, 0) | ||
end | ||
end | ||
|
||
return visualizationMesh | ||
end | ||
|
||
NormalsVisualization.__call = NormalsVisualization.Construct | ||
NormalsVisualization.__index = NormalsVisualization | ||
setmetatable(NormalsVisualization, NormalsVisualization) | ||
|
||
return NormalsVisualization |
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
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
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,2 @@ | ||
-- stylua: ignore | ||
return{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71} |
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,2 @@ | ||
-- stylua: ignore | ||
return{-0.5,-0.5,-0.5,-0.5,-0.5,-1.5,-0.6,-0.6,-0.6,0.5,-0.5,-0.5,0.5,-0.5,-1.5,0.4,-0.6,-0.6,0.5,0.5,-0.5,0.5,0.5,-1.5,0.4,0.4,-0.6,-0.5,0.5,-0.5,-0.5,0.5,-1.5,-0.6,0.4,-0.6,-0.5,-0.5,0.5,-0.5,-0.5,1.5,-0.6,-0.6,0.4,0.5,-0.5,0.5,0.5,-0.5,1.5,0.4,-0.6,0.4,0.5,0.5,0.5,0.5,0.5,1.5,0.4,0.4,0.4,-0.5,0.5,0.5,-0.5,0.5,1.5,-0.6,0.4,0.4,-0.5,-0.5,-0.5,-0.5,-1.5,-0.5,-0.6,-0.6,-0.6,0.5,-0.5,-0.5,0.5,-1.5,-0.5,0.4,-0.6,-0.6,0.5,-0.5,0.5,0.5,-1.5,0.5,0.4,-0.6,0.4,-0.5,-0.5,0.5,-0.5,-1.5,0.5,-0.6,-0.6,0.4,0.5,0.5,-0.5,0.5,1.5,-0.5,0.4,0.4,-0.6,-0.5,0.5,-0.5,-0.5,1.5,-0.5,-0.6,0.4,-0.6,-0.5,0.5,0.5,-0.5,1.5,0.5,-0.6,0.4,0.4,0.5,0.5,0.5,0.5,1.5,0.5,0.4,0.4,0.4,-0.5,-0.5,-0.5,-1.5,-0.5,-0.5,-0.6,-0.6,-0.6,-0.5,0.5,-0.5,-1.5,0.5,-0.5,-0.6,0.4,-0.6,-0.5,0.5,0.5,-1.5,0.5,0.5,-0.6,0.4,0.4,-0.5,-0.5,0.5,-1.5,-0.5,0.5,-0.6,-0.6,0.4,0.5,-0.5,-0.5,1.5,-0.5,-0.5,0.4,-0.6,-0.6,0.5,0.5,-0.5,1.5,0.5,-0.5,0.4,0.4,-0.6,0.5,0.5,0.5,1.5,0.5,0.5,0.4,0.4,0.4,0.5,-0.5,0.5,1.5,-0.5,0.5,0.4,-0.6,0.4} |
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,2 @@ | ||
-- stylua: ignore | ||
return{0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,0} |
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,2 @@ | ||
-- stylua: ignore | ||
return{0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,0} |
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,2 @@ | ||
-- stylua: ignore | ||
return {0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,0} |
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
42 changes: 42 additions & 0 deletions
42
Tests/NativeClient/DebugDraw/NormalsVisualization.spec.lua
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,42 @@ | ||
local Box = require("Core.NativeClient.DebugDraw.Box") | ||
local NormalsVisualization = require("Core.NativeClient.DebugDraw.NormalsVisualization") | ||
|
||
local function table_fill(value, count) | ||
local filledTable = table.new(count, 0) | ||
for index = 1, count, 1 do | ||
filledTable[index] = value | ||
end | ||
return filledTable | ||
end | ||
|
||
describe("NormalsVisualization", function() | ||
describe("Construct", function() | ||
it("should return a new mesh that includes triangles aligned with all normals of the input mesh", function() | ||
local box = Box() | ||
local visualizationMesh = NormalsVisualization(box) | ||
|
||
assertEquals(visualizationMesh.displayName, "BoxNormalsVisualization") | ||
assertEquals( | ||
visualizationMesh.vertexPositions, | ||
require("Tests.Fixtures.Snapshots.normals-visualization-positions") | ||
) | ||
assertEquals( | ||
visualizationMesh.triangleConnections, | ||
require("Tests.Fixtures.Snapshots.normals-visualization-indices") | ||
) | ||
|
||
local expectedColors = {} | ||
for index = 1, #visualizationMesh.vertexPositions / 3, 1 do | ||
table.insert(expectedColors, NormalsVisualization.color.red) | ||
table.insert(expectedColors, NormalsVisualization.color.green) | ||
table.insert(expectedColors, NormalsVisualization.color.blue) | ||
end | ||
assertEquals(visualizationMesh.vertexColors, expectedColors) | ||
assertEquals( | ||
visualizationMesh.diffuseTextureCoords, | ||
table_fill(0, 2 * #visualizationMesh.vertexPositions / 3) | ||
) | ||
assertEquals(visualizationMesh.surfaceNormals, table_fill(0, 3 * #visualizationMesh.vertexPositions / 3)) | ||
end) | ||
end) | ||
end) |
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
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