Mercurial > games > semicongine
annotate tests/test_mesh.nim @ 785:1880ab140165
add: first complete working version of multiple materials and shaders per scene, yipie :)
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 19 Aug 2023 23:30:25 +0700 |
parents | a2c14402acd2 |
children | 27aaf43e18b4 |
rev | line source |
---|---|
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
1 import std/sequtils |
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
2 import std/tables |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import semicongine |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 proc main() = |
763 | 6 var ent1 = newEntity("hoho", {"mesh": Component(rect())}) |
7 var ent2 = newEntity("hehe", [], ent1) | |
708 | 8 var myScene = newScene("hi", ent2) |
9 myScene.root.transform = translate3d(0.2'f32, 0'f32, 0'f32) | |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
10 myScene.root[0].transform = translate3d(0'f32, 0.2'f32, 0'f32) |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
11 var scenes = [ |
729 | 12 # loadScene("default_cube.glb", "1"), |
708 | 13 # loadScene("default_cube1.glb", "3"), |
14 # loadScene("default_cube2.glb", "4"), | |
15 # loadScene("flat.glb", "5"), | |
729 | 16 loadScene("tutorialk-donat.glb", "6"), |
708 | 17 # myScene, |
18 # loadScene("personv3.glb", "2"), | |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
19 ] |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 var engine = initEngine("Test meshes") |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 const |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
23 vertexInput = @[ |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
24 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
25 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead), |
708 | 26 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead), |
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), | |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
28 ] |
730 | 29 intermediate = @[ |
729 | 30 attr[Vec4f]("vertexColor"), |
31 attr[Vec2f]("colorTexCoord"), | |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
32 attr[uint16]("materialIndexOut", noInterpolation=true) |
729 | 33 ] |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 fragOutput = @[attr[Vec4f]("color")] |
706 | 35 uniforms = @[ |
36 attr[Mat4]("projection"), | |
37 attr[Mat4]("view"), | |
729 | 38 attr[Vec4f]("baseColorFactor", arrayCount=4), |
706 | 39 ] |
730 | 40 samplers = @[attr[Sampler2DType]("baseColorTexture", arrayCount=4)] |
41 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( | |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 inputs=vertexInput, |
730 | 43 intermediate=intermediate, |
44 outputs=fragOutput, | |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 uniforms=uniforms, |
708 | 46 samplers=samplers, |
730 | 47 vertexCode=""" |
708 | 48 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
49 vertexColor = Uniforms.baseColorFactor[materialIndex]; |
729 | 50 colorTexCoord = texcoord_0; |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
51 materialIndexOut = materialIndex; |
730 | 52 """, |
53 fragmentCode=""" | |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
54 // vec4 col[4] = vec4[4](vec4(1, 0, 0, 1), vec4(0, 1, 0, 1), vec4(0, 0, 1, 1), vec4(1, 1, 1, 1)); |
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
55 color = texture(baseColorTexture[materialIndexOut], colorTexCoord) * vertexColor; |
729 | 56 """ |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 ) |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
58 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
59 for scene in scenes.mitems: |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
60 engine.addScene(scene, vertexInput, samplers) |
706 | 61 scene.addShaderGlobal("projection", Unit4) |
62 scene.addShaderGlobal("view", Unit4) | |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
63 let baseColors = scene.materials.map(proc(i: Material): Vec4f = getValue[Vec4f](i[].constants["baseColorFactor"])) |
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
64 scene.addShaderGlobalArray("baseColorFactor", baseColors) |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
65 var |
708 | 66 size = 1'f32 |
707 | 67 elevation = 0'f32 |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
68 azimut = 0'f32 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
69 currentScene = 0 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
70 |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
72 if engine.keyWasPressed(`1`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
73 currentScene = 0 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
74 elif engine.keyWasPressed(`2`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
75 currentScene = 1 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
76 elif engine.keyWasPressed(`3`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
77 currentScene = 2 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
78 elif engine.keyWasPressed(`4`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
79 currentScene = 3 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
80 elif engine.keyWasPressed(`5`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
81 currentScene = 4 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
82 elif engine.keyWasPressed(`6`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
83 currentScene = 5 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
84 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
85 if engine.keyWasPressed(NumberRowExtra3): |
707 | 86 size = 0.3'f32 |
87 elevation = 0'f32 | |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
88 azimut = 0'f32 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
89 |
707 | 90 let ratio = engine.getWindow().size[0] / engine.getWindow().size[1] |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
91 size *= 1'f32 + engine.mouseWheel() * 0.05 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
92 azimut += engine.mouseMove().x / 180'f32 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
93 elevation -= engine.mouseMove().y / 180'f32 |
706 | 94 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1)) |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
95 scenes[currentScene].setShaderGlobal( |
706 | 96 "view", |
97 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) | |
691 | 98 ) |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
99 engine.renderScene(scenes[currentScene]) |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 engine.destroy() |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 when isMainModule: |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
103 main() |