annotate semicongine/mesh.nim @ 950:fe48b091e83f

did: tons of small improvments, on the way to make GPU sync (more) correct I guess
author sam <sam@basx.dev>
date Sun, 31 Mar 2024 22:11:50 +0700
parents c66503386e8b
children c104dbf5bbb8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/hashes
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/options
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/typetraits
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/tables
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/strformat
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
6 import std/enumerate
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
7 import std/strutils
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
8 import std/sequtils
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
9
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
10 import ./core
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
11 import ./collision
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
12 import ./material
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
13
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
14 const DEFAULT_POSITION_ATTRIBUTE = "position"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
15
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
16 var instanceCounter* = 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
17
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
18 type
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
19 MeshIndexType* = enum
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
20 None
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
21 Tiny # up to 2^8 vertices # TODO: need to check and enable support for this
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
22 Small # up to 2^16 vertices
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
23 Big # up to 2^32 vertices
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
24 MeshObject* = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25 name*: string
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
26 vertexCount*: int
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
27 case indexType*: MeshIndexType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
28 of None: discard
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
29 of Tiny: tinyIndices*: seq[array[3, uint8]]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
30 of Small: smallIndices*: seq[array[3, uint16]]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
31 of Big: bigIndices*: seq[array[3, uint32]]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
32 material*: MaterialData
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
33 transform*: Mat4 = Unit4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
34 instanceTransforms*: seq[Mat4]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
35 applyMeshTransformToInstances*: bool = true # if true, the transform attribute for the shader will apply the instance transform AND the mesh transform, to each instance
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
36 visible*: bool = true
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
37 transformCache: seq[Mat4]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
38 vertexData: Table[string, DataList]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
39 instanceData: Table[string, DataList]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
40 dirtyAttributes: seq[string]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
41 Mesh* = ref MeshObject
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
42
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
43 func material*(mesh: MeshObject): MaterialData =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
44 mesh.material
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
45
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
46 func `material=`*(mesh: var MeshObject, material: MaterialData) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
47 for name, theType in material.theType.vertexAttributes:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
48 if mesh.vertexData.contains(name):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
49 assert mesh.vertexData[name].theType == theType, &"{material.theType} expected mesh attribute '{name}' to be '{theType}' but it is {mesh.vertexData[name].theType}"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
50 elif mesh.instanceData.contains(name):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
51 assert mesh.instanceData[name].theType == theType, &"{material.theType} expected mesh attribute '{name}' to be '{theType}' but it is {mesh.instanceData[name].theType}"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
52 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
53 assert false, &"Mesh '{mesh.name}' is missing required mesh attribute '{name}: {theType}' for {material.theType}"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
54 mesh.material = material
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
55
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
56 func instanceCount*(mesh: MeshObject): int =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
57 mesh.instanceTransforms.len
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
58
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
59 func indicesCount*(mesh: MeshObject): int =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
60 (
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
61 case mesh.indexType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
62 of None: 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
63 of Tiny: mesh.tinyIndices.len
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
64 of Small: mesh.smallIndices.len
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
65 of Big: mesh.bigIndices.len
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
66 ) * 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
67
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
68 func `$`*(mesh: MeshObject): string =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
69 if mesh.indexType == None:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
70 &"Mesh('{mesh.name}', vertexCount: {mesh.vertexCount}, instanceCount: {mesh.instanceCount}, vertexData: {mesh.vertexData.keys().toSeq()}, instanceData: {mesh.instanceData.keys().toSeq()}, indexType: {mesh.indexType}, material: {mesh.material})"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
71 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
72 &"Mesh('{mesh.name}', vertexCount: {mesh.vertexCount}, indexCount: {mesh.indicesCount}, instanceCount: {mesh.instanceCount}, vertexData: {mesh.vertexData.keys().toSeq()}, instanceData: {mesh.instanceData.keys().toSeq()}, indexType: {mesh.indexType}, material: {mesh.material})"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
73 func `$`*(mesh: Mesh): string =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
74 $mesh[]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
75
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
76 func vertexAttributes*(mesh: MeshObject): seq[string] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
77 mesh.vertexData.keys.toSeq
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
78
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
79 func instanceAttributes*(mesh: MeshObject): seq[string] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
80 mesh.instanceData.keys.toSeq
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
81
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
82 func attributes*(mesh: MeshObject): seq[string] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
83 mesh.vertexAttributes & mesh.instanceAttributes
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
84
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
85 func hash*(mesh: Mesh): Hash =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
86 hash(cast[ptr MeshObject](mesh))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
87
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
88 converter toVulkan*(indexType: MeshIndexType): VkIndexType =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
89 case indexType:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
90 of None: VK_INDEX_TYPE_NONE_KHR
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
91 of Tiny: VK_INDEX_TYPE_UINT8_EXT
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
92 of Small: VK_INDEX_TYPE_UINT16
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
93 of Big: VK_INDEX_TYPE_UINT32
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
94
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
95 proc initVertexAttribute*[T](mesh: var MeshObject, attribute: string, value: seq[T]) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
96 assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute)
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
97 mesh.vertexData[attribute] = initDataList(thetype = getDataType[T]())
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
98 mesh.vertexData[attribute].setLen(mesh.vertexCount)
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
99 mesh.vertexData[attribute] = value
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
100 # `=`(mesh.vertexData[attribute], value)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
101 proc initVertexAttribute*[T](mesh: var MeshObject, attribute: string, value: T) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
102 initVertexAttribute(mesh, attribute, newSeqWith(mesh.vertexCount, value))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
103 proc initVertexAttribute*[T](mesh: var MeshObject, attribute: string) =
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
104 initVertexAttribute(mesh = mesh, attribute = attribute, value = default(T))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
105 proc initVertexAttribute*(mesh: var MeshObject, attribute: string, datatype: DataType) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
106 assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute)
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
107 mesh.vertexData[attribute] = initDataList(thetype = datatype)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
108 mesh.vertexData[attribute].setLen(mesh.vertexCount)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
109 proc initVertexAttribute*(mesh: var MeshObject, attribute: string, data: DataList) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
110 assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
111 mesh.vertexData[attribute] = data
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
112
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
113
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
114 proc initInstanceAttribute*[T](mesh: var MeshObject, attribute: string, value: seq[T]) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
115 assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute)
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
116 mesh.instanceData[attribute] = initDataList(thetype = getDataType[T]())
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
117 mesh.instanceData[attribute].setLen(mesh.instanceCount)
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
118 mesh.instanceData[attribute] = value
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
119 proc initInstanceAttribute*[T](mesh: var MeshObject, attribute: string, value: T) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
120 initInstanceAttribute(mesh, attribute, newSeqWith(mesh.instanceCount, value))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
121 proc initInstanceAttribute*[T](mesh: var MeshObject, attribute: string) =
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
122 initInstanceAttribute(mesh = mesh, attribute = attribute, value = default(T))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
123 proc initInstanceAttribute*(mesh: var MeshObject, attribute: string, datatype: DataType) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
124 assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute)
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
125 mesh.instanceData[attribute] = initDataList(thetype = datatype)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
126 mesh.instanceData[attribute].setLen(mesh.instanceCount)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
127 proc initInstanceAttribute*(mesh: var MeshObject, attribute: string, data: DataList) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
128 assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
129 mesh.instanceData[attribute] = data
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
130
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
131 proc newMesh*(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
132 positions: openArray[Vec3f],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
133 indices: openArray[array[3, uint32|uint16|uint8]],
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
134 colors: openArray[Vec4f] = [],
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
135 uvs: openArray[Vec2f] = [],
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
136 transform: Mat4 = Unit4,
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
137 instanceTransforms: openArray[Mat4] = [Unit4],
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
138 material = EMPTY_MATERIAL.initMaterialData(),
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
139 autoResize = true,
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
140 name: string = ""
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
141 ): Mesh =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
142 assert colors.len == 0 or colors.len == positions.len
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
143 assert uvs.len == 0 or uvs.len == positions.len
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
144 var theName = name
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
145 if theName == "":
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
146 theName = &"mesh-{instanceCounter}"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
147 inc instanceCounter
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
148
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
149 # determine index type (uint8, uint16, uint32)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
150 var indexType = None
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
151 if indices.len > 0:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
152 indexType = Big
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
153 if autoResize and uint32(positions.len) < uint32(high(uint8)) and false: # TODO: check feature support
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
154 indexType = Tiny
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
155 elif autoResize and uint32(positions.len) < uint32(high(uint16)):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
156 indexType = Small
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
157
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
158 result = Mesh(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
159 name: theName,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
160 indexType: indexType,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
161 vertexCount: positions.len,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
162 instanceTransforms: @instanceTransforms,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
163 transform: transform,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
164 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
165
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
166 result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, positions.toSeq)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
167 if colors.len > 0: result[].initVertexAttribute("color", colors.toSeq)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
168 if uvs.len > 0: result[].initVertexAttribute("uv", uvs.toSeq)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
169
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
170 # assert all indices are valid
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
171 for i in indices:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
172 assert int(i[0]) < result[].vertexCount
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
173 assert int(i[1]) < result[].vertexCount
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
174 assert int(i[2]) < result[].vertexCount
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
175
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
176 # cast index values to appropiate type
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
177 if result[].indexType == Tiny and uint32(positions.len) < uint32(high(uint8)) and false: # TODO: check feature support
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
178 for i, tri in enumerate(indices):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
179 result[].tinyIndices.add [uint8(tri[0]), uint8(tri[1]), uint8(tri[2])]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
180 elif result[].indexType == Small and uint32(positions.len) < uint32(high(uint16)):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
181 for i, tri in enumerate(indices):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
182 result[].smallIndices.add [uint16(tri[0]), uint16(tri[1]), uint16(tri[2])]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
183 elif result[].indexType == Big:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
184 for i, tri in enumerate(indices):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
185 result[].bigIndices.add [uint32(tri[0]), uint32(tri[1]), uint32(tri[2])]
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
186 `material=`(result[], material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
187
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
188 proc newMesh*(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
189 positions: openArray[Vec3f],
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
190 colors: openArray[Vec4f] = [],
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
191 uvs: openArray[Vec2f] = [],
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
192 transform: Mat4 = Unit4,
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
193 instanceTransforms: openArray[Mat4] = [Unit4],
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
194 material = EMPTY_MATERIAL.initMaterialData(),
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
195 name: string = "",
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
196 ): Mesh =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
197 newMesh(
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
198 positions = positions,
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
199 indices = newSeq[array[3, uint16]](),
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
200 colors = colors,
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
201 uvs = uvs,
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
202 transform = transform,
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
203 instanceTransforms = instanceTransforms,
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
204 material = material,
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
205 name = name,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
206 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
207
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
208 func attributeSize*(mesh: MeshObject, attribute: string): int =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
209 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
210 mesh.vertexData[attribute].size
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
211 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
212 mesh.instanceData[attribute].size
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
213 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
214 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
215
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
216 func attributeType*(mesh: MeshObject, attribute: string): DataType =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
217 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
218 mesh.vertexData[attribute].theType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
219 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
220 mesh.instanceData[attribute].theType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
221 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
222 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
223
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
224 func indexSize*(mesh: MeshObject): int =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
225 case mesh.indexType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
226 of None: 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
227 of Tiny: mesh.tinyIndices.len * sizeof(get(genericParams(typeof(mesh.tinyIndices)), 0))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
228 of Small: mesh.smallIndices.len * sizeof(get(genericParams(typeof(mesh.smallIndices)), 0))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
229 of Big: mesh.bigIndices.len * sizeof(get(genericParams(typeof(mesh.bigIndices)), 0))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
230
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
231 func rawData[T: seq](value: T): (pointer, int) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
232 (pointer(addr(value[0])), sizeof(get(genericParams(typeof(value)), 0)) * value.len)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
233
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
234 func getRawIndexData*(mesh: MeshObject): (pointer, int) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
235 case mesh.indexType:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
236 of None: raise newException(Exception, "Trying to get index data for non-indexed mesh")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
237 of Tiny: rawData(mesh.tinyIndices)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
238 of Small: rawData(mesh.smallIndices)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
239 of Big: rawData(mesh.bigIndices)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
240
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
241 func getPointer*(mesh: var MeshObject, attribute: string): pointer =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
242 if mesh.vertexData.contains(attribute):
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
243 mesh.vertexData[attribute].getPointer()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
244 elif mesh.instanceData.contains(attribute):
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
245 mesh.instanceData[attribute].getPointer()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
246 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
247 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
248
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
249 proc getAttribute[T: GPUType|int|uint|float](mesh: MeshObject, attribute: string): ref seq[T] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
250 if mesh.vertexData.contains(attribute):
869
65afec4cb6c6 did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents: 866
diff changeset
251 mesh.vertexData[attribute][T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
252 elif mesh.instanceData.contains(attribute):
869
65afec4cb6c6 did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents: 866
diff changeset
253 mesh.instanceData[attribute][T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
254 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
255 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
256
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
257 proc getAttribute[T: GPUType|int|uint|float](mesh: MeshObject, attribute: string, i: int): T =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
258 if mesh.vertexData.contains(attribute):
869
65afec4cb6c6 did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents: 866
diff changeset
259 mesh.vertexData[attribute][i, T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
260 elif mesh.instanceData.contains(attribute):
869
65afec4cb6c6 did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents: 866
diff changeset
261 mesh.instanceData[attribute][i, T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
262 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
263 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
264
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
265 template `[]`*(mesh: MeshObject, attribute: string, t: typedesc): ref seq[t] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
266 getAttribute[t](mesh, attribute)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
267 template `[]`*(mesh: MeshObject, attribute: string, i: int, t: typedesc): untyped =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
268 getAttribute[t](mesh, attribute, i)
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
269 template `[]=`*[T](mesh: MeshObject, attribute: string, value: seq[T]) =
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
270 getAttribute[t](mesh, attribute)
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
271 template `[]=`*[T](mesh: MeshObject, attribute: string, i: int, value: T) =
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
272 getAttribute[t](mesh, attribute, i)
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
273
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
274 template `[]`*(mesh: Mesh, attribute: string, t: typedesc): ref seq[t] =
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
275 mesh[][attribute, t]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
276 template `[]`*(mesh: Mesh, attribute: string, i: int, t: typedesc): untyped =
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
277 mesh[][attribute, i, t]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
278
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
279 proc updateAttributeData[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, data: DataList) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
280 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
281 assert data.len == mesh.vertexCount
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
282 assert data.theType == mesh.vertexData[attribute].theType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
283 mesh.vertexData[attribute] = data
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
284 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
285 assert data.len == mesh.instanceCount
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
286 assert data.theType == mesh.instanceData[attribute].theType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
287 mesh.instanceData[attribute] = data
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
288 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
289 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
290 if not mesh.dirtyAttributes.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
291 mesh.dirtyAttributes.add attribute
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
292
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
293 proc updateAttributeData[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, data: seq[T]) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
294 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
295 assert data.len == mesh.vertexCount
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
296 mesh.vertexData[attribute] = data
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
297 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
298 assert data.len == mesh.instanceCount
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
299 mesh.instanceData[attribute] = data
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
300 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
301 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
302 if not mesh.dirtyAttributes.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
303 mesh.dirtyAttributes.add attribute
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
304
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
305 proc updateAttributeData[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, i: int, value: T) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
306 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
307 assert i < mesh.vertexData[attribute].len
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
308 mesh.vertexData[attribute][i] = value
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
309 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
310 assert i < mesh.instanceData[attribute].len
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
311 mesh.instanceData[attribute][i] = value
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
312 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
313 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
314 if not mesh.dirtyAttributes.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
315 mesh.dirtyAttributes.add attribute
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
316
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
317 proc `[]=`*[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, data: DataList) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
318 updateAttributeData[T](mesh, attribute, data)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
319 proc `[]=`*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, data: DataList) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
320 updateAttributeData[t](mesh[], attribute, data)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
321
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
322 proc `[]=`*[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, data: seq[T]) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
323 updateAttributeData[T](mesh, attribute, data)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
324 proc `[]=`*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, data: seq[T]) =
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 905
diff changeset
325 updateAttributeData[T](mesh[], attribute, data)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
326
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
327 proc `[]=`*[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, value: T) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
328 updateAttributeData[T](mesh, attribute, newSeqWith(mesh.vertexCount, value))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
329 proc `[]=`*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, value: T) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
330 updateAttributeData[T](mesh[], attribute, newSeqWith(mesh.vertexCount, value))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
331
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
332 proc `[]=`*[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, i: int, value: T) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
333 updateAttributeData[T](mesh, attribute, i, value)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
334 proc `[]=`*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, i: int, value: T) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
335 updateAttributeData[T](mesh[], attribute, i, value)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
336
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
337 proc removeAttribute*(mesh: var MeshObject, attribute: string) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
338 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
339 mesh.vertexData.del(attribute)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
340 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
341 mesh.instanceData.del(attribute)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
342 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
343 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
344
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
345 proc appendIndicesData*(mesh: var MeshObject, v1, v2, v3: int) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
346 case mesh.indexType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
347 of None: raise newException(Exception, "Mesh does not support indexed data")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
348 of Tiny: mesh.tinyIndices.add([uint8(v1), uint8(v2), uint8(v3)])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
349 of Small: mesh.smallIndices.add([uint16(v1), uint16(v2), uint16(v3)])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
350 of Big: mesh.bigIndices.add([uint32(v1), uint32(v2), uint32(v3)])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
351
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
352 proc updateInstanceTransforms*(mesh: var MeshObject, attribute: string) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
353 var currentTransforms: seq[Mat4]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
354 if mesh.applyMeshTransformToInstances:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
355 currentTransforms = mesh.instanceTransforms.mapIt(mesh.transform * it)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
356 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
357 currentTransforms = mesh.instanceTransforms
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
358 if currentTransforms != mesh.transformCache:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
359 mesh[attribute] = currentTransforms
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
360 mesh.transformCache = currentTransforms
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
361
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
362 proc renameAttribute*(mesh: var MeshObject, oldname, newname: string) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
363 if mesh.vertexData.contains(oldname):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
364 mesh.vertexData[newname] = mesh.vertexData[oldname]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
365 mesh.vertexData.del oldname
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
366 elif mesh.instanceData.contains(oldname):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
367 mesh.instanceData[newname] = mesh.vertexData[oldname]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
368 mesh.instanceData.del oldname
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
369 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
370 raise newException(Exception, &"Attribute {oldname} is not defined for mesh {mesh}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
371
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
372 func dirtyAttributes*(mesh: MeshObject): seq[string] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
373 mesh.dirtyAttributes
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
374
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
375 proc clearDirtyAttributes*(mesh: var MeshObject) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
376 mesh.dirtyAttributes.reset
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
377
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
378 proc transform*[T: GPUType](mesh: var MeshObject, attribute: string, transform: Mat4) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
379 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
380 for i in 0 ..< mesh.vertexData[attribute].len:
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
381 mesh.vertexData[attribute][i] = transform * mesh.vertexData[attribute][i, T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
382 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
383 for i in 0 ..< mesh.instanceData[attribute].len:
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
384 mesh.instanceData[attribute][i] = transform * mesh.vertexData[attribute][i, T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
385 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
386 raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}")
950
fe48b091e83f did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents: 920
diff changeset
387 mesh.dirtyAttributes.add attribute
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
388
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
389 proc applyTransformToVertices*(mesh: var MeshObject, positionAttribute = DEFAULT_POSITION_ATTRIBUTE) =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
390 for i in 0 ..< mesh.vertexData[positionAttribute].len:
870
b975eab2b694 did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents: 869
diff changeset
391 mesh.vertexData[positionAttribute][i] = mesh.transform * mesh.vertexData[positionAttribute][i, Vec3f]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
392 mesh.transform = Unit4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
393
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
394 func getCollisionPoints*(mesh: MeshObject, positionAttribute = DEFAULT_POSITION_ATTRIBUTE): seq[Vec3f] =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
395 for p in mesh[positionAttribute, Vec3f][]:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
396 result.add mesh.transform * p
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
397
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
398 func getCollider*(mesh: MeshObject, positionAttribute = DEFAULT_POSITION_ATTRIBUTE): Collider =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
399 return mesh.getCollisionPoints(positionAttribute).calculateCollider(Points)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
400
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
401 proc asNonIndexedMesh*(mesh: MeshObject): MeshObject =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
402 if mesh.indexType == None:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
403 return mesh
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
404
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
405 result = MeshObject(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
406 vertexCount: mesh.indicesCount,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
407 indexType: None,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
408 transform: mesh.transform,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
409 instanceTransforms: mesh.instanceTransforms,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
410 visible: mesh.visible,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
411 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
412 for attribute, datalist in mesh.vertexData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
413 result.initVertexAttribute(attribute, datalist.theType)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
414 for attribute, datalist in mesh.instanceData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
415 result.instanceData[attribute] = datalist.copy()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
416 var i = 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
417 case mesh.indexType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
418 of Tiny:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
419 for indices in mesh.tinyIndices:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
420 for attribute, value in mesh.vertexData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
421 result.vertexData[attribute].appendFrom(i, mesh.vertexData[attribute], int(indices[0]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
422 result.vertexData[attribute].appendFrom(i + 1, mesh.vertexData[attribute], int(indices[1]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
423 result.vertexData[attribute].appendFrom(i + 2, mesh.vertexData[attribute], int(indices[2]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
424 i += 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
425 of Small:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
426 for indices in mesh.smallIndices:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
427 for attribute, value in mesh.vertexData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
428 result.vertexData[attribute].appendFrom(i, value, int(indices[0]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
429 result.vertexData[attribute].appendFrom(i + 1, value, int(indices[1]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
430 result.vertexData[attribute].appendFrom(i + 2, value, int(indices[2]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
431 i += 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
432 of Big:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
433 for indices in mesh.bigIndices:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
434 for attribute, value in mesh.vertexData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
435 result.vertexData[attribute].appendFrom(i, mesh.vertexData[attribute], int(indices[0]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
436 result.vertexData[attribute].appendFrom(i + 1, mesh.vertexData[attribute], int(indices[1]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
437 result.vertexData[attribute].appendFrom(i + 2, mesh.vertexData[attribute], int(indices[2]))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
438 i += 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
439 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
440 discard
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
441 `material=`(result, mesh.material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
442
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
443
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
444 # GENERATORS ============================================================================
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
445
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
446 proc rect*(width = 1'f32, height = 1'f32, color = "ffffffff", material = EMPTY_MATERIAL.initMaterialData()): Mesh =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
447 result = Mesh(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
448 vertexCount: 4,
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
449 instanceTransforms: @[Unit4],
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
450 indexType: Small,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
451 smallIndices: @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
452 name: &"rect-{instanceCounter}",
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
453 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
454 inc instanceCounter
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
455
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
456 let
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
457 half_w = width / 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
458 half_h = height / 2
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
459 pos = @[newVec3f(-half_w, -half_h), newVec3f(half_w, -half_h), newVec3f(half_w, half_h), newVec3f(-half_w, half_h)]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
460 c = toRGBA(color)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
461
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
462 result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
463 result[].initVertexAttribute("color", @[c, c, c, c])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
464 result[].initVertexAttribute("uv", @[newVec2f(0, 0), newVec2f(1, 0), newVec2f(1, 1), newVec2f(0, 1)])
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
465 `material=`(result[], material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
466
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
467 proc tri*(width = 1'f32, height = 1'f32, color = "ffffffff", material = EMPTY_MATERIAL.initMaterialData()): Mesh =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
468 result = Mesh(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
469 vertexCount: 3,
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
470 instanceTransforms: @[Unit4],
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
471 name: &"tri-{instanceCounter}",
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
472 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
473 inc instanceCounter
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
474 let
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
475 half_w = width / 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
476 half_h = height / 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
477 colorVec = toRGBA(color)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
478
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
479 result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, @[newVec3f(0, -half_h), newVec3f(half_w, half_h), newVec3f(-half_w, half_h)])
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
480 result[].initVertexAttribute("color", @[colorVec, colorVec, colorVec])
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
481 `material=`(result[], material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
482
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
483 proc circle*(width = 1'f32, height = 1'f32, nSegments = 12, color = "ffffffff", material = EMPTY_MATERIAL.initMaterialData()): Mesh =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
484 assert nSegments >= 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
485 result = Mesh(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
486 vertexCount: 3 + nSegments,
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
487 instanceTransforms: @[Unit4],
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
488 indexType: Small,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
489 name: &"circle-{instanceCounter}",
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
490 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
491 inc instanceCounter
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
492
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
493 let
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
494 rX = width / 2
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
495 rY = height / 2
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
496 c = toRGBA(color)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
497 step = (2'f32 * PI) / float32(nSegments)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
498 var
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
499 pos = @[newVec3f(0, 0), newVec3f(rX, 0)]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
500 col = @[c, c]
915
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
501 uv = @[newVec2f(0.5, 0.5), newVec2f(rX, height / 2)]
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
502 for i in 1 .. nSegments:
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
503 pos.add newVec3f(cos(float32(i) * step) * rX, sin(float32(i) * step) * rY)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
504 col.add c
915
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
505 uv.add newVec2f(cos(float32(i) * step) * 0.5 + 0.5, sin(float32(i) * step) * 0.5 + 0.5)
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
506 result[].smallIndices.add [uint16(0), uint16(i), uint16(i + 1)]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
507
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
508 result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
509 result[].initVertexAttribute("color", col)
915
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
510 result[].initVertexAttribute("uv", uv)
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
511 `material=`(result[], material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
512
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
513 proc grid*(columns, rows: uint16, cellSize = 1.0'f32, color = "ffffffff", material = EMPTY_MATERIAL.initMaterialData()): Mesh =
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
514
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
515 result = Mesh(
857
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
516 vertexCount: int((rows + 1) * (columns + 1)),
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
517 instanceTransforms: @[Unit4],
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
518 indexType: Small,
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
519 name: &"grid-{instanceCounter}",
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
520 )
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
521 inc instanceCounter
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
522
857
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
523 let
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
524 color = toRGBA(color)
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
525 center_offset_x = -(float32(columns) * cellSize) / 2'f32
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
526 center_offset_y = -(float32(rows) * cellSize) / 2'f32
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
527 var
865
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
528 pos: seq[Vec3f]
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
529 col: seq[Vec4f]
864
db6f2ec42e27 add: correct grid-mesh indices
Sam <sam@basx.dev>
parents: 863
diff changeset
530 i = 0'u16
db6f2ec42e27 add: correct grid-mesh indices
Sam <sam@basx.dev>
parents: 863
diff changeset
531 for h in 0'u16 .. rows:
db6f2ec42e27 add: correct grid-mesh indices
Sam <sam@basx.dev>
parents: 863
diff changeset
532 for w in 0'u16 .. columns:
857
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
533 pos.add newVec3f(center_offset_x + float32(w) * cellSize, center_offset_y + float32(h) * cellSize)
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
534 col.add color
863
64bf751ca680 did: an "intermediate" commit, for syncing between machines, nothing special to see here
Sam <sam@basx.dev>
parents: 857
diff changeset
535 if w > 0 and h > 0:
865
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
536 result[].smallIndices.add [i, i - 1, i - rows - 2]
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
537 result[].smallIndices.add [i, i - rows - 2, i - rows - 1]
864
db6f2ec42e27 add: correct grid-mesh indices
Sam <sam@basx.dev>
parents: 863
diff changeset
538 i.inc
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
539
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
540 result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos)
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
541 result[].initVertexAttribute("color", col)
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
542 `material=`(result[], material)
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
543
915
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
544 proc mergeMeshData*(a: var Mesh, b: Mesh) =
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
545 let originalOffset = a.vertexCount
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
546 a.vertexCount = a.vertexCount + b.vertexCount
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
547 assert a.indexType == b.indexType
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
548 for key in a.vertexData.keys:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
549 assert key in b.vertexData, &"Mesh {b} is missing vertex data for '{key}'"
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
550 for (key, value) in b.vertexData.pairs:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
551 a.vertexData[key].appendValues(value)
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
552
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
553 case a.indexType:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
554 of None:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
555 discard
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
556 of Tiny:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
557 let offset = uint8(originalOffset)
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
558 for i in b.tinyIndices:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
559 a.tinyIndices.add [i[0] + offset, i[1] + offset, i[2] + offset]
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
560 of Small:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
561 let offset = uint16(originalOffset)
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
562 for i in b.smallIndices:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
563 a.smallIndices.add [i[0] + offset, i[1] + offset, i[2] + offset]
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
564 of Big:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
565 let offset = uint32(originalOffset)
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
566 for i in b.bigIndices:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
567 a.bigIndices.add [i[0] + offset, i[1] + offset, i[2] + offset]
865
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
568
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
569 # MESH TREES =============================================================================
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
570
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
571 type
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
572 MeshTree* = ref object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
573 mesh*: Mesh
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
574 transform*: Mat4 = Unit4
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
575 children*: seq[MeshTree]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
576
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
577 func toStringRec*(tree: MeshTree, theindent = 0): seq[string] =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
578 if tree.mesh.isNil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
579 result.add "*"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
580 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
581 result.add indent($tree.mesh, theindent)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
582 for child in tree.children:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
583 result.add child.toStringRec(theindent + 4)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
584
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
585 func `$`*(tree: MeshTree): string =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
586 toStringRec(tree).join("\n")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
587
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
588
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
589 proc toSeq*(tree: MeshTree): seq[Mesh] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
590 var queue = @[tree]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
591 while queue.len > 0:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
592 var current = queue.pop
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
593 if not current.mesh.isNil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
594 result.add current.mesh
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
595 queue.add current.children
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
596
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
597 proc updateTransforms*(tree: MeshTree, parentTransform = Unit4) =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
598 let currentTransform = parentTransform * tree.transform
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
599 if not tree.mesh.isNil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
600 tree.mesh.transform = currentTransform
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
601 for child in tree.children:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
602 child.updateTransforms(currentTransform)