annotate semicongine/mesh.nim @ 959:c104dbf5bbb8

did: adjust integer sizes to match vulkan API (more) directly
author sam <sam@basx.dev>
date Tue, 02 Apr 2024 16:09:38 +0700
parents fe48b091e83f
children 7e89c8fe57a8
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
959
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
208 func attributeSize*(mesh: MeshObject, attribute: string): uint64 =
840
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
959
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
224 func indexSize*(mesh: MeshObject): uint64 =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
225 case mesh.indexType
959
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
226 of None: 0'u64
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
227 of Tiny: uint64(mesh.tinyIndices.len * sizeof(get(genericParams(typeof(mesh.tinyIndices)), 0)))
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
228 of Small: uint64(mesh.smallIndices.len * sizeof(get(genericParams(typeof(mesh.smallIndices)), 0)))
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
229 of Big: uint64(mesh.bigIndices.len * sizeof(get(genericParams(typeof(mesh.bigIndices)), 0)))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
230
959
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
231 func rawData[T: seq](value: T): (pointer, uint64) =
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
232 (
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
233 pointer(addr(value[0])),
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
234 uint64(sizeof(get(genericParams(typeof(value)), 0)) * value.len)
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
235 )
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
236
959
c104dbf5bbb8 did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents: 950
diff changeset
237 func getRawIndexData*(mesh: MeshObject): (pointer, uint64) =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
238 case mesh.indexType:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
239 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
240 of Tiny: rawData(mesh.tinyIndices)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
241 of Small: rawData(mesh.smallIndices)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
242 of Big: rawData(mesh.bigIndices)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
243
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
244 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
245 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
246 mesh.vertexData[attribute].getPointer()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
247 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
248 mesh.instanceData[attribute].getPointer()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
249 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
250 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
251
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
252 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
253 if mesh.vertexData.contains(attribute):
869
65afec4cb6c6 did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents: 866
diff changeset
254 mesh.vertexData[attribute][T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
255 elif mesh.instanceData.contains(attribute):
869
65afec4cb6c6 did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents: 866
diff changeset
256 mesh.instanceData[attribute][T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
257 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
258 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
259
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
260 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
261 if mesh.vertexData.contains(attribute):
869
65afec4cb6c6 did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents: 866
diff changeset
262 mesh.vertexData[attribute][i, T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
263 elif mesh.instanceData.contains(attribute):
869
65afec4cb6c6 did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents: 866
diff changeset
264 mesh.instanceData[attribute][i, T]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
265 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
266 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
267
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
268 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
269 getAttribute[t](mesh, attribute)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
270 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
271 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
272 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
273 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
274 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
275 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
276
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 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
278 mesh[][attribute, t]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
279 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
280 mesh[][attribute, i, t]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
281
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
282 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
283 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
284 assert data.len == mesh.vertexCount
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
285 assert data.theType == mesh.vertexData[attribute].theType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
286 mesh.vertexData[attribute] = data
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
287 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
288 assert data.len == mesh.instanceCount
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
289 assert data.theType == mesh.instanceData[attribute].theType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
290 mesh.instanceData[attribute] = data
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
291 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
292 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
293 if not mesh.dirtyAttributes.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
294 mesh.dirtyAttributes.add attribute
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
295
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
296 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
297 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
298 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
299 mesh.vertexData[attribute] = data
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
300 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
301 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
302 mesh.instanceData[attribute] = data
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
303 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
304 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
305 if not mesh.dirtyAttributes.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
306 mesh.dirtyAttributes.add attribute
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
307
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
308 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
309 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
310 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
311 mesh.vertexData[attribute][i] = value
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
312 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
313 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
314 mesh.instanceData[attribute][i] = value
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
315 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
316 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
317 if not mesh.dirtyAttributes.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
318 mesh.dirtyAttributes.add attribute
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
319
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
320 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
321 updateAttributeData[T](mesh, attribute, data)
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: Mesh, attribute: string, data: DataList) =
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
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
325 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
326 updateAttributeData[T](mesh, attribute, data)
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: 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
328 updateAttributeData[T](mesh[], attribute, data)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
329
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
330 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
331 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
332 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
333 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
334
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
335 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
336 updateAttributeData[T](mesh, attribute, i, value)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
337 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
338 updateAttributeData[T](mesh[], attribute, i, value)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
339
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
340 proc removeAttribute*(mesh: var MeshObject, attribute: string) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
341 if mesh.vertexData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
342 mesh.vertexData.del(attribute)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
343 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
344 mesh.instanceData.del(attribute)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
345 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
346 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
347
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
348 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
349 case mesh.indexType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
350 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
351 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
352 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
353 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
354
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
355 proc updateInstanceTransforms*(mesh: var MeshObject, attribute: string) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
356 var currentTransforms: seq[Mat4]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
357 if mesh.applyMeshTransformToInstances:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
358 currentTransforms = mesh.instanceTransforms.mapIt(mesh.transform * it)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
359 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
360 currentTransforms = mesh.instanceTransforms
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
361 if currentTransforms != mesh.transformCache:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
362 mesh[attribute] = currentTransforms
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
363 mesh.transformCache = currentTransforms
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
364
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
365 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
366 if mesh.vertexData.contains(oldname):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
367 mesh.vertexData[newname] = mesh.vertexData[oldname]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
368 mesh.vertexData.del oldname
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
369 elif mesh.instanceData.contains(oldname):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
370 mesh.instanceData[newname] = mesh.vertexData[oldname]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
371 mesh.instanceData.del oldname
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
372 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
373 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
374
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
375 func dirtyAttributes*(mesh: MeshObject): seq[string] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
376 mesh.dirtyAttributes
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 clearDirtyAttributes*(mesh: var MeshObject) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
379 mesh.dirtyAttributes.reset
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
380
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
381 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
382 if mesh.vertexData.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.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
384 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
385 elif mesh.instanceData.contains(attribute):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
386 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
387 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
388 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
389 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
390 mesh.dirtyAttributes.add attribute
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
391
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
392 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
393 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
394 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
395 mesh.transform = Unit4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
396
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
397 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
398 for p in mesh[positionAttribute, Vec3f][]:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
399 result.add mesh.transform * p
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
400
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
401 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
402 return mesh.getCollisionPoints(positionAttribute).calculateCollider(Points)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
403
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
404 proc asNonIndexedMesh*(mesh: MeshObject): MeshObject =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
405 if mesh.indexType == None:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
406 return mesh
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
407
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
408 result = MeshObject(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
409 vertexCount: mesh.indicesCount,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
410 indexType: None,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
411 transform: mesh.transform,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
412 instanceTransforms: mesh.instanceTransforms,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
413 visible: mesh.visible,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
414 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
415 for attribute, datalist in mesh.vertexData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
416 result.initVertexAttribute(attribute, datalist.theType)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
417 for attribute, datalist in mesh.instanceData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
418 result.instanceData[attribute] = datalist.copy()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
419 var i = 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
420 case mesh.indexType
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
421 of Tiny:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
422 for indices in mesh.tinyIndices:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
423 for attribute, value in mesh.vertexData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
424 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
425 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
426 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
427 i += 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
428 of Small:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
429 for indices in mesh.smallIndices:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
430 for attribute, value in mesh.vertexData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
431 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
432 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
433 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
434 i += 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
435 of Big:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
436 for indices in mesh.bigIndices:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
437 for attribute, value in mesh.vertexData.pairs:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
438 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
439 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
440 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
441 i += 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
442 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
443 discard
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
444 `material=`(result, mesh.material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
445
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
446
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
447 # GENERATORS ============================================================================
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
448
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
449 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
450 result = Mesh(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
451 vertexCount: 4,
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
452 instanceTransforms: @[Unit4],
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
453 indexType: Small,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
454 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
455 name: &"rect-{instanceCounter}",
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
456 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
457 inc instanceCounter
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
458
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
459 let
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
460 half_w = width / 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
461 half_h = height / 2
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
462 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
463 c = toRGBA(color)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
464
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
465 result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
466 result[].initVertexAttribute("color", @[c, c, c, c])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
467 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
468 `material=`(result[], material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
469
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
470 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
471 result = Mesh(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
472 vertexCount: 3,
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
473 instanceTransforms: @[Unit4],
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
474 name: &"tri-{instanceCounter}",
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
475 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
476 inc instanceCounter
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
477 let
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
478 half_w = width / 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
479 half_h = height / 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
480 colorVec = toRGBA(color)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
481
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
482 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
483 result[].initVertexAttribute("color", @[colorVec, colorVec, colorVec])
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
484 `material=`(result[], material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
485
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
486 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
487 assert nSegments >= 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
488 result = Mesh(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
489 vertexCount: 3 + nSegments,
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
490 instanceTransforms: @[Unit4],
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
491 indexType: Small,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
492 name: &"circle-{instanceCounter}",
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
493 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
494 inc instanceCounter
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
495
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
496 let
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
497 rX = width / 2
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
498 rY = height / 2
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
499 c = toRGBA(color)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
500 step = (2'f32 * PI) / float32(nSegments)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
501 var
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
502 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
503 col = @[c, c]
915
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
504 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
505 for i in 1 .. nSegments:
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 907
diff changeset
506 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
507 col.add c
915
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
508 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
509 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
510
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
511 result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
512 result[].initVertexAttribute("color", col)
915
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
513 result[].initVertexAttribute("uv", uv)
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
514 `material=`(result[], material)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
515
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
516 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
517
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
518 result = Mesh(
857
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
519 vertexCount: int((rows + 1) * (columns + 1)),
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
520 instanceTransforms: @[Unit4],
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
521 indexType: Small,
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
522 name: &"grid-{instanceCounter}",
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
523 )
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
524 inc instanceCounter
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
525
857
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
526 let
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
527 color = toRGBA(color)
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
528 center_offset_x = -(float32(columns) * cellSize) / 2'f32
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
529 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
530 var
865
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
531 pos: seq[Vec3f]
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
532 col: seq[Vec4f]
864
db6f2ec42e27 add: correct grid-mesh indices
Sam <sam@basx.dev>
parents: 863
diff changeset
533 i = 0'u16
db6f2ec42e27 add: correct grid-mesh indices
Sam <sam@basx.dev>
parents: 863
diff changeset
534 for h in 0'u16 .. rows:
db6f2ec42e27 add: correct grid-mesh indices
Sam <sam@basx.dev>
parents: 863
diff changeset
535 for w in 0'u16 .. columns:
857
64b40937d0df did: adjust build-variables and config
Sam <sam@basx.dev>
parents: 854
diff changeset
536 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
537 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
538 if w > 0 and h > 0:
865
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
539 result[].smallIndices.add [i, i - 1, i - rows - 2]
dc7b1312fc76 fix: grid-generation not correct
Sam <sam@basx.dev>
parents: 864
diff changeset
540 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
541 i.inc
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
542
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
543 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
544 result[].initVertexAttribute("color", col)
914
c5ca795af740 add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents: 912
diff changeset
545 `material=`(result[], material)
854
48a2ac8bec07 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 840
diff changeset
546
915
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
547 proc mergeMeshData*(a: var Mesh, b: Mesh) =
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
548 let originalOffset = a.vertexCount
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
549 a.vertexCount = a.vertexCount + b.vertexCount
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
550 assert a.indexType == b.indexType
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
551 for key in a.vertexData.keys:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
552 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
553 for (key, value) in b.vertexData.pairs:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
554 a.vertexData[key].appendValues(value)
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
555
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
556 case a.indexType:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
557 of None:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
558 discard
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
559 of Tiny:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
560 let offset = uint8(originalOffset)
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
561 for i in b.tinyIndices:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
562 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
563 of Small:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
564 let offset = uint16(originalOffset)
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
565 for i in b.smallIndices:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
566 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
567 of Big:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
568 let offset = uint32(originalOffset)
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
569 for i in b.bigIndices:
48b02bc3ad79 add: mesh-merging function
Sam <sam@basx.dev>
parents: 914
diff changeset
570 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
571
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
572 # MESH TREES =============================================================================
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
573
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
574 type
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
575 MeshTree* = ref object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
576 mesh*: Mesh
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
577 transform*: Mat4 = Unit4
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
578 children*: seq[MeshTree]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
579
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 870
diff changeset
580 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
581 if tree.mesh.isNil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
582 result.add "*"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
583 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
584 result.add indent($tree.mesh, theindent)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
585 for child in tree.children:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
586 result.add child.toStringRec(theindent + 4)
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 func `$`*(tree: MeshTree): string =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
589 toStringRec(tree).join("\n")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
590
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
591
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
592 proc toSeq*(tree: MeshTree): seq[Mesh] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
593 var queue = @[tree]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
594 while queue.len > 0:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
595 var current = queue.pop
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
596 if not current.mesh.isNil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
597 result.add current.mesh
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
598 queue.add current.children
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
599
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 902
diff changeset
600 proc updateTransforms*(tree: MeshTree, parentTransform = Unit4) =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
601 let currentTransform = parentTransform * tree.transform
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
602 if not tree.mesh.isNil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
603 tree.mesh.transform = currentTransform
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
604 for child in tree.children:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
605 child.updateTransforms(currentTransform)