Lut.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /**
  2. * @author daron1337 / http://daron1337.github.io/
  3. */
  4. THREE.Lut = function ( colormap, numberofcolors ) {
  5. this.lut = [];
  6. this.map = THREE.ColorMapKeywords[ colormap ];
  7. this.n = numberofcolors || 32;
  8. this.mapname = colormap;
  9. var step = 1.0 / this.n;
  10. for ( var i = 0; i <= 1; i += step ) {
  11. for ( var j = 0; j < this.map.length - 1; j ++ ) {
  12. if ( i >= this.map[ j ][ 0 ] && i < this.map[ j + 1 ][ 0 ] ) {
  13. var min = this.map[ j ][ 0 ];
  14. var max = this.map[ j + 1 ][ 0 ];
  15. var minColor = new THREE.Color( this.map[ j ][ 1 ] );
  16. var maxColor = new THREE.Color( this.map[ j + 1 ][ 1 ] );
  17. var color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  18. this.lut.push( color );
  19. }
  20. }
  21. }
  22. return this.set( this );
  23. };
  24. var defaultLegendParamters = {
  25. layout: 'vertical',
  26. position: new THREE.Vector3(),
  27. dimensions: { width: 0.5, height: 3 }
  28. };
  29. var defaultLabelParameters = {
  30. fontsize: 24,
  31. fontface: 'Arial',
  32. title: '',
  33. um: '',
  34. ticks: 0,
  35. decimal: 2,
  36. notation: 'standard'
  37. };
  38. var defaultBackgroundColor = { r: 255, g: 100, b: 100, a: 0.8 };
  39. var defaultBorderColor = { r: 255, g: 0, b: 0, a: 1.0 };
  40. var defaultBorderThickness = 4;
  41. THREE.Lut.prototype = {
  42. constructor: THREE.Lut,
  43. lut: [], map: [], mapname: 'rainbow', n: 256, minV: 0, maxV: 1, legend: null,
  44. set: function ( value ) {
  45. if ( value instanceof THREE.Lut ) {
  46. this.copy( value );
  47. }
  48. return this;
  49. },
  50. setMin: function ( min ) {
  51. this.minV = min;
  52. return this;
  53. },
  54. setMax: function ( max ) {
  55. this.maxV = max;
  56. return this;
  57. },
  58. changeNumberOfColors: function ( numberofcolors ) {
  59. this.n = numberofcolors;
  60. return new THREE.Lut( this.mapname, this.n );
  61. },
  62. changeColorMap: function ( colormap ) {
  63. this.mapname = colormap;
  64. return new THREE.Lut( this.mapname, this.n );
  65. },
  66. copy: function ( lut ) {
  67. this.lut = lut.lut;
  68. this.mapname = lut.mapname;
  69. this.map = lut.map;
  70. this.n = lut.n;
  71. this.minV = lut.minV;
  72. this.maxV = lut.maxV;
  73. return this;
  74. },
  75. getColor: function ( alpha ) {
  76. if ( alpha <= this.minV ) {
  77. alpha = this.minV;
  78. } else if ( alpha >= this.maxV ) {
  79. alpha = this.maxV;
  80. }
  81. alpha = ( alpha - this.minV ) / ( this.maxV - this.minV );
  82. var colorPosition = Math.round( alpha * this.n );
  83. colorPosition == this.n ? colorPosition -= 1 : colorPosition;
  84. return this.lut[ colorPosition ];
  85. },
  86. addColorMap: function ( colormapName, arrayOfColors ) {
  87. THREE.ColorMapKeywords[ colormapName ] = arrayOfColors;
  88. },
  89. setLegendOn: function ( parameters ) {
  90. parameters = parameters || defaultLegendParamters;
  91. this.legend = {};
  92. this.legend.layout = parameters.layout || defaultLegendParamters.layout;
  93. this.legend.position = parameters.position || defaultLegendParamters.position;
  94. this.legend.dimensions = parameters.dimensions || defaultLegendParamters.dimensions;
  95. this.legend.canvas = document.createElement( 'canvas' );
  96. this.legend.canvas.setAttribute( 'id', 'legend' );
  97. this.legend.canvas.setAttribute( 'hidden', true );
  98. document.body.appendChild( this.legend.canvas );
  99. this.legend.ctx = this.legend.canvas.getContext( '2d' );
  100. this.legend.canvas.setAttribute( 'width', 1 );
  101. this.legend.canvas.setAttribute( 'height', this.n );
  102. this.legend.texture = new THREE.Texture( this.legend.canvas );
  103. var imageData = this.legend.ctx.getImageData( 0, 0, 1, this.n );
  104. var data = imageData.data;
  105. this.map = THREE.ColorMapKeywords[ this.mapname ];
  106. var k = 0;
  107. var step = 1.0 / this.n;
  108. for ( var i = 1; i >= 0; i -= step ) {
  109. for ( var j = this.map.length - 1; j >= 0; j -- ) {
  110. if ( i < this.map[ j ][ 0 ] && i >= this.map[ j - 1 ][ 0 ] ) {
  111. var min = this.map[ j - 1 ][ 0 ];
  112. var max = this.map[ j ][ 0 ];
  113. var minColor = new THREE.Color( this.map[ j - 1 ][ 1 ] );
  114. var maxColor = new THREE.Color( this.map[ j ][ 1 ] );
  115. var color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  116. data[ k * 4 ] = Math.round( color.r * 255 );
  117. data[ k * 4 + 1 ] = Math.round( color.g * 255 );
  118. data[ k * 4 + 2 ] = Math.round( color.b * 255 );
  119. data[ k * 4 + 3 ] = 255;
  120. k += 1;
  121. }
  122. }
  123. }
  124. this.legend.ctx.putImageData( imageData, 0, 0 );
  125. this.legend.texture.needsUpdate = true;
  126. this.legend.legendGeometry = new THREE.PlaneBufferGeometry( this.legend.dimensions.width, this.legend.dimensions.height );
  127. this.legend.legendMaterial = new THREE.MeshBasicMaterial( { map: this.legend.texture, side: THREE.DoubleSide } );
  128. this.legend.mesh = new THREE.Mesh( this.legend.legendGeometry, this.legend.legendMaterial );
  129. if ( this.legend.layout == 'horizontal' ) {
  130. this.legend.mesh.rotation.z = - 90 * ( Math.PI / 180 );
  131. }
  132. this.legend.mesh.position.copy( this.legend.position );
  133. return this.legend.mesh;
  134. },
  135. setLegendOff: function () {
  136. this.legend = null;
  137. return this.legend;
  138. },
  139. setLegendLayout: function ( layout ) {
  140. if ( ! this.legend ) {
  141. return false;
  142. }
  143. if ( this.legend.layout == layout ) {
  144. return false;
  145. }
  146. if ( layout != 'horizontal' && layout != 'vertical' ) {
  147. return false;
  148. }
  149. this.layout = layout;
  150. if ( layout == 'horizontal' ) {
  151. this.legend.mesh.rotation.z = 90 * ( Math.PI / 180 );
  152. }
  153. if ( layout == 'vertical' ) {
  154. this.legend.mesh.rotation.z = - 90 * ( Math.PI / 180 );
  155. }
  156. return this.legend.mesh;
  157. },
  158. setLegendPosition: function ( position ) {
  159. if ( position.isVector3 ) {
  160. this.legend.position.copy( position );
  161. } else {
  162. this.legend.position.set( position.x, position.y, position.z );
  163. }
  164. return this.legend;
  165. },
  166. setLegendLabels: function ( parameters, callback ) {
  167. if ( ! this.legend ) {
  168. return false;
  169. }
  170. if ( typeof parameters === 'function' ) {
  171. callback = parameters;
  172. parameters = undefined;
  173. }
  174. parameters = parameters || defaultLabelParameters;
  175. this.legend.labels = {};
  176. this.legend.labels.fontsize = parameters.fontsize || defaultLabelParameters.fontsize;
  177. this.legend.labels.fontface = parameters.fontface || defaultLabelParameters.fontface;
  178. this.legend.labels.title = parameters.title || defaultLabelParameters.title;
  179. this.legend.labels.um = parameters.um || defaultLabelParameters.um;
  180. this.legend.labels.ticks = parameters.ticks || defaultLabelParameters.ticks;
  181. this.legend.labels.decimal = parameters.decimal || defaultLabelParameters.decimal;
  182. this.legend.labels.notation = parameters.notation || defaultLabelParameters.notation;
  183. var canvasTitle = document.createElement( 'canvas' );
  184. var contextTitle = canvasTitle.getContext( '2d' );
  185. contextTitle.font = 'Normal ' + this.legend.labels.fontsize * 1.2 + 'px ' + this.legend.labels.fontface;
  186. contextTitle.fillStyle = 'rgba(' + defaultBackgroundColor.r + ',' + defaultBackgroundColor.g + ',' + defaultBackgroundColor.b + ',' + defaultBackgroundColor.a + ')';
  187. contextTitle.strokeStyle = 'rgba(' + defaultBorderColor.r + ',' + defaultBorderColor.g + ',' + defaultBorderColor.b + ',' + defaultBorderColor.a + ')';
  188. contextTitle.lineWidth = defaultBorderThickness;
  189. contextTitle.fillStyle = 'rgba( 0, 0, 0, 1.0 )';
  190. contextTitle.fillText( this.legend.labels.title.toString() + this.legend.labels.um.toString(), defaultBorderThickness, this.legend.labels.fontsize + defaultBorderThickness );
  191. var txtTitle = new THREE.CanvasTexture( canvasTitle );
  192. txtTitle.minFilter = THREE.LinearFilter;
  193. var spriteMaterialTitle = new THREE.SpriteMaterial( { map: txtTitle } );
  194. var spriteTitle = new THREE.Sprite( spriteMaterialTitle );
  195. spriteTitle.scale.set( 2, 1, 1.0 );
  196. if ( this.legend.layout == 'vertical' ) {
  197. spriteTitle.position.set( this.legend.position.x + this.legend.dimensions.width, this.legend.position.y + ( this.legend.dimensions.height * 0.45 ), this.legend.position.z );
  198. }
  199. if ( this.legend.layout == 'horizontal' ) {
  200. spriteTitle.position.set( this.legend.position.x * 1.015, this.legend.position.y + ( this.legend.dimensions.height * 0.03 ), this.legend.position.z );
  201. }
  202. if ( this.legend.labels.ticks > 0 ) {
  203. var ticks = {};
  204. var lines = {};
  205. if ( this.legend.layout == 'vertical' ) {
  206. var topPositionY = this.legend.position.y + ( this.legend.dimensions.height * 0.36 );
  207. var bottomPositionY = this.legend.position.y - ( this.legend.dimensions.height * 0.61 );
  208. }
  209. if ( this.legend.layout == 'horizontal' ) {
  210. var topPositionX = this.legend.position.x + ( this.legend.dimensions.height * 0.75 );
  211. var bottomPositionX = this.legend.position.x - ( this.legend.dimensions.width * 1.2 );
  212. }
  213. for ( var i = 0; i < this.legend.labels.ticks; i ++ ) {
  214. var value = ( this.maxV - this.minV ) / ( this.legend.labels.ticks - 1 ) * i + this.minV;
  215. if ( callback ) {
  216. value = callback( value );
  217. } else {
  218. if ( this.legend.labels.notation == 'scientific' ) {
  219. value = value.toExponential( this.legend.labels.decimal );
  220. } else {
  221. value = value.toFixed( this.legend.labels.decimal );
  222. }
  223. }
  224. var canvasTick = document.createElement( 'canvas' );
  225. var contextTick = canvasTick.getContext( '2d' );
  226. contextTick.font = 'Normal ' + this.legend.labels.fontsize + 'px ' + this.legend.labels.fontface;
  227. contextTick.fillStyle = 'rgba(' + defaultBackgroundColor.r + ',' + defaultBackgroundColor.g + ',' + defaultBackgroundColor.b + ',' + defaultBackgroundColor.a + ')';
  228. contextTick.strokeStyle = 'rgba(' + defaultBorderColor.r + ',' + defaultBorderColor.g + ',' + defaultBorderColor.b + ',' + defaultBorderColor.a + ')';
  229. contextTick.lineWidth = defaultBorderThickness;
  230. contextTick.fillStyle = 'rgba( 0, 0, 0, 1.0 )';
  231. contextTick.fillText( value.toString(), defaultBorderThickness, this.legend.labels.fontsize + defaultBorderThickness );
  232. var txtTick = new THREE.CanvasTexture( canvasTick );
  233. txtTick.minFilter = THREE.LinearFilter;
  234. var spriteMaterialTick = new THREE.SpriteMaterial( { map: txtTick } );
  235. var spriteTick = new THREE.Sprite( spriteMaterialTick );
  236. spriteTick.scale.set( 2, 1, 1.0 );
  237. if ( this.legend.layout == 'vertical' ) {
  238. var position = bottomPositionY + ( topPositionY - bottomPositionY ) * ( ( value - this.minV ) / ( this.maxV - this.minV ) );
  239. spriteTick.position.set( this.legend.position.x + ( this.legend.dimensions.width * 2.7 ), position, this.legend.position.z );
  240. }
  241. if ( this.legend.layout == 'horizontal' ) {
  242. var position = bottomPositionX + ( topPositionX - bottomPositionX ) * ( ( value - this.minV ) / ( this.maxV - this.minV ) );
  243. if ( this.legend.labels.ticks > 5 ) {
  244. if ( i % 2 === 0 ) {
  245. var offset = 1.7;
  246. } else {
  247. var offset = 2.1;
  248. }
  249. } else {
  250. var offset = 1.7;
  251. }
  252. spriteTick.position.set( position, this.legend.position.y - this.legend.dimensions.width * offset, this.legend.position.z );
  253. }
  254. var material = new THREE.LineBasicMaterial( { color: 0x000000, linewidth: 2 } );
  255. var points = [];
  256. if ( this.legend.layout == 'vertical' ) {
  257. var linePosition = ( this.legend.position.y - ( this.legend.dimensions.height * 0.5 ) + 0.01 ) + ( this.legend.dimensions.height ) * ( ( value - this.minV ) / ( this.maxV - this.minV ) * 0.99 );
  258. points.push( new THREE.Vector3( this.legend.position.x + this.legend.dimensions.width * 0.55, linePosition, this.legend.position.z ) );
  259. points.push( new THREE.Vector3( this.legend.position.x + this.legend.dimensions.width * 0.7, linePosition, this.legend.position.z ) );
  260. }
  261. if ( this.legend.layout == 'horizontal' ) {
  262. var linePosition = ( this.legend.position.x - ( this.legend.dimensions.height * 0.5 ) + 0.01 ) + ( this.legend.dimensions.height ) * ( ( value - this.minV ) / ( this.maxV - this.minV ) * 0.99 );
  263. points.push( new THREE.Vector3( linePosition, this.legend.position.y - this.legend.dimensions.width * 0.55, this.legend.position.z ) );
  264. points.push( new THREE.Vector3( linePosition, this.legend.position.y - this.legend.dimensions.width * 0.7, this.legend.position.z ) );
  265. }
  266. var geometry = new THREE.BufferGeometry().setFromPoints( points );
  267. var line = new THREE.Line( geometry, material );
  268. lines[ i ] = line;
  269. ticks[ i ] = spriteTick;
  270. }
  271. }
  272. return { 'title': spriteTitle, 'ticks': ticks, 'lines': lines };
  273. }
  274. };
  275. THREE.ColorMapKeywords = {
  276. "rainbow": [[ 0.0, 0x0000FF ], [ 0.2, 0x00FFFF ], [ 0.5, 0x00FF00 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFF0000 ]],
  277. "cooltowarm": [[ 0.0, 0x3C4EC2 ], [ 0.2, 0x9BBCFF ], [ 0.5, 0xDCDCDC ], [ 0.8, 0xF6A385 ], [ 1.0, 0xB40426 ]],
  278. "blackbody": [[ 0.0, 0x000000 ], [ 0.2, 0x780000 ], [ 0.5, 0xE63200 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFFFFFF ]],
  279. "grayscale": [[ 0.0, 0x000000 ], [ 0.2, 0x404040 ], [ 0.5, 0x7F7F80 ], [ 0.8, 0xBFBFBF ], [ 1.0, 0xFFFFFF ]]
  280. };