annotate semiconginev2/gltf.nim @ 1254:b0f4c8ccd49a

did: stuff to test gltf importer
author sam <sam@basx.dev>
date Sat, 27 Jul 2024 20:47:54 +0700
parents c4f98eb4bb05
children 2b5ca798f6d6
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
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
3 children*: seq[int]
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
4 mesh*: int = -1
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
5 transform*: Mat4 = Unit4
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
6 GltfData*[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
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
77 proc getBufferViewData(bufferView: JsonNode, mainBuffer: seq[uint8],
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
78 baseBufferOffset = 0): seq[uint8] =
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
79 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
80
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
81 result = newSeq[uint8](bufferView["byteLength"].getInt())
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
82 let bufferOffset = bufferView["byteOffset"].getInt() + baseBufferOffset
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
83 var dstPointer = addr result[0]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
84
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
85 if bufferView.hasKey("byteStride"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
86 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
87 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
88
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
89 proc componentTypeId(t: typedesc): int =
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
90 if t is int8: return 5120
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
91 elif t is uint8: return 5121
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
92 elif t is int16: return 5122
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
93 elif t is uint16: return 5123
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
94 elif t is uint32: return 5125
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
95 elif t is float32: return 5126
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
96
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
97 proc getAccessorData[T](root: JsonNode, accessor: JsonNode, mainBuffer: seq[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
98 uint8]): seq[T] =
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
99 let componentType = accessor["componentType"].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
100 let itemType = accessor["type"].getStr()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
101
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
102 when T is TVec or T is TMat:
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
103 assert componentTypeId(elementType(default(T))) == componentType, name(T) &
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
104 " != " & $componentType
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
105 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
106 assert componentTypeId(T) == componentType, name(T) & " != " & $componentType
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
107
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
108 when T is TVec:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
109 when len(default(T)) == 2: assert itemType == "VEC2"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
110 elif len(default(T)) == 3: assert itemType == "VEC3"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
111 elif len(default(T)) == 4: assert itemType == "VEC4"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
112 elif T is TMat:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
113 when T is Mat2: assert itemType == "MAT2"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
114 elif T is Mat3: assert itemType == "MAT3"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
115 elif T is Mat4: assert itemType == "MAT4"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
116 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
117 assert itemType == "SCALAR"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
118
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
119 result.setLen(accessor["count"].getInt())
1243
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 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
122 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
123
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
124 if accessor.hasKey("sparse"):
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
125 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
126
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
127 let accessorOffset = if accessor.hasKey("byteOffset"): accessor[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
128 "byteOffset"].getInt() else: 0
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
129 let length = bufferView["byteLength"].getInt()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
130 let bufferOffset = bufferView["byteOffset"].getInt() + accessorOffset
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
131 var dstPointer = result.ToCPointer()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
132
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
133 if bufferView.hasKey("byteStride"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
134 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
135 # 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
136 for i in 0 ..< result.len:
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
137 copyMem(dstPointer, addr mainBuffer[bufferOffset + i * bufferView[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
138 "byteStride"].getInt()], sizeof(T))
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
139 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
140 else:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
141 copyMem(dstPointer, addr mainBuffer[bufferOffset], length)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
142
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
143 proc loadTexture(root: JsonNode, textureNode: JsonNode, mainBuffer: seq[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
144 uint8]): Image[BGRA] =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
145
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
146 let imageIndex = textureNode["source"].getInt()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
147
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
148 if root["images"][imageIndex].hasKey("uri"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
149 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
150 let imageType = root["images"][imageIndex]["mimeType"].getStr()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
151 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
152
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
153 let bufferView = root["bufferViews"][root["images"][imageIndex][
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
154 "bufferView"].getInt()]
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1251
diff changeset
155 result = LoadImageData[BGRA](getBufferViewData(bufferView, mainBuffer))
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
156
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
157 if textureNode.hasKey("sampler"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
158 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
159 if sampler.hasKey("magFilter"):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
160 result.magInterpolation = SAMPLER_FILTER_MODE_MAP[sampler[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
161 "magFilter"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
162 if sampler.hasKey("minFilter"):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
163 result.minInterpolation = SAMPLER_FILTER_MODE_MAP[sampler[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
164 "minFilter"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
165 if sampler.hasKey("wrapS"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
166 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
167 if sampler.hasKey("wrapT"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
168 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
169
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
170 proc getVec4f(node: JsonNode): Vec4f =
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
171 NewVec4f(node[0].getFloat(), node[1].getFloat(), node[2].getFloat(), node[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
172 3].getFloat())
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
173
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
174 proc loadMaterial[TMaterial](
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
175 root: JsonNode,
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
176 materialNode: JsonNode,
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
177 mapping: static MaterialAttributeNames,
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
178 mainBuffer: seq[uint8],
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
179 ): TMaterial =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
180 result = TMaterial()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
181
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
182 let pbr = materialNode["pbrMetallicRoughness"]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
183 for name, value in fieldPairs(result):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
184 for gltfAttribute, mappedName in fieldPairs(mapping):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
185 when gltfAttribute != "" and name == mappedName:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
186 if pbr.hasKey(gltfAttribute):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
187 when gltfAttribute.endsWith("Texture"):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
188 value = typeof(value)(pbr[gltfAttribute]["index"].getInt())
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
189 elif gltfAttribute.endsWith("TextureUv"):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
190 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
191 elif gltfAttribute in ["baseColorFactor", "emissiveFactor"]:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
192 value = pbr[gltfAttribute].getVec4f()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
193 elif gltfAttribute in ["metallicFactor", "roughnessFactor"]:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
194 value = pbr[gltfAttribute].getFloat()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
195 else:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
196 {.error: "Unsupported gltf material attribute".}
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
197
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
198 proc loadPrimitive[TMesh](
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
199 root: JsonNode,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
200 primitive: JsonNode,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
201 mapping: static MeshAttributeNames,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
202 mainBuffer: seq[uint8]
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
203 ): (TMesh, VkPrimitiveTopology) =
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
204 result[0] = TMesh()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
205 result[1] = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
206 if primitive.hasKey("mode"):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
207 result[1] = PRIMITIVE_MODE_MAP[primitive["mode"].getInt()]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
208
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
209 for resultFieldName, resultValue in fieldPairs(result[0]):
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
210 for gltfAttribute, mappedName in fieldPairs(mapping):
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
211 when typeof(mappedName) is string:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
212 when gltfAttribute != "" and resultFieldName == mappedName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
213 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
214 when gltfAttribute == "indices":
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
215 if primitive.hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
216 let accessor = primitive[gltfAttribute].getInt()
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
217 resultValue.data = getAccessorData[elementType(resultValue.data)](
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
218 root, root["accessors"][accessor], mainBuffer)
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
219 elif gltfAttribute == "material":
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
220 if primitive.hasKey(gltfAttribute):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
221 resultValue.data = typeof(resultValue.data)(primitive[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
222 gltfAttribute].getInt())
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
223 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
224 if primitive["attributes"].hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
225 let accessor = primitive["attributes"][gltfAttribute].getInt()
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
226 resultValue.data = getAccessorData[elementType(resultValue.data)](
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
227 root, root["accessors"][accessor], mainBuffer)
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
228 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
229 var i = 0
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
230 for mappedIndexName in mappedName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
231 if gltfAttribute != "" and resultFieldName == mappedIndexName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
232 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
233 let gltfAttributeIndexed = gltfAttribute & "_" & $i
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
234 if primitive["attributes"].hasKey(gltfAttributeIndexed):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
235 let accessor = primitive["attributes"][
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
236 gltfAttributeIndexed].getInt()
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
237 resultValue.data = getAccessorData[elementType(resultValue.data)](
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
238 root, root["accessors"][accessor], mainBuffer)
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
239 inc i
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
240
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
241 proc loadNode(node: JsonNode): GltfNode =
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
242 result = GltfNode()
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
243 if "mesh" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
244 result.mesh = node["mesh"].getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
245 if "children" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
246 for child in items(node["children"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
247 result.children.add child.getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
248 if "matrix" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
249 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
250 result.transform[i] = node["matrix"][i].getFloat()
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
251 result.transform = result.transform.Transposed()
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
252 else:
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
253 var (t, r, s) = (Unit4, Unit4, Unit4)
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
254 if "translation" in node:
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
255 t = Translate(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
256 float32(node["translation"][0].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
257 float32(node["translation"][1].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
258 float32(node["translation"][2].getFloat())
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
259 )
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
260 if "rotation" in node:
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
261 t = Rotate(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
262 float32(node["rotation"][3].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
263 NewVec3f(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
264 float32(node["rotation"][0].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
265 float32(node["rotation"][1].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
266 float32(node["rotation"][2].getFloat())
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
267 )
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
268 )
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
269 if "scale" in node:
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
270 t = Scale(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
271 float32(node["scale"][0].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
272 float32(node["scale"][1].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
273 float32(node["scale"][2].getFloat())
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
274 )
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
275
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
276 result.transform = t * r * s
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
277
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
278 proc ReadglTF*[TMesh, TMaterial](
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
279 stream: Stream,
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
280 meshAttributesMapping: static MeshAttributeNames,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
281 materialAttributesMapping: static MaterialAttributeNames,
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
282 ): GltfData[TMesh, TMaterial] =
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
283 var
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
284 header: glTFHeader
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
285 data: glTFData
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
286
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
287 for name, value in fieldPairs(header):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
288 stream.read(value)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
289
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
290 assert header.magic == HEADER_MAGIC
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
291 assert header.version == 2
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 var chunkLength = stream.readUint32()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
294 assert stream.readUint32() == JSON_CHUNK
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
295 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
296
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
297 chunkLength = stream.readUint32()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
298 assert stream.readUint32() == BINARY_CHUNK
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
299 data.binaryBufferData.setLen(chunkLength)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
300 assert stream.readData(addr data.binaryBufferData[0], int(chunkLength)) ==
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
301 int(chunkLength)
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
302
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
303 # 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
304 # external binary buffers are not supported
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
305 assert data.structuredContent["buffers"].len == 1
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
306 assert not data.structuredContent["buffers"][0].hasKey("uri")
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
307 let bufferLenDiff = int(chunkLength) - data.structuredContent["buffers"][0][
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
308 "byteLength"].getInt()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
309 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
310
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
311 debug "Loading mesh: ", data.structuredContent.pretty
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
312
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
313 if "materials" in data.structuredContent:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
314 for materialnode in items(data.structuredContent["materials"]):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
315 result.materials.add loadMaterial[TMaterial](data.structuredContent,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
316 materialnode, materialAttributesMapping, data.binaryBufferData)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
317
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
318 if "textures" in data.structuredContent:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
319 for texturenode in items(data.structuredContent["textures"]):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
320 result.textures.add loadTexture(data.structuredContent, texturenode,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
321 data.binaryBufferData)
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
322
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
323 if "meshes" in data.structuredContent:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
324 for mesh in items(data.structuredContent["meshes"]):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
325 var primitives: seq[(TMesh, VkPrimitiveTopology)]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
326 for primitive in items(mesh["primitives"]):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
327 primitives.add loadPrimitive[TMesh](data.structuredContent, primitive,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
328 meshAttributesMapping, data.binaryBufferData)
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
329 result.meshes.add primitives
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
330
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
331 if "nodes" in data.structuredContent:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
332 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
333 result.nodes.add loadNode(node)
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
334
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
335 if "scenes" in data.structuredContent:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
336 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
337 if "nodes" in scene:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
338 var nodes: seq[int]
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
339 for nodeId in items(scene["nodes"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
340 nodes.add nodeId.getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
341 result.scenes.add nodes
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
342
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
343 proc LoadMeshes*[TMesh, TMaterial](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
344 path: string,
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
345 meshAttributesMapping: static MeshAttributeNames,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
346 materialAttributesMapping: static MaterialAttributeNames,
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
347 package = DEFAULT_PACKAGE
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
348 ): GltfData[TMesh, TMaterial] =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
349 ReadglTF[TMesh, TMaterial](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
350 stream = loadResource_intern(path, package = package),
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
351 meshAttributesMapping = meshAttributesMapping,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
352 materialAttributesMapping = materialAttributesMapping,
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
353 )