Lut.js 13 KB

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