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