annotate semiconginev2/gltf.nim @ 1249:d83726af7abb

did: first triangles getting loaded from gltf
author sam <sam@basx.dev>
date Thu, 25 Jul 2024 22:41:24 +0700
parents 317bb5a73606
children 9ceb509af5ea
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
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
2 GLTFMesh*[TMesh, TMaterial] = object
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
3 scenes*: seq[seq[int]] # each scene has a seq of node indices
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
4 nodes*: seq[seq[int]] # each node has a seq of mesh indices
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
5 meshes*: seq[seq[(TMesh, VkPrimitiveTopology)]]
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
6 materials*: seq[TMaterial]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
7 textures*: seq[Image[BGRA]]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
8 glTFHeader = object
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
9 magic: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
10 version: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
11 length: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
12 glTFData = object
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
13 structuredContent: JsonNode
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
14 binaryBufferData: seq[uint8]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
15
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
16 MaterialAttributeNames* = object
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
17 # pbr
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
18 baseColorTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
19 baseColorTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
20 baseColorFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
21 metallicRoughnessTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
22 metallicRoughnessTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
23 metallicFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
24 roughnessFactor*: string
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
25
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
26 # other
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
27 normalTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
28 normalTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
29 occlusionTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
30 occlusionTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
31 emissiveTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
32 emissiveTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
33 emissiveFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
34
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
35 MeshAttributeNames* = object
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
36 POSITION*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
37 NORMAL*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
38 TANGENT*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
39 TEXCOORD*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
40 COLOR*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
41 JOINTS*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
42 WEIGHTS*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
43 indices*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
44 material*: string
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
45
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
46 #[
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
47 static:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
48 let TypeIds = {
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
49 int8: 5120,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
50 uint8: 5121,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
51 int16: 5122,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
52 uint16: 5123,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
53 uint32: 5125,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
54 float32: 5126,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
55 }.toTable
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
56 ]#
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
57
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
58 const
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
59 HEADER_MAGIC = 0x46546C67
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
60 JSON_CHUNK = 0x4E4F534A
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
61 BINARY_CHUNK = 0x004E4942
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
62 #[
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
63 ACCESSOR_TYPE_MAP = {
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
64 5120: Int8,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
65 5121: UInt8,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
66 5122: Int16,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
67 5123: UInt16,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
68 5125: UInt32,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
69 5126: Float32,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
70 }.toTable
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
71 ]#
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
72 SAMPLER_FILTER_MODE_MAP = {
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
73 9728: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
74 9729: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
75 9984: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
76 9985: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
77 9986: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
78 9987: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
79 }.toTable
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
80 SAMPLER_WRAP_MODE_MAP = {
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
81 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
82 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
83 10497: VK_SAMPLER_ADDRESS_MODE_REPEAT
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
84 }.toTable
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
85 PRIMITIVE_MODE_MAP = [
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
86 0: VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
87 1: VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
88 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
89 3: VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
90 4: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
91 5: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
92 6: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
93 ]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
94
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
95 #[
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
96 proc getGPUType(accessor: JsonNode, attribute: string): DataType =
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
97 # TODO: no full support for all datatypes that glTF may provide
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
98 # semicongine/core/gpu_data should maybe generated with macros to allow for all combinations
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
99 let componentType = ACCESSOR_TYPE_MAP[accessor["componentType"].getInt()]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
100 let theType = accessor["type"].getStr()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
101 case theType
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
102 of "SCALAR":
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
103 return componentType
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
104 of "VEC2":
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
105 case componentType
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
106 of UInt32: return Vec2U32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
107 of Float32: return Vec2F32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
108 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
109 of "VEC3":
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
110 case componentType
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
111 of UInt32: return Vec3U32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
112 of Float32: return Vec3F32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
113 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
114 of "VEC4":
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
115 case componentType
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
116 of UInt32: return Vec4U32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
117 of Float32: return Vec4F32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
118 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
119 of "MAT2":
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
120 case componentType
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
121 of Float32: return Vec4F32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
122 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
123 of "MAT3":
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
124 case componentType
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
125 of Float32: return Vec4F32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
126 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
127 of "MAT4":
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
128 case componentType
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
129 of Float32: return Vec4F32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
130 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
131 ]#
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 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
134 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
135
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
136 result = newSeq[uint8](bufferView["byteLength"].getInt())
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
137 let bufferOffset = bufferView["byteOffset"].getInt() + baseBufferOffset
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
138 var dstPointer = addr result[0]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
139
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
140 if bufferView.hasKey("byteStride"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
141 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
142 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
143
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
144 proc componentTypeId(t: typedesc): int =
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
145 if t is int8: return 5120
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
146 elif t is uint8: return 5121
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
147 elif t is int16: return 5122
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
148 elif t is uint16: return 5123
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
149 elif t is uint32: return 5125
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
150 elif t is float32: return 5126
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
151
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
152 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
153 let componentType = accessor["componentType"].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
154 let itemType = accessor["type"].getStr()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
155
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
156 when T is TVec or T is TMat:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
157 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
158 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
159 assert componentTypeId(T) == componentType, name(T) & " != " & $componentType
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
160
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
161 when T is TVec:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
162 when len(default(T)) == 2: assert itemType == "VEC2"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
163 elif len(default(T)) == 3: assert itemType == "VEC3"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
164 elif len(default(T)) == 4: assert itemType == "VEC4"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
165 elif T is TMat:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
166 when T is Mat2: assert itemType == "MAT2"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
167 elif T is Mat3: assert itemType == "MAT3"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
168 elif T is Mat4: assert itemType == "MAT4"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
169 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
170 assert itemType == "SCALAR"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
171
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
172 result.setLen(accessor["count"].getInt())
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
173
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
174 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
175 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
176
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
177 if accessor.hasKey("sparse"):
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
178 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
179
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
180 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
181 let length = bufferView["byteLength"].getInt()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
182 let bufferOffset = bufferView["byteOffset"].getInt() + accessorOffset
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
183 var dstPointer = result.ToCPointer()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
184
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
185 if bufferView.hasKey("byteStride"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
186 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
187 # 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
188 for i in 0 ..< result.len:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
189 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
190 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
191 else:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
192 copyMem(dstPointer, addr mainBuffer[bufferOffset], length)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
193
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
194 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
195
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
196 let imageIndex = textureNode["source"].getInt()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
197
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
198 if root["images"][imageIndex].hasKey("uri"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
199 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
200 let imageType = root["images"][imageIndex]["mimeType"].getStr()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
201 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
202
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
203 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
204 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
205
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
206 if textureNode.hasKey("sampler"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
207 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
208 if sampler.hasKey("magFilter"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
209 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
210 if sampler.hasKey("minFilter"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
211 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
212 if sampler.hasKey("wrapS"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
213 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
214 if sampler.hasKey("wrapT"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
215 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
216
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
217 proc getVec4f(node: JsonNode): Vec4f =
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
218 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
219
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
220 proc loadMaterial[TMaterial](
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
221 root: JsonNode,
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
222 materialNode: JsonNode,
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
223 mapping: static MaterialAttributeNames,
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
224 mainBuffer: seq[uint8],
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
225 ): TMaterial =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
226 result = TMaterial()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
227
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
228 let pbr = materialNode["pbrMetallicRoughness"]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
229 for name, value in fieldPairs(result):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
230 for gltfAttribute, mappedName in fieldPairs(mapping):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
231 when gltfAttribute != "" and name == mappedName:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
232 if pbr.hasKey(gltfAttribute):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
233 when gltfAttribute.endsWith("Texture"):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
234 value = typeof(value)(pbr[gltfAttribute]["index"].getInt())
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
235 elif gltfAttribute.endsWith("TextureUv"):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
236 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
237 elif gltfAttribute in ["baseColorFactor", "emissiveFactor"]:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
238 value = pbr[gltfAttribute].getVec4f()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
239 elif gltfAttribute in ["metallicFactor", "roughnessFactor"]:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
240 value = pbr[gltfAttribute].getFloat()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
241 else:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
242 {.error: "Unsupported gltf material attribute".}
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
243
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
244 proc loadPrimitive[TMesh](
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
245 root: JsonNode,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
246 primitive: JsonNode,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
247 mapping: static MeshAttributeNames,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
248 mainBuffer: seq[uint8]
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
249 ): (TMesh, VkPrimitiveTopology) =
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
250 result[0] = TMesh()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
251 result[1] = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
252 if primitive.hasKey("mode"):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
253 result[1] = PRIMITIVE_MODE_MAP[primitive["mode"].getInt()]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
254
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
255 for resultFieldName, resultValue in fieldPairs(result[0]):
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
256 for gltfAttribute, mappedName in fieldPairs(mapping):
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
257 when typeof(mappedName) is string:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
258 when gltfAttribute != "" and resultFieldName == mappedName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
259 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
260 when gltfAttribute == "indices":
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
261 if primitive.hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
262 let accessor = primitive[gltfAttribute].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
263 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
264 elif gltfAttribute == "material":
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
265 if primitive.hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
266 resultValue.data = typeof(resultValue.data)(primitive[gltfAttribute].getInt())
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
267 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
268 if primitive["attributes"].hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
269 let accessor = primitive["attributes"][gltfAttribute].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
270 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
271 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
272 var i = 0
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
273 for mappedIndexName in mappedName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
274 if gltfAttribute != "" and resultFieldName == mappedIndexName:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
275 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
276 let gltfAttributeIndexed = gltfAttribute & "_" & $i
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
277 if primitive["attributes"].hasKey(gltfAttributeIndexed):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
278 let accessor = primitive["attributes"][gltfAttributeIndexed].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
279 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
280 inc i
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
281 #[
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
282 when gltfAttribute == "indices":
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
283 if primitive.hasKey(gltfAttribute):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
284 let accessor = primitive[gltfAttribute].getInt()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
285 value.data = getAccessorData[elementType(value.data)](root, root["accessors"][accessor], mainBuffer)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
286 elif gltfAttribute == "material":
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
287 if primitive.hasKey(gltfAttribute):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
288 value.data = typeof(value.data)(primitive[gltfAttribute].getInt())
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
289 else:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
290 if primitive["attributes"].hasKey(gltfAttribute):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
291 let accessor = primitive["attributes"][gltfAttribute].getInt()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
292 value.data = getAccessorData[elementType(value.data)](root, root["accessors"][accessor], mainBuffer)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
293 ]#
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
294
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
295 #[
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
296 var indexType = None
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
297 let indexed = primitive.hasKey("indices")
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
298 if indexed:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
299 var indexCount = root["accessors"][primitive["indices"].getInt()]["count"].getInt()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
300 if indexCount < int(high(uint16)):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
301 indexType = Small
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
302 else:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
303 indexType = Big
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
304
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
305 for attribute, accessor in primitive["attributes"].pairs:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
306 let data = root.getAccessorData(root["accessors"][accessor.getInt()], mainBuffer)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
307 if result.vertexCount == 0:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
308 result.vertexCount = data.len
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
309 assert data.len == result.vertexCount
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
310 result[].InitVertexAttribute(attribute.toLowerAscii, data)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
311
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
312 if primitive.hasKey("material"):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
313 let materialId = primitive["material"].getInt()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
314 result[].material = materials[materialId]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
315 else:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
316 result[].material = EMPTY_MATERIAL.InitMaterialData()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
317
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
318 if primitive.hasKey("indices"):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
319 assert result[].indexType != None
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
320 let data = root.getAccessorData(root["accessors"][primitive["indices"].getInt()], mainBuffer)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
321 var tri: seq[int]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
322 case data.thetype
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
323 of UInt16:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
324 for entry in data[uint16][]:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
325 tri.add int(entry)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
326 if tri.len == 3:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
327 # FYI gltf uses counter-clockwise indexing
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
328 result[].AppendIndicesData(tri[0], tri[1], tri[2])
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
329 tri.setLen(0)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
330 of UInt32:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
331 for entry in data[uint32][]:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
332 tri.add int(entry)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
333 if tri.len == 3:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
334 # FYI gltf uses counter-clockwise indexing
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
335 result[].AppendIndicesData(tri[0], tri[1], tri[2])
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
336 tri.setLen(0)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
337 else:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
338 raise newException(Exception, &"Unsupported index data type: {data.thetype}")
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
339 ]#
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
340
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
341
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
342 #[
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
343
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
344 proc loadPrimitive(meshname: string, root: JsonNode, primitiveNode: JsonNode, materials: seq[MaterialData], mainBuffer: seq[uint8]): Mesh =
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
345 if primitiveNode.hasKey("mode") and primitiveNode["mode"].getInt() != 4:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
346 raise newException(Exception, "Currently only TRIANGLE mode is supported for geometry mode")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
347
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
348 var indexType = None
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
349 let indexed = primitiveNode.hasKey("indices")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
350 if indexed:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
351 # TODO: Tiny indices
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
352 var indexCount = root["accessors"][primitiveNode["indices"].getInt()]["count"].getInt()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
353 if indexCount < int(high(uint16)):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
354 indexType = Small
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
355 else:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
356 indexType = Big
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
357
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
358 result = Mesh(
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
359 instanceTransforms: @[Unit4F32],
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
360 indexType: indexType,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
361 name: meshname,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
362 vertexCount: 0,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
363 )
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
364
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
365 for attribute, accessor in primitiveNode["attributes"].pairs:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
366 let data = root.getAccessorData(root["accessors"][accessor.getInt()], mainBuffer)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
367 if result.vertexCount == 0:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
368 result.vertexCount = data.len
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
369 assert data.len == result.vertexCount
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
370 result[].InitVertexAttribute(attribute.toLowerAscii, data)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
371
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
372 if primitiveNode.hasKey("material"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
373 let materialId = primitiveNode["material"].getInt()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
374 result[].material = materials[materialId]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
375 else:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
376 result[].material = EMPTY_MATERIAL.InitMaterialData()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
377
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
378 if primitiveNode.hasKey("indices"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
379 assert result[].indexType != None
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
380 let data = root.getAccessorData(root["accessors"][primitiveNode["indices"].getInt()], mainBuffer)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
381 var tri: seq[int]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
382 case data.thetype
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
383 of UInt16:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
384 for entry in data[uint16][]:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
385 tri.add int(entry)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
386 if tri.len == 3:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
387 # FYI gltf uses counter-clockwise indexing
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
388 result[].AppendIndicesData(tri[0], tri[1], tri[2])
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
389 tri.setLen(0)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
390 of UInt32:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
391 for entry in data[uint32][]:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
392 tri.add int(entry)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
393 if tri.len == 3:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
394 # FYI gltf uses counter-clockwise indexing
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
395 result[].AppendIndicesData(tri[0], tri[1], tri[2])
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
396 tri.setLen(0)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
397 else:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
398 raise newException(Exception, &"Unsupported index data type: {data.thetype}")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
399 # TODO: getting from gltf to vulkan system is still messed up somehow, see other TODO
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
400 Transform[Vec3f](result[], "position", Scale(1, -1, 1))
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
401
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
402
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
403 proc loadNode(root: JsonNode, node: JsonNode, materials: seq[MaterialData], mainBuffer: var seq[uint8]): MeshTree =
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
404 result = MeshTree()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
405 # mesh
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
406 if node.hasKey("mesh"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
407 let mesh = root["meshes"][node["mesh"].getInt()]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
408 for primitive in mesh["primitives"]:
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
409 result.children.add MeshTree(mesh: loadPrimitive(mesh["name"].getStr(), root, primitive, materials, mainBuffer))
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
410
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
411 # transformation
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
412 if node.hasKey("matrix"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
413 var mat: Mat4
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
414 for i in 0 ..< node["matrix"].len:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
415 mat[i] = node["matrix"][i].getFloat()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
416 result.transform = mat
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
417 else:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
418 var (t, r, s) = (Unit4F32, Unit4F32, Unit4F32)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
419 if node.hasKey("translation"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
420 t = Translate(
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
421 float32(node["translation"][0].getFloat()),
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
422 float32(node["translation"][1].getFloat()),
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
423 float32(node["translation"][2].getFloat())
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
424 )
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
425 if node.hasKey("rotation"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
426 t = Rotate(
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
427 float32(node["rotation"][3].getFloat()),
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
428 NewVec3f(
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
429 float32(node["rotation"][0].getFloat()),
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
430 float32(node["rotation"][1].getFloat()),
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
431 float32(node["rotation"][2].getFloat())
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
432 )
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
433 )
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
434 if node.hasKey("scale"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
435 t = Scale(
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
436 float32(node["scale"][0].getFloat()),
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
437 float32(node["scale"][1].getFloat()),
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
438 float32(node["scale"][2].getFloat())
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
439 )
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
440 result.transform = t * r * s
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
441 result.transform = Scale(1, -1, 1) * result.transform
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
442
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
443 # children
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
444 if node.hasKey("children"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
445 for childNode in node["children"]:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
446 result.children.add loadNode(root, root["nodes"][childNode.getInt()], materials, mainBuffer)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
447
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
448 proc loadScene(root: JsonNode, scenenode: JsonNode, materials: seq[MaterialData], mainBuffer: var seq[uint8]): MeshTree =
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
449 result = MeshTree()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
450 for nodeId in scenenode["nodes"]:
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
451 result.children.add loadNode(root, root["nodes"][nodeId.getInt()], materials, mainBuffer)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
452 # TODO: getting from gltf to vulkan system is still messed up somehow (i.e. not consistent for different files), see other TODO
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
453 # result.transform = Scale(1, -1, 1)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
454 result.updateTransforms()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
455
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
456 ]#
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
457
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
458 proc ReadglTF*[TMesh, TMaterial](
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
459 stream: Stream,
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
460 meshAttributesMapping: static MeshAttributeNames,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
461 materialAttributesMapping: static MaterialAttributeNames,
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
462 ): GLTFMesh[TMesh, TMaterial] =
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
463 var
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
464 header: glTFHeader
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
465 data: glTFData
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
466
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
467 for name, value in fieldPairs(header):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
468 stream.read(value)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
469
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
470 assert header.magic == HEADER_MAGIC
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
471 assert header.version == 2
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
472
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
473 var chunkLength = stream.readUint32()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
474 assert stream.readUint32() == JSON_CHUNK
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
475 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
476
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
477 chunkLength = stream.readUint32()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
478 assert stream.readUint32() == BINARY_CHUNK
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
479 data.binaryBufferData.setLen(chunkLength)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
480 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
481
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
482 # 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
483 # external binary buffers are not supported
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
484 assert data.structuredContent["buffers"].len == 1
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
485 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
486 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
487 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
488
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
489 debug "Loading mesh: ", data.structuredContent.pretty
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
490
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
491 if "materials" in data.structuredContent:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
492 for materialnode in items(data.structuredContent["materials"]):
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
493 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
494
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
495 if "textures" in data.structuredContent:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
496 for texturenode in items(data.structuredContent["textures"]):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
497 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
498
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
499 if "meshes" in data.structuredContent:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
500 for mesh in items(data.structuredContent["meshes"]):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
501 var primitives: seq[(TMesh, VkPrimitiveTopology)]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
502 for primitive in items(mesh["primitives"]):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
503 primitives.add loadPrimitive[TMesh](data.structuredContent, primitive, meshAttributesMapping, data.binaryBufferData)
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
504 result.meshes.add primitives
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
505
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
506 echo "Textures:"
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
507 for t in result.textures:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
508 echo " ", t
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
509
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
510 echo "Materials:"
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
511 for m in result.materials:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
512 echo " ", m
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
513
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
514 echo "Meshes:"
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
515 for m in result.meshes:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
516 echo " Primitives:"
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
517 for p in m:
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
518 for field, value in fieldPairs(p[0]):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
519 if typeof(value) is GPUData:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
520 echo " ", field, ": ", value.data.len
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
521
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
522 proc LoadMeshes*[TMesh, TMaterial](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
523 path: string,
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
524 meshAttributesMapping: static MeshAttributeNames,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
525 materialAttributesMapping: static MaterialAttributeNames,
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
526 package = DEFAULT_PACKAGE
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
527 ): GLTFMesh[TMesh, TMaterial] =
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
528 ReadglTF[TMesh, TMaterial](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
529 stream = loadResource_intern(path, package = package),
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
530 meshAttributesMapping = meshAttributesMapping,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
531 materialAttributesMapping = materialAttributesMapping,
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
532 )