comparison tests/test_mesh.nim @ 794:59c54c4486c4

fix: material handling, gltf loading, loader example
author Sam <sam@basx.dev>
date Mon, 04 Sep 2023 00:31:17 +0700
parents a2c14402acd2
children b83b3a1ccb05
comparison
equal deleted inserted replaced
793:c31e42d72253 794:59c54c4486c4
1 import std/algorithm
1 import std/sequtils 2 import std/sequtils
2 import std/tables 3 import std/tables
3 import semicongine 4 import semicongine
4 5
5 proc main() = 6 proc main() =
6 var ent1 = newEntity("hoho", {"mesh": Component(rect())}) 7 # var myScene = Scene(name: "hi", meshes: @[rect()])
7 var ent2 = newEntity("hehe", [], ent1) 8 # myScene.meshes[0].transform = translate3d(0.2'f32, 0'f32, 0'f32)
8 var myScene = newScene("hi", ent2) 9 # myScene.root[0].transform = translate3d(0'f32, 0.2'f32, 0'f32)
9 myScene.root.transform = translate3d(0.2'f32, 0'f32, 0'f32)
10 myScene.root[0].transform = translate3d(0'f32, 0.2'f32, 0'f32)
11 var scenes = [ 10 var scenes = [
12 # loadScene("default_cube.glb", "1"), 11 # loadScene("default_cube.glb", "1"),
13 # loadScene("default_cube1.glb", "3"), 12 # loadScene("default_cube1.glb", "3"),
14 # loadScene("default_cube2.glb", "4"), 13 # loadScene("default_cube2.glb", "4"),
15 # loadScene("flat.glb", "5"), 14 # loadScene("flat.glb", "5"),
16 loadScene("tutorialk-donat.glb", "6"), 15 Scene(name: "Donut", meshes: loadMeshes("tutorialk-donat.glb")[0].toSeq),
17 # myScene, 16 # myScene,
18 # loadScene("personv3.glb", "2"), 17 # loadScene("personv3.glb", "2"),
19 ] 18 ]
20 19
21 var engine = initEngine("Test meshes") 20 var engine = initEngine("Test meshes")
22 const 21 const
23 vertexInput = @[ 22 shaderConfiguration = createShaderConfiguration(
24 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 23 inputs=[
25 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead), 24 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
26 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead), 25 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead),
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), 26 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead),
28 ] 27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
29 intermediate = @[ 28 ],
30 attr[Vec4f]("vertexColor"), 29 intermediates=[
31 attr[Vec2f]("colorTexCoord"), 30 attr[Vec4f]("vertexColor"),
32 attr[uint16]("materialIndexOut", noInterpolation=true) 31 attr[Vec2f]("colorTexCoord"),
33 ] 32 attr[uint16]("materialIndexOut", noInterpolation=true)
34 fragOutput = @[attr[Vec4f]("color")] 33 ],
35 uniforms = @[ 34 outputs=[attr[Vec4f]("color")],
36 attr[Mat4]("projection"), 35 uniforms=[
37 attr[Mat4]("view"), 36 attr[Mat4]("projection"),
38 attr[Vec4f]("baseColorFactor", arrayCount=4), 37 attr[Mat4]("view"),
39 ] 38 attr[Vec4f]("baseColorFactor", arrayCount=4),
40 samplers = @[attr[Sampler2DType]("baseColorTexture", arrayCount=4)] 39 ],
41 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( 40 samplers=[attr[Texture]("baseColorTexture", arrayCount=4)],
42 inputs=vertexInput,
43 intermediate=intermediate,
44 outputs=fragOutput,
45 uniforms=uniforms,
46 samplers=samplers,
47 vertexCode=""" 41 vertexCode="""
48 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); 42 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection);
49 vertexColor = Uniforms.baseColorFactor[materialIndex]; 43 vertexColor = Uniforms.baseColorFactor[materialIndex];
50 colorTexCoord = texcoord_0; 44 colorTexCoord = texcoord_0;
51 materialIndexOut = materialIndex; 45 materialIndexOut = materialIndex;
52 """, 46 """,
53 fragmentCode=""" 47 fragmentCode="color = texture(baseColorTexture[materialIndexOut], colorTexCoord) * vertexColor;"
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));
55 color = texture(baseColorTexture[materialIndexOut], colorTexCoord) * vertexColor;
56 """
57 ) 48 )
58 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) 49 engine.initRenderer({
50 "Material": shaderConfiguration,
51 "Material.001": shaderConfiguration,
52 "Material.002": shaderConfiguration,
53 "Material.004": shaderConfiguration,
54 }.toTable)
55
59 for scene in scenes.mitems: 56 for scene in scenes.mitems:
60 engine.addScene(scene, vertexInput, samplers) 57 scene.addShaderGlobal("projection", Unit4F32)
61 scene.addShaderGlobal("projection", Unit4) 58 scene.addShaderGlobal("view", Unit4F32)
62 scene.addShaderGlobal("view", Unit4) 59 var materials: Table[uint16, Material]
63 let baseColors = scene.materials.map(proc(i: Material): Vec4f = getValue[Vec4f](i[].constants["baseColorFactor"])) 60 for mesh in scene.meshes:
61 if not materials.contains(mesh.material.index):
62 materials[mesh.material.index] = mesh.material
63 let baseColors = sortedByIt(values(materials).toSeq, it.index).mapIt(getValue[Vec4f](it.constants["baseColorFactor"], 0))
64 let baseTextures = sortedByIt(values(materials).toSeq, it.index).mapIt(it.textures["baseColorTexture"])
65 for t in baseTextures:
66 echo "- ", t
64 scene.addShaderGlobalArray("baseColorFactor", baseColors) 67 scene.addShaderGlobalArray("baseColorFactor", baseColors)
68 scene.addShaderGlobalArray("baseColorTexture", baseTextures)
69 engine.addScene(scene)
70
65 var 71 var
66 size = 1'f32 72 size = 1'f32
67 elevation = 0'f32 73 elevation = 0'f32
68 azimut = 0'f32 74 azimut = 0'f32
69 currentScene = 0 75 currentScene = 0
92 azimut += engine.mouseMove().x / 180'f32 98 azimut += engine.mouseMove().x / 180'f32
93 elevation -= engine.mouseMove().y / 180'f32 99 elevation -= engine.mouseMove().y / 180'f32
94 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1)) 100 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1))
95 scenes[currentScene].setShaderGlobal( 101 scenes[currentScene].setShaderGlobal(
96 "view", 102 "view",
97 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) 103 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32)
98 ) 104 )
99 engine.renderScene(scenes[currentScene]) 105 engine.renderScene(scenes[currentScene])
100 engine.destroy() 106 engine.destroy()
101 107
102 when isMainModule: 108 when isMainModule: