annotate src/semicongine/scene.nim @ 321:30117d8f0052

did next step in renderpipeline-refactoring, using shaderconfiguration objects instead for less ambigious shader-pipeline configuration
author Sam <sam@basx.dev>
date Tue, 15 Aug 2023 23:51:37 +0700
parents b145a05c2459
children 6dab370d1758
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
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
3 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
4 import std/tables
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
5 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
6 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
7
206
7f921d7d0a2b did: small refactoring of module structure
Sam <sam@basx.dev>
parents: 201
diff changeset
8 import ./core
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
9 import ./material
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]
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
17 materials: OrderedTable[string, Material]
321
30117d8f0052 did next step in renderpipeline-refactoring, using shaderconfiguration objects instead for less ambigious shader-pipeline configuration
Sam <sam@basx.dev>
parents: 316
diff changeset
18 transformAttribute*: string = "transform"
30117d8f0052 did next step in renderpipeline-refactoring, using shaderconfiguration objects instead for less ambigious shader-pipeline configuration
Sam <sam@basx.dev>
parents: 316
diff changeset
19 materialIndexAttribute*: string = "materialIndex"
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
20
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
21 Component* = ref object of RootObj
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
22 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
23
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
24 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
25 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
26 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
27 parent: Entity
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
28 children: seq[Entity]
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
29 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
30
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
31 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
32 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
33
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 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
35 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
36 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
37
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 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
39 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
40 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
41
312
2f5933af1ee4 del: unnecessary var
Sam <sam@basx.dev>
parents: 308
diff changeset
42 func start*(animation: EntityAnimation) =
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
43 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
44
312
2f5933af1ee4 del: unnecessary var
Sam <sam@basx.dev>
parents: 308
diff changeset
45 func stop*(animation: EntityAnimation) =
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
46 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
47
313
210cab7958ab add: reset for animation
Sam <sam@basx.dev>
parents: 312
diff changeset
48 func reset*(animation: EntityAnimation) =
210cab7958ab add: reset for animation
Sam <sam@basx.dev>
parents: 312
diff changeset
49 animation.player.stop()
210cab7958ab add: reset for animation
Sam <sam@basx.dev>
parents: 312
diff changeset
50 animation.player.resetPlayer()
210cab7958ab add: reset for animation
Sam <sam@basx.dev>
parents: 312
diff changeset
51
314
325823d1b61e did: add "playing" query to public API
Sam <sam@basx.dev>
parents: 313
diff changeset
52 func playing*(animation: EntityAnimation): bool =
325823d1b61e did: add "playing" query to public API
Sam <sam@basx.dev>
parents: 313
diff changeset
53 animation.player.playing
325823d1b61e did: add "playing" query to public API
Sam <sam@basx.dev>
parents: 313
diff changeset
54
312
2f5933af1ee4 del: unnecessary var
Sam <sam@basx.dev>
parents: 308
diff changeset
55 func update*(animation: EntityAnimation, dt: float32) =
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
56 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
57
305
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
58 func parent(entity: Entity): Entity =
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
59 entity.parent
44ecc0a01a9f fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents: 302
diff changeset
60
314
325823d1b61e did: add "playing" query to public API
Sam <sam@basx.dev>
parents: 313
diff changeset
61 # TODO: this is wrong: transfrom setter + getter are not "symetric"
290
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
62 func transform*(entity: Entity): Mat4 =
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
63 result = entity.internal_transform
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
64 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
65 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
66 result = result * EntityAnimation(component).player.currentValue
290
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
67
314
325823d1b61e did: add "playing" query to public API
Sam <sam@basx.dev>
parents: 313
diff changeset
68 func `transform=`*(entity: Entity, value: Mat4) =
325823d1b61e did: add "playing" query to public API
Sam <sam@basx.dev>
parents: 313
diff changeset
69 entity.internal_transform = value
325823d1b61e did: add "playing" query to public API
Sam <sam@basx.dev>
parents: 313
diff changeset
70
308
c73224f9d38f add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents: 306
diff changeset
71 # TODO: position-setter
c73224f9d38f add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents: 306
diff changeset
72 func position*(entity: Entity): Vec3f =
c73224f9d38f add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents: 306
diff changeset
73 return entity.transform.col(3)
c73224f9d38f add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents: 306
diff changeset
74
290
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
75 func originalTransform*(entity: Entity): Mat4 =
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
76 entity.internal_transform
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
77
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
78 func getModelTransform*(entity: Entity): Mat4 =
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
79 result = entity.transform
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
80 if not entity.parent.isNil:
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
81 result = entity.transform * entity.parent.getModelTransform()
b9f349ac7550 add: access to non-animated transform
Sam <sam@basx.dev>
parents: 288
diff changeset
82
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
83 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
84 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]())
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
85 setValues(scene.shaderGlobals[name], @[data])
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
86
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
87 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
88 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]())
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
89 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
90
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
91 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
92 getValues[T](scene.shaderGlobals[name])[0]
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
93
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
94 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
95 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
96
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
97 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
98 setValues[T](scene.shaderGlobals[name], @[value])
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
99
7741bca03e7c add: support for struct members to be array
Sam <sam@basx.dev>
parents: 211
diff changeset
100 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
101 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
102
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
103 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
104 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
105
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
106 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
107 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
108
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
109 func addMaterial*(scene: var Scene, material: Material) =
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
110 assert not scene.materials.contains(material.name), &"Material with name '{material.name}' already exists in scene"
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
111 for name, value in material.constants.pairs:
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
112 scene.shaderGlobals[name] = newDataList(thetype=value.thetype)
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
113
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
114 for name, value in material.constants.pairs:
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
115 scene.shaderGlobals[name].appendValue(value)
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
116
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
117 scene.materials[material.name] = material
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
118
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
119 func materialIndex*(scene: Scene, materialName: string): int =
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
120 for name in scene.materials.keys:
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
121 if name == materialName:
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
122 return result
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
123 inc result
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
124 return -1
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
125
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
126 func materials*(scene: Scene): auto =
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 314
diff changeset
127 scene.materials.values.toSeq
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
128
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
129 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
130 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
131
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
132 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
133 a.name == b.name
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents: 122
diff changeset
134
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
135 func hash*(entity: Entity): Hash =
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
136 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
137
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
138 func hash*(component: Component): Hash =
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
139 hash(cast[pointer](component))
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 128
diff changeset
140
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
141 method `$`*(entity: Entity): string {.base.} = entity.name
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
142 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
143 "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
144 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
145 &"Entity animation: {animation.player.animation}"
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
146
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
147 proc add*(entity: Entity, child: Entity) =
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
148 child.parent = entity
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
149 entity.children.add child
312
2f5933af1ee4 del: unnecessary var
Sam <sam@basx.dev>
parents: 308
diff changeset
150 proc `[]=`*[T](entity: Entity, index: int, child: var T) =
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
151 child.parent = entity
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
152 entity.children[index] = child
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
153 proc `[]`*(entity: Entity, index: int): Entity =
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
154 entity.children[index]
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
155
306
046f7e2b1e13 add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents: 305
diff changeset
156 proc `[]=`*[T](entity: Entity, name: string, component: T) =
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
157 component.entity = entity
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
158 entity.components[name] = component
306
046f7e2b1e13 add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents: 305
diff changeset
159 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
160 T(entity.components[name])
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
161
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
162 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
163 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
164 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
165 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
166 result.name = name
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
167 for (name, comp) in components:
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
168 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
169 if result.name == "":
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 315
diff changeset
170 result.name = &"Entity[{$(cast[uint](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
171 result.internal_transform = Unit4
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
172
263
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
173 iterator allEntitiesOfType*[T: Entity](root: Entity): T =
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
174 var queue = @[root]
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
175 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
176 var entity = queue.pop
263
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
177 if entity of T:
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
178 yield T(entity)
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
179 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
180 queue.add entity.children[i]
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 254
diff changeset
181
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
182 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
183 var queue = @[root]
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
184 while queue.len > 0:
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
185 let entity = queue.pop
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
186 for component in entity.components.mvalues:
122
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 yield T(component)
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
189 for i in countdown(entity.children.len - 1, 0):
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
190 queue.add entity.children[i]
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
191
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
192 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
193 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
194 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
195 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
196 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
197 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
198 return child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
199 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
200
254
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
201 func `[]`*(scene: Scene, name: string): Entity =
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
202 return scene.root.firstWithName(name)
0fab53ca30b7 did: various improvments
Sam <sam@basx.dev>
parents: 247
diff changeset
203
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
204 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
205 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
206 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
207 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
208 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
209 if child.name == name:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
210 for component in child.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
211 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
212 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
213 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
214
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222 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
223
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
224 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
225 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
226 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
227 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
228 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
229 if child.name == name:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
230 for component in child.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
231 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
232 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
233 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
234
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
235 iterator allEntities*(root: Entity): Entity =
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
236 var queue = @[root]
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
237 while queue.len > 0:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
238 let next = queue.pop
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
239 for child in next.children:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
240 queue.add child
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
241 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
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 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
244 var compList: seq[string]
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
245 for (name, comp) in entity.components.pairs:
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 292
diff changeset
246 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
247
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
248 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
249 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
250 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
251 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
252 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
253
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
254 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
255 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
256 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
257
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
258 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
259 entity.prettyRecursive.join("\n")