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

Commit

Permalink
IR-2365 Fix erroneous material updates and prototype switching errors (
Browse files Browse the repository at this point in the history
…#10287)

* Fix erroneous material updates and prototype switching errors

* get material previews updating
  • Loading branch information
AidanCaruso authored May 31, 2024
1 parent 18c5465 commit 966c7c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import React, { useEffect, useRef } from 'react'
import { Mesh, SphereGeometry } from 'three'

import { useRender3DPanelSystem } from '@etherealengine/client-core/src/user/components/Panel3D/useRender3DPanelSystem'
import { generateEntityUUID, getMutableComponent, setComponent, UUIDComponent } from '@etherealengine/ecs'
import { generateEntityUUID, getMutableComponent, setComponent, useComponent, UUIDComponent } from '@etherealengine/ecs'
import { EnvmapComponent } from '@etherealengine/engine/src/scene/components/EnvmapComponent'
import { MaterialSelectionState } from '@etherealengine/engine/src/scene/materials/MaterialLibraryState'
import { getState, useMutableState } from '@etherealengine/hyperflux'
import { CameraOrbitComponent } from '@etherealengine/spatial/src/camera/components/CameraOrbitComponent'
import { NameComponent } from '@etherealengine/spatial/src/common/NameComponent'
import { addObjectToGroup } from '@etherealengine/spatial/src/renderer/components/GroupComponent'
import { VisibleComponent } from '@etherealengine/spatial/src/renderer/components/VisibleComponent'
import { MaterialComponent, MaterialComponents } from '@etherealengine/spatial/src/renderer/materials/MaterialComponent'
import { getMaterial } from '@etherealengine/spatial/src/renderer/materials/materialFunctions'

export const MaterialPreviewCanvas = () => {
Expand All @@ -55,7 +56,11 @@ export const MaterialPreviewCanvas = () => {
const orbitCamera = getMutableComponent(cameraEntity, CameraOrbitComponent)
orbitCamera.focusedEntities.set([sceneEntity])
orbitCamera.refocus.set(true)
}, [selectedMaterial])
}, [
selectedMaterial,
useComponent(UUIDComponent.getEntityByUUID(selectedMaterial.value!), MaterialComponent[MaterialComponents.State])
.material
])
return (
<>
<div id="materialPreview" style={{ minHeight: '250px', width: '100%', height: '100%' }}>
Expand Down
15 changes: 4 additions & 11 deletions packages/spatial/src/renderer/materials/materialFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const getPluginObject = (pluginId: string) => {

export const applyMaterialPlugins = (materialEntity: Entity) => {
const materialComponent = getComponent(materialEntity, MaterialComponent[MaterialComponents.State])
if (!materialComponent.pluginEntities || !materialComponent.material) return
if (!materialComponent.pluginEntities?.length || !materialComponent.material) return
const material = materialComponent.material as Material
material.plugins = []
for (const pluginEntity of materialComponent.pluginEntities) {
Expand Down Expand Up @@ -170,21 +170,13 @@ export const updateMaterialPrototype = (materialEntity: Entity) => {
if (!prototypeConstructor || !prototypeComponent.prototypeArguments) return
const material = materialComponent.material
if (!material || material.type === prototypeName) return
const matKeys = Object.keys(material)
const commonParameters = Object.fromEntries(
Object.keys(prototypeComponent.prototypeArguments)
.filter((key) => matKeys.includes(key))
.map((key) => [key, material[key]])
)
const fullParameters = { ...extractDefaults(prototypeComponent.prototypeArguments), ...commonParameters }
const fullParameters = { ...extractDefaults(prototypeComponent.prototypeArguments) }
const newMaterial = new prototypeConstructor(fullParameters)
if (newMaterial.plugins) {
newMaterial.customProgramCacheKey = () =>
newMaterial.plugins!.map((plugin) => plugin.toString()).reduce((x, y) => x + y, '')
}
newMaterial.uuid = material.uuid
newMaterial.name = material.name
newMaterial.type = prototypeName

if (material.defines?.['USE_COLOR']) {
newMaterial.defines = newMaterial.defines ?? {}
newMaterial.defines!['USE_COLOR'] = material.defines!['USE_COLOR']
Expand All @@ -197,5 +189,6 @@ export const updateMaterialPrototype = (materialEntity: Entity) => {
material: newMaterial,
parameters: fullParameters
})

return newMaterial
}

0 comments on commit 966c7c7

Please sign in to comment.