From a9c43a3b515eda3900375adc8124f6ec42289162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Fri, 26 Jul 2024 10:37:58 +0200 Subject: [PATCH] wip: make MaterialInstance params configurable --- package/src/react/EntitySelector.tsx | 48 ++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/package/src/react/EntitySelector.tsx b/package/src/react/EntitySelector.tsx index 7b1a2458..55b5cf87 100644 --- a/package/src/react/EntitySelector.tsx +++ b/package/src/react/EntitySelector.tsx @@ -1,6 +1,6 @@ import { useContext, useMemo } from 'react' import { ParentInstancesContext } from './ParentInstancesContext' -import { Entity, extractTransformationProps, TextureFlags, TransformationProps } from '../types' +import { Entity, extractTransformationProps, Float3, Float4, Mat3f, TextureFlags, TransformationProps } from '../types' import { useFilamentContext } from '../hooks/useFilamentContext' import React from 'react' import { useApplyTransformations } from '../hooks/internal/useApplyTransformations' @@ -21,8 +21,31 @@ export type TextureMap = { textureFlags?: TextureFlags } +export type ParameterType = + | { + type: 'Float3' + value: Float3 + } + | { + type: 'Float4' + value: Float4 + } + | { + type: 'Float' + value: number + } + | { + type: 'Int' + value: number + } + | { + type: 'Mat3f' + value: Mat3f + } + export type ModifierProps = TransformationProps & { textureMap?: TextureMap + parameters?: Record } export type EntitySelectorProps = SelectorProps & ModifierProps @@ -77,9 +100,30 @@ type ImplProps = { * @private */ function EntitySelectorImpl(props: ImplProps) { - const [transformProps, { entity, textureMap }] = extractTransformationProps(props) + const [transformProps, { entity, textureMap, parameters }] = extractTransformationProps(props) useApplyTransformations({ to: entity, transformProps }) + useWorkletEffect(() => { + 'worklet' + + // TODO: oh, this operates on a material instance, we'd need ot have a "MaterialSelector" + // The material selector probably should be addable as a child component to an entity selector. + // However, often times a material is used for multiple entities, and it would also be nice to have it apply to all? + Object.entries(parameters ?? {}).forEach(([name, { type, value }]) => { + if (type === 'Float3') { + entity.setFloat3Parameter(name, value) + } else if (type === 'Float4') { + entity.setFloat4Parameter(name, value) + } else if (type === 'Float') { + entity.setFloatParameter(name, value) + } else if (type === 'Int') { + entity.setIntParameter(name, value) + } else if (type === 'Mat3f') { + entity.setMat3fParameter(name, value) + } + }) + }) + return <>{textureMap != null && } }