From 6f7bb5495e51fa172612a5dad60025d444f32cbc Mon Sep 17 00:00:00 2001 From: Appaji Date: Thu, 16 Nov 2023 05:32:17 +0530 Subject: [PATCH 1/3] Check for shadow component --- packages/engine/src/scene/components/UVOL1Component.ts | 9 ++++++--- packages/engine/src/scene/components/UVOL2Component.ts | 7 ++++--- .../engine/src/scene/components/VolumetricComponent.ts | 2 ++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/engine/src/scene/components/UVOL1Component.ts b/packages/engine/src/scene/components/UVOL1Component.ts index f257e6472c2..d1bf6fed00e 100644 --- a/packages/engine/src/scene/components/UVOL1Component.ts +++ b/packages/engine/src/scene/components/UVOL1Component.ts @@ -44,6 +44,7 @@ import { EngineState } from '../../ecs/classes/EngineState' import { defineComponent, getMutableComponent, + getOptionalMutableComponent, hasComponent, removeComponent, setComponent, @@ -160,9 +161,11 @@ function UVOL1Reactor() { if (volumetric.useLoadingEffect.value) { setComponent(entity, UVOLDissolveComponent) } - const shadow = getMutableComponent(entity, ShadowComponent) - shadow.cast.set(true) - shadow.receive.set(true) + const shadow = getOptionalMutableComponent(entity, ShadowComponent) + if (shadow) { + shadow.cast.set(true) + shadow.receive.set(true) + } video.src = component.manifestPath.value.replace('.manifest', '.mp4') video.load() diff --git a/packages/engine/src/scene/components/UVOL2Component.ts b/packages/engine/src/scene/components/UVOL2Component.ts index 123fba41682..6bd74768ba8 100644 --- a/packages/engine/src/scene/components/UVOL2Component.ts +++ b/packages/engine/src/scene/components/UVOL2Component.ts @@ -48,6 +48,7 @@ import { EngineState } from '../../ecs/classes/EngineState' import { defineComponent, getMutableComponent, + getOptionalMutableComponent, removeComponent, setComponent, useComponent @@ -401,12 +402,12 @@ transformed.z += mix(keyframeA.z, keyframeB.z, mixRatio); manifest.current = calculatePriority(component.data.get({ noproxy: true })) component.data.set(manifest.current) - const shadow = getMutableComponent(entity, ShadowComponent) - if (manifest.current.type === UVOL_TYPE.UNIFORM_SOLVE_WITH_COMPRESSED_TEXTURE) { + const shadow = getOptionalMutableComponent(entity, ShadowComponent) + if (manifest.current.type === UVOL_TYPE.UNIFORM_SOLVE_WITH_COMPRESSED_TEXTURE && shadow) { // TODO: Cast shadows properly with uniform solve shadow.cast.set(false) shadow.receive.set(false) - } else { + } else if (shadow) { shadow.cast.set(true) shadow.receive.set(true) } diff --git a/packages/engine/src/scene/components/VolumetricComponent.ts b/packages/engine/src/scene/components/VolumetricComponent.ts index 5c0cfd483c6..bf3b8e2936a 100755 --- a/packages/engine/src/scene/components/VolumetricComponent.ts +++ b/packages/engine/src/scene/components/VolumetricComponent.ts @@ -40,6 +40,7 @@ import { useEntityContext } from '../../ecs/functions/EntityFunctions' import { EngineRenderer } from '../../renderer/WebGLRendererSystem' import { PlayMode } from '../constants/PlayMode' import { AudioNodeGroups, MediaElementComponent, createAudioNodeGroup, getNextTrack } from './MediaComponent' +import { ShadowComponent } from './ShadowComponent' import { UVOL1Component } from './UVOL1Component' import { UVOL2Component } from './UVOL2Component' @@ -166,6 +167,7 @@ export function VolumetricReactor() { setComponent(entity, MediaElementComponent, { element: document.createElement('video') as HTMLMediaElement }) + setComponent(entity, ShadowComponent) const videoElement = getMutableComponent(entity, MediaElementComponent) const element = videoElement.element.value as HTMLVideoElement element.playsInline = true From ddead7ca20f61ad93a40b53b83a6faf24d746ae0 Mon Sep 17 00:00:00 2001 From: Appaji Date: Thu, 16 Nov 2023 05:44:36 +0530 Subject: [PATCH 2/3] Move shadow logic to useEffect --- .../src/scene/components/UVOL1Component.ts | 17 +++++++----- .../src/scene/components/UVOL2Component.ts | 26 +++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/engine/src/scene/components/UVOL1Component.ts b/packages/engine/src/scene/components/UVOL1Component.ts index d1bf6fed00e..beea50a147a 100644 --- a/packages/engine/src/scene/components/UVOL1Component.ts +++ b/packages/engine/src/scene/components/UVOL1Component.ts @@ -44,11 +44,11 @@ import { EngineState } from '../../ecs/classes/EngineState' import { defineComponent, getMutableComponent, - getOptionalMutableComponent, hasComponent, removeComponent, setComponent, - useComponent + useComponent, + useOptionalComponent } from '../../ecs/functions/ComponentFunctions' import { AnimationSystemGroup } from '../../ecs/functions/EngineFunctions' import { useEntityContext } from '../../ecs/functions/EntityFunctions' @@ -114,6 +114,7 @@ function UVOL1Reactor() { const entity = useEntityContext() const volumetric = useComponent(entity, VolumetricComponent) const component = useComponent(entity, UVOL1Component) + const shadow = useOptionalComponent(entity, ShadowComponent) const videoElement = getMutableComponent(entity, MediaElementComponent).value const audioContext = getState(AudioState).audioContext const video = videoElement.element as HTMLVideoElement @@ -161,11 +162,6 @@ function UVOL1Reactor() { if (volumetric.useLoadingEffect.value) { setComponent(entity, UVOLDissolveComponent) } - const shadow = getOptionalMutableComponent(entity, ShadowComponent) - if (shadow) { - shadow.cast.set(true) - shadow.receive.set(true) - } video.src = component.manifestPath.value.replace('.manifest', '.mp4') video.load() @@ -184,6 +180,13 @@ function UVOL1Reactor() { } }, []) + useEffect(() => { + if (shadow) { + shadow.cast.set(true) + shadow.receive.set(true) + } + }, [shadow]) + useEffect(() => { if (component.loadingEffectStarted.value && !component.loadingEffectEnded.value) { // Loading effect in progress. Let it finish diff --git a/packages/engine/src/scene/components/UVOL2Component.ts b/packages/engine/src/scene/components/UVOL2Component.ts index 6bd74768ba8..e8f966393ad 100644 --- a/packages/engine/src/scene/components/UVOL2Component.ts +++ b/packages/engine/src/scene/components/UVOL2Component.ts @@ -48,10 +48,10 @@ import { EngineState } from '../../ecs/classes/EngineState' import { defineComponent, getMutableComponent, - getOptionalMutableComponent, removeComponent, setComponent, - useComponent + useComponent, + useOptionalComponent } from '../../ecs/functions/ComponentFunctions' import { AnimationSystemGroup } from '../../ecs/functions/EngineFunctions' import { useEntityContext } from '../../ecs/functions/EntityFunctions' @@ -283,6 +283,7 @@ function UVOL2Reactor() { const entity = useEntityContext() const volumetric = useComponent(entity, VolumetricComponent) const component = useComponent(entity, UVOL2Component) + const shadow = useOptionalComponent(entity, ShadowComponent) // These are accessed very frequently, Better not to fetch from state everytime const manifest = useRef(component.data.value) @@ -402,15 +403,6 @@ transformed.z += mix(keyframeA.z, keyframeB.z, mixRatio); manifest.current = calculatePriority(component.data.get({ noproxy: true })) component.data.set(manifest.current) - const shadow = getOptionalMutableComponent(entity, ShadowComponent) - if (manifest.current.type === UVOL_TYPE.UNIFORM_SOLVE_WITH_COMPRESSED_TEXTURE && shadow) { - // TODO: Cast shadows properly with uniform solve - shadow.cast.set(false) - shadow.receive.set(false) - } else if (shadow) { - shadow.cast.set(true) - shadow.receive.set(true) - } geometryTargets.current = Object.keys(manifest.current.geometry.targets) geometryTargets.current.sort((a, b) => { @@ -460,6 +452,18 @@ transformed.z += mix(keyframeA.z, keyframeB.z, mixRatio); } }, []) + useEffect(() => { + if (!shadow) return + if (manifest.current.type === UVOL_TYPE.UNIFORM_SOLVE_WITH_COMPRESSED_TEXTURE && shadow) { + // TODO: Cast shadows properly with uniform solve + shadow.cast.set(false) + shadow.receive.set(false) + } else if (shadow) { + shadow.cast.set(true) + shadow.receive.set(true) + } + }, [shadow]) + const fetchNonUniformSolveGeometry = (startFrame: number, endFrame: number, target: string) => { // TODO: Needs thorough testing const targetData = manifest.current.geometry.targets[target] From 05db98e6d03fa8fda250ad8cd764e08d8fcea4f2 Mon Sep 17 00:00:00 2001 From: Appaji Date: Thu, 16 Nov 2023 05:56:26 +0530 Subject: [PATCH 3/3] Remove unnecessary checks --- packages/engine/src/scene/components/UVOL2Component.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/engine/src/scene/components/UVOL2Component.ts b/packages/engine/src/scene/components/UVOL2Component.ts index e8f966393ad..f5011a0ed35 100644 --- a/packages/engine/src/scene/components/UVOL2Component.ts +++ b/packages/engine/src/scene/components/UVOL2Component.ts @@ -454,11 +454,11 @@ transformed.z += mix(keyframeA.z, keyframeB.z, mixRatio); useEffect(() => { if (!shadow) return - if (manifest.current.type === UVOL_TYPE.UNIFORM_SOLVE_WITH_COMPRESSED_TEXTURE && shadow) { + if (manifest.current.type === UVOL_TYPE.UNIFORM_SOLVE_WITH_COMPRESSED_TEXTURE) { // TODO: Cast shadows properly with uniform solve shadow.cast.set(false) shadow.receive.set(false) - } else if (shadow) { + } else { shadow.cast.set(true) shadow.receive.set(true) }