ImageUtils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author Daosheng Mu / https://github.com/DaoshengMu/
  5. */
  6. THREE.ImageUtils = {
  7. getNormalMap: function ( image, depth ) {
  8. // Adapted from http://www.paulbrunt.co.uk/lab/heightnormal/
  9. function cross( a, b ) {
  10. return [ a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ], a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ], a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ] ];
  11. }
  12. function subtract( a, b ) {
  13. return [ a[ 0 ] - b[ 0 ], a[ 1 ] - b[ 1 ], a[ 2 ] - b[ 2 ] ];
  14. }
  15. function normalize( a ) {
  16. var l = Math.sqrt( a[ 0 ] * a[ 0 ] + a[ 1 ] * a[ 1 ] + a[ 2 ] * a[ 2 ] );
  17. return [ a[ 0 ] / l, a[ 1 ] / l, a[ 2 ] / l ];
  18. }
  19. depth = depth | 1;
  20. var width = image.width;
  21. var height = image.height;
  22. var canvas = document.createElement( 'canvas' );
  23. canvas.width = width;
  24. canvas.height = height;
  25. var context = canvas.getContext( '2d' );
  26. context.drawImage( image, 0, 0 );
  27. var data = context.getImageData( 0, 0, width, height ).data;
  28. var imageData = context.createImageData( width, height );
  29. var output = imageData.data;
  30. for ( var x = 0; x < width; x ++ ) {
  31. for ( var y = 0; y < height; y ++ ) {
  32. var ly = y - 1 < 0 ? 0 : y - 1;
  33. var uy = y + 1 > height - 1 ? height - 1 : y + 1;
  34. var lx = x - 1 < 0 ? 0 : x - 1;
  35. var ux = x + 1 > width - 1 ? width - 1 : x + 1;
  36. var points = [];
  37. var origin = [ 0, 0, data[ ( y * width + x ) * 4 ] / 255 * depth ];
  38. points.push( [ - 1, 0, data[ ( y * width + lx ) * 4 ] / 255 * depth ] );
  39. points.push( [ - 1, - 1, data[ ( ly * width + lx ) * 4 ] / 255 * depth ] );
  40. points.push( [ 0, - 1, data[ ( ly * width + x ) * 4 ] / 255 * depth ] );
  41. points.push( [ 1, - 1, data[ ( ly * width + ux ) * 4 ] / 255 * depth ] );
  42. points.push( [ 1, 0, data[ ( y * width + ux ) * 4 ] / 255 * depth ] );
  43. points.push( [ 1, 1, data[ ( uy * width + ux ) * 4 ] / 255 * depth ] );
  44. points.push( [ 0, 1, data[ ( uy * width + x ) * 4 ] / 255 * depth ] );
  45. points.push( [ - 1, 1, data[ ( uy * width + lx ) * 4 ] / 255 * depth ] );
  46. var normals = [];
  47. var num_points = points.length;
  48. for ( var i = 0; i < num_points; i ++ ) {
  49. var v1 = points[ i ];
  50. var v2 = points[ ( i + 1 ) % num_points ];
  51. v1 = subtract( v1, origin );
  52. v2 = subtract( v2, origin );
  53. normals.push( normalize( cross( v1, v2 ) ) );
  54. }
  55. var normal = [ 0, 0, 0 ];
  56. for ( var i = 0; i < normals.length; i ++ ) {
  57. normal[ 0 ] += normals[ i ][ 0 ];
  58. normal[ 1 ] += normals[ i ][ 1 ];
  59. normal[ 2 ] += normals[ i ][ 2 ];
  60. }
  61. normal[ 0 ] /= normals.length;
  62. normal[ 1 ] /= normals.length;
  63. normal[ 2 ] /= normals.length;
  64. var idx = ( y * width + x ) * 4;
  65. output[ idx ] = ( ( normal[ 0 ] + 1.0 ) / 2.0 * 255 ) | 0;
  66. output[ idx + 1 ] = ( ( normal[ 1 ] + 1.0 ) / 2.0 * 255 ) | 0;
  67. output[ idx + 2 ] = ( normal[ 2 ] * 255 ) | 0;
  68. output[ idx + 3 ] = 255;
  69. }
  70. }
  71. context.putImageData( imageData, 0, 0 );
  72. return canvas;
  73. },
  74. generateDataTexture: function ( width, height, color ) {
  75. var size = width * height;
  76. var data = new Uint8Array( 3 * size );
  77. var r = Math.floor( color.r * 255 );
  78. var g = Math.floor( color.g * 255 );
  79. var b = Math.floor( color.b * 255 );
  80. for ( var i = 0; i < size; i ++ ) {
  81. data[ i * 3 ] = r;
  82. data[ i * 3 + 1 ] = g;
  83. data[ i * 3 + 2 ] = b;
  84. }
  85. var texture = new THREE.DataTexture( data, width, height, THREE.RGBFormat );
  86. texture.needsUpdate = true;
  87. return texture;
  88. }
  89. };