Texture.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author szimek / https://github.com/szimek/
  5. */
  6. THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  7. Object.defineProperty( this, 'id', { value: THREE.TextureIdCount ++ } );
  8. this.uuid = THREE.Math.generateUUID();
  9. this.name = '';
  10. this.sourceFile = '';
  11. this.image = image !== undefined ? image : THREE.Texture.DEFAULT_IMAGE;
  12. this.mipmaps = [];
  13. this.mapping = mapping !== undefined ? mapping : THREE.Texture.DEFAULT_MAPPING;
  14. this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
  15. this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
  16. this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
  17. this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
  18. this.anisotropy = anisotropy !== undefined ? anisotropy : 1;
  19. this.format = format !== undefined ? format : THREE.RGBAFormat;
  20. this.type = type !== undefined ? type : THREE.UnsignedByteType;
  21. this.offset = new THREE.Vector2( 0, 0 );
  22. this.repeat = new THREE.Vector2( 1, 1 );
  23. this.generateMipmaps = true;
  24. this.premultiplyAlpha = false;
  25. this.flipY = true;
  26. this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
  27. this.version = 0;
  28. this.onUpdate = null;
  29. };
  30. THREE.Texture.DEFAULT_IMAGE = undefined;
  31. THREE.Texture.DEFAULT_MAPPING = THREE.UVMapping;
  32. THREE.Texture.prototype = {
  33. constructor: THREE.Texture,
  34. set needsUpdate ( value ) {
  35. if ( value === true ) this.version ++;
  36. },
  37. clone: function () {
  38. return new this.constructor().copy( this );
  39. },
  40. copy: function ( source ) {
  41. this.image = source.image;
  42. this.mipmaps = source.mipmaps.slice( 0 );
  43. this.mapping = source.mapping;
  44. this.wrapS = source.wrapS;
  45. this.wrapT = source.wrapT;
  46. this.magFilter = source.magFilter;
  47. this.minFilter = source.minFilter;
  48. this.anisotropy = source.anisotropy;
  49. this.format = source.format;
  50. this.type = source.type;
  51. this.offset.copy( source.offset );
  52. this.repeat.copy( source.repeat );
  53. this.generateMipmaps = source.generateMipmaps;
  54. this.premultiplyAlpha = source.premultiplyAlpha;
  55. this.flipY = source.flipY;
  56. this.unpackAlignment = source.unpackAlignment;
  57. return this;
  58. },
  59. toJSON: function ( meta ) {
  60. if ( meta.textures[ this.uuid ] !== undefined ) {
  61. return meta.textures[ this.uuid ];
  62. }
  63. function getDataURL( image ) {
  64. var canvas;
  65. if ( image.toDataURL !== undefined ) {
  66. canvas = image;
  67. } else {
  68. canvas = document.createElement( 'canvas' );
  69. canvas.width = image.width;
  70. canvas.height = image.height;
  71. canvas.getContext( '2d' ).drawImage( image, 0, 0, image.width, image.height );
  72. }
  73. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  74. return canvas.toDataURL( 'image/jpeg', 0.6 );
  75. } else {
  76. return canvas.toDataURL( 'image/png' );
  77. }
  78. }
  79. var output = {
  80. metadata: {
  81. version: 4.4,
  82. type: 'Texture',
  83. generator: 'Texture.toJSON'
  84. },
  85. uuid: this.uuid,
  86. name: this.name,
  87. mapping: this.mapping,
  88. repeat: [ this.repeat.x, this.repeat.y ],
  89. offset: [ this.offset.x, this.offset.y ],
  90. wrap: [ this.wrapS, this.wrapT ],
  91. minFilter: this.minFilter,
  92. magFilter: this.magFilter,
  93. anisotropy: this.anisotropy
  94. };
  95. if ( this.image !== undefined ) {
  96. // TODO: Move to THREE.Image
  97. var image = this.image;
  98. if ( image.uuid === undefined ) {
  99. image.uuid = THREE.Math.generateUUID(); // UGH
  100. }
  101. if ( meta.images[ image.uuid ] === undefined ) {
  102. meta.images[ image.uuid ] = {
  103. uuid: image.uuid,
  104. url: getDataURL( image )
  105. };
  106. }
  107. output.image = image.uuid;
  108. }
  109. meta.textures[ this.uuid ] = output;
  110. return output;
  111. },
  112. dispose: function () {
  113. this.dispatchEvent( { type: 'dispose' } );
  114. },
  115. transformUv: function ( uv ) {
  116. if ( this.mapping !== THREE.UVMapping ) return;
  117. uv.multiply( this.repeat );
  118. uv.add( this.offset );
  119. if ( uv.x < 0 || uv.x > 1 ) {
  120. switch ( this.wrapS ) {
  121. case THREE.RepeatWrapping:
  122. uv.x = uv.x - Math.floor( uv.x );
  123. break;
  124. case THREE.ClampToEdgeWrapping:
  125. uv.x = uv.x < 0 ? 0 : 1;
  126. break;
  127. case THREE.MirroredRepeatWrapping:
  128. if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {
  129. uv.x = Math.ceil( uv.x ) - uv.x;
  130. } else {
  131. uv.x = uv.x - Math.floor( uv.x );
  132. }
  133. break;
  134. }
  135. }
  136. if ( uv.y < 0 || uv.y > 1 ) {
  137. switch ( this.wrapT ) {
  138. case THREE.RepeatWrapping:
  139. uv.y = uv.y - Math.floor( uv.y );
  140. break;
  141. case THREE.ClampToEdgeWrapping:
  142. uv.y = uv.y < 0 ? 0 : 1;
  143. break;
  144. case THREE.MirroredRepeatWrapping:
  145. if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
  146. uv.y = Math.ceil( uv.y ) - uv.y;
  147. } else {
  148. uv.y = uv.y - Math.floor( uv.y );
  149. }
  150. break;
  151. }
  152. }
  153. if ( this.flipY ) {
  154. uv.y = 1 - uv.y;
  155. }
  156. }
  157. };
  158. THREE.EventDispatcher.prototype.apply( THREE.Texture.prototype );
  159. THREE.TextureIdCount = 0;