annotate src/semicongine/scene.nim @ 306:046f7e2b1e13

add: nicer api to prevent need of component casting
author Sam <sam@basx.dev>
date Wed, 28 Jun 2023 00:35:09 +0700
parents 44ecc0a01a9f
children c73224f9d38f
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
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
2 import std/sequtils
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
3 import std/algorithm
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
4 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
5 import std/tables
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
6 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
7 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
8
206
7f921d7d0a2b did: small refactoring of module structure
Sam <sam@basx.dev>
parents: 201
diff changeset
9 import ./core
288
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
10 import ./animation
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
11
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
12 type
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]
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
17 materials: seq[Material]
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
18
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
19 Material* = object
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
20 name*: string
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
21 textures*: Table[string, Texture]
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
22 data*: Table[string, DataValue]
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
23
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
24 Component* = ref object of RootObj
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
25 entity*: Entity
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
26
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
27 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
28 name*: string
288
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
29 internal_transform: Mat4 # todo: cache transform + only update VBO when transform changed
305
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
30 parent: Entity
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
31 children: seq[Entity]
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
32 components: Table[string, Component]
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
33
288
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
34 EntityAnimation* = ref object of Component
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
35 player: AnimationPlayer[Mat4]
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
36
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
37 func newEntityAnimation*(animation: Animation[Mat4]): EntityAnimation =
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
38 result = EntityAnimation(player: newAnimator(animation))
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
39 result.player.currentValue = Unit4
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
40
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
41 func setAnimation*(entityAnimation: EntityAnimation, animation: Animation[Mat4]) =
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
42 entityAnimation.player.animation = animation
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
43 entityAnimation.player.resetPlayer()
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
44
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
45 func start*(animation: var EntityAnimation) =
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
46 animation.player.start()
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
47
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
48 func stop*(animation: var EntityAnimation) =
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
49 animation.player.stop()
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
50
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
51 func update*(animation: var EntityAnimation, dt: float32) =
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
52 animation.player.advance(dt)
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
53
305
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
54 func parent(entity: Entity): Entity =
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
55 entity.parent
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
56
290
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
57 func transform*(entity: Entity): Mat4 =
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
58 result = entity.internal_transform
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
59 for component in entity.components.mvalues:
292
d4b4ad203578 fix: do not apply last frame value after animation has stopped
Sam <sam@basx.dev>
parents: 290
diff changeset
60 if component of EntityAnimation and EntityAnimation(component).player.playing:
d4b4ad203578 fix: do not apply last frame value after animation has stopped
Sam <sam@basx.dev>
parents: 290
diff changeset
61 result = result * EntityAnimation(component).player.currentValue
290
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
62
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
63 func originalTransform*(entity: Entity): Mat4 =
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
64 entity.internal_transform
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
65
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
66 func `transform=`*(entity: Entity, value: Mat4) =
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
67 entity.internal_transform = value
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
68
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
69 func getModelTransform*(entity: Entity): Mat4 =
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
70 result = entity.transform
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
71 if not entity.parent.isNil:
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
72 result = entity.transform * entity.parent.getModelTransform()
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
73
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
74 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
75 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]())
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
76 setValues(scene.shaderGlobals[name], @[data])
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
77
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
78 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
79 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]())
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
80 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
81
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
82 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
83 getValues[T](scene.shaderGlobals[name])[0]
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
84
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
85 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
86 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
87
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
88 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
89 setValues[T](scene.shaderGlobals[name], @[value])
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
90
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
91 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
92 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
93
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
94 func appendShaderGlobalArray*[T](scene: var Scene, name: string, value: seq[T]) =
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
95 appendValues[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
96
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
97 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
98 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
99
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
100 func getMaterials*(scene: Scene): seq[Material] = scene.materials
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
101
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
102 func addMaterial*(scene: var Scene, material: Material) =
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
103 if scene.materials.len > 0:
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
104 assert material.data.keys.toSeq.sorted() == scene.materials[0].data.keys.toSeq.sorted(), &"{material.data.keys.toSeq.sorted()} == {scene.materials[0].data.keys.toSeq.sorted()}"
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
105 else:
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
106 for name, value in material.data.pairs:
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
107 scene.shaderGlobals[name] = newDataList(thetype=value.thetype)
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
108
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
109 for name, value in material.data.pairs:
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
110 scene.shaderGlobals[name].appendValue(value)
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
111
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
112 scene.materials.add material
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
113
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
114 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
115 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
116
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
117 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
118 a.name == b.name
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents: 122
diff changeset
119
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
120 func hash*(entity: Entity): Hash =
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
121 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
122
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
123 func hash*(component: Component): Hash =
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
124 hash(cast[pointer](component))
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
125
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
126 method `$`*(entity: Entity): string {.base.} = entity.name
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
127 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
128 "Unknown Component"
288
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
129 method `$`*(animation: EntityAnimation): string =
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
130 &"Entity animation: {animation.player.animation}"
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
131
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
132 proc add*(entity: Entity, child: Entity) =
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
133 child.parent = entity
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
134 entity.children.add child
306
046f7e2b1e13 add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents: 305
diff changeset
135 proc `[]=`*[T](entity: var Entity, index: int, child: var T) =
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
136 child.parent = entity
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
137 entity.children[index] = child
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
138 proc `[]`*(entity: Entity, index: int): Entity =
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
139 entity.children[index]
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
140
306
046f7e2b1e13 add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents: 305
diff changeset
141 proc `[]=`*[T](entity: Entity, name: string, component: T) =
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
142 component.entity = entity
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
143 entity.components[name] = component
306
046f7e2b1e13 add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents: 305
diff changeset
144 proc `[]`*[T](entity: Entity, name: string, component: T): T =
046f7e2b1e13 add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents: 305
diff changeset
145 T(entity.components[name])
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
146
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
147 func newEntity*(name: string, components: openArray[(string, Component)] = [], children: varargs[Entity]): Entity =
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
148 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
149 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
150 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
151 result.name = name
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
152 for (name, comp) in components:
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
153 result[name] = comp
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
154 if result.name == "":
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
155 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
288
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
156 result.internal_transform = Unit4
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
157
263
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
158 iterator allEntitiesOfType*[T: Entity](root: Entity): T =
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
159 var queue = @[root]
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
160 while queue.len > 0:
270
563ca4a82931 did: overhaul some of the mesh-data uploading and transformation handling, added: text/font rendering
Sam <sam@basx.dev>
parents: 266
diff changeset
161 var entity = queue.pop
263
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
162 if entity of T:
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
163 yield T(entity)
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
164 for i in countdown(entity.children.len - 1, 0):
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
165 queue.add entity.children[i]
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
166
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
167 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
168 var queue = @[root]
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
169 while queue.len > 0:
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
170 let entity = queue.pop
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
171 for component in entity.components.mvalues:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
172 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
173 yield T(component)
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
174 for i in countdown(entity.children.len - 1, 0):
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
175 queue.add entity.children[i]
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
176
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
177 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
178 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
179 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
180 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
181 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
182 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
183 return child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
184 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
185
254
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
186 func `[]`*(scene: Scene, name: string): Entity =
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
187 return scene.root.firstWithName(name)
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
188
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
189 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
190 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
191 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
192 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
193 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
194 if child.name == name:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
195 for component in child.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
196 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
197 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
198 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
199
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
200 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
201 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
202 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
203 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
204 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
205 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
206 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
207 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
208
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
209 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
210 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
211 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
212 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
213 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
214 if child.name == name:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
215 for component in child.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
216 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
217 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
218 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
219
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
220 iterator allEntities*(root: Entity): Entity =
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
221 var queue = @[root]
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
222 while queue.len > 0:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
223 let next = queue.pop
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
224 for child in next.children:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
225 queue.add child
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
226 yield next
288
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
227
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
228 proc prettyRecursive*(entity: Entity): seq[string] =
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
229 var compList: seq[string]
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
230 for (name, comp) in entity.components.pairs:
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
231 compList.add name & ": " & $comp
288
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
232
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
233 var trans = entity.transform.col(3)
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
234 var pos = entity.getModelTransform().col(3)
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
235 result.add "- " & $entity & " [" & $trans.x & ", " & $trans.y & ", " & $trans.z & "] -> [" & $pos.x & ", " & $pos.y & ", " & $pos.z & "]"
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
236 if compList.len > 0:
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
237 result.add " [" & compList.join(", ") & "]"
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
238
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
239 for child in entity.children:
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
240 for childLine in child.prettyRecursive:
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
241 result.add " " & childLine
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
242
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
243 proc pretty*(entity: Entity): string =
5af702c95b16 add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 270
diff changeset
244 entity.prettyRecursive.join("\n")