11import React , { useEffect , useMemo } from 'react'
2- import { useGlobalStore , usePlotStore } from '@/utils/GlobalStates'
2+ import { useAnalysisStore , useGlobalStore , usePlotStore } from '@/utils/GlobalStates'
33import { useShallow } from 'zustand/shallow'
44import * as THREE from 'three'
55import { sphereBlocksFrag , flatBlocksVert , flatBlocksVert3D } from '../textures/shaders'
@@ -20,14 +20,25 @@ const FlatBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.DataTe
2020 displacement : state . displacement , sphereResolution : state . sphereResolution ,
2121 offsetNegatives : state . offsetNegatives , rotateFlat :state . rotateFlat
2222 } ) ) )
23-
24- const width = dataShape [ dataShape . length - 1 ]
25- const height = dataShape [ dataShape . length - 2 ]
23+ const { analysisMode, axis} = useAnalysisStore ( useShallow ( state => ( {
24+ analysisMode : state . analysisMode , axis :state . axis
25+ } ) ) )
26+ const { width, height} = useMemo ( ( ) => {
27+ if ( dataShape . length == 2 ) {
28+ return { width : dataShape [ 1 ] , height : dataShape [ 0 ] }
29+ } else if ( analysisMode ) {
30+ const thisShape = dataShape . filter ( ( _val , idx ) => idx != axis )
31+ return { width : thisShape [ 1 ] , height : thisShape [ 0 ] }
32+ } else {
33+ return { width : dataShape [ 2 ] , height : dataShape [ 1 ] }
34+ }
35+ } , [ analysisMode , axis , dataShape ] )
36+ const rotateMap = analysisMode && axis == 2 ;
2637 const geometry = useMemo ( ( ) => {
2738 const count = width * height ;
28- const sqWidth = 1 ;
39+ const sqWidth = 2 ;
2940 const aspect = width / height
30- const geo = new THREE . BoxGeometry ( sqWidth / width * aspect , sqWidth / height , .01 ) ;
41+ const geo = new THREE . BoxGeometry ( sqWidth / width , sqWidth / height / aspect , .01 ) ;
3142 const uvs = new Float32Array ( count * 2 ) ;
3243 let idx = 0 ;
3344 for ( let i = 0 ; i < width ; i ++ ) {
@@ -45,6 +56,7 @@ const FlatBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.DataTe
4556 ) ;
4657 return geo
4758 } , [ width , height ] )
59+
4860 const shaderMaterial = useMemo ( ( ) => {
4961 const shader = new THREE . ShaderMaterial ( {
5062 glslVersion : THREE . GLSL3 ,
@@ -81,15 +93,16 @@ const FlatBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.DataTe
8193 uniforms . cOffset . value = cOffset
8294 uniforms . cScale . value = cScale
8395 uniforms . displaceZero . value = offsetNegatives ? 0 : ( - valueScales . minVal / ( valueScales . maxVal - valueScales . minVal ) )
96+ uniforms . aspect . value = width / height ;
8497 }
8598 invalidate ( ) ;
86- } , [ animProg , valueScales , displacement , colormap , cScale , cOffset , offsetNegatives , textures ] )
99+ } , [ animProg , valueScales , displacement , colormap , cScale , cOffset , offsetNegatives , textures , analysisMode , axis , width , height ] )
87100
88101 return (
89102
90103 < instancedMesh
91- scale = { [ 1 , flipY ? - 1 : 1 , 1 ] }
92- rotation = { [ rotateFlat ? - Math . PI / 2 : 0 , 0 , 0 ] }
104+ scale = { [ ( ( analysisMode && axis == 2 ) && flipY ) ? - 1 : 1 , flipY ? - 1 : ( ( analysisMode && axis == 2 ) ? - 1 : 1 ) , 1 ] }
105+ rotation = { [ rotateFlat ? - Math . PI / 2 : 0 , 0 , rotateMap ? Math . PI / 2 : 0 ] }
93106 args = { [ geometry , shaderMaterial , ( width * height ) ] }
94107 frustumCulled = { false }
95108 />
0 commit comments