comparison semiconginev2/old/scene.nim @ 1218:56781cc0fc7c compiletime-tests

did: renamge main package
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:01:37 +0700
parents semicongine/old/scene.nim@a3eb305bcac2
children
comparison
equal deleted inserted replaced
1217:f819a874058f 1218:56781cc0fc7c
1 import std/tables
2 import std/sequtils
3 import std/strformat
4 import std/hashes
5
6 import ./core
7 import ./mesh
8 import ./material
9
10 type
11 Scene* = ref object
12 name*: string
13 shaderGlobals*: Table[string, DataList]
14 meshes*: seq[Mesh]
15 dirtyShaderGlobals: seq[string]
16 loaded*: bool = false
17
18 proc Add*(scene: var Scene, mesh: MeshObject) =
19 assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add meshes"
20 var tmp = new Mesh
21 tmp[] = mesh
22 scene.meshes.add tmp
23
24 proc Add*(scene: var Scene, mesh: Mesh) =
25 assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add meshes"
26 assert not mesh.isNil, "Cannot add a mesh that is 'nil'"
27 scene.meshes.add mesh
28
29 proc Add*(scene: var Scene, meshes: seq[Mesh]) =
30 assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add meshes"
31 for mesh in meshes:
32 assert not mesh.isNil, "Cannot add a mesh that is 'nil'"
33 scene.meshes.add meshes
34
35 # generic way to add objects that have a mesh-attribute
36 proc Add*[T](scene: var Scene, obj: T) =
37 assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add meshes"
38 for name, value in obj.fieldPairs:
39 when typeof(value) is Mesh:
40 assert not value.isNil, "Cannot add a mesh that is 'nil': " & name
41 scene.meshes.add value
42 when typeof(value) is seq[Mesh]:
43 assert not value.isNil, &"Cannot add a mesh that is 'nil': " & name
44 scene.meshes.add value
45
46 proc AddShaderGlobalArray*[T](scene: var Scene, name: string, data: openArray[T]) =
47 assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add shader values"
48 scene.shaderGlobals[name] = InitDataList(data)
49 scene.dirtyShaderGlobals.add name
50
51 proc AddShaderGlobal*[T](scene: var Scene, name: string, data: T) =
52 scene.AddShaderGlobalArray(name, [data])
53
54 proc GetShaderGlobalArray*[T](scene: Scene, name: string): ref seq[T] =
55 scene.shaderGlobals[name][T]
56
57 proc GetShaderGlobal*[T](scene: Scene, name: string): T =
58 GetShaderGlobalArray[T](scene, name)[][0]
59
60 proc SetShaderGlobalArray*[T](scene: var Scene, name: string, value: openArray[T]) =
61 if scene.shaderGlobals[name, T][] == @value:
62 return
63 scene.shaderGlobals[name] = value
64 if not scene.dirtyShaderGlobals.contains(name):
65 scene.dirtyShaderGlobals.add name
66
67 proc SetShaderGlobal*[T](scene: var Scene, name: string, value: T) =
68 scene.SetShaderGlobalArray(name, [value])
69
70 func DirtyShaderGlobals*(scene: Scene): seq[string] =
71 scene.dirtyShaderGlobals
72
73 proc ClearDirtyShaderGlobals*(scene: var Scene) =
74 scene.dirtyShaderGlobals.reset
75
76 func hash*(scene: Scene): Hash =
77 hash(scene.name)
78
79 func UsesMaterial*(scene: Scene, materialType: MaterialType): bool =
80 return scene.meshes.anyIt(it.material.theType == materialType)
81
82 func GetMaterials*(scene: Scene, materialType: MaterialType): seq[MaterialData] =
83 for mesh in scene.meshes:
84 if mesh.material.theType == materialType and (not result.contains(mesh.material)):
85 result.add mesh.material