From a16b30310ed3f86f00a35f250869bf6fca658e90 Mon Sep 17 00:00:00 2001 From: esterTion Date: Wed, 11 Oct 2023 19:24:31 +1100 Subject: [PATCH] feat(renderer): add ability to render a height map & add camera controls (#159) --- src/components/ParametersBar.tsx | 57 ++++++++++++++++++++++++++++++++ src/components/Simulation.tsx | 23 +++++++++++-- src/pages/index.tsx | 7 +++- src/shaders/vert_height.glsl | 29 ++++++++++++++++ 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/shaders/vert_height.glsl diff --git a/src/components/ParametersBar.tsx b/src/components/ParametersBar.tsx index 9c21c0f..d437e24 100644 --- a/src/components/ParametersBar.tsx +++ b/src/components/ParametersBar.tsx @@ -141,6 +141,20 @@ export default function ParametersBar(props: { ControlDifficulty.Expert, ); + const setParams = props.setParams; + const [renderHeightMap, setRenderHeightMap] = useState(false); + const [isCameraControlMode, setIsCameraControlMode] = useState(false); + + useEffect(() => { + setParams((prev) => { + return { + ...prev, + renderHeightMap, + isCameraControlMode, + }; + }); + }, [renderHeightMap, isCameraControlMode, setParams]); + if (isPaneVisible) { return ( @@ -186,6 +200,49 @@ export default function ParametersBar(props: { {/* add controls to be shown to both here */} + + + + + + { + setIsCameraControlMode(false); + setRenderHeightMap(false); + }} + /> + + + { + setRenderHeightMap(true); + }} + /> + + + + + + + + { + setIsCameraControlMode(false); + }} + /> + + + { + setIsCameraControlMode(true); + }} + /> + + {/* choose initial model */} { const shaderMat = new t.ShaderMaterial(); - shaderMat.vertexShader = applyConfigToShader(vertexShader as string); + if (props.params.renderHeightMap) { + shaderMat.vertexShader = applyConfigToShader( + vertexShaderForHeightMap as string, + ); + } else { + shaderMat.vertexShader = applyConfigToShader(vertexShader as string); + } shaderMat.fragmentShader = applyConfigToShader(fragmentShader as string); + shaderMat.side = t.DoubleSide; // provide a dummy density field first @@ -103,13 +114,19 @@ function DiffusionPlane( }; return shaderMat; - }, [props.params.densityHighColour, props.params.densityLowColour]); + }, [ + props.params.densityHighColour, + props.params.densityLowColour, + props.params.renderHeightMap, + ]); // HOOKS useFrame((state) => { + if (disableInteraction) return; // potential performance issue? state.camera.setRotationFromAxisAngle(new t.Vector3(1, 0, 0), -Math.PI / 2); + state.camera.position.set(0, 10, 0); ref.current.lookAt(0, 99, 0); }); @@ -125,6 +142,8 @@ function DiffusionPlane( // update the density uniforms every time // output is received function output(data: Float32Array): void { + // create a copy to prevent modifying original data + data = data.slice(0); const param: Record = { densityRangeHigh: parseFloat(renderConfig.densityRangeHigh), densityRangeLow: parseFloat(renderConfig.densityRangeLow), diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 4a5fbe0..2346eab 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -12,6 +12,7 @@ import { type OutgoingMessage, } from '../workers/modelWorkerMessage'; import { type ModelSave } from '../services/model/modelService'; +import { OrbitControls } from '@react-three/drei'; import RestorePopup from '../components/RestoreComponents/restorePopUp'; const SimulatorContainer = styled.div` @@ -113,8 +114,12 @@ export default function Home(props: IndexProp): React.ReactElement { }} > +