comparison semicongine/resources/mesh.nim @ 870:b975eab2b694

did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
author Sam <sam@basx.dev>
date Sun, 07 Jan 2024 00:56:44 +0700
parents 65afec4cb6c6
children 1ee397815b0b
comparison
equal deleted inserted replaced
869:65afec4cb6c6 870:b975eab2b694
114 raise newException(Exception, "Sparce accessors are currently not implemented") 114 raise newException(Exception, "Sparce accessors are currently not implemented")
115 115
116 let accessorOffset = if accessor.hasKey("byteOffset"): accessor["byteOffset"].getInt() else: 0 116 let accessorOffset = if accessor.hasKey("byteOffset"): accessor["byteOffset"].getInt() else: 0
117 let length = bufferView["byteLength"].getInt() 117 let length = bufferView["byteLength"].getInt()
118 let bufferOffset = bufferView["byteOffset"].getInt() + accessorOffset 118 let bufferOffset = bufferView["byteOffset"].getInt() + accessorOffset
119 var dstPointer = result.getRawData()[0] 119 var dstPointer = result.getPointer()
120 120
121 if bufferView.hasKey("byteStride"): 121 if bufferView.hasKey("byteStride"):
122 warn "Congratulations, you try to test a feature (loading buffer data with stride attributes) that we have no idea where it is used and how it can be tested (need a coresponding *.glb file)." 122 warn "Congratulations, you try to test a feature (loading buffer data with stride attributes) that we have no idea where it is used and how it can be tested (need a coresponding *.glb file)."
123 # we don't support stride, have to convert stuff here... does this even work? 123 # we don't support stride, have to convert stuff here... does this even work?
124 for i in 0 ..< int(result.len): 124 for i in 0 ..< int(result.len):
168 168
169 # color 169 # color
170 if defaultMaterial.attributes.contains("color"): 170 if defaultMaterial.attributes.contains("color"):
171 attributes["color"] = initDataList(thetype=Vec4F32) 171 attributes["color"] = initDataList(thetype=Vec4F32)
172 if pbr.hasKey(GLTF_MATERIAL_MAPPING["color"]): 172 if pbr.hasKey(GLTF_MATERIAL_MAPPING["color"]):
173 setValue(attributes["color"], @[newVec4f( 173 attributes["color"] = @[newVec4f(
174 pbr[GLTF_MATERIAL_MAPPING["color"]][0].getFloat(), 174 pbr[GLTF_MATERIAL_MAPPING["color"]][0].getFloat(),
175 pbr[GLTF_MATERIAL_MAPPING["color"]][1].getFloat(), 175 pbr[GLTF_MATERIAL_MAPPING["color"]][1].getFloat(),
176 pbr[GLTF_MATERIAL_MAPPING["color"]][2].getFloat(), 176 pbr[GLTF_MATERIAL_MAPPING["color"]][2].getFloat(),
177 pbr[GLTF_MATERIAL_MAPPING["color"]][3].getFloat(), 177 pbr[GLTF_MATERIAL_MAPPING["color"]][3].getFloat(),
178 )]) 178 )]
179 else: 179 else:
180 setValue(attributes["color"], @[newVec4f(1, 1, 1, 1)]) 180 attributes["color"] = @[newVec4f(1, 1, 1, 1)]
181 181
182 # pbr material values 182 # pbr material values
183 for factor in ["metallic", "roughness"]: 183 for factor in ["metallic", "roughness"]:
184 if defaultMaterial.attributes.contains(factor): 184 if defaultMaterial.attributes.contains(factor):
185 attributes[factor] = initDataList(thetype=Float32) 185 attributes[factor] = initDataList(thetype=Float32)
186 if pbr.hasKey(GLTF_MATERIAL_MAPPING[factor]): 186 if pbr.hasKey(GLTF_MATERIAL_MAPPING[factor]):
187 setValue(attributes[factor], @[float32(pbr[GLTF_MATERIAL_MAPPING[factor]].getFloat())]) 187 attributes[factor] = @[float32(pbr[GLTF_MATERIAL_MAPPING[factor]].getFloat())]
188 else: 188 else:
189 setValue(attributes[factor], @[0.5'f32]) 189 attributes[factor] = @[0.5'f32]
190 190
191 # pbr material textures 191 # pbr material textures
192 for texture in ["baseTexture", "metallicRoughnessTexture"]: 192 for texture in ["baseTexture", "metallicRoughnessTexture"]:
193 if defaultMaterial.attributes.contains(texture): 193 if defaultMaterial.attributes.contains(texture):
194 attributes[texture] = initDataList(thetype=TextureType) 194 attributes[texture] = initDataList(thetype=TextureType)
195 # attributes[texture & "Index"] = initDataList(thetype=UInt8) 195 # attributes[texture & "Index"] = initDataList(thetype=UInt8)
196 if pbr.hasKey(GLTF_MATERIAL_MAPPING[texture]): 196 if pbr.hasKey(GLTF_MATERIAL_MAPPING[texture]):
197 setValue(attributes[texture], @[loadTexture(root, pbr[GLTF_MATERIAL_MAPPING[texture]]["index"].getInt(), mainBuffer)]) 197 attributes[texture] = @[loadTexture(root, pbr[GLTF_MATERIAL_MAPPING[texture]]["index"].getInt(), mainBuffer)]
198 # setValue(attributes[texture & "Index"], @[pbr[GLTF_MATERIAL_MAPPING[texture]].getOrDefault("texCoord").getInt(0).uint8])
199 else: 198 else:
200 setValue(attributes[texture], @[EMPTY_TEXTURE]) 199 attributes[texture] = @[EMPTY_TEXTURE]
201 # setValue(attributes[texture & "Index"], @[0'u8])
202 200
203 # generic material textures 201 # generic material textures
204 for texture in ["normalTexture", "occlusionTexture", "emissiveTexture"]: 202 for texture in ["normalTexture", "occlusionTexture", "emissiveTexture"]:
205 if defaultMaterial.attributes.contains(texture): 203 if defaultMaterial.attributes.contains(texture):
206 attributes[texture] = initDataList(thetype=TextureType) 204 attributes[texture] = initDataList(thetype=TextureType)
207 # attributes[texture & "Index"] = initDataList(thetype=UInt8) 205 # attributes[texture & "Index"] = initDataList(thetype=UInt8)
208 if materialNode.hasKey(GLTF_MATERIAL_MAPPING[texture]): 206 if materialNode.hasKey(GLTF_MATERIAL_MAPPING[texture]):
209 setValue(attributes[texture], @[loadTexture(root, materialNode[texture]["index"].getInt(), mainBuffer)]) 207 attributes[texture] = @[loadTexture(root, materialNode[texture]["index"].getInt(), mainBuffer)]
210 # setValue(attributes[texture & "Index"], @[materialNode[texture].getOrDefault("texCoord").getInt(0).uint8])
211 else: 208 else:
212 setValue(attributes[texture], @[EMPTY_TEXTURE]) 209 attributes[texture] = @[EMPTY_TEXTURE]
213 # setValue(attributes[texture & "Index"], @[0'u8])
214 210
215 # emissiv color 211 # emissiv color
216 if defaultMaterial.attributes.contains("emissiveColor"): 212 if defaultMaterial.attributes.contains("emissiveColor"):
217 attributes["emissiveColor"] = initDataList(thetype=Vec3F32) 213 attributes["emissiveColor"] = initDataList(thetype=Vec3F32)
218 if materialNode.hasKey(GLTF_MATERIAL_MAPPING["emissiveColor"]): 214 if materialNode.hasKey(GLTF_MATERIAL_MAPPING["emissiveColor"]):
219 setValue(attributes["emissiveColor"], @[newVec3f( 215 attributes["emissiveColor"] = @[newVec3f(
220 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][0].getFloat(), 216 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][0].getFloat(),
221 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][1].getFloat(), 217 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][1].getFloat(),
222 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][2].getFloat(), 218 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][2].getFloat(),
223 )]) 219 )]
224 else: 220 else:
225 setValue(attributes["emissiveColor"], @[newVec3f(1'f32, 1'f32, 1'f32)]) 221 attributes["emissiveColor"] = @[newVec3f(1'f32, 1'f32, 1'f32)]
226 222
227 result = initMaterialData(theType=defaultMaterial, name=materialNode["name"].getStr(), attributes=attributes) 223 result = initMaterialData(theType=defaultMaterial, name=materialNode["name"].getStr(), attributes=attributes)
228 224
229 proc loadMesh(meshname: string, root: JsonNode, primitiveNode: JsonNode, defaultMaterial: MaterialType, mainBuffer: seq[uint8]): Mesh = 225 proc loadMesh(meshname: string, root: JsonNode, primitiveNode: JsonNode, defaultMaterial: MaterialType, mainBuffer: seq[uint8]): Mesh =
230 if primitiveNode.hasKey("mode") and primitiveNode["mode"].getInt() != 4: 226 if primitiveNode.hasKey("mode") and primitiveNode["mode"].getInt() != 4: