Mercurial > games > semicongine
annotate semiconginev2/old/mesh.nim @ 1218:56781cc0fc7c compiletime-tests
did: renamge main package
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:01:37 +0700 |
parents | semicongine/old/mesh.nim@a3eb305bcac2 |
children |
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 |
1172 | 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 | 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 |
1172 | 29 of Tiny: tinyIndices*: seq[array[3, uint8]] |
840
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 |
1138 | 43 func Material*(mesh: MeshObject): MaterialData = |
840
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): |
998
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
959
diff
changeset
|
49 assert mesh.vertexData[name].theType == theType, &"{material.theType} expected vertex attribute '{name}' to be '{theType}' but it is {mesh.vertexData[name].theType}" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 else: |
998
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
959
diff
changeset
|
51 assert false, &"Mesh '{mesh.name}' is missing required vertex attribute '{name}: {theType}' for {material.theType}" |
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
959
diff
changeset
|
52 for name, theType in material.theType.instanceAttributes: |
1008
443e8090614c
fix: some mesh attributes will be handled automatically, ignore them when doing mesh-material-shader compatability-check
sam <sam@basx.dev>
parents:
1003
diff
changeset
|
53 if name in [TRANSFORM_ATTRIB, MATERIALINDEX_ATTRIBUTE]: |
443e8090614c
fix: some mesh attributes will be handled automatically, ignore them when doing mesh-material-shader compatability-check
sam <sam@basx.dev>
parents:
1003
diff
changeset
|
54 continue |
998
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
959
diff
changeset
|
55 if mesh.instanceData.contains(name): |
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
959
diff
changeset
|
56 assert mesh.instanceData[name].theType == theType, &"{material.theType} expected instance attribute '{name}' to be '{theType}' but it is {mesh.instanceData[name].theType}" |
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
959
diff
changeset
|
57 else: |
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
959
diff
changeset
|
58 assert false, &"Mesh '{mesh.name}' is missing required instance attribute '{name}: {theType}' for {material.theType}" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 mesh.material = material |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
60 |
1138 | 61 func InstanceCount*(mesh: MeshObject): int = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 mesh.instanceTransforms.len |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 |
1138 | 64 func IndicesCount*(mesh: MeshObject): int = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 ( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 case mesh.indexType |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 of None: 0 |
1172 | 68 of Tiny: mesh.tinyIndices.len |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 of Small: mesh.smallIndices.len |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 of Big: mesh.bigIndices.len |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 ) * 3 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
73 func `$`*(mesh: MeshObject): string = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 if mesh.indexType == None: |
1138 | 75 &"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})" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 else: |
1138 | 77 &"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})" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 func `$`*(mesh: Mesh): string = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 $mesh[] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 |
1138 | 81 func VertexAttributes*(mesh: MeshObject): seq[string] = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
82 mesh.vertexData.keys.toSeq |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
83 |
1138 | 84 func InstanceAttributes*(mesh: MeshObject): seq[string] = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
85 mesh.instanceData.keys.toSeq |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
86 |
1138 | 87 func Attributes*(mesh: MeshObject): seq[string] = |
88 mesh.VertexAttributes & mesh.InstanceAttributes | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
89 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
90 func hash*(mesh: Mesh): Hash = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 hash(cast[ptr MeshObject](mesh)) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 |
1138 | 93 converter ToVulkan*(indexType: MeshIndexType): VkIndexType = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 case indexType: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 of None: VK_INDEX_TYPE_NONE_KHR |
1172 | 96 of Tiny: VK_INDEX_TYPE_UINT8_EXT |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 of Small: VK_INDEX_TYPE_UINT16 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 of Big: VK_INDEX_TYPE_UINT32 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 |
1138 | 100 proc InitVertexAttribute*[T](mesh: var MeshObject, attribute: string, value: seq[T]) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute) |
1137 | 102 mesh.vertexData[attribute] = InitDataList(thetype = GetDataType[T]()) |
103 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
|
104 mesh.vertexData[attribute] = value |
1138 | 105 proc InitVertexAttribute*[T](mesh: var MeshObject, attribute: string, value: T) = |
106 InitVertexAttribute(mesh, attribute, newSeqWith(mesh.vertexCount, value)) | |
107 proc InitVertexAttribute*[T](mesh: var MeshObject, attribute: string) = | |
108 InitVertexAttribute(mesh = mesh, attribute = attribute, value = default(T)) | |
109 proc InitVertexAttribute*(mesh: var MeshObject, attribute: string, datatype: DataType) = | |
840
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) |
1137 | 111 mesh.vertexData[attribute] = InitDataList(thetype = datatype) |
112 mesh.vertexData[attribute].SetLen(mesh.vertexCount) | |
1138 | 113 proc InitVertexAttribute*(mesh: var MeshObject, attribute: string, data: DataList) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 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
|
115 mesh.vertexData[attribute] = data |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
116 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
117 |
1138 | 118 proc InitInstanceAttribute*[T](mesh: var MeshObject, attribute: string, value: seq[T]) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute) |
1137 | 120 mesh.instanceData[attribute] = InitDataList(thetype = GetDataType[T]()) |
1138 | 121 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
|
122 mesh.instanceData[attribute] = value |
1138 | 123 proc InitInstanceAttribute*[T](mesh: var MeshObject, attribute: string, value: T) = |
124 InitInstanceAttribute(mesh, attribute, newSeqWith(mesh.InstanceCount, value)) | |
125 proc InitInstanceAttribute*[T](mesh: var MeshObject, attribute: string) = | |
126 InitInstanceAttribute(mesh = mesh, attribute = attribute, value = default(T)) | |
127 proc InitInstanceAttribute*(mesh: var MeshObject, attribute: string, datatype: DataType) = | |
840
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) |
1137 | 129 mesh.instanceData[attribute] = InitDataList(thetype = datatype) |
1138 | 130 mesh.instanceData[attribute].SetLen(mesh.InstanceCount) |
131 proc InitInstanceAttribute*(mesh: var MeshObject, attribute: string, data: DataList) = | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
132 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
|
133 mesh.instanceData[attribute] = data |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
134 |
1138 | 135 proc NewMesh*( |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
136 positions: openArray[Vec3f], |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
137 indices: openArray[array[3, uint32|uint16|uint8]], |
902 | 138 colors: openArray[Vec4f] = [], |
139 uvs: openArray[Vec2f] = [], | |
905 | 140 transform: Mat4 = Unit4, |
141 instanceTransforms: openArray[Mat4] = [Unit4], | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
142 material = EMPTY_MATERIAL.InitMaterialData(), |
902 | 143 autoResize = true, |
144 name: string = "" | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
145 ): Mesh = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
146 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
|
147 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
|
148 var theName = name |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
149 if theName == "": |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
150 theName = &"mesh-{instanceCounter}" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
151 inc instanceCounter |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
152 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
153 # determine index type (uint8, uint16, uint32) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
154 var indexType = None |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
155 if indices.len > 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
156 indexType = Big |
1172 | 157 if autoResize and uint32(positions.len) < uint32(high(uint8)) and false: # TODO: check feature support |
158 indexType = Tiny | |
159 elif autoResize and uint32(positions.len) < uint32(high(uint16)): | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
160 indexType = Small |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
161 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
162 result = Mesh( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
163 name: theName, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
164 indexType: indexType, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
165 vertexCount: positions.len, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
166 instanceTransforms: @instanceTransforms, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
167 transform: transform, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
168 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
169 |
1138 | 170 result[].InitVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, positions.toSeq) |
171 if colors.len > 0: result[].InitVertexAttribute("color", colors.toSeq) | |
172 if uvs.len > 0: result[].InitVertexAttribute("uv", uvs.toSeq) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
173 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
174 # assert all indices are valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
175 for i in indices: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
176 assert int(i[0]) < result[].vertexCount |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
177 assert int(i[1]) < result[].vertexCount |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
178 assert int(i[2]) < result[].vertexCount |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
179 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
180 # cast index values to appropiate type |
1172 | 181 if result[].indexType == Tiny and uint32(positions.len) < uint32(high(uint8)) and false: # TODO: check feature support |
182 for i, tri in enumerate(indices): | |
183 result[].tinyIndices.add [uint8(tri[0]), uint8(tri[1]), uint8(tri[2])] | |
184 elif result[].indexType == Small and uint32(positions.len) < uint32(high(uint16)): | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
185 for i, tri in enumerate(indices): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
186 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
|
187 elif result[].indexType == Big: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
188 for i, tri in enumerate(indices): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
189 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
|
190 `material=`(result[], material) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
191 |
1138 | 192 proc NewMesh*( |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
193 positions: openArray[Vec3f], |
902 | 194 colors: openArray[Vec4f] = [], |
195 uvs: openArray[Vec2f] = [], | |
905 | 196 transform: Mat4 = Unit4, |
197 instanceTransforms: openArray[Mat4] = [Unit4], | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
198 material = EMPTY_MATERIAL.InitMaterialData(), |
902 | 199 name: string = "", |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
200 ): Mesh = |
1138 | 201 NewMesh( |
902 | 202 positions = positions, |
203 indices = newSeq[array[3, uint16]](), | |
204 colors = colors, | |
205 uvs = uvs, | |
206 transform = transform, | |
207 instanceTransforms = instanceTransforms, | |
208 material = material, | |
209 name = name, | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
210 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
211 |
1138 | 212 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
|
213 if mesh.vertexData.contains(attribute): |
1137 | 214 mesh.vertexData[attribute].Size |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
215 elif mesh.instanceData.contains(attribute): |
1137 | 216 mesh.instanceData[attribute].Size |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
217 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
218 0 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
219 |
1138 | 220 func AttributeType*(mesh: MeshObject, attribute: string): DataType = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
221 if mesh.vertexData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
222 mesh.vertexData[attribute].theType |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
223 elif mesh.instanceData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
224 mesh.instanceData[attribute].theType |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
225 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
226 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
|
227 |
1138 | 228 func IndexSize*(mesh: MeshObject): uint64 = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
229 case mesh.indexType |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
950
diff
changeset
|
230 of None: 0'u64 |
1172 | 231 of Tiny: uint64(mesh.tinyIndices.len * sizeof(get(genericParams(typeof(mesh.tinyIndices)), 0))) |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
950
diff
changeset
|
232 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
|
233 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
|
234 |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
950
diff
changeset
|
235 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
|
236 ( |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
950
diff
changeset
|
237 pointer(addr(value[0])), |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
950
diff
changeset
|
238 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
|
239 ) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
240 |
1138 | 241 func GetRawIndexData*(mesh: MeshObject): (pointer, uint64) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
242 case mesh.indexType: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
243 of None: raise newException(Exception, "Trying to get index data for non-indexed mesh") |
1172 | 244 of Tiny: rawData(mesh.tinyIndices) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
245 of Small: rawData(mesh.smallIndices) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
246 of Big: rawData(mesh.bigIndices) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
247 |
1138 | 248 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
|
249 if mesh.vertexData.contains(attribute): |
1137 | 250 mesh.vertexData[attribute].GetPointer() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
251 elif mesh.instanceData.contains(attribute): |
1137 | 252 mesh.instanceData[attribute].GetPointer() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
253 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
254 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
|
255 |
1138 | 256 proc GetAttribute[T: GPUType|int|uint|float](mesh: MeshObject, attribute: string): ref seq[T] = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
257 if mesh.vertexData.contains(attribute): |
869
65afec4cb6c6
did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents:
866
diff
changeset
|
258 mesh.vertexData[attribute][T] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
259 elif mesh.instanceData.contains(attribute): |
869
65afec4cb6c6
did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents:
866
diff
changeset
|
260 mesh.instanceData[attribute][T] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
261 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
262 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
|
263 |
1138 | 264 proc GetAttribute[T: GPUType|int|uint|float](mesh: MeshObject, attribute: string, i: int): T = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
265 if mesh.vertexData.contains(attribute): |
869
65afec4cb6c6
did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents:
866
diff
changeset
|
266 mesh.vertexData[attribute][i, T] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
267 elif mesh.instanceData.contains(attribute): |
869
65afec4cb6c6
did: overhaul dynamic array-api in a few places
Sam <sam@basx.dev>
parents:
866
diff
changeset
|
268 mesh.instanceData[attribute][i, T] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
269 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
270 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
|
271 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
272 template `[]`*(mesh: MeshObject, attribute: string, t: typedesc): ref seq[t] = |
1138 | 273 GetAttribute[t](mesh, attribute) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
274 template `[]`*(mesh: MeshObject, attribute: string, i: int, t: typedesc): untyped = |
1138 | 275 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
|
276 template `[]=`*[T](mesh: MeshObject, attribute: string, value: seq[T]) = |
1138 | 277 GetAttribute[t](mesh, 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
|
278 template `[]=`*[T](mesh: MeshObject, attribute: string, i: int, value: T) = |
1138 | 279 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
|
280 |
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
|
281 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
|
282 mesh[][attribute, t] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
283 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
|
284 mesh[][attribute, i, t] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
285 |
1138 | 286 proc UpdateAttributeData[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, data: DataList) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
287 if mesh.vertexData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
288 assert data.len == mesh.vertexCount |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
289 assert data.theType == mesh.vertexData[attribute].theType |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
290 mesh.vertexData[attribute] = data |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
291 elif mesh.instanceData.contains(attribute): |
1138 | 292 assert data.len == mesh.InstanceCount |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
293 assert data.theType == mesh.instanceData[attribute].theType |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
294 mesh.instanceData[attribute] = data |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
295 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
296 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
|
297 if not mesh.dirtyAttributes.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
298 mesh.dirtyAttributes.add attribute |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
299 |
1138 | 300 proc UpdateAttributeData[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, data: seq[T]) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
301 if mesh.vertexData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
302 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
|
303 mesh.vertexData[attribute] = data |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
304 elif mesh.instanceData.contains(attribute): |
1138 | 305 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
|
306 mesh.instanceData[attribute] = data |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
307 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
308 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
|
309 if not mesh.dirtyAttributes.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
310 mesh.dirtyAttributes.add attribute |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
311 |
1138 | 312 proc UpdateAttributeData[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, i: int, value: T) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
313 if mesh.vertexData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
314 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
|
315 mesh.vertexData[attribute][i] = value |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
316 elif mesh.instanceData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
317 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
|
318 mesh.instanceData[attribute][i] = value |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
319 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
320 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
|
321 if not mesh.dirtyAttributes.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
322 mesh.dirtyAttributes.add attribute |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
323 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
324 proc `[]=`*[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, data: DataList) = |
1138 | 325 UpdateAttributeData[T](mesh, attribute, data) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
326 proc `[]=`*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, data: DataList) = |
1138 | 327 UpdateAttributeData[t](mesh[], attribute, data) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
328 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
329 proc `[]=`*[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, data: seq[T]) = |
1138 | 330 UpdateAttributeData[T](mesh, attribute, data) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
331 proc `[]=`*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, data: seq[T]) = |
1138 | 332 UpdateAttributeData[T](mesh[], attribute, data) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
333 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
334 proc `[]=`*[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, value: T) = |
1138 | 335 UpdateAttributeData[T](mesh, attribute, newSeqWith(mesh.vertexCount, value)) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
336 proc `[]=`*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, value: T) = |
1138 | 337 UpdateAttributeData[T](mesh[], attribute, newSeqWith(mesh.vertexCount, value)) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
338 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
339 proc `[]=`*[T: GPUType|int|uint|float](mesh: var MeshObject, attribute: string, i: int, value: T) = |
1138 | 340 UpdateAttributeData[T](mesh, attribute, i, value) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
341 proc `[]=`*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, i: int, value: T) = |
1138 | 342 UpdateAttributeData[T](mesh[], attribute, i, value) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
343 |
1138 | 344 proc RemoveAttribute*(mesh: var MeshObject, attribute: string) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
345 if mesh.vertexData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
346 mesh.vertexData.del(attribute) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
347 elif mesh.instanceData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
348 mesh.instanceData.del(attribute) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
349 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
350 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
|
351 |
1138 | 352 proc AppendIndicesData*(mesh: var MeshObject, v1, v2, v3: int) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
353 case mesh.indexType |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
354 of None: raise newException(Exception, "Mesh does not support indexed data") |
1172 | 355 of Tiny: mesh.tinyIndices.add([uint8(v1), uint8(v2), uint8(v3)]) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
356 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
|
357 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
|
358 |
1138 | 359 proc UpdateInstanceTransforms*(mesh: var MeshObject, attribute: string) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
360 var currentTransforms: seq[Mat4] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
361 if mesh.applyMeshTransformToInstances: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
362 currentTransforms = mesh.instanceTransforms.mapIt(mesh.transform * it) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
363 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
364 currentTransforms = mesh.instanceTransforms |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
365 if currentTransforms != mesh.transformCache: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
366 mesh[attribute] = currentTransforms |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
367 mesh.transformCache = currentTransforms |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
368 |
1138 | 369 proc RenameAttribute*(mesh: var MeshObject, oldname, newname: string) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
370 if mesh.vertexData.contains(oldname): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
371 mesh.vertexData[newname] = mesh.vertexData[oldname] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
372 mesh.vertexData.del oldname |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
373 elif mesh.instanceData.contains(oldname): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
374 mesh.instanceData[newname] = mesh.vertexData[oldname] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
375 mesh.instanceData.del oldname |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
376 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
377 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
|
378 |
1138 | 379 func DirtyAttributes*(mesh: MeshObject): seq[string] = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
380 mesh.dirtyAttributes |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
381 |
1138 | 382 proc ClearDirtyAttributes*(mesh: var MeshObject) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
383 mesh.dirtyAttributes.reset |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
384 |
1138 | 385 proc SetShaderMaterialIndices*(mesh: var MeshObject, shadername: string, values: seq[uint16], attributeName = MATERIALINDEX_ATTRIBUTE) = |
1026 | 386 let indexAttribute = shadername & "_" & attributeName |
1138 | 387 assert values.len == mesh.InstanceCount, &"Mesh {mesh}: While trying to set shader material indices for shader '{shadername}': indices have len {values.len}, but instance count is {mesh.InstanceCount}" |
1026 | 388 mesh[indexAttribute] = values |
389 | |
1025 | 390 # MESH-TOOLS |
391 | |
1138 | 392 proc Transform*[T: GPUType](mesh: var MeshObject, attribute: string, transform: Mat4) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
393 if mesh.vertexData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
394 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
|
395 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
|
396 elif mesh.instanceData.contains(attribute): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
397 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
|
398 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
|
399 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
400 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
|
401 mesh.dirtyAttributes.add attribute |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
402 |
1138 | 403 func GetMeshPoints*(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
|
404 for p in mesh[positionAttribute, Vec3f][]: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
405 result.add mesh.transform * p |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
406 |
1138 | 407 func GetCollider*(mesh: MeshObject, positionAttribute = DEFAULT_POSITION_ATTRIBUTE): Collider = |
408 return mesh.GetMeshPoints(positionAttribute).CalculateCollider(Points) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
409 |
1138 | 410 proc AsNonIndexedMesh*(mesh: MeshObject): MeshObject = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
411 if mesh.indexType == None: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
412 return mesh |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
413 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
414 result = MeshObject( |
1138 | 415 vertexCount: mesh.IndicesCount, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
416 indexType: None, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
417 transform: mesh.transform, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
418 instanceTransforms: mesh.instanceTransforms, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
419 visible: mesh.visible, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
420 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
421 for attribute, datalist in mesh.vertexData.pairs: |
1138 | 422 result.InitVertexAttribute(attribute, datalist.theType) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
423 for attribute, datalist in mesh.instanceData.pairs: |
1137 | 424 result.instanceData[attribute] = datalist.Copy() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
425 var i = 0 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
426 case mesh.indexType |
1172 | 427 of Tiny: |
428 for indices in mesh.tinyIndices: | |
429 for attribute, value in mesh.vertexData.pairs: | |
430 result.vertexData[attribute].AppendFrom(i, mesh.vertexData[attribute], int(indices[0])) | |
431 result.vertexData[attribute].AppendFrom(i + 1, mesh.vertexData[attribute], int(indices[1])) | |
432 result.vertexData[attribute].AppendFrom(i + 2, mesh.vertexData[attribute], int(indices[2])) | |
433 i += 3 | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
434 of Small: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
435 for indices in mesh.smallIndices: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
436 for attribute, value in mesh.vertexData.pairs: |
1137 | 437 result.vertexData[attribute].AppendFrom(i, value, int(indices[0])) |
438 result.vertexData[attribute].AppendFrom(i + 1, value, int(indices[1])) | |
439 result.vertexData[attribute].AppendFrom(i + 2, value, int(indices[2])) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
440 i += 3 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
441 of Big: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
442 for indices in mesh.bigIndices: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
443 for attribute, value in mesh.vertexData.pairs: |
1137 | 444 result.vertexData[attribute].AppendFrom(i, mesh.vertexData[attribute], int(indices[0])) |
445 result.vertexData[attribute].AppendFrom(i + 1, mesh.vertexData[attribute], int(indices[1])) | |
446 result.vertexData[attribute].AppendFrom(i + 2, mesh.vertexData[attribute], int(indices[2])) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
447 i += 3 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
448 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
449 discard |
914
c5ca795af740
add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents:
912
diff
changeset
|
450 `material=`(result, mesh.material) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
451 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
452 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
453 # GENERATORS ============================================================================ |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
454 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
455 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
|
456 result = Mesh( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
457 vertexCount: 4, |
905 | 458 instanceTransforms: @[Unit4], |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
459 indexType: Small, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
460 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
|
461 name: &"rect-{instanceCounter}", |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
462 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
463 inc instanceCounter |
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 let |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
466 half_w = width / 2 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
467 half_h = height / 2 |
1136 | 468 pos = @[NewVec3f(-half_w, -half_h), NewVec3f(half_w, -half_h), NewVec3f(half_w, half_h), NewVec3f(-half_w, half_h)] |
1137 | 469 c = ToRGBA(color) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
470 |
1138 | 471 result[].InitVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos) |
472 result[].InitVertexAttribute("color", @[c, c, c, c]) | |
473 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
|
474 `material=`(result[], material) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
475 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
476 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
|
477 result = Mesh( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
478 vertexCount: 3, |
905 | 479 instanceTransforms: @[Unit4], |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
480 name: &"tri-{instanceCounter}", |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
481 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
482 inc instanceCounter |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
483 let |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
484 half_w = width / 2 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
485 half_h = height / 2 |
1137 | 486 colorVec = ToRGBA(color) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
487 |
1138 | 488 result[].InitVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, @[NewVec3f(0, -half_h), NewVec3f(half_w, half_h), NewVec3f(-half_w, half_h)]) |
489 result[].InitVertexAttribute("color", @[colorVec, colorVec, colorVec]) | |
914
c5ca795af740
add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents:
912
diff
changeset
|
490 `material=`(result[], material) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
491 |
1133
a52e924e5666
add: function to generate circle-mesh data (not mesh object)
sam <sam@basx.dev>
parents:
1026
diff
changeset
|
492 proc CircleMesh*(nSegments: int): (seq[Vec3f], seq[array[3, uint16]]) = |
a52e924e5666
add: function to generate circle-mesh data (not mesh object)
sam <sam@basx.dev>
parents:
1026
diff
changeset
|
493 let |
a52e924e5666
add: function to generate circle-mesh data (not mesh object)
sam <sam@basx.dev>
parents:
1026
diff
changeset
|
494 rX = 0.5 |
a52e924e5666
add: function to generate circle-mesh data (not mesh object)
sam <sam@basx.dev>
parents:
1026
diff
changeset
|
495 rY = 0.5 |
a52e924e5666
add: function to generate circle-mesh data (not mesh object)
sam <sam@basx.dev>
parents:
1026
diff
changeset
|
496 step = (2'f32 * PI) / float32(nSegments) |
1136 | 497 result[0] = @[NewVec3f(0, 0), NewVec3f(rX, 0)] |
1133
a52e924e5666
add: function to generate circle-mesh data (not mesh object)
sam <sam@basx.dev>
parents:
1026
diff
changeset
|
498 for i in 1 .. nSegments: |
1136 | 499 result[0].add NewVec3f(cos(float32(i) * step) * rX, sin(float32(i) * step) * rY) |
1133
a52e924e5666
add: function to generate circle-mesh data (not mesh object)
sam <sam@basx.dev>
parents:
1026
diff
changeset
|
500 result[1].add [uint16(0), uint16(i), uint16(i + 1)] |
a52e924e5666
add: function to generate circle-mesh data (not mesh object)
sam <sam@basx.dev>
parents:
1026
diff
changeset
|
501 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
502 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
|
503 assert nSegments >= 3 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
504 result = Mesh( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
505 vertexCount: 3 + nSegments, |
905 | 506 instanceTransforms: @[Unit4], |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
507 indexType: Small, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
508 name: &"circle-{instanceCounter}", |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
509 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
510 inc instanceCounter |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
511 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
512 let |
912 | 513 rX = width / 2 |
514 rY = height / 2 | |
1137 | 515 c = ToRGBA(color) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
516 step = (2'f32 * PI) / float32(nSegments) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
517 var |
1136 | 518 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
|
519 col = @[c, c] |
1136 | 520 uv = @[NewVec2f(0.5, 0.5), NewVec2f(rX, height / 2)] |
912 | 521 for i in 1 .. nSegments: |
1136 | 522 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
|
523 col.add c |
1136 | 524 uv.add NewVec2f(cos(float32(i) * step) * 0.5 + 0.5, sin(float32(i) * step) * 0.5 + 0.5) |
912 | 525 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
|
526 |
1138 | 527 result[].InitVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos) |
528 result[].InitVertexAttribute("color", col) | |
529 result[].InitVertexAttribute("uv", uv) | |
914
c5ca795af740
add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents:
912
diff
changeset
|
530 `material=`(result[], material) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
531 |
1138 | 532 proc CircleMesh*(width = 1'f32, height = 1'f32, nSegments = 12): (seq[Vec3f], seq[array[3, uint16]]) = |
1003
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
533 assert nSegments >= 3 |
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
534 result[0] = newSeq[Vec3f](3 + nSegments) |
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
535 |
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
536 let |
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
537 rX = width / 2 |
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
538 rY = height / 2 |
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
539 step = (2'f32 * PI) / float32(nSegments) |
1136 | 540 result[0][0] = NewVec3f(0, 0) |
541 result[0][1] = NewVec3f(rX, 0) | |
1003
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
542 for i in 1 .. nSegments: |
1136 | 543 result[0][i + 1] = NewVec3f(cos(float32(i) * step) * rX, sin(float32(i) * step) * rY) |
1003
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
544 result[1].add [uint16(0), uint16(i), uint16(i + 1)] |
2f92d7e80597
add: temporary helper function, maybe need to do this better anyways
sam <sam@basx.dev>
parents:
1001
diff
changeset
|
545 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
546 proc Grid*(columns, rows: uint16, cellSize = 1.0'f32, color = "ffffffff", material = EMPTY_MATERIAL.InitMaterialData()): Mesh = |
902 | 547 |
854
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
548 result = Mesh( |
857 | 549 vertexCount: int((rows + 1) * (columns + 1)), |
905 | 550 instanceTransforms: @[Unit4], |
854
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
551 indexType: Small, |
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
552 name: &"grid-{instanceCounter}", |
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
553 ) |
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
554 inc instanceCounter |
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
555 |
857 | 556 let |
1137 | 557 color = ToRGBA(color) |
857 | 558 center_offset_x = -(float32(columns) * cellSize) / 2'f32 |
559 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
|
560 var |
865 | 561 pos: seq[Vec3f] |
562 col: seq[Vec4f] | |
864 | 563 i = 0'u16 |
564 for h in 0'u16 .. rows: | |
565 for w in 0'u16 .. columns: | |
1136 | 566 pos.add NewVec3f(center_offset_x + float32(w) * cellSize, center_offset_y + float32(h) * cellSize) |
857 | 567 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
|
568 if w > 0 and h > 0: |
865 | 569 result[].smallIndices.add [i, i - 1, i - rows - 2] |
570 result[].smallIndices.add [i, i - rows - 2, i - rows - 1] | |
864 | 571 i.inc |
854
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
572 |
1138 | 573 result[].InitVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos) |
574 result[].InitVertexAttribute("color", col) | |
914
c5ca795af740
add: correct checks for assigned material compatability
Sam <sam@basx.dev>
parents:
912
diff
changeset
|
575 `material=`(result[], material) |
854
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
576 |
1138 | 577 proc MergeMeshData*(a: var Mesh, b: Mesh) = |
915 | 578 let originalOffset = a.vertexCount |
579 a.vertexCount = a.vertexCount + b.vertexCount | |
580 assert a.indexType == b.indexType | |
581 for key in a.vertexData.keys: | |
582 assert key in b.vertexData, &"Mesh {b} is missing vertex data for '{key}'" | |
583 for (key, value) in b.vertexData.pairs: | |
1137 | 584 a.vertexData[key].AppendValues(value) |
915 | 585 |
586 case a.indexType: | |
587 of None: | |
588 discard | |
1172 | 589 of Tiny: |
590 let offset = uint8(originalOffset) | |
591 for i in b.tinyIndices: | |
592 a.tinyIndices.add [i[0] + offset, i[1] + offset, i[2] + offset] | |
915 | 593 of Small: |
594 let offset = uint16(originalOffset) | |
595 for i in b.smallIndices: | |
596 a.smallIndices.add [i[0] + offset, i[1] + offset, i[2] + offset] | |
597 of Big: | |
598 let offset = uint32(originalOffset) | |
599 for i in b.bigIndices: | |
600 a.bigIndices.add [i[0] + offset, i[1] + offset, i[2] + offset] | |
865 | 601 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
602 # MESH TREES ============================================================================= |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
603 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
604 type |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
605 MeshTree* = ref object |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
606 mesh*: Mesh |
905 | 607 transform*: Mat4 = Unit4 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
608 children*: seq[MeshTree] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
609 |
902 | 610 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
|
611 if tree.mesh.isNil: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
612 result.add "*" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
613 else: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
614 result.add indent($tree.mesh, theindent) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
615 for child in tree.children: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
616 result.add child.toStringRec(theindent + 4) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
617 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
618 func `$`*(tree: MeshTree): string = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
619 toStringRec(tree).join("\n") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
620 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
621 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
622 proc toSeq*(tree: MeshTree): seq[Mesh] = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
623 var queue = @[tree] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
624 while queue.len > 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
625 var current = queue.pop |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
626 if not current.mesh.isNil: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
627 result.add current.mesh |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
628 queue.add current.children |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
629 |
905 | 630 proc updateTransforms*(tree: MeshTree, parentTransform = Unit4) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
631 let currentTransform = parentTransform * tree.transform |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
632 if not tree.mesh.isNil: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
633 tree.mesh.transform = currentTransform |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
634 for child in tree.children: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
635 child.updateTransforms(currentTransform) |