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

Commit

Permalink
Scene loading improvement (#9025)
Browse files Browse the repository at this point in the history
* scene loading improvement

* add comment
  • Loading branch information
HexaField authored Oct 19, 2023
1 parent c2759a7 commit 4aa11bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
11 changes: 10 additions & 1 deletion packages/editor/src/services/EditorHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import { defineAction, defineState, getMutableState, getState, useHookstate } from '@etherealengine/hyperflux'
import { Topic, defineActionQueue, dispatchAction } from '@etherealengine/hyperflux/functions/ActionFunctions'

import { NotificationService } from '@etherealengine/client-core/src/common/services/NotificationService'
import { EngineActions, EngineState } from '@etherealengine/engine/src/ecs/classes/EngineState'
import { defineQuery, setComponent } from '@etherealengine/engine/src/ecs/functions/ComponentFunctions'
import { EntityTreeComponent } from '@etherealengine/engine/src/ecs/functions/EntityTree'
Expand Down Expand Up @@ -77,9 +78,17 @@ export const EditorHistoryState = defineState({

resetHistory: () => {
const sceneData = getState(SceneState).sceneData!
let migratedSceneData
try {
migratedSceneData = migrateSceneData(sceneData)
} catch (e) {
console.error(e)
migratedSceneData = JSON.parse(JSON.stringify(sceneData))
NotificationService.dispatchNotify('Failed to migrate scene.', { variant: 'error' })
}
getMutableState(EditorHistoryState).set({
index: 0,
history: [{ data: migrateSceneData(sceneData), selectedEntities: [] }]
history: [{ data: migratedSceneData, selectedEntities: [] }]
})
EditorHistoryState.applyCurrentSnapshot()
},
Expand Down
44 changes: 17 additions & 27 deletions packages/engine/src/transform/components/TransformComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type TransformComponentType = {
rotation: Quaternion
scale: Vector3
matrix: Matrix4
matrixInverse: Matrix4
}

const { f64 } = Types
Expand All @@ -68,46 +69,37 @@ export const TransformComponent = defineComponent({
schema: TransformSchema,

onInit: (entity) => {
return {
position: null! as Vector3,
rotation: null! as Quaternion,
scale: null! as Vector3,
const dirtyTransforms = TransformComponent.dirtyTransforms
const component = {
position: proxifyVector3WithDirty(TransformComponent.position, entity, dirtyTransforms) as Vector3,
rotation: proxifyQuaternionWithDirty(TransformComponent.rotation, entity, dirtyTransforms) as Quaternion,
scale: proxifyVector3WithDirty(
TransformComponent.scale,
entity,
dirtyTransforms,
new Vector3(1, 1, 1)
) as Vector3,
matrix: new Matrix4(),
matrixInverse: new Matrix4()
}
} as TransformComponentType
return component
},

onSet: (entity, component, json) => {
const dirtyTransforms = TransformComponent.dirtyTransforms

if (!component.position.value)
component.position.set(
proxifyVector3WithDirty(TransformComponent.position, entity, dirtyTransforms, new Vector3())
)
if (!component.rotation.value)
component.rotation.set(
proxifyQuaternionWithDirty(TransformComponent.rotation, entity, dirtyTransforms, new Quaternion())
)
if (!component.scale.value)
component.scale.set(
proxifyVector3WithDirty(TransformComponent.scale, entity, dirtyTransforms, new Vector3(1, 1, 1))
)

if (!json) return

const rotation = json.rotation
const rotation = json?.rotation
? typeof json.rotation.w === 'number'
? json.rotation
: new Quaternion().setFromEuler(new Euler().setFromVector3(json.rotation as any as Vector3))
: undefined

if (json.position) component.position.value.copy(json.position)
if (json?.position) component.position.value.copy(json.position)
if (rotation) component.rotation.value.copy(rotation)
if (json.scale && !isZero(json.scale)) component.scale.value.copy(json.scale)
if (json?.scale && !isZero(json.scale)) component.scale.value.copy(json.scale)

component.matrix.value.compose(component.position.value, component.rotation.value, component.scale.value)
component.matrixInverse.value.copy(component.matrix.value).invert()

/** Update local transform */
const localTransform = getOptionalComponent(entity, LocalTransformComponent)
const entityTree = getOptionalComponent(entity, EntityTreeComponent)
if (localTransform && entityTree?.parentEntity) {
Expand Down Expand Up @@ -202,5 +194,3 @@ export const LocalTransformComponent = defineComponent({
component.matrix.value.compose(component.position.value, component.rotation.value, component.scale.value)
}
})

globalThis.TransformComponent = TransformComponent

0 comments on commit 4aa11bb

Please sign in to comment.