Texture.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.encoding = THREE.DefaultEncoding; // Values !== THREE.LinearEncoding only supported on map, envMap and emissiveMap (as these maps regularly have unbounded intensity values, i.e. via an *.hdr or *.exr image.)
  28. this.version = 0;
  29. this.onUpdate = null;
  30. };
  31. THREE.Texture.DEFAULT_IMAGE = undefined;
  32. THREE.Texture.DEFAULT_MAPPING = THREE.UVMapping;
  33. THREE.Texture.prototype = {
  34. constructor: THREE.Texture,
  35. set needsUpdate ( value ) {
  36. if ( value === true ) this.version ++;
  37. },
  38. clone: function () {
  39. return new this.constructor().copy( this );
  40. },
  41. copy: function ( source ) {
  42. this.image = source.image;
  43. this.mipmaps = source.mipmaps.slice( 0 );
  44. this.mapping = source.mapping;
  45. this.wrapS = source.wrapS;
  46. this.wrapT = source.wrapT;
  47. this.magFilter = source.magFilter;
  48. this.minFilter = source.minFilter;
  49. this.anisotropy = source.anisotropy;
  50. this.format = source.format;
  51. this.type = source.type;
  52. this.offset.copy( source.offset );
  53. this.repeat.copy( source.repeat );
  54. this.generateMipmaps = source.generateMipmaps;
  55. this.premultiplyAlpha = source.premultiplyAlpha;
  56. this.flipY = source.flipY;
  57. this.unpackAlignment = source.unpackAlignment;
  58. this.encoding = source.encoding;
  59. return this;
  60. },
  61. toJSON: function ( meta ) {
  62. if ( meta.textures[ this.uuid ] !== undefined ) {
  63. return meta.textures[ this.uuid ];
  64. }
  65. function getDataURL( image ) {
  66. var canvas;
  67. if ( image.toDataURL !== undefined ) {
  68. canvas = image;
  69. } else {
  70. canvas = document.createElement( 'canvas' );
  71. canvas.width = image.width;
  72. canvas.height = image.height;
  73. canvas.getContext( '2d' ).drawImage( image, 0, 0, image.width, image.height );
  74. }
  75. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  76. return canvas.toDataURL( 'image/jpeg', 0.6 );
  77. } else {
  78. return canvas.toDataURL( 'image/png' );
  79. }
  80. }
  81. var output = {
  82. metadata: {
  83. version: 4.4,
  84. type: 'Texture',
  85. generator: 'Texture.toJSON'
  86. },
  87. uuid: this.uuid,
  88. name: this.name,
  89. mapping: this.mapping,
  90. repeat: [ this.repeat.x, this.repeat.y ],
  91. offset: [ this.offset.x, this.offset.y ],
  92. wrap: [ this.wrapS, this.wrapT ],
  93. minFilter: this.minFilter,
  94. magFilter: this.magFilter,
  95. anisotropy: this.anisotropy
  96. };
  97. if ( this.image !== undefined ) {
  98. // TODO: Move to THREE.Image
  99. var image = this.image;
  100. if ( image.uuid === undefined ) {
  101. image.uuid = THREE.Math.generateUUID(); // UGH
  102. }
  103. if ( meta.images[ image.uuid ] === undefined ) {
  104. meta.images[ image.uuid ] = {
  105. uuid: image.uuid,
  106. url: getDataURL( image )
  107. };
  108. }
  109. output.image = image.uuid;
  110. }
  111. meta.textures[ this.uuid ] = output;
  112. return output;
  113. },
  114. dispose: function () {
  115. this.dispatchEvent( { type: 'dispose' } );
  116. },
  117. transformUv: function ( uv ) {
  118. if ( this.mapping !== THREE.UVMapping ) return;
  119. uv.multiply( this.repeat );
  120. uv.add( this.offset );
  121. if ( uv.x < 0 || uv.x > 1 ) {
  122. switch ( this.wrapS ) {
  123. case THREE.RepeatWrapping:
  124. uv.x = uv.x - Math.floor( uv.x );
  125. break;
  126. case THREE.ClampToEdgeWrapping:
  127. uv.x = uv.x < 0 ? 0 : 1;
  128. break;
  129. case THREE.MirroredRepeatWrapping:
  130. if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {
  131. uv.x = Math.ceil( uv.x ) - uv.x;
  132. } else {
  133. uv.x = uv.x - Math.floor( uv.x );
  134. }
  135. break;
  136. }
  137. }
  138. if ( uv.y < 0 || uv.y > 1 ) {
  139. switch ( this.wrapT ) {
  140. case THREE.RepeatWrapping:
  141. uv.y = uv.y - Math.floor( uv.y );
  142. break;
  143. case THREE.ClampToEdgeWrapping:
  144. uv.y = uv.y < 0 ? 0 : 1;
  145. break;
  146. case THREE.MirroredRepeatWrapping:
  147. if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
  148. uv.y = Math.ceil( uv.y ) - uv.y;
  149. } else {
  150. uv.y = uv.y - Math.floor( uv.y );
  151. }
  152. break;
  153. }
  154. }
  155. if ( this.flipY ) {
  156. uv.y = 1 - uv.y;
  157. }
  158. }
  159. };
  160. THREE.EventDispatcher.prototype.apply( THREE.Texture.prototype );
  161. THREE.TextureIdCount = 0;
粤ICP备19079148号