Skip to content

Commit

Permalink
fix: hide GLTFs on entities nested under the Camera or Player (#930)
Browse files Browse the repository at this point in the history
* fix: hide gltfs on entities nested under camera or player entities

* chore: fix tests
  • Loading branch information
cazala authored Apr 15, 2024
1 parent f417f2c commit 2f5f210
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export class EcsEntity extends BABYLON.TransformNode {
getRoot() {
const ctx = this.context.deref()
const nodes = ctx?.editorComponents.Nodes.getOrNull(ctx.engine.RootEntity)?.value || []
if (nodes.length === 0) return null
const root = getRoot(this.entityId, nodes)
return root
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import { EntityType } from '@dcl/schemas'
import { DataLayerRpcClient } from '../../data-layer/types'
import { Operations } from '../../sdk/operations'
import { Entity } from '@dcl/ecs'
import { EditorComponents, Node } from '../../sdk/components'
import { CAMERA, PLAYER, ROOT } from '../../sdk/tree'

describe('GizmoManager', () => {
let engine: Engine
let scene: Scene
let context: SceneContext
let entities: Entity[] = []
let nodes: Node[] = [
{ entity: ROOT, children: entities },
{ entity: CAMERA, children: [] },
{ entity: PLAYER, children: [] }
]
beforeEach(() => {
engine = new NullEngine()
scene = new Scene(engine)
Expand All @@ -30,6 +38,13 @@ describe('GizmoManager', () => {
dispatch: jest.fn(),
getSelectedEntities: jest.fn(() => [])
} as unknown as Operations
;(context.editorComponents as any) = {
Nodes: {
getOrNull: jest.fn().mockReturnValue({
value: nodes
})
}
} as unknown as EditorComponents
})
describe('When creating a new gizmo manager', () => {
let gizmos: Gizmos
Expand All @@ -51,11 +66,15 @@ describe('GizmoManager', () => {
handler = jest.fn()
gizmos.onChange(handler)
gizmos.setEntity(babylonEntity)
entities = [dclEntity]
nodes.push({ entity: dclEntity, children: [] })
})
afterEach(() => {
babylonEntity.dispose()
context.engine.removeEntity(dclEntity)
gizmos.unsetEntity()
entities = []
nodes = nodes.filter(($) => $.entity !== dclEntity)
})
it('should set the entity', () => {
expect(gizmos.getEntity()).toBe(babylonEntity)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import future from 'fp-future'
import * as BABYLON from '@babylonjs/core'
import { GLTFFileLoader, GLTFLoaderAnimationStartMode } from '@babylonjs/loaders'
import { GLTFLoader } from '@babylonjs/loaders/glTF/2.0'
Expand All @@ -7,6 +8,7 @@ import { markAsCollider } from '../colliders-utils'
import type { ComponentOperation } from '../component-operations'
import { EcsEntity } from '../EcsEntity'
import { SceneContext } from '../SceneContext'
import { CAMERA, PLAYER } from '../../../sdk/tree'

let sceneContext: WeakRef<SceneContext>

Expand Down Expand Up @@ -75,14 +77,22 @@ export function loadGltf(entity: EcsEntity, value: string) {
sceneContext = entity.context
}

const loadingSpinner = createLoadingSpinner(entity, context.scene)
const root = entity.getRoot()

const shouldHide = root === PLAYER || root === CAMERA || root === null

const loadingSpinner: BABYLON.Mesh | null = shouldHide ? null : createLoadingSpinner(entity, context.scene)

tryLoadGltfAsync(context.loadableScene.id, entity, value)
.catch((err) => {
console.error('Error trying to load gltf ' + value, err)
})
.finally(() => {
loadingSpinner.dispose(false, true)
if (shouldHide) {
entity.setVisibility(false)
} else {
loadingSpinner?.dispose(false, true)
}
})
}

Expand Down Expand Up @@ -131,6 +141,8 @@ async function tryLoadGltfAsync(sceneId: string, entity: EcsEntity, filePath: st
const file = new File([content], finalSrc)
const extension = filePath.toLowerCase().endsWith('.gltf') ? '.gltf' : '.glb'

const loadAssetFuture = future<void>()

loadAssetContainer(
file,
entity.getScene(),
Expand Down Expand Up @@ -160,14 +172,18 @@ async function tryLoadGltfAsync(sceneId: string, entity: EcsEntity, filePath: st
entity.generateBoundingBox()
entity.setGltfAssetContainer(assetContainer)
entity.resolveGltfPathLoading(filePath)
loadAssetFuture.resolve()
},
undefined,
(_scene, _message, _exception) => {
console.error('Error while calling LoadAssetContainer: ', _message, _exception)
(_scene, message, _exception) => {
console.error('Error while calling LoadAssetContainer: ', message, _exception)
entity.resolveGltfPathLoading(filePath)
loadAssetFuture.reject(new Error(message))
},
extension
)

return loadAssetFuture
}

export function loadAssetContainer(
Expand Down

0 comments on commit 2f5f210

Please sign in to comment.