annotate semiconginev2/gltf.nim @ 1250:9ceb509af5ea

add: loading of most kinds of data from gltf
author sam <sam@basx.dev>
date Thu, 25 Jul 2024 23:15:05 +0700
parents d83726af7abb
children 3f98ad20a9d3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
1 type
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
2 GltfNode* = object
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
3 children: seq[int]
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
4 mesh: int
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
5 transform: Mat4
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
6 GltfMesh*[TMesh, TMaterial] = object
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
7 scenes*: seq[seq[int]] # each scene has a seq of node indices
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
8 nodes*: seq[GltfNode] # each node has a seq of mesh indices
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
9 meshes*: seq[seq[(TMesh, VkPrimitiveTopology)]]
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
10 materials*: seq[TMaterial]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
11 textures*: seq[Image[BGRA]]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
12 glTFHeader = object
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
13 magic: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
14 version: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
15 length: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
16 glTFData = object
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
17 structuredContent: JsonNode
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
18 binaryBufferData: seq[uint8]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
19
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
20 MaterialAttributeNames* = object
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
21 # pbr
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
22 baseColorTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
23 baseColorTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
24 baseColorFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
25 metallicRoughnessTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
26 metallicRoughnessTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
27 metallicFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
28 roughnessFactor*: string
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
29
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
30 # other
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
31 normalTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
32 normalTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
33 occlusionTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
34 occlusionTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
35 emissiveTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
36 emissiveTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
37 emissiveFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
38
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
39 MeshAttributeNames* = object
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
40 POSITION*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
41 NORMAL*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
42 TANGENT*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
43 TEXCOORD*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
44 COLOR*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
45 JOINTS*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
46 WEIGHTS*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
47 indices*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
48 material*: string
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
49
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
50 const
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
51 HEADER_MAGIC = 0x46546C67
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
52 JSON_CHUNK = 0x4E4F534A
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
53 BINARY_CHUNK = 0x004E4942
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
54 SAMPLER_FILTER_MODE_MAP = {
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
55 9728: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
56 9729: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
57 9984: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
58 9985: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
59 9986: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
60 9987: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
61 }.toTable
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
62 SAMPLER_WRAP_MODE_MAP = {
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
63 33071: VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
64 33648: VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
65 10497: VK_SAMPLER_ADDRESS_MODE_REPEAT
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
66 }.toTable
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
67 PRIMITIVE_MODE_MAP = [
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
68 0: VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
69 1: VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
70 2: VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, # not correct, as mode 2 would be a loo, but vulkan has no concept of this
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
71 3: VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
72 4: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
73 5: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
74 6: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
75 ]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
76
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
77 proc getBufferViewData(bufferView: JsonNode, mainBuffer: seq[uint8], baseBufferOffset = 0): seq[uint8] =
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
78 assert bufferView["buffer"].getInt() == 0, "Currently no external buffers supported"
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
79
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
80 result = newSeq[uint8](bufferView["byteLength"].getInt())
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
81 let bufferOffset = bufferView["byteOffset"].getInt() + baseBufferOffset
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
82 var dstPointer = addr result[0]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
83
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
84 if bufferView.hasKey("byteStride"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
85 raise newException(Exception, "Unsupported feature: byteStride in buffer view")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
86 copyMem(dstPointer, addr mainBuffer[bufferOffset], result.len)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
87
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
88 proc componentTypeId(t: typedesc): int =
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
89 if t is int8: return 5120
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
90 elif t is uint8: return 5121
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
91 elif t is int16: return 5122
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
92 elif t is uint16: return 5123
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
93 elif t is uint32: return 5125
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
94 elif t is float32: return 5126
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
95
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
96 proc getAccessorData[T](root: JsonNode, accessor: JsonNode, mainBuffer: seq[uint8]): seq[T] =
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
97 let componentType = accessor["componentType"].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
98 let itemType = accessor["type"].getStr()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
99
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
100 when T is TVec or T is TMat:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
101 assert componentTypeId(elementType(default(T))) == componentType, name(T) & " != " & $componentType
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
102 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
103 assert componentTypeId(T) == componentType, name(T) & " != " & $componentType
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
104
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
105 when T is TVec:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
106 when len(default(T)) == 2: assert itemType == "VEC2"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
107 elif len(default(T)) == 3: assert itemType == "VEC3"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
108 elif len(default(T)) == 4: assert itemType == "VEC4"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
109 elif T is TMat:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
110 when T is Mat2: assert itemType == "MAT2"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
111 elif T is Mat3: assert itemType == "MAT3"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
112 elif T is Mat4: assert itemType == "MAT4"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
113 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
114 assert itemType == "SCALAR"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
115
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
116 result.setLen(accessor["count"].getInt())
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
117
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
118 let bufferView = root["bufferViews"][accessor["bufferView"].getInt()]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
119 assert bufferView["buffer"].getInt() == 0, "Currently no external buffers supported"
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
120
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
121 if accessor.hasKey("sparse"):
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
122 raise newException(Exception, "Sparce accessors are currently not supported")
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
123
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
124 let accessorOffset = if accessor.hasKey("byteOffset"): accessor["byteOffset"].getInt() else: 0
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
125 let length = bufferView["byteLength"].getInt()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
126 let bufferOffset = bufferView["byteOffset"].getInt() + accessorOffset
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
127 var dstPointer = result.ToCPointer()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
128
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
129 if bufferView.hasKey("byteStride"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
130 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)."
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
131 # we don't support stride, have to convert stuff here... does this even work?
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
132 for i in 0 ..< result.len:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
133 copyMem(dstPointer, addr mainBuffer[bufferOffset + i * bufferView["byteStride"].getInt()], sizeof(T))
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
134 dstPointer = cast[typeof(dstPointer)](cast[uint](dstPointer) + sizeof(T).uint)
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
135 else:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
136 copyMem(dstPointer, addr mainBuffer[bufferOffset], length)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
137
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
138 proc loadTexture(root: JsonNode, textureNode: JsonNode, mainBuffer: seq[uint8]): Image[BGRA] =
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
139
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
140 let imageIndex = textureNode["source"].getInt()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
141
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
142 if root["images"][imageIndex].hasKey("uri"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
143 raise newException(Exception, "Unsupported feature: Cannot load images from external files")
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
144 let imageType = root["images"][imageIndex]["mimeType"].getStr()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
145 assert imageType == "image/png", "glTF loader currently only supports PNG"
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
146
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
147 let bufferView = root["bufferViews"][root["images"][imageIndex]["bufferView"].getInt()]
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
148 result = LoadImage[BGRA](getBufferViewData(bufferView, mainBuffer))
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
149
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
150 if textureNode.hasKey("sampler"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
151 let sampler = root["samplers"][textureNode["sampler"].getInt()]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
152 if sampler.hasKey("magFilter"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
153 result.magInterpolation = SAMPLER_FILTER_MODE_MAP[sampler["magFilter"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
154 if sampler.hasKey("minFilter"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
155 result.minInterpolation = SAMPLER_FILTER_MODE_MAP[sampler["minFilter"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
156 if sampler.hasKey("wrapS"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
157 result.wrapU = SAMPLER_WRAP_MODE_MAP[sampler["wrapS"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
158 if sampler.hasKey("wrapT"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
159 result.wrapV = SAMPLER_WRAP_MODE_MAP[sampler["wrapT"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
160
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
161 proc getVec4f(node: JsonNode): Vec4f =
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
162 NewVec4f(node[0].getFloat(), node[1].getFloat(), node[2].getFloat(), node[3].getFloat())
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
163
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
164 proc loadMaterial[TMaterial](
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
165 root: JsonNode,
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
166 materialNode: JsonNode,
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
167 mapping: static MaterialAttributeNames,
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
168 mainBuffer: seq[uint8],
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
169 ): TMaterial =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
170 result = TMaterial()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
171
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
172 let pbr = materialNode["pbrMetallicRoughness"]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
173 for name, value in fieldPairs(result):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
174 for gltfAttribute, mappedName in fieldPairs(mapping):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
175 when gltfAttribute != "" and name == mappedName:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
176 if pbr.hasKey(gltfAttribute):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
177 when gltfAttribute.endsWith("Texture"):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
178 value = typeof(value)(pbr[gltfAttribute]["index"].getInt())
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
179 elif gltfAttribute.endsWith("TextureUv"):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
180 value = typeof(pbr[gltfAttribute[0 ..< ^2]]["index"].getInt())
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
181 elif gltfAttribute in ["baseColorFactor", "emissiveFactor"]:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
182 value = pbr[gltfAttribute].getVec4f()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
183 elif gltfAttribute in ["metallicFactor", "roughnessFactor"]:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
184 value = pbr[gltfAttribute].getFloat()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
185 else:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
186 {.error: "Unsupported gltf material attribute".}
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
187
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
188 proc loadPrimitive[TMesh](
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
189 root: JsonNode,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
190 primitive: JsonNode,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
191 mapping: static MeshAttributeNames,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
192 mainBuffer: seq[uint8]
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
193 ): (TMesh, VkPrimitiveTopology) =
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
194 result[0] = TMesh()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
195 result[1] = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
196 if primitive.hasKey("mode"):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
197 result[1] = PRIMITIVE_MODE_MAP[primitive["mode"].getInt()]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
198
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
199 for resultFieldName, resultValue in fieldPairs(result[0]):
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
200 for gltfAttribute, mappedName in fieldPairs(mapping):
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
201 when typeof(mappedName) is string:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
202 when gltfAttribute != "" and resultFieldName == mappedName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
203 assert resultValue is GPUData, "Attribute " & resultFieldName & " must be of type GPUData"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
204 when gltfAttribute == "indices":
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
205 if primitive.hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
206 let accessor = primitive[gltfAttribute].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
207 resultValue.data = getAccessorData[elementType(resultValue.data)](root, root["accessors"][accessor], mainBuffer)
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
208 elif gltfAttribute == "material":
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
209 if primitive.hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
210 resultValue.data = typeof(resultValue.data)(primitive[gltfAttribute].getInt())
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
211 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
212 if primitive["attributes"].hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
213 let accessor = primitive["attributes"][gltfAttribute].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
214 resultValue.data = getAccessorData[elementType(resultValue.data)](root, root["accessors"][accessor], mainBuffer)
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
215 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
216 var i = 0
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
217 for mappedIndexName in mappedName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
218 if gltfAttribute != "" and resultFieldName == mappedIndexName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
219 assert resultValue is GPUData, "Attribute " & resultFieldName & " must be of type GPUData"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
220 let gltfAttributeIndexed = gltfAttribute & "_" & $i
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
221 if primitive["attributes"].hasKey(gltfAttributeIndexed):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
222 let accessor = primitive["attributes"][gltfAttributeIndexed].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
223 resultValue.data = getAccessorData[elementType(resultValue.data)](root, root["accessors"][accessor], mainBuffer)
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
224 inc i
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
225
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
226 proc loadNode(node: JsonNode): GltfNode =
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
227 result.transform = Unit4
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
228 if "mesh" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
229 result.mesh = node["mesh"].getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
230 if "children" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
231 for child in items(node["children"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
232 result.children.add child.getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
233 if "matrix" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
234 for i in 0 ..< node["matrix"].len:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
235 result.transform[i] = node["matrix"][i].getFloat()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
236
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
237 var (t, r, s) = (Unit4, Unit4, Unit4)
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
238 if "translation" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
239 t = Translate(
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
240 float32(node["translation"][0].getFloat()),
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
241 float32(node["translation"][1].getFloat()),
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
242 float32(node["translation"][2].getFloat())
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
243 )
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
244 if "rotation" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
245 t = Rotate(
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
246 float32(node["rotation"][3].getFloat()),
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
247 NewVec3f(
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
248 float32(node["rotation"][0].getFloat()),
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
249 float32(node["rotation"][1].getFloat()),
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
250 float32(node["rotation"][2].getFloat())
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
251 )
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
252 )
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
253 if "scale" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
254 t = Scale(
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
255 float32(node["scale"][0].getFloat()),
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
256 float32(node["scale"][1].getFloat()),
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
257 float32(node["scale"][2].getFloat())
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
258 )
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
259
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
260 result.transform = t * r * s * result.transform
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
261
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
262 proc ReadglTF*[TMesh, TMaterial](
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
263 stream: Stream,
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
264 meshAttributesMapping: static MeshAttributeNames,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
265 materialAttributesMapping: static MaterialAttributeNames,
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
266 ): GltfMesh[TMesh, TMaterial] =
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
267 var
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
268 header: glTFHeader
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
269 data: glTFData
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
270
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
271 for name, value in fieldPairs(header):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
272 stream.read(value)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
273
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
274 assert header.magic == HEADER_MAGIC
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
275 assert header.version == 2
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
276
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
277 var chunkLength = stream.readUint32()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
278 assert stream.readUint32() == JSON_CHUNK
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
279 data.structuredContent = parseJson(stream.readStr(int(chunkLength)))
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
280
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
281 chunkLength = stream.readUint32()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
282 assert stream.readUint32() == BINARY_CHUNK
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
283 data.binaryBufferData.setLen(chunkLength)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
284 assert stream.readData(addr data.binaryBufferData[0], int(chunkLength)) == int(chunkLength)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
285
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
286 # check that the refered buffer is the same as the binary chunk
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
287 # external binary buffers are not supported
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
288 assert data.structuredContent["buffers"].len == 1
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
289 assert not data.structuredContent["buffers"][0].hasKey("uri")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
290 let bufferLenDiff = int(chunkLength) - data.structuredContent["buffers"][0]["byteLength"].getInt()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
291 assert 0 <= bufferLenDiff and bufferLenDiff <= 3 # binary buffer may be aligned to 4 bytes
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
292
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
293 debug "Loading mesh: ", data.structuredContent.pretty
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
294
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
295 if "materials" in data.structuredContent:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
296 for materialnode in items(data.structuredContent["materials"]):
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
297 result.materials.add loadMaterial[TMaterial](data.structuredContent, materialnode, materialAttributesMapping, data.binaryBufferData)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
298
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
299 if "textures" in data.structuredContent:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
300 for texturenode in items(data.structuredContent["textures"]):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
301 result.textures.add loadTexture(data.structuredContent, texturenode, data.binaryBufferData)
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
302
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
303 if "meshes" in data.structuredContent:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
304 for mesh in items(data.structuredContent["meshes"]):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
305 var primitives: seq[(TMesh, VkPrimitiveTopology)]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
306 for primitive in items(mesh["primitives"]):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
307 primitives.add loadPrimitive[TMesh](data.structuredContent, primitive, meshAttributesMapping, data.binaryBufferData)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
308 result.meshes.add primitives
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
309
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
310 if "nodes" in data.structuredContent:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
311 for node in items(data.structuredContent["nodes"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
312 result.nodes.add loadNode(node)
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
313
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
314 if "scenes" in data.structuredContent:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
315 for scene in items(data.structuredContent["scenes"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
316 if "nodes" in scene:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
317 var nodes: seq[int]
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
318 for nodeId in items(scene["nodes"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
319 nodes.add nodeId.getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
320 result.scenes.add nodes
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
321
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
322 proc LoadMeshes*[TMesh, TMaterial](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
323 path: string,
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
324 meshAttributesMapping: static MeshAttributeNames,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
325 materialAttributesMapping: static MaterialAttributeNames,
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
326 package = DEFAULT_PACKAGE
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
327 ): GltfMesh[TMesh, TMaterial] =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
328 ReadglTF[TMesh, TMaterial](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
329 stream = loadResource_intern(path, package = package),
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
330 meshAttributesMapping = meshAttributesMapping,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
331 materialAttributesMapping = materialAttributesMapping,
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
332 )