Lut.js 13 KB

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