comparison tests/test_mesh.nim @ 708:3bb199dd45ba

fix: gltf loading
author Sam <sam@basx.dev>
date Tue, 23 May 2023 01:05:06 +0700
parents 3742bba2293f
children ad078e26a1c7
comparison
equal deleted inserted replaced
707:3742bba2293f 708:3bb199dd45ba
1 import semicongine 1 import semicongine
2 2
3 proc main() = 3 proc main() =
4 var ent1 = newEntity("hoho", rect())
5 var ent2 = newEntity("hehe", ent1)
6 var myScene = newScene("hi", ent2)
7 myScene.root.transform = translate3d(0.2'f32, 0'f32, 0'f32)
8 myScene.root.children[0].transform = translate3d(0'f32, 0.2'f32, 0'f32)
4 var scenes = [ 9 var scenes = [
5 loadScene("default_cube.glb", "1"), 10 # loadScene("default_cube.glb", "1"),
6 loadScene("default_cube1.glb", "3"), 11 # loadScene("default_cube1.glb", "3"),
7 loadScene("default_cube2.glb", "4"), 12 # loadScene("default_cube2.glb", "4"),
8 loadScene("flat.glb", "5"), 13 # loadScene("flat.glb", "5"),
9 loadScene("tutorialk-donat.glb", "6"), 14 loadScene("tutorialk-donat.glb", "6"),
10 loadScene("personv3.glb", "2"), 15 # myScene,
16 # loadScene("personv3.glb", "2"),
11 ] 17 ]
12 18
13 var engine = initEngine("Test meshes") 19 var engine = initEngine("Test meshes")
14 const 20 const
15 vertexInput = @[ 21 vertexInput = @[
16 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 22 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
17 attr[uint8]("material", memoryPerformanceHint=PreferFastRead), 23 attr[uint8]("material", memoryPerformanceHint=PreferFastRead),
24 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead),
25 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
18 ] 26 ]
19 vertexOutput = @[attr[Vec4f]("vertexColor")] 27 vertexOutput = @[attr[Vec4f]("vertexColor"), attr[Vec2f]("colorTexCoord")]
20 fragOutput = @[attr[Vec4f]("color")] 28 fragOutput = @[attr[Vec4f]("color")]
21 uniforms = @[ 29 uniforms = @[
22 attr[Mat4]("projection"), 30 attr[Mat4]("projection"),
23 attr[Mat4]("view"), 31 attr[Mat4]("view"),
24 attr[Mat4]("model"),
25 attr[Vec4f]("material_color", arrayCount=16), 32 attr[Vec4f]("material_color", arrayCount=16),
33 ]
34 samplers = @[
35 attr[Sampler2DType]("material_color_texture", arrayCount=1),
26 ] 36 ]
27 vertexCode = compileGlslShader( 37 vertexCode = compileGlslShader(
28 stage=VK_SHADER_STAGE_VERTEX_BIT, 38 stage=VK_SHADER_STAGE_VERTEX_BIT,
29 inputs=vertexInput, 39 inputs=vertexInput,
30 outputs=vertexOutput, 40 outputs=vertexOutput,
31 uniforms=uniforms, 41 uniforms=uniforms,
32 main="""gl_Position = Uniforms.projection * Uniforms.view * Uniforms.model * vec4(position, 1.0); vertexColor = Uniforms.material_color[material];""" 42 samplers=samplers,
43 main="""
44 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection);
45 vertexColor = Uniforms.material_color[material];
46 colorTexCoord = texcoord_0;
47 """
33 ) 48 )
34 fragmentCode = compileGlslShader( 49 fragmentCode = compileGlslShader(
35 stage=VK_SHADER_STAGE_FRAGMENT_BIT, 50 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
36 inputs=vertexOutput, 51 inputs=vertexOutput,
37 outputs=fragOutput, 52 outputs=fragOutput,
38 uniforms=uniforms, 53 uniforms=uniforms,
39 main="""color = vertexColor;""" 54 samplers=samplers,
55 main="""color = texture(material_color_texture[0], colorTexCoord) * vertexColor;"""
40 ) 56 )
41 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) 57 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1)))
42 for scene in scenes.mitems: 58 for scene in scenes.mitems:
43 engine.addScene(scene, vertexInput) 59 engine.addScene(scene, vertexInput, transformAttribute="transform")
44 scene.addShaderGlobal("projection", Unit4) 60 scene.addShaderGlobal("projection", Unit4)
45 scene.addShaderGlobal("view", Unit4) 61 scene.addShaderGlobal("view", Unit4)
46 scene.addShaderGlobal("model", Unit4)
47 var 62 var
48 size = 0.3'f32 63 size = 1'f32
49 elevation = 0'f32 64 elevation = 0'f32
50 azimut = 0'f32 65 azimut = 0'f32
51 currentScene = 0 66 currentScene = 0
52 67
53 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 68 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
76 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1)) 91 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1))
77 scenes[currentScene].setShaderGlobal( 92 scenes[currentScene].setShaderGlobal(
78 "view", 93 "view",
79 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) 94 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32)
80 ) 95 )
81 scenes[currentScene].setShaderGlobal("model", Unit4f32)
82 engine.renderScene(scenes[currentScene]) 96 engine.renderScene(scenes[currentScene])
83 engine.destroy() 97 engine.destroy()
84 98
85
86 when isMainModule: 99 when isMainModule:
87 main() 100 main()