Lut.js 12 KB

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