Mercurial > games > semicongine
comparison tests/test_mesh.nim @ 832:388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 22 Nov 2023 23:24:47 +0700 |
parents | 6a09fe5dc99b |
children | f4f1474dc70a |
comparison
equal
deleted
inserted
replaced
831:902735626b66 | 832:388c4b35a6e3 |
---|---|
2 import std/sequtils | 2 import std/sequtils |
3 import std/tables | 3 import std/tables |
4 import semicongine | 4 import semicongine |
5 | 5 |
6 proc main() = | 6 proc main() = |
7 # var myScene = Scene(name: "hi", meshes: @[rect()]) | |
8 # myScene.meshes[0].transform = translate3d(0.2'f32, 0'f32, 0'f32) | |
9 # myScene.root[0].transform = translate3d(0'f32, 0.2'f32, 0'f32) | |
10 var scenes = [ | 7 var scenes = [ |
11 # loadScene("default_cube.glb", "1"), | 8 Scene(name: "Donut", meshes: loadMeshes("tutorialk-donat.glb", COLORED_SINGLE_TEXTURE_MATERIAL)[0].toSeq), |
12 # loadScene("default_cube1.glb", "3"), | |
13 # loadScene("default_cube2.glb", "4"), | |
14 # loadScene("flat.glb", "5"), | |
15 Scene(name: "Donut", meshes: loadMeshes("tutorialk-donat.glb")[0].toSeq), | |
16 # myScene, | |
17 # loadScene("personv3.glb", "2"), | |
18 ] | 9 ] |
19 | 10 |
20 var engine = initEngine("Test meshes") | 11 var engine = initEngine("Test meshes") |
21 const | 12 const |
22 shaderConfiguration = createShaderConfiguration( | 13 shaderConfiguration = createShaderConfiguration( |
23 inputs=[ | 14 inputs=[ |
24 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), | 15 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
25 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead), | 16 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead, perInstance=true), |
26 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead), | 17 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead), |
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), | 18 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), |
28 ], | 19 ], |
29 intermediates=[ | 20 intermediates=[ |
30 attr[Vec4f]("vertexColor"), | 21 attr[Vec4f]("vertexColor"), |
33 ], | 24 ], |
34 outputs=[attr[Vec4f]("color")], | 25 outputs=[attr[Vec4f]("color")], |
35 uniforms=[ | 26 uniforms=[ |
36 attr[Mat4]("projection"), | 27 attr[Mat4]("projection"), |
37 attr[Mat4]("view"), | 28 attr[Mat4]("view"), |
38 attr[Vec4f]("baseColorFactor", arrayCount=4), | 29 attr[Vec4f]("color", arrayCount=4), |
39 ], | 30 ], |
40 samplers=[attr[Texture]("baseColorTexture", arrayCount=4)], | 31 samplers=[attr[Texture]("baseTexture", arrayCount=4)], |
41 vertexCode=""" | 32 vertexCode=""" |
42 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); | 33 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); |
43 vertexColor = Uniforms.baseColorFactor[materialIndex]; | 34 vertexColor = Uniforms.color[materialIndex]; |
44 colorTexCoord = texcoord_0; | 35 colorTexCoord = texcoord_0; |
45 materialIndexOut = materialIndex; | 36 materialIndexOut = materialIndex; |
46 """, | 37 """, |
47 fragmentCode="color = texture(baseColorTexture[materialIndexOut], colorTexCoord) * vertexColor;" | 38 fragmentCode="color = texture(baseTexture[materialIndexOut], colorTexCoord) * vertexColor;" |
48 ) | 39 ) |
49 engine.initRenderer({ | 40 engine.initRenderer({COLORED_SINGLE_TEXTURE_MATERIAL: shaderConfiguration}) |
50 "Material": shaderConfiguration, | |
51 "Material.001": shaderConfiguration, | |
52 "Material.002": shaderConfiguration, | |
53 "Material.004": shaderConfiguration, | |
54 }) | |
55 | 41 |
56 for scene in scenes.mitems: | 42 for scene in scenes.mitems: |
57 scene.addShaderGlobal("projection", Unit4F32) | 43 scene.addShaderGlobal("projection", Unit4F32) |
58 scene.addShaderGlobal("view", Unit4F32) | 44 scene.addShaderGlobal("view", Unit4F32) |
59 var materials: Table[uint16, Material] | 45 engine.loadScene(scene) |
60 for mesh in scene.meshes: | |
61 for material in mesh.materials: | |
62 if not materials.contains(material.index): | |
63 materials[material.index] = material | |
64 let baseColors = sortedByIt(values(materials).toSeq, it.index).mapIt(getValue[Vec4f](it.constants["baseColorFactor"], 0)) | |
65 let baseTextures = sortedByIt(values(materials).toSeq, it.index).mapIt(it.textures["baseColorTexture"]) | |
66 scene.addShaderGlobalArray("baseColorFactor", baseColors) | |
67 scene.addShaderGlobalArray("baseColorTexture", baseTextures) | |
68 engine.addScene(scene) | |
69 | 46 |
70 var | 47 var |
71 size = 1'f32 | 48 size = 1'f32 |
72 elevation = 0'f32 | 49 elevation = 0'f32 |
73 azimut = 0'f32 | 50 azimut = 0'f32 |