Mercurial > games > semicongine
annotate semiconginev2/gltf.nim @ 1247:c15770761865
add: gltf loading test, gltf loading for materials
author | sam <sam@basx.dev> |
---|---|
date | Wed, 24 Jul 2024 23:26:34 +0700 |
parents | d594b1d07d49 |
children | 317bb5a73606 |
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 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
5 meshes*: seq[TMesh] |
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 |
1245 | 16 MaterialAttributeNames = object |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
17 # pbr |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
18 baseColorTexture: string |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
19 baseColorTextureUv: string |
1245 | 20 baseColorFactor: string |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
21 metallicRoughnessTexture: string |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
22 metallicRoughnessTextureUv: string |
1245 | 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 |
1245 | 27 normalTexture: string |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
28 normalTextureUv: string |
1245 | 29 occlusionTexture: string |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
30 occlusionTextureUv: string |
1245 | 31 emissiveTexture: string |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
32 emissiveTextureUv: string |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
33 emissiveFactor: string |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
34 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
35 #[ |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
36 static: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
37 let TypeIds = { |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
38 int8: 5120, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
39 uint8: 5121, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
40 int16: 5122, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
41 uint16: 5123, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
42 uint32: 5125, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
43 float32: 5126, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
44 }.toTable |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
45 ]# |
1245 | 46 |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
47 const |
1245 | 48 HEADER_MAGIC = 0x46546C67 |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
49 JSON_CHUNK = 0x4E4F534A |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
50 BINARY_CHUNK = 0x004E4942 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
51 #[ |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
52 ACCESSOR_TYPE_MAP = { |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
53 5120: Int8, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
54 5121: UInt8, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
55 5122: Int16, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
56 5123: UInt16, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
57 5125: UInt32, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
58 5126: Float32, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
59 }.toTable |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
60 ]# |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
61 SAMPLER_FILTER_MODE_MAP = { |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
62 9728: VK_FILTER_NEAREST, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
63 9729: VK_FILTER_LINEAR, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
64 9984: VK_FILTER_NEAREST, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
65 9985: VK_FILTER_LINEAR, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
66 9986: VK_FILTER_NEAREST, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
67 9987: VK_FILTER_LINEAR, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
68 }.toTable |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
69 SAMPLER_WRAP_MODE_MAP = { |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
70 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
|
71 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
|
72 10497: VK_SAMPLER_ADDRESS_MODE_REPEAT |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
73 }.toTable |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
74 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
75 #[ |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
76 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
|
77 # 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
|
78 # 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
|
79 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
|
80 let theType = accessor["type"].getStr() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
81 case theType |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
82 of "SCALAR": |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
83 return componentType |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
84 of "VEC2": |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
85 case componentType |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
86 of UInt32: return Vec2U32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
87 of Float32: return Vec2F32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
88 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
|
89 of "VEC3": |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
90 case componentType |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
91 of UInt32: return Vec3U32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
92 of Float32: return Vec3F32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
93 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
|
94 of "VEC4": |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
95 case componentType |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
96 of UInt32: return Vec4U32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
97 of Float32: return Vec4F32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
98 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
|
99 of "MAT2": |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
100 case componentType |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
101 of Float32: return Vec4F32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
102 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
|
103 of "MAT3": |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
104 case componentType |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
105 of Float32: return Vec4F32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
106 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
|
107 of "MAT4": |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
108 case componentType |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
109 of Float32: return Vec4F32 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
110 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
|
111 ]# |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
112 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
113 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
|
114 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
|
115 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
116 result = newSeq[uint8](bufferView["byteLength"].getInt()) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
117 let bufferOffset = bufferView["byteOffset"].getInt() + baseBufferOffset |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
118 var dstPointer = addr result[0] |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
119 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
120 if bufferView.hasKey("byteStride"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
121 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
|
122 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
|
123 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
124 #[ |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
125 proc getAccessorData(root: JsonNode, accessor: JsonNode, mainBuffer: seq[uint8]): DataList = |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
126 result = InitDataList(thetype = accessor.getGPUType("??")) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
127 result.SetLen(accessor["count"].getInt()) |
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 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
|
130 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
|
131 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
132 if accessor.hasKey("sparse"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
133 raise newException(Exception, "Sparce accessors are currently not implemented") |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
134 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
135 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
|
136 let length = 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() + accessorOffset |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
138 var dstPointer = result.GetPointer() |
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 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
|
142 # 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
|
143 for i in 0 ..< int(result.len): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
144 copyMem(dstPointer, addr mainBuffer[bufferOffset + i * bufferView["byteStride"].getInt()], int(result.thetype.Size)) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
145 dstPointer = cast[pointer](cast[uint](dstPointer) + result.thetype.Size) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
146 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
147 copyMem(dstPointer, addr mainBuffer[bufferOffset], length) |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
148 ]# |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
149 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
150 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
|
151 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
152 let imageIndex = textureNode["source"].getInt() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
153 |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
154 if root["images"][imageIndex].hasKey("uri"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
155 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
|
156 let imageType = root["images"][imageIndex]["mimeType"].getStr() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
157 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
|
158 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
159 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
|
160 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
|
161 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
162 if textureNode.hasKey("sampler"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
163 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
|
164 if sampler.hasKey("magFilter"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
165 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
|
166 if sampler.hasKey("minFilter"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
167 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
|
168 if sampler.hasKey("wrapS"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
169 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
|
170 if sampler.hasKey("wrapT"): |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
171 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
|
172 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
173 proc getVec4f(node: JsonNode): Vec4f = |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
174 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
|
175 |
1245 | 176 proc loadMaterial[TMaterial]( |
177 root: JsonNode, | |
178 materialNode: JsonNode, | |
179 mainBuffer: seq[uint8], | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
180 mapping: static MaterialAttributeNames |
1245 | 181 ): TMaterial = |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
182 result = TMaterial() |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
183 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
184 let pbr = materialNode["pbrMetallicRoughness"] |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
185 for name, value in fieldPairs(result): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
186 for gltfAttribute, mappedName in fieldPairs(mapping): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
187 when gltfAttribute != "" and name == mappedName: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
188 if pbr.hasKey(gltfAttribute): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
189 when gltfAttribute.endsWith("Texture"): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
190 value = typeof(value)(pbr[gltfAttribute]["index"].getInt()) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
191 elif gltfAttribute.endsWith("TextureUv"): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
192 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
|
193 elif gltfAttribute in ["baseColorFactor", "emissiveFactor"]: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
194 value = pbr[gltfAttribute].getVec4f() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
195 elif gltfAttribute in ["metallicFactor", "roughnessFactor"]: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
196 value = pbr[gltfAttribute].getFloat() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
197 else: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
198 {.error: "Unsupported gltf material attribute".} |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
199 |
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 #[ |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
202 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
203 proc loadMesh(meshname: string, root: JsonNode, primitiveNode: JsonNode, materials: seq[MaterialData], mainBuffer: seq[uint8]): Mesh = |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
204 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
|
205 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
|
206 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
207 var indexType = None |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
208 let indexed = primitiveNode.hasKey("indices") |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
209 if indexed: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
210 # TODO: Tiny indices |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
211 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
|
212 if indexCount < int(high(uint16)): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
213 indexType = Small |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
214 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
215 indexType = Big |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
216 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
217 result = Mesh( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
218 instanceTransforms: @[Unit4F32], |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
219 indexType: indexType, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
220 name: meshname, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
221 vertexCount: 0, |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
222 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
223 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
224 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
|
225 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
|
226 if result.vertexCount == 0: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
227 result.vertexCount = data.len |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
228 assert data.len == result.vertexCount |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
229 result[].InitVertexAttribute(attribute.toLowerAscii, data) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
230 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
231 if primitiveNode.hasKey("material"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
232 let materialId = primitiveNode["material"].getInt() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
233 result[].material = materials[materialId] |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
234 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
235 result[].material = EMPTY_MATERIAL.InitMaterialData() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
236 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
237 if primitiveNode.hasKey("indices"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
238 assert result[].indexType != None |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
239 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
|
240 var tri: seq[int] |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
241 case data.thetype |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
242 of UInt16: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
243 for entry in data[uint16][]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
244 tri.add int(entry) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
245 if tri.len == 3: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
246 # FYI gltf uses counter-clockwise indexing |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
247 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
|
248 tri.setLen(0) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
249 of UInt32: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
250 for entry in data[uint32][]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
251 tri.add int(entry) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
252 if tri.len == 3: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
253 # FYI gltf uses counter-clockwise indexing |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
254 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
|
255 tri.setLen(0) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
256 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
257 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
|
258 # 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
|
259 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
|
260 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
261 |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
262 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
|
263 result = MeshTree() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
264 # mesh |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
265 if node.hasKey("mesh"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
266 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
|
267 for primitive in mesh["primitives"]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
268 result.children.add MeshTree(mesh: loadMesh(mesh["name"].getStr(), root, primitive, materials, mainBuffer)) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
269 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
270 # transformation |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
271 if node.hasKey("matrix"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
272 var mat: Mat4 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
273 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
|
274 mat[i] = node["matrix"][i].getFloat() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
275 result.transform = mat |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
276 else: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
277 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
|
278 if node.hasKey("translation"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
279 t = Translate( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
280 float32(node["translation"][0].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
281 float32(node["translation"][1].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
282 float32(node["translation"][2].getFloat()) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
283 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
284 if node.hasKey("rotation"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
285 t = Rotate( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
286 float32(node["rotation"][3].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
287 NewVec3f( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
288 float32(node["rotation"][0].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
289 float32(node["rotation"][1].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
290 float32(node["rotation"][2].getFloat()) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
291 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
292 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
293 if node.hasKey("scale"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
294 t = Scale( |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
295 float32(node["scale"][0].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
296 float32(node["scale"][1].getFloat()), |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
297 float32(node["scale"][2].getFloat()) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
298 ) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
299 result.transform = t * r * s |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
300 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
|
301 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
302 # children |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
303 if node.hasKey("children"): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
304 for childNode in node["children"]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
305 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
|
306 |
1245 | 307 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
|
308 result = MeshTree() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
309 for nodeId in scenenode["nodes"]: |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
310 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
|
311 # 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
|
312 # result.transform = Scale(1, -1, 1) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
313 result.updateTransforms() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
314 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
315 ]# |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
316 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
317 proc ReadglTF*[TMesh, TMaterial]( |
1245 | 318 stream: Stream, |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
319 baseColorFactor: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
320 emissiveFactor: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
321 metallicFactor: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
322 roughnessFactor: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
323 baseColorTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
324 metallicRoughnessTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
325 normalTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
326 occlusionTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
327 emissiveTexture: static string = "", |
1245 | 328 ): GLTFMesh[TMesh, TMaterial] = |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
329 const mapping = MaterialAttributeNames( |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
330 baseColorFactor: baseColorFactor, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
331 emissiveFactor: emissiveFactor, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
332 metallicFactor: metallicFactor, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
333 roughnessFactor: roughnessFactor, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
334 baseColorTexture: baseColorTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
335 metallicRoughnessTexture: metallicRoughnessTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
336 normalTexture: normalTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
337 occlusionTexture: occlusionTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
338 emissiveTexture: emissiveTexture, |
1245 | 339 ) |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
340 var |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
341 header: glTFHeader |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
342 data: glTFData |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
343 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
344 for name, value in fieldPairs(header): |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
345 stream.read(value) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
346 |
1245 | 347 assert header.magic == HEADER_MAGIC |
1243
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
348 assert header.version == 2 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
349 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
350 var chunkLength = stream.readUint32() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
351 assert stream.readUint32() == JSON_CHUNK |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
352 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
|
353 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
354 chunkLength = stream.readUint32() |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
355 assert stream.readUint32() == BINARY_CHUNK |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
356 data.binaryBufferData.setLen(chunkLength) |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
357 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
|
358 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
359 # 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
|
360 # external binary buffers are not supported |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
361 assert data.structuredContent["buffers"].len == 1 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
362 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
|
363 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
|
364 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
|
365 |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
366 debug "Loading mesh: ", data.structuredContent.pretty |
7e55fde39ca8
did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff
changeset
|
367 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
368 if "materials" in data.structuredContent: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
369 for materialnode in items(data.structuredContent["materials"]): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
370 result.materials.add loadMaterial[TMaterial](data.structuredContent, materialnode, data.binaryBufferData, mapping) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
371 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
372 if "textures" in data.structuredContent: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
373 for texturenode in items(data.structuredContent["textures"]): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
374 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
|
375 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
376 echo result |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
377 # for scenedata in data.structuredContent["scenes"]: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
378 # result.add data.structuredContent.loadScene(scenedata, materials, data.binaryBufferData) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
379 # |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
380 proc LoadMeshes*[TMesh, TMaterial]( |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
381 path: string, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
382 baseColorFactor: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
383 emissiveFactor: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
384 metallicFactor: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
385 roughnessFactor: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
386 baseColorTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
387 metallicRoughnessTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
388 normalTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
389 occlusionTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
390 emissiveTexture: static string = "", |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
391 package = DEFAULT_PACKAGE |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
392 ): GLTFMesh[TMesh, TMaterial] = |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
393 ReadglTF[TMesh, TMaterial]( |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
394 stream = loadResource_intern(path, package = package), |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
395 baseColorFactor = baseColorFactor, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
396 emissiveFactor = emissiveFactor, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
397 metallicFactor = metallicFactor, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
398 roughnessFactor = roughnessFactor, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
399 baseColorTexture = baseColorTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
400 metallicRoughnessTexture = metallicRoughnessTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
401 normalTexture = normalTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
402 occlusionTexture = occlusionTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
403 emissiveTexture = emissiveTexture, |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1245
diff
changeset
|
404 ) |