annotate tests/test_mesh.nim @ 353:61c5d5fe9d93

add: multi-material for meshes
author Sam <sam@basx.dev>
date Wed, 20 Sep 2023 22:35:04 +0700
parents b83b3a1ccb05
children f054b8bacab8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
1 import std/algorithm
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
2 import std/sequtils
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
3 import std/tables
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
4 import semicongine
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
5
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
6 proc main() =
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
7 # var myScene = Scene(name: "hi", meshes: @[rect()])
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
8 # myScene.meshes[0].transform = translate3d(0.2'f32, 0'f32, 0'f32)
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
9 # myScene.root[0].transform = translate3d(0'f32, 0.2'f32, 0'f32)
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
10 var scenes = [
268
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
11 # loadScene("default_cube.glb", "1"),
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
12 # loadScene("default_cube1.glb", "3"),
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
13 # loadScene("default_cube2.glb", "4"),
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
14 # loadScene("flat.glb", "5"),
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
15 Scene(name: "Donut", meshes: loadMeshes("tutorialk-donat.glb")[0].toSeq),
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
16 # myScene,
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
17 # loadScene("personv3.glb", "2"),
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
18 ]
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
19
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
20 var engine = initEngine("Test meshes")
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
21 const
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
22 shaderConfiguration = createShaderConfiguration(
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
23 inputs=[
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
24 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
25 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
26 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
28 ],
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
29 intermediates=[
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
30 attr[Vec4f]("vertexColor"),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
31 attr[Vec2f]("colorTexCoord"),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
32 attr[uint16]("materialIndexOut", noInterpolation=true)
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
33 ],
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
34 outputs=[attr[Vec4f]("color")],
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
35 uniforms=[
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
36 attr[Mat4]("projection"),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
37 attr[Mat4]("view"),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
38 attr[Vec4f]("baseColorFactor", arrayCount=4),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
39 ],
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
40 samplers=[attr[Texture]("baseColorTexture", arrayCount=4)],
269
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
41 vertexCode="""
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
42 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection);
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
43 vertexColor = Uniforms.baseColorFactor[materialIndex];
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
44 colorTexCoord = texcoord_0;
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
45 materialIndexOut = materialIndex;
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
46 """,
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
47 fragmentCode="color = texture(baseColorTexture[materialIndexOut], colorTexCoord) * vertexColor;"
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
48 )
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
49 engine.initRenderer({
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
50 "Material": shaderConfiguration,
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
51 "Material.001": shaderConfiguration,
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
52 "Material.002": shaderConfiguration,
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
53 "Material.004": shaderConfiguration,
344
b83b3a1ccb05 fix: all tests
Sam <sam@basx.dev>
parents: 333
diff changeset
54 })
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
55
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
56 for scene in scenes.mitems:
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
57 scene.addShaderGlobal("projection", Unit4F32)
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
58 scene.addShaderGlobal("view", Unit4F32)
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
59 var materials: Table[uint16, Material]
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
60 for mesh in scene.meshes:
353
61c5d5fe9d93 add: multi-material for meshes
Sam <sam@basx.dev>
parents: 344
diff changeset
61 for material in mesh.materials:
61c5d5fe9d93 add: multi-material for meshes
Sam <sam@basx.dev>
parents: 344
diff changeset
62 if not materials.contains(material.index):
61c5d5fe9d93 add: multi-material for meshes
Sam <sam@basx.dev>
parents: 344
diff changeset
63 materials[material.index] = material
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
64 let baseColors = sortedByIt(values(materials).toSeq, it.index).mapIt(getValue[Vec4f](it.constants["baseColorFactor"], 0))
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
65 let baseTextures = sortedByIt(values(materials).toSeq, it.index).mapIt(it.textures["baseColorTexture"])
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
66 scene.addShaderGlobalArray("baseColorFactor", baseColors)
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
67 scene.addShaderGlobalArray("baseColorTexture", baseTextures)
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
68 engine.addScene(scene)
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
69
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
70 var
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
71 size = 1'f32
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
72 elevation = 0'f32
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
73 azimut = 0'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
74 currentScene = 0
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
75
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
76 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
77 if engine.keyWasPressed(`1`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
78 currentScene = 0
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
79 elif engine.keyWasPressed(`2`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
80 currentScene = 1
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
81 elif engine.keyWasPressed(`3`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
82 currentScene = 2
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
83 elif engine.keyWasPressed(`4`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
84 currentScene = 3
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
85 elif engine.keyWasPressed(`5`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
86 currentScene = 4
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
87 elif engine.keyWasPressed(`6`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
88 currentScene = 5
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
89
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
90 if engine.keyWasPressed(NumberRowExtra3):
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
91 size = 0.3'f32
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
92 elevation = 0'f32
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
93 azimut = 0'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
94
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
95 let ratio = engine.getWindow().size[0] / engine.getWindow().size[1]
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
96 size *= 1'f32 + engine.mouseWheel() * 0.05
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
97 azimut += engine.mouseMove().x / 180'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
98 elevation -= engine.mouseMove().y / 180'f32
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
99 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1))
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
100 scenes[currentScene].setShaderGlobal(
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
101 "view",
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
102 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32)
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 222
diff changeset
103 )
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
104 engine.renderScene(scenes[currentScene])
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
105 engine.destroy()
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
106
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
107 when isMainModule:
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
108 main()