Mercurial > games > semicongine
annotate semiconginev2/gltf.nim @ 1248:317bb5a73606
did: continue on gltf importer
author | sam <sam@basx.dev> |
---|---|
date | Thu, 25 Jul 2024 20:23:54 +0700 |
parents | c15770761865 |
children | d83726af7abb |
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 | 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 | 16 MaterialAttributeNames* = object |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
17 # pbr |
1248 | 18 baseColorTexture*: string |
19 baseColorTextureUv*: string | |
20 baseColorFactor*: string | |
21 metallicRoughnessTexture*: string | |
22 metallicRoughnessTextureUv*: string | |
23 metallicFactor*: string | |
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 | 27 normalTexture*: string |
28 normalTextureUv*: string | |
29 occlusionTexture*: string | |
30 occlusionTextureUv*: string | |
31 emissiveTexture*: string | |
32 emissiveTextureUv*: string | |
33 emissiveFactor*: string | |
34 | |
35 MeshAttributeNames* = object | |
36 POSITION*: string | |
37 NORMAL*: string | |
38 TANGENT*: string | |
39 TEXCOORD*: seq[string] | |
40 COLOR*: seq[string] | |
41 JOINTS*: seq[string] | |
42 WEIGHTS*: seq[string] | |
43 indices*: string | |
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 | 57 |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
58 const |
1245 | 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 | 85 PRIMITIVE_MODE_MAP = [ |
86 0: VK_PRIMITIVE_TOPOLOGY_POINT_LIST, | |
87 1: VK_PRIMITIVE_TOPOLOGY_LINE_LIST, | |
88 2: VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, # not correct, as mode 2 would be a loo, but vulkan has no concept of this | |
89 3: VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, | |
90 4: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, | |
91 5: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, | |
92 6: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN, | |
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 |
1248 | 144 proc getAccessorData[T](root: JsonNode, accessor: JsonNode, mainBuffer: seq[uint8]): seq[T] = |
145 result.setLen(accessor["count"].getInt()) | |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
146 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
147 let bufferView = root["bufferViews"][accessor["bufferView"].getInt()] |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
148 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
|
149 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
150 if accessor.hasKey("sparse"): |
1248 | 151 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
|
152 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
153 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
|
154 let length = bufferView["byteLength"].getInt() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
155 let bufferOffset = bufferView["byteOffset"].getInt() + accessorOffset |
1248 | 156 var dstPointer = result.ToCPointer() |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
157 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
158 if bufferView.hasKey("byteStride"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
159 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
|
160 # we don't support stride, have to convert stuff here... does this even work? |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
161 for i in 0 ..< int(result.len): |
1248 | 162 copyMem(dstPointer, addr mainBuffer[bufferOffset + i * bufferView["byteStride"].getInt()], result.len * sizeof(T)) |
163 dstPointer = cast[typeof(dstPointer)](cast[uint](dstPointer) + (result.len * sizeof(T)).uint) | |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
164 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
165 copyMem(dstPointer, addr mainBuffer[bufferOffset], length) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
166 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
167 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
|
168 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
169 let imageIndex = textureNode["source"].getInt() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
170 |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
171 if root["images"][imageIndex].hasKey("uri"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
172 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
|
173 let imageType = root["images"][imageIndex]["mimeType"].getStr() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
174 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
|
175 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
176 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
|
177 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
|
178 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
179 if textureNode.hasKey("sampler"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
180 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
|
181 if sampler.hasKey("magFilter"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
182 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
|
183 if sampler.hasKey("minFilter"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
184 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
|
185 if sampler.hasKey("wrapS"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
186 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
|
187 if sampler.hasKey("wrapT"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
188 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
|
189 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
190 proc getVec4f(node: JsonNode): Vec4f = |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
191 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
|
192 |
1245 | 193 proc loadMaterial[TMaterial]( |
194 root: JsonNode, | |
195 materialNode: JsonNode, | |
196 mainBuffer: seq[uint8], | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
197 mapping: static MaterialAttributeNames |
1245 | 198 ): TMaterial = |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
199 result = TMaterial() |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
200 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
201 let pbr = materialNode["pbrMetallicRoughness"] |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
202 for name, value in fieldPairs(result): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
203 for gltfAttribute, mappedName in fieldPairs(mapping): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
204 when gltfAttribute != "" and name == mappedName: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
205 if pbr.hasKey(gltfAttribute): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
206 when gltfAttribute.endsWith("Texture"): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
207 value = typeof(value)(pbr[gltfAttribute]["index"].getInt()) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
208 elif gltfAttribute.endsWith("TextureUv"): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
209 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
|
210 elif gltfAttribute in ["baseColorFactor", "emissiveFactor"]: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
211 value = pbr[gltfAttribute].getVec4f() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
212 elif gltfAttribute in ["metallicFactor", "roughnessFactor"]: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
213 value = pbr[gltfAttribute].getFloat() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
214 else: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
215 {.error: "Unsupported gltf material attribute".} |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
216 |
1248 | 217 proc loadPrimitive[TMesh](root: JsonNode, primitive: JsonNode, mapping: static MeshAttributeNames, mainBuffer: seq[uint8]): (TMesh, VkPrimitiveTopology) = |
218 result[0] = TMesh() | |
219 result[1] = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST | |
220 if primitive.hasKey("mode"): | |
221 result[1] = PRIMITIVE_MODE_MAP[primitive["mode"].getInt()] | |
222 | |
223 for name, value in fieldPairs(result[0]): | |
224 for gltfAttribute, mappedName in fieldPairs(mapping): | |
225 when gltfAttribute != "" and name == mappedName: | |
226 assert value is GPUData, "Attribute " & name & " must be of type GPUData" | |
227 #[ | |
228 when gltfAttribute == "indices": | |
229 if primitive.hasKey(gltfAttribute): | |
230 let accessor = primitive[gltfAttribute].getInt() | |
231 value.data = getAccessorData[elementType(value.data)](root, root["accessors"][accessor], mainBuffer) | |
232 elif gltfAttribute == "material": | |
233 if primitive.hasKey(gltfAttribute): | |
234 value.data = typeof(value.data)(primitive[gltfAttribute].getInt()) | |
235 else: | |
236 if primitive["attributes"].hasKey(gltfAttribute): | |
237 let accessor = primitive["attributes"][gltfAttribute].getInt() | |
238 value.data = getAccessorData[elementType(value.data)](root, root["accessors"][accessor], mainBuffer) | |
239 ]# | |
240 | |
241 #[ | |
242 var indexType = None | |
243 let indexed = primitive.hasKey("indices") | |
244 if indexed: | |
245 var indexCount = root["accessors"][primitive["indices"].getInt()]["count"].getInt() | |
246 if indexCount < int(high(uint16)): | |
247 indexType = Small | |
248 else: | |
249 indexType = Big | |
250 | |
251 for attribute, accessor in primitive["attributes"].pairs: | |
252 let data = root.getAccessorData(root["accessors"][accessor.getInt()], mainBuffer) | |
253 if result.vertexCount == 0: | |
254 result.vertexCount = data.len | |
255 assert data.len == result.vertexCount | |
256 result[].InitVertexAttribute(attribute.toLowerAscii, data) | |
257 | |
258 if primitive.hasKey("material"): | |
259 let materialId = primitive["material"].getInt() | |
260 result[].material = materials[materialId] | |
261 else: | |
262 result[].material = EMPTY_MATERIAL.InitMaterialData() | |
263 | |
264 if primitive.hasKey("indices"): | |
265 assert result[].indexType != None | |
266 let data = root.getAccessorData(root["accessors"][primitive["indices"].getInt()], mainBuffer) | |
267 var tri: seq[int] | |
268 case data.thetype | |
269 of UInt16: | |
270 for entry in data[uint16][]: | |
271 tri.add int(entry) | |
272 if tri.len == 3: | |
273 # FYI gltf uses counter-clockwise indexing | |
274 result[].AppendIndicesData(tri[0], tri[1], tri[2]) | |
275 tri.setLen(0) | |
276 of UInt32: | |
277 for entry in data[uint32][]: | |
278 tri.add int(entry) | |
279 if tri.len == 3: | |
280 # FYI gltf uses counter-clockwise indexing | |
281 result[].AppendIndicesData(tri[0], tri[1], tri[2]) | |
282 tri.setLen(0) | |
283 else: | |
284 raise newException(Exception, &"Unsupported index data type: {data.thetype}") | |
285 ]# | |
286 | |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
287 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
288 #[ |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
289 |
1248 | 290 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
|
291 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
|
292 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
|
293 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
294 var indexType = None |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
295 let indexed = primitiveNode.hasKey("indices") |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
296 if indexed: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
297 # TODO: Tiny indices |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
298 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
|
299 if indexCount < int(high(uint16)): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
300 indexType = Small |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
301 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
302 indexType = Big |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
303 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
304 result = Mesh( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
305 instanceTransforms: @[Unit4F32], |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
306 indexType: indexType, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
307 name: meshname, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
308 vertexCount: 0, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
309 ) |
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 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
|
312 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
|
313 if result.vertexCount == 0: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
314 result.vertexCount = data.len |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
315 assert data.len == result.vertexCount |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
316 result[].InitVertexAttribute(attribute.toLowerAscii, data) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
317 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
318 if primitiveNode.hasKey("material"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
319 let materialId = primitiveNode["material"].getInt() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
320 result[].material = materials[materialId] |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
321 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
322 result[].material = EMPTY_MATERIAL.InitMaterialData() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
323 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
324 if primitiveNode.hasKey("indices"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
325 assert result[].indexType != None |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
326 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
|
327 var tri: seq[int] |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
328 case data.thetype |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
329 of UInt16: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
330 for entry in data[uint16][]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
331 tri.add int(entry) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
332 if tri.len == 3: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
333 # FYI gltf uses counter-clockwise indexing |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
334 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
|
335 tri.setLen(0) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
336 of UInt32: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
337 for entry in data[uint32][]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
338 tri.add int(entry) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
339 if tri.len == 3: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
340 # FYI gltf uses counter-clockwise indexing |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
341 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
|
342 tri.setLen(0) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
343 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
344 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
|
345 # 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
|
346 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
|
347 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
348 |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
349 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
|
350 result = MeshTree() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
351 # mesh |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
352 if node.hasKey("mesh"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
353 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
|
354 for primitive in mesh["primitives"]: |
1248 | 355 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
|
356 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
357 # transformation |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
358 if node.hasKey("matrix"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
359 var mat: Mat4 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
360 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
|
361 mat[i] = node["matrix"][i].getFloat() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
362 result.transform = mat |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
363 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
364 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
|
365 if node.hasKey("translation"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
366 t = Translate( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
367 float32(node["translation"][0].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
368 float32(node["translation"][1].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
369 float32(node["translation"][2].getFloat()) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
370 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
371 if node.hasKey("rotation"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
372 t = Rotate( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
373 float32(node["rotation"][3].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
374 NewVec3f( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
375 float32(node["rotation"][0].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
376 float32(node["rotation"][1].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
377 float32(node["rotation"][2].getFloat()) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
378 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
379 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
380 if node.hasKey("scale"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
381 t = Scale( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
382 float32(node["scale"][0].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
383 float32(node["scale"][1].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
384 float32(node["scale"][2].getFloat()) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
385 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
386 result.transform = t * r * s |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
387 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
|
388 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
389 # children |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
390 if node.hasKey("children"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
391 for childNode in node["children"]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
392 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
|
393 |
1245 | 394 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
|
395 result = MeshTree() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
396 for nodeId in scenenode["nodes"]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
397 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
|
398 # 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
|
399 # result.transform = Scale(1, -1, 1) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
400 result.updateTransforms() |
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 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
404 proc ReadglTF*[TMesh, TMaterial]( |
1245 | 405 stream: Stream, |
1248 | 406 meshAttributesMapping: static MeshAttributeNames, |
407 materialAttributesMapping: static MaterialAttributeNames, | |
1245 | 408 ): GLTFMesh[TMesh, TMaterial] = |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
409 var |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
410 header: glTFHeader |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
411 data: glTFData |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
412 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
413 for name, value in fieldPairs(header): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
414 stream.read(value) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
415 |
1245 | 416 assert header.magic == HEADER_MAGIC |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
417 assert header.version == 2 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
418 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
419 var chunkLength = stream.readUint32() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
420 assert stream.readUint32() == JSON_CHUNK |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
421 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
|
422 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
423 chunkLength = stream.readUint32() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
424 assert stream.readUint32() == BINARY_CHUNK |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
425 data.binaryBufferData.setLen(chunkLength) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
426 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
|
427 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
428 # 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
|
429 # external binary buffers are not supported |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
430 assert data.structuredContent["buffers"].len == 1 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
431 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
|
432 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
|
433 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
|
434 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
435 debug "Loading mesh: ", data.structuredContent.pretty |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
436 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
437 if "materials" in data.structuredContent: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
438 for materialnode in items(data.structuredContent["materials"]): |
1248 | 439 result.materials.add loadMaterial[TMaterial](data.structuredContent, materialnode, data.binaryBufferData, materialAttributesMapping) |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
440 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
441 if "textures" in data.structuredContent: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
442 for texturenode in items(data.structuredContent["textures"]): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
443 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
|
444 |
1248 | 445 if "meshes" in data.structuredContent: |
446 for mesh in items(data.structuredContent["meshes"]): | |
447 var primitives: seq[(TMesh, VkPrimitiveTopology)] | |
448 for primitive in items(mesh["primitives"]): | |
449 primitives.add loadPrimitive[TMesh](data.structuredContent, primitive, meshAttributesMapping, data.binaryBufferData) | |
450 result.meshes.add primitives | |
451 | |
452 echo "Textures:" | |
453 for t in result.textures: | |
454 echo " ", t | |
455 | |
456 echo "Materials:" | |
457 for m in result.materials: | |
458 echo " ", m | |
459 | |
460 echo "Meshes:" | |
461 for m in result.meshes: | |
462 echo " Primitives:" | |
463 for p in m: | |
464 echo " ", p[1], ": ", p[0] | |
465 | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
466 proc LoadMeshes*[TMesh, TMaterial]( |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
467 path: string, |
1248 | 468 meshAttributesMapping: static MeshAttributeNames, |
469 materialAttributesMapping: static MaterialAttributeNames, | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
470 package = DEFAULT_PACKAGE |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
471 ): GLTFMesh[TMesh, TMaterial] = |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
472 ReadglTF[TMesh, TMaterial]( |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
473 stream = loadResource_intern(path, package = package), |
1248 | 474 meshAttributesMapping = meshAttributesMapping, |
475 materialAttributesMapping = materialAttributesMapping, | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
476 ) |