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

Commit

Permalink
Frustum cull meshes (#10947)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelEstes authored Aug 13, 2024
1 parent a3efe4d commit e419b5d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/engine/src/scene/systems/SceneObjectSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
Texture
} from 'three'

import { useEntityContext, UUIDComponent } from '@etherealengine/ecs'
import { entityExists, useEntityContext, UUIDComponent } from '@etherealengine/ecs'
import {
getComponent,
getOptionalComponent,
Expand All @@ -52,7 +52,7 @@ import { Entity, EntityUUID } from '@etherealengine/ecs/src/Entity'
import { defineQuery, QueryReactor } from '@etherealengine/ecs/src/QueryFunctions'
import { defineSystem } from '@etherealengine/ecs/src/SystemFunctions'
import { AnimationSystemGroup } from '@etherealengine/ecs/src/SystemGroups'
import { getMutableState, getState, useHookstate } from '@etherealengine/hyperflux'
import { getMutableState, getState, useHookstate, useImmediateEffect } from '@etherealengine/hyperflux'
import { CallbackComponent } from '@etherealengine/spatial/src/common/CallbackComponent'
import { ColliderComponent } from '@etherealengine/spatial/src/physics/components/ColliderComponent'
import { RigidBodyComponent } from '@etherealengine/spatial/src/physics/components/RigidBodyComponent'
Expand Down Expand Up @@ -167,6 +167,13 @@ function SceneObjectReactor(props: { entity: Entity; obj: Object3D }) {
const renderState = getMutableState(RendererState)
const forceBasicMaterials = useHookstate(renderState.forceBasicMaterials)

useImmediateEffect(() => {
setComponent(entity, DistanceFromCameraComponent)
return () => {
if (entityExists(entity)) removeComponent(entity, DistanceFromCameraComponent)
}
}, [])

useEffect(() => {
const source = hasComponent(entity, ModelComponent)
? getModelSceneID(entity)
Expand Down
15 changes: 13 additions & 2 deletions packages/spatial/src/renderer/components/MeshComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Ethereal Engine. All Rights Reserved.
*/

import { useEffect } from 'react'
import { BufferGeometry, Material, Mesh } from 'three'
import { Box3, BufferGeometry, Material, Mesh } from 'three'

import { Entity, useEntityContext } from '@etherealengine/ecs'
import {
Expand All @@ -34,9 +34,10 @@ import {
setComponent,
useComponent
} from '@etherealengine/ecs/src/ComponentFunctions'
import { State, useImmediateEffect } from '@etherealengine/hyperflux'
import { NO_PROXY, State, useImmediateEffect } from '@etherealengine/hyperflux'

import { useResource } from '../../resources/resourceHooks'
import { BoundingBoxComponent } from '../../transform/components/BoundingBoxComponents'
import { addObjectToGroup, removeObjectFromGroup } from './GroupComponent'

export const MeshComponent = defineComponent({
Expand All @@ -61,6 +62,16 @@ export const MeshComponent = defineComponent({
!Array.isArray(meshComponent.material.value) ? (meshComponent.material.value as Material).uuid : undefined
)

useEffect(() => {
const box = geometryResource.boundingBox.get(NO_PROXY) as Box3 | null
if (!box) return

setComponent(entity, BoundingBoxComponent, { box: box })
return () => {
removeComponent(entity, BoundingBoxComponent)
}
}, [geometryResource.boundingBox])

useEffect(() => {
if (meshComponent.value !== meshResource.value) meshResource.set(meshComponent.value)
}, [meshComponent])
Expand Down

0 comments on commit e419b5d

Please sign in to comment.