Mercurial > games > semicongine
comparison tests/test_gltf.nim @ 1448:96753bec055c default tip
add: support for "name" and "extras" on gltf nodes
author | sam <sam@basx.dev> |
---|---|
date | Sun, 09 Mar 2025 22:59:34 +0700 |
parents | 676fc13685a9 |
children |
comparison
equal
deleted
inserted
replaced
1447:b17098eb052f | 1448:96753bec055c |
---|---|
6 | 6 |
7 import ../semicongine | 7 import ../semicongine |
8 import ../semicongine/rendering | 8 import ../semicongine/rendering |
9 import ../semicongine/loaders | 9 import ../semicongine/loaders |
10 import ../semicongine/input | 10 import ../semicongine/input |
11 import ../semicongine/gltf | |
11 | 12 |
12 proc test_gltf(time: float32, renderPass: RenderPass) = | 13 proc test_gltf(time: float32, renderPass: RenderPass) = |
13 var renderdata = initRenderData() | 14 var renderdata = initRenderData() |
14 | 15 |
15 type | 16 type |
94 Mesh = object | 95 Mesh = object |
95 position: GPUArray[Vec3f, VertexBuffer] | 96 position: GPUArray[Vec3f, VertexBuffer] |
96 color: GPUArray[Vec4f, VertexBuffer] | 97 color: GPUArray[Vec4f, VertexBuffer] |
97 normal: GPUArray[Vec3f, VertexBuffer] | 98 normal: GPUArray[Vec3f, VertexBuffer] |
98 indices: GPUArray[uint32, IndexBuffer] | 99 indices: GPUArray[uint32, IndexBuffer] |
99 material: int32 | |
100 | 100 |
101 var gltfData = loadMeshes[Mesh, Material]( | 101 var gltfData = loadMeshes[Mesh, Material]( |
102 "town.glb", | 102 "town.glb", |
103 # "forest.glb", | 103 # "forest.glb", |
104 MeshAttributeNames( | 104 MeshAttributeNames( |
105 POSITION: "position", | 105 POSITION: "position", COLOR: @["color"], NORMAL: "normal", indices: "indices" |
106 COLOR: @["color"], | |
107 NORMAL: "normal", | |
108 indices: "indices", | |
109 material: "material", | |
110 ), | 106 ), |
111 MaterialAttributeNames( | 107 MaterialAttributeNames( |
112 baseColorFactor: "color", | 108 baseColorFactor: "color", |
113 baseColorTexture: "colorTexture", | 109 baseColorTexture: "colorTexture", |
114 metallicFactor: "metallic", | 110 metallicFactor: "metallic", |
128 ) | 124 ) |
129 ) | 125 ) |
130 for i in 0 ..< gltfData.materials.len: | 126 for i in 0 ..< gltfData.materials.len: |
131 descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer) | 127 descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer) |
132 for mesh in mitems(gltfData.meshes): | 128 for mesh in mitems(gltfData.meshes): |
133 for primitive in mitems(mesh): | 129 for primitive in mitems(mesh.primitives): |
134 primitive[0].color = asGPUArray( | 130 primitive.data.color = asGPUArray( |
135 newSeqWith(primitive[0].position.data.len, vec4(1, 1, 1, 1)), VertexBuffer | 131 newSeqWith(primitive.data.position.data.len, vec4(1, 1, 1, 1)), VertexBuffer |
136 ) | 132 ) |
137 renderdata.assignBuffers(primitive[0]) | 133 renderdata.assignBuffers(primitive.data) |
138 renderdata.assignBuffers(descriptors) | 134 renderdata.assignBuffers(descriptors) |
139 | 135 |
140 var pipeline = createPipeline(Shader(), renderPass = renderPass, cullMode = []) | 136 var pipeline = createPipeline(Shader(), renderPass = renderPass, cullMode = []) |
141 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) | 137 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) |
142 | 138 |
145 proc drawNode( | 141 proc drawNode( |
146 commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 | 142 commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 |
147 ) = | 143 ) = |
148 let nodeTransform = gltfData.nodes[nodeId].transform * transform | 144 let nodeTransform = gltfData.nodes[nodeId].transform * transform |
149 if gltfData.nodes[nodeId].mesh >= 0: | 145 if gltfData.nodes[nodeId].mesh >= 0: |
150 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh].mitems: | 146 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh].primitives: |
151 renderWithPushConstant( | 147 renderWithPushConstant( |
152 commandbuffer = commandbuffer, | 148 commandbuffer = commandbuffer, |
153 pipeline = pipeline, | 149 pipeline = pipeline, |
154 mesh = primitive[0], | 150 mesh = primitive.data, |
155 pushConstant = | 151 pushConstant = |
156 ObjectData(transform: nodeTransform, materialId: primitive[0].material), | 152 ObjectData(transform: nodeTransform, materialId: primitive.material.int32), |
157 ) | 153 ) |
158 for childNode in gltfData.nodes[nodeId].children: | 154 for childNode in gltfData.nodes[nodeId].children: |
159 drawNode( | 155 drawNode( |
160 commandbuffer = commandbuffer, | 156 commandbuffer = commandbuffer, |
161 pipeline = pipeline, | 157 pipeline = pipeline, |