Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
physics debugger uses ecs
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField committed Nov 22, 2023
1 parent 1010fcb commit d59eb58
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
54 changes: 33 additions & 21 deletions packages/engine/src/debug/systems/DebugRendererSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ import { MeshBVHVisualizer } from 'three-mesh-bvh'

import { getMutableState, getState, useHookstate } from '@etherealengine/hyperflux'

import { Engine } from '../../ecs/classes/Engine'
import { removeEntity } from '../../ecs/functions/EntityFunctions'
import { getComponent } from '../../ecs/functions/ComponentFunctions'
import { createEntity, removeEntity } from '../../ecs/functions/EntityFunctions'
import { defineSystem } from '../../ecs/functions/SystemFunctions'
import { PhysicsState } from '../../physics/state/PhysicsState'
import { RendererState } from '../../renderer/RendererState'
import { WebGLRendererSystem } from '../../renderer/WebGLRendererSystem'
import { createInfiniteGridHelper } from '../../scene/classes/InfiniteGridHelper'
import { GroupQueryReactor, GroupReactorProps } from '../../scene/components/GroupComponent'
import {
GroupComponent,
GroupQueryReactor,
GroupReactorProps,
addObjectToGroup
} from '../../scene/components/GroupComponent'
import { setVisibleComponent } from '../../scene/components/VisibleComponent'
import { ObjectLayers } from '../../scene/constants/ObjectLayers'
import { setObjectLayers } from '../../scene/functions/setObjectLayers'

const lineMaterial = new LineBasicMaterial({ vertexColors: true })
const _lineSegments = new LineSegments(new BufferGeometry(), lineMaterial)
_lineSegments.frustumCulled = false

const visualizers = [] as MeshBVHVisualizer[]

const DebugGroupChildReactor = (props: GroupReactorProps) => {
Expand Down Expand Up @@ -84,16 +86,16 @@ const DebugGroupChildReactor = (props: GroupReactorProps) => {
}

const execute = () => {
const enabled = getState(RendererState).physicsDebug

_lineSegments.visible = enabled

const physicsWorld = getState(PhysicsState).physicsWorld

if (enabled && physicsWorld) {
const debugRenderBuffer = physicsWorld.debugRender()
_lineSegments.geometry.setAttribute('position', new BufferAttribute(debugRenderBuffer.vertices, 3))
_lineSegments.geometry.setAttribute('color', new BufferAttribute(debugRenderBuffer.colors, 4))
const physicsDebugEntity = getState(RendererState).physicsDebugEntity

if (physicsDebugEntity) {
const lineSegments = getComponent(physicsDebugEntity, GroupComponent)[0] as any as LineSegments
const physicsWorld = getState(PhysicsState).physicsWorld
if (physicsWorld) {
const debugRenderBuffer = physicsWorld.debugRender()
lineSegments.geometry.setAttribute('position', new BufferAttribute(debugRenderBuffer.vertices, 3))
lineSegments.geometry.setAttribute('color', new BufferAttribute(debugRenderBuffer.colors, 4))
}
}

for (const visualizer of visualizers) {
Expand All @@ -103,13 +105,23 @@ const execute = () => {

const reactor = () => {
const engineRendererSettings = useHookstate(getMutableState(RendererState))

useEffect(() => {
setObjectLayers(_lineSegments, ObjectLayers.PhysicsHelper)
Engine.instance.scene.add(_lineSegments)
const lineMaterial = new LineBasicMaterial({ vertexColors: true })
const lineSegments = new LineSegments(new BufferGeometry(), lineMaterial)
lineSegments.frustumCulled = false
setObjectLayers(lineSegments, ObjectLayers.PhysicsHelper)

const lineSegmentsEntity = createEntity()
setVisibleComponent(lineSegmentsEntity, true)
addObjectToGroup(lineSegmentsEntity, lineSegments)
engineRendererSettings.physicsDebugEntity.set(lineSegmentsEntity)

return () => {
_lineSegments.removeFromParent()
removeEntity(lineSegmentsEntity)
engineRendererSettings.physicsDebugEntity.set(null)
}
}, [])
}, [engineRendererSettings.physicsDebug])

useEffect(() => {
if (!engineRendererSettings.gridVisibility.value) return
Expand Down
3 changes: 2 additions & 1 deletion packages/engine/src/renderer/RendererState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const RendererState = defineState({
gridHeight: 0,
forceBasicMaterials: false,
shadowMapResolution: isMobile ? 256 : 2048,
infiniteGridHelperEntity: null as Entity | null
infiniteGridHelperEntity: null as Entity | null,
physicsDebugEntity: null as Entity | null
}),
onCreate: (store, state) => {
syncStateWithLocalStorage(RendererState, [
Expand Down
24 changes: 7 additions & 17 deletions packages/engine/src/scene/systems/ShadowSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ const raycaster = new Raycaster()
raycaster.firstHitOnly = true
const raycasterPosition = new Vector3()

const useCSMEffects = () => {
const rendererState = useHookstate(getMutableState(RendererState))
useEffect(() => {
if (!rendererState.csm.value) return
rendererState.csm.value.helper.paused = !rendererState.nodeHelperVisibility.value
rendererState.csm.value.helper.updateVisibility()
}, [rendererState.csm, rendererState.nodeHelperVisibility])
}

/** @deprecated @todo replace this whith EntityCSM when WebXR Light Estimation is entity driven */
const SimpleCSM = (props: { light: DirectionalLight }) => {
useEffect(() => {
Expand All @@ -118,9 +109,6 @@ const SimpleCSM = (props: { light: DirectionalLight }) => {
getMutableState(RendererState).csm.set(null)
}
}, [])

useCSMEffects()

return null
}

Expand Down Expand Up @@ -165,8 +153,6 @@ const EntityCSMReactor = (props: { entity: Entity }) => {
directionalLightComponent?.cameraFar
])

useCSMEffects()

return null
}

Expand Down Expand Up @@ -198,8 +184,6 @@ const PlainCSMReactor = () => {
}
}, [shadowMapResolution])

useCSMEffects()

return null
}

Expand All @@ -210,8 +194,14 @@ function CSMReactor() {
const isEstimatingLight = useHookstate(xrState.isEstimatingLight)
const directionalLights = useQuery([VisibleComponent, DirectionalLightComponent])

const csmEnabled = useHookstate(getMutableState(RenderSettingsState))?.csm?.value
const rendererState = useHookstate(getMutableState(RendererState))
useEffect(() => {
if (!rendererState.csm.value) return
rendererState.csm.value.helper.paused = !rendererState.nodeHelperVisibility.value
rendererState.csm.value.helper.updateVisibility()
}, [rendererState.csm, rendererState.nodeHelperVisibility])

const csmEnabled = useHookstate(getMutableState(RenderSettingsState))?.csm?.value
if (!csmEnabled) return null

if (isEstimatingLight.value)
Expand Down

0 comments on commit d59eb58

Please sign in to comment.