11import * as THREE from 'three' ;
22import * as Nodes from 'three/examples/jsm/nodes/Nodes' ;
33
4+ const d3Format = require ( 'd3-format' ) ;
5+
46import {
57 Effect , Input , InputDimension
68} from '../EffectBlock' ;
@@ -18,27 +20,25 @@ import {
1820} from '../NodeMesh' ;
1921
2022import {
21- getColorMapTexture
23+ ScaleType , getColorMapTexture , getColorInterpolator , getColorBar , updateColorBar
2224} from '../utils/colormaps' ;
2325
2426
2527export
2628class IsoColor extends Effect {
2729
28- constructor ( parent : Block , input : Input , min : number , max : number , colorMap : string = 'Viridis' ) {
30+ constructor ( parent : Block , input : Input , min : number , max : number , colorMap : string = 'Viridis' , type : ScaleType = ScaleType . linear ) {
2931 super ( parent , input ) ;
3032
31- this . texture = getColorMapTexture ( colorMap ) ;
33+ this . format = d3Format . format ( '.2e' ) ;
34+ this . _type = type ;
35+ this . colorInterpolator = getColorInterpolator ( colorMap ) ;
36+ this . colorBar = getColorBar ( this . colorInterpolator , [ min , max ] , type , this . format ) ;
37+ this . texture = getColorMapTexture ( this . colorInterpolator ) ;
3238
3339 this . textureNode = new Nodes . TextureNode ( this . texture ) ;
3440
35- const functionNode = new Nodes . FunctionNode (
36- `vec3 isoColorFunc${ this . id } (sampler2D textureMap, float min, float max, float data){
37- vec2 colorPosition = vec2((data - min) / (max - min), 0.0);
38-
39- return vec3(texture2D(textureMap, colorPosition));
40- }`
41- ) ;
41+ const functionNode = this . getFunctionNode ( ) ;
4242
4343 this . minNode = new Nodes . FloatNode ( min ) ;
4444 this . maxNode = new Nodes . FloatNode ( max ) ;
@@ -68,15 +68,21 @@ class IsoColor extends Effect {
6868 }
6969
7070 set min ( value : number ) {
71+ updateColorBar ( this . colorBar , this . colorInterpolator , [ value , this . maxNode . value ] , this . _type , this . format ) ;
7172 this . minNode . value = value ;
73+
74+ this . trigger ( 'change:colorbar' ) ;
7275 }
7376
7477 get min ( ) {
7578 return this . minNode . value ;
7679 }
7780
7881 set max ( value : number ) {
82+ updateColorBar ( this . colorBar , this . colorInterpolator , [ this . minNode . value , value ] , this . _type , this . format ) ;
7983 this . maxNode . value = value ;
84+
85+ this . trigger ( 'change:colorbar' ) ;
8086 }
8187
8288 get max ( ) {
@@ -88,14 +94,57 @@ class IsoColor extends Effect {
8894 }
8995
9096 set colorMap ( colorMap : string ) {
91- this . texture = getColorMapTexture ( colorMap ) ;
97+ this . colorInterpolator = getColorInterpolator ( colorMap ) ;
98+ updateColorBar ( this . colorBar , this . colorInterpolator , [ this . minNode . value , this . maxNode . value ] , this . _type , this . format ) ;
99+ this . texture = getColorMapTexture ( this . colorInterpolator ) ;
92100 this . textureNode . value = this . texture ;
101+
102+ this . trigger ( 'change:colorbar' ) ;
93103 }
94104
105+ set type ( type : ScaleType ) {
106+ this . _type = type ;
107+
108+ updateColorBar ( this . colorBar , this . colorInterpolator , [ this . minNode . value , this . maxNode . value ] , this . _type , this . format ) ;
109+
110+ const functionNode = this . getFunctionNode ( ) ;
111+ this . functionCallNode . setFunction ( functionNode , [ this . textureNode , this . minNode , this . maxNode , this . inputNode ] ) ;
112+
113+ this . buildMaterial ( ) ;
114+
115+ this . trigger ( 'change:colorbar' ) ;
116+ }
117+
118+ private getFunctionNode ( ) {
119+ if ( this . _type == ScaleType . linear ) {
120+ return new Nodes . FunctionNode (
121+ `vec3 isoColorFunc${ this . id } (sampler2D textureMap, float min, float max, float data){
122+ vec2 colorPosition = vec2((data - min) / (max - min), 0.0);
123+
124+ return vec3(texture2D(textureMap, colorPosition));
125+ }`
126+ ) ;
127+ } else {
128+ return new Nodes . FunctionNode (
129+ `vec3 isoColorFunc${ this . id } (sampler2D textureMap, float min, float max, float data){
130+ vec2 colorPosition = vec2((log(data) - log(min)) / (log(max) - log(min)), 0.0);
131+
132+ return vec3(texture2D(textureMap, colorPosition));
133+ }`
134+ ) ;
135+ }
136+ }
137+
138+ colorBar : HTMLCanvasElement ;
139+
95140 private initialized : boolean = false ;
96141
97142 private functionCallNode : Nodes . FunctionCallNode ;
98143
144+ private colorInterpolator : ( v : number ) => string ;
145+ private format : ( v : number ) => string ;
146+ private _type : ScaleType ;
147+
99148 private minNode : Nodes . FloatNode ;
100149 private maxNode : Nodes . FloatNode ;
101150
0 commit comments