Lut.js 13 KB

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