annotate src/semicongine/scene.nim @ 254:0fab53ca30b7

did: various improvments
author Sam <sam@basx.dev>
date Wed, 24 May 2023 22:37:57 +0700
parents beb41c93aa3f
children d1ee2a815fa1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
1 import std/strformat
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
2 import std/strutils
189
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
3 import std/tables
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
4 import std/hashes
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
5 import std/typetraits
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
6
206
7f921d7d0a2b did: small refactoring of module structure
Sam <sam@basx.dev>
parents: 201
diff changeset
7 import ./core
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
8
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
9 type
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
10 Component* = ref object of RootObj
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
11 entity*: Entity
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
12
189
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
13 Scene* = object
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
14 name*: string
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
15 root*: Entity
229
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
16 shaderGlobals*: Table[string, DataList]
243
3b388986c7fd did: refactor texture data structures, add more complete (untested) material import
Sam <sam@basx.dev>
parents: 229
diff changeset
17 textures*: Table[string, seq[Texture]]
189
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
18
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
19 Entity* = ref object of RootObj
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
20 name*: string
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
21 transform*: Mat4 # todo: cache transform + only update VBO when transform changed
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
22 parent*: Entity
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
23 children*: seq[Entity]
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
24 components*: seq[Component]
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
25
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
26 func getModelTransform*(entity: Entity): Mat4 =
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
27 result = Unit4
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
28 var currentEntity = entity
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
29 while currentEntity != nil:
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
30 result = currentEntity.transform * result
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
31 currentEntity = currentEntity.parent
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
32
189
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
33 func addShaderGlobal*[T](scene: var Scene, name: string, data: T) =
229
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
34 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]())
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
35 setValues(scene.shaderGlobals[name], @[data])
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
36
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
37 func addShaderGlobalArray*[T](scene: var Scene, name: string, data: seq[T]) =
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
38 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]())
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
39 setValues(scene.shaderGlobals[name], data)
189
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
40
190
8f2eaf0d2808 add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
Sam <sam@basx.dev>
parents: 189
diff changeset
41 func getShaderGlobal*[T](scene: Scene, name: string): T =
229
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
42 getValues[T](scene.shaderGlobals[name])[0]
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
43
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
44 func getShaderGlobalArray*[T](scene: Scene, name: string): seq[T] =
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
45 getValues[T](scene.shaderGlobals[name])
190
8f2eaf0d2808 add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
Sam <sam@basx.dev>
parents: 189
diff changeset
46
8f2eaf0d2808 add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
Sam <sam@basx.dev>
parents: 189
diff changeset
47 func setShaderGlobal*[T](scene: var Scene, name: string, value: T) =
229
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
48 setValues[T](scene.shaderGlobals[name], @[value])
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
49
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
50 func setShaderGlobalArray*[T](scene: var Scene, name: string, value: seq[T]) =
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
51 setValues[T](scene.shaderGlobals[name], value)
189
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
52
243
3b388986c7fd did: refactor texture data structures, add more complete (untested) material import
Sam <sam@basx.dev>
parents: 229
diff changeset
53 func addTextures*(scene: var Scene, name: string, textures: seq[Texture], interpolation=VK_FILTER_LINEAR) =
3b388986c7fd did: refactor texture data structures, add more complete (untested) material import
Sam <sam@basx.dev>
parents: 229
diff changeset
54 scene.textures[name] = textures
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
55
243
3b388986c7fd did: refactor texture data structures, add more complete (untested) material import
Sam <sam@basx.dev>
parents: 229
diff changeset
56 func addTexture*(scene: var Scene, name: string, texture: Texture) =
3b388986c7fd did: refactor texture data structures, add more complete (untested) material import
Sam <sam@basx.dev>
parents: 229
diff changeset
57 scene.textures[name] = @[texture]
189
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
58
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
59 func newScene*(name: string, root: Entity): Scene =
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
60 Scene(name: name, root: root)
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
61
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
62 func hash*(scene: Scene): Hash =
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
63 hash(scene.name)
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
64
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
65 func `==`*(a, b: Scene): bool =
df92519d4d68 add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 142
diff changeset
66 a.name == b.name
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents: 122
diff changeset
67
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
68 func hash*(entity: Entity): Hash =
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
69 hash(cast[pointer](entity))
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
70
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
71 func hash*(component: Component): Hash =
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
72 hash(cast[pointer](component))
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
73
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 109
diff changeset
74 method `$`*(entity: Entity): string {.base.} = entity.name
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
75 method `$`*(component: Component): string {.base.} =
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 109
diff changeset
76 "Unknown Component"
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
77
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
78 proc prettyRecursive*(entity: Entity): seq[string] =
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
79 var compList: seq[string]
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
80 for comp in entity.components:
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
81 compList.add $comp
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
82
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
83 var trans = entity.transform.col(3)
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
84 var pos = entity.getModelTransform().col(3)
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
85 result.add "- " & $entity & " [" & $trans.x & ", " & $trans.y & ", " & $trans.z & "] -> [" & $pos.x & ", " & $pos.y & ", " & $pos.z & "]"
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
86 if compList.len > 0:
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
87 result.add " [" & compList.join(", ") & "]"
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
88
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
89 for child in entity.children:
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
90 for childLine in child.prettyRecursive:
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
91 result.add " " & childLine
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
92
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
93 proc pretty*(entity: Entity): string =
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
94 entity.prettyRecursive.join("\n")
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
95
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
96 proc add*(entity: Entity, child: Entity) =
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
97 child.parent = entity
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
98 entity.children.add child
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
99 proc add*(entity: Entity, component: Component) =
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
100 component.entity = entity
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
101 entity.components.add component
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
102 proc add*(entity: Entity, children: seq[Entity]) =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
103 for child in children:
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
104 child.parent = entity
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
105 entity.children.add child
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
106 proc add*(entity: Entity, components: seq[Component]) =
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
107 for component in components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
108 component.entity = entity
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
109 entity.components.add component
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
110
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
111 func newEntity*(name: string = ""): Entity =
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
112 result = new Entity
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
113 result.name = name
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
114 result.transform = Unit4
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
115 if result.name == "":
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
116 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
117
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 109
diff changeset
118 func newEntity*(name: string, firstChild: Entity, children: varargs[Entity]): Entity =
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
119 result = new Entity
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
120 result.add firstChild
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
121 for child in children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
122 result.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
123 result.name = name
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
124 result.transform = Unit4
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
125 if result.name == "":
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
126 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
127
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
128 proc newEntity*(name: string, firstComponent: Component, components: varargs[Component]): Entity =
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
129 result = new Entity
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
130 result.name = name
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
131 result.add firstComponent
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
132 for component in components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
133 result.add component
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
134 if result.name == "":
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
135 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
136 result.transform = Unit4
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
137
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
138 iterator allComponentsOfType*[T: Component](root: Entity): var T =
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
139 var queue = @[root]
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
140 while queue.len > 0:
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
141 let entity = queue.pop
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
142 for component in entity.components.mitems:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
143 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
144 yield T(component)
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
145 for i in countdown(entity.children.len - 1, 0):
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
146 queue.add entity.children[i]
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
147
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
148 func firstWithName*(root: Entity, name: string): Entity =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
149 var queue = @[root]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
150 while queue.len > 0:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
151 let next = queue.pop
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
152 for child in next.children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
153 if child.name == name:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
154 return child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
155 queue.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
156
254
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
157 func `[]`*(scene: Scene, name: string): Entity =
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
158 return scene.root.firstWithName(name)
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
159
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
160 func firstComponentWithName*[T: Component](root: Entity, name: string): T =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
161 var queue = @[root]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
162 while queue.len > 0:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
163 let next = queue.pop
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
164 for child in next.children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
165 if child.name == name:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
166 for component in child.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
167 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
168 return T(component)
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
169 queue.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
170
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
171 func allWithName*(root: Entity, name: string): seq[Entity] =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
172 var queue = @[root]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
173 while queue.len > 0:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
174 let next = queue.pop
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
175 for child in next.children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
176 if child.name == name:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
177 result.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
178 queue.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
179
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
180 func allComponentsWithName*[T: Component](root: Entity, name: string): seq[T] =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
181 var queue = @[root]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
182 while queue.len > 0:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
183 let next = queue.pop
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
184 for child in next.children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
185 if child.name == name:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
186 for component in child.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
187 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
188 result.add T(component)
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
189 queue.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
190
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
191 iterator allEntities*(root: Entity): Entity =
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
192 var queue = @[root]
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
193 while queue.len > 0:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
194 let next = queue.pop
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
195 for child in next.children:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
196 queue.add child
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
197 yield next