Skip to content

Commit

Permalink
[feat] relax Object3D checks in the reconciler (#2806)
Browse files Browse the repository at this point in the history
  • Loading branch information
krispya committed May 30, 2023
1 parent 6a873aa commit 35333c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/fiber/src/core/reconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ import * as THREE from 'three'
import Reconciler from 'react-reconciler'
import { ContinuousEventPriority, DiscreteEventPriority, DefaultEventPriority } from 'react-reconciler/constants'
import { unstable_IdlePriority as idlePriority, unstable_scheduleCallback as scheduleCallback } from 'scheduler'
import { is, diffProps, applyProps, invalidateInstance, attach, detach, prepare, globalScope, now } from './utils'
import {
is,
diffProps,
applyProps,
invalidateInstance,
attach,
detach,
prepare,
globalScope,
now,
isObject3D,
} from './utils'
import type { RootStore } from './store'
import { removeInteractivity, type EventHandlers } from './events'

Expand Down Expand Up @@ -106,7 +117,7 @@ function handleContainerEffects(parent: Instance, child: Instance) {
if (!parent.parent && parent.object !== state.scene) return

// Handle interactivity
if (child.eventCount > 0 && child.object.raycast !== null && child.object instanceof THREE.Object3D) {
if (child.eventCount > 0 && child.object.raycast !== null && isObject3D(child.object)) {
state.internal.interaction.push(child.object)
}

Expand All @@ -123,7 +134,7 @@ function appendChild(parent: HostConfig['instance'], child: HostConfig['instance
parent.children.push(child)

// Add Object3Ds if able
if (!child.props.attach && parent.object instanceof THREE.Object3D && child.object instanceof THREE.Object3D) {
if (!child.props.attach && isObject3D(parent.object) && isObject3D(child.object)) {
parent.object.add(child.object)
}

Expand All @@ -149,12 +160,7 @@ function insertBefore(
if (replace) beforeChild.parent = null

// Manually splice Object3Ds
if (
!child.props.attach &&
parent.object instanceof THREE.Object3D &&
child.object instanceof THREE.Object3D &&
beforeChild.object instanceof THREE.Object3D
) {
if (!child.props.attach && isObject3D(parent.object) && isObject3D(child.object) && isObject3D(beforeChild.object)) {
child.object.parent = parent.object
parent.object.children.splice(parent.object.children.indexOf(beforeChild.object), replace ? 1 : 0, child.object)
child.object.dispatchEvent({ type: 'added' })
Expand Down Expand Up @@ -185,7 +191,7 @@ function removeChild(
// Eagerly tear down tree
if (child.props.attach) {
detach(parent, child)
} else if (child.object instanceof THREE.Object3D && parent.object instanceof THREE.Object3D) {
} else if (isObject3D(child.object) && isObject3D(parent.object)) {
parent.object.remove(child.object)
removeInteractivity(child.root, child.object)
}
Expand Down Expand Up @@ -360,7 +366,7 @@ export const reconciler = Reconciler<
hideInstance(instance) {
if (instance.props.attach && instance.parent?.object) {
detach(instance.parent, instance)
} else if (instance.object instanceof THREE.Object3D) {
} else if (isObject3D(instance.object)) {
instance.object.visible = false
}

Expand All @@ -371,7 +377,7 @@ export const reconciler = Reconciler<
if (instance.isHidden) {
if (instance.props.attach && instance.parent?.object) {
attach(instance.parent, instance)
} else if (instance.object instanceof THREE.Object3D && instance.props.visible !== false) {
} else if (isObject3D(instance.object) && instance.props.visible !== false) {
instance.object.visible = true
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/fiber/src/core/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,5 @@ export const globalScope =
(typeof global !== 'undefined' && global) ||
(typeof self !== 'undefined' && self) ||
(typeof window !== 'undefined' && window)

export const isObject3D = (object: any): object is THREE.Object3D => object?.isObject3D

0 comments on commit 35333c9

Please sign in to comment.