Update cubemap probes automatically.

This commit is contained in:
Alberto Torres 2024-08-21 21:19:47 +02:00
parent 924fa2413a
commit 40276c443b
5 changed files with 53 additions and 11 deletions

View file

@ -192,7 +192,24 @@ proc draw_all*(self: RenderManager) =
continue
scene.update_all_matrices()
scene.update_lights()
# TODO: render probes required by cameras of enabled screens
# TODO: move this
var update_probe_ubo = false
for probe in scene.cubemap_probes:
let upd = case probe.update_strategy:
of UpdateNever: false
of UpdateOnFirstUse:
# TODO: detect when relevant objects are fully loaded
# and also when anything that affects it is visible and
# nearby
not probe.used
of UpdateAlways: true
if upd:
probe.render_cubemap(true)
probe.used = true
update_probe_ubo = true
if update_probe_ubo:
scene.cubemap_UBO.update()
# TODO: render shadows required by cameras of enabled screens
for screen in self.engine.screens:
if not screen.enabled:

View file

@ -584,13 +584,18 @@ method loadScene*(self: BlendLoader, name: string="", scene: Scene=nil, callback
if scn["id"]["name"].cstr == idname.cstring:
let node = scn
var scene = scene
let was_first_scene = self.engine.scenes.len == 0
if scene == nil:
scene = self.engine.new_scene(name=name)
self.loadSceneImpl(node, scene)
callback(scene)
# TODO: when loading is async, move this stuff after loading has
# finished
if was_first_scene and not scene.enabled:
echo "Warning: Your scene is not enabled, use 'scene.enable_render()' or 'scene.enable_all()'"
scene.render_all_cubemaps(true)
return
assert false, &"Scene {name} not found"
proc debug_dump*(self: BlendLoader) =
self.blend_file.debug_dump
self.blend_file.debug_dump

View file

@ -96,6 +96,7 @@ proc newCubemapProbe*(engine: MyouEngine, name: string="camera", scene: Scene =
parallax_distance
else:
influence_distance
self.update_strategy = UpdateOnFirstUse
return self

View file

@ -351,6 +351,17 @@ proc destroy*(self: Scene) =
self.engine.scenes.del(self.name)
return
proc enable_render*(self: Scene) =
self.enabled = true
proc enable_physics*(self: Scene) =
if self.world != nil:
self.world.enabled = true
proc enable_all*(self: Scene) =
self.enable_render()
self.enable_physics()
proc new_gameobject*(self: Scene, name: string): GameObject =
return self.engine.new_gameobject(name=name, scene=self)
@ -528,10 +539,6 @@ proc get_lighting_code*(self: Scene): string =
code.add staticOrDebugRead "shadows/simple_shadow.glsl"
return code.join "\n"
# template as_float_array(x: untyped): untyped =
# cast[array[sizeof(typeof(x)) div 4, float]](x)
proc update_lights*(self: Scene) =
if self.lighting_UBOs.len == 0:
return

View file

@ -694,6 +694,11 @@ type
# cubemap_probe.nim
ProbeUpdateStrategy* = enum
UpdateNever
UpdateOnFirstUse
UpdateAlways
ProbeInfluenceType* = enum
SphereInfluence
BoxInfluence
@ -715,6 +720,8 @@ type
intensity*: float32
clipping_start*: float32
clipping_end*: float32
update_strategy*: ProbeUpdateStrategy
used*: bool
# NOTE: layout must match struct CubemapInfo in scene.nim
CubemapProbeUniform* = object
@ -761,6 +768,8 @@ type
parent_object*: GameObject
World* = ref object of RootObj
enabled*: bool
DebugDraw* = ref object of RootObj
Body* = ref object
@ -824,12 +833,15 @@ type BlendLoader* = ref object of Loader
cached_materials*: Table[(FNode, string, bool),
(string, seq[Varying], OrderedTable[string, string], OrderedTable[string, TexturePixels])] ## private
resource*: LoadableResource
template enqueue*(self: RenderManager, fun: proc()) =
if self.initialized:
template enqueue*(renderer: RenderManager, fun: proc()) =
## Run a proc after the renderer has been initialized.
##
## If it was already initialized, it runs it immediately.
if renderer.initialized:
fun()
else:
self.queue.add fun
renderer.queue.add fun
when not defined(release):
from sugar import dump