annotate semiconginev2/gltf.nim @ 1245:d594b1d07d49

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