Texture.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import { EventDispatcher } from '../core/EventDispatcher.js';
  2. import {
  3. MirroredRepeatWrapping,
  4. ClampToEdgeWrapping,
  5. RepeatWrapping,
  6. LinearEncoding,
  7. UnsignedByteType,
  8. RGBAFormat,
  9. LinearMipmapLinearFilter,
  10. LinearFilter,
  11. UVMapping
  12. } from '../constants.js';
  13. import { MathUtils } from '../math/MathUtils.js';
  14. import { Vector2 } from '../math/Vector2.js';
  15. import { Matrix3 } from '../math/Matrix3.js';
  16. import { ImageUtils } from '../extras/ImageUtils.js';
  17. let textureId = 0;
  18. class Texture extends EventDispatcher {
  19. constructor( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) {
  20. super();
  21. Object.defineProperty( this, 'id', { value: textureId ++ } );
  22. this.uuid = MathUtils.generateUUID();
  23. this.name = '';
  24. this.image = image !== undefined ? image : Texture.DEFAULT_IMAGE;
  25. this.mipmaps = [];
  26. this.mapping = mapping !== undefined ? mapping : Texture.DEFAULT_MAPPING;
  27. this.wrapS = wrapS !== undefined ? wrapS : ClampToEdgeWrapping;
  28. this.wrapT = wrapT !== undefined ? wrapT : ClampToEdgeWrapping;
  29. this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
  30. this.minFilter = minFilter !== undefined ? minFilter : LinearMipmapLinearFilter;
  31. this.anisotropy = anisotropy !== undefined ? anisotropy : 1;
  32. this.format = format !== undefined ? format : RGBAFormat;
  33. this.internalFormat = null;
  34. this.type = type !== undefined ? type : UnsignedByteType;
  35. this.offset = new Vector2( 0, 0 );
  36. this.repeat = new Vector2( 1, 1 );
  37. this.center = new Vector2( 0, 0 );
  38. this.rotation = 0;
  39. this.matrixAutoUpdate = true;
  40. this.matrix = new Matrix3();
  41. this.generateMipmaps = true;
  42. this.premultiplyAlpha = false;
  43. this.flipY = true;
  44. this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
  45. // Values of encoding !== THREE.LinearEncoding only supported on map, envMap and emissiveMap.
  46. //
  47. // Also changing the encoding after already used by a Material will not automatically make the Material
  48. // update. You need to explicitly call Material.needsUpdate to trigger it to recompile.
  49. this.encoding = encoding !== undefined ? encoding : LinearEncoding;
  50. this.version = 0;
  51. this.onUpdate = null;
  52. }
  53. updateMatrix() {
  54. this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y );
  55. }
  56. clone() {
  57. return new this.constructor().copy( this );
  58. }
  59. copy( source ) {
  60. this.name = source.name;
  61. this.image = source.image;
  62. this.mipmaps = source.mipmaps.slice( 0 );
  63. this.mapping = source.mapping;
  64. this.wrapS = source.wrapS;
  65. this.wrapT = source.wrapT;
  66. this.magFilter = source.magFilter;
  67. this.minFilter = source.minFilter;
  68. this.anisotropy = source.anisotropy;
  69. this.format = source.format;
  70. this.internalFormat = source.internalFormat;
  71. this.type = source.type;
  72. this.offset.copy( source.offset );
  73. this.repeat.copy( source.repeat );
  74. this.center.copy( source.center );
  75. this.rotation = source.rotation;
  76. this.matrixAutoUpdate = source.matrixAutoUpdate;
  77. this.matrix.copy( source.matrix );
  78. this.generateMipmaps = source.generateMipmaps;
  79. this.premultiplyAlpha = source.premultiplyAlpha;
  80. this.flipY = source.flipY;
  81. this.unpackAlignment = source.unpackAlignment;
  82. this.encoding = source.encoding;
  83. return this;
  84. }
  85. toJSON( meta ) {
  86. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  87. if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) {
  88. return meta.textures[ this.uuid ];
  89. }
  90. const output = {
  91. metadata: {
  92. version: 4.5,
  93. type: 'Texture',
  94. generator: 'Texture.toJSON'
  95. },
  96. uuid: this.uuid,
  97. name: this.name,
  98. mapping: this.mapping,
  99. repeat: [ this.repeat.x, this.repeat.y ],
  100. offset: [ this.offset.x, this.offset.y ],
  101. center: [ this.center.x, this.center.y ],
  102. rotation: this.rotation,
  103. wrap: [ this.wrapS, this.wrapT ],
  104. format: this.format,
  105. type: this.type,
  106. encoding: this.encoding,
  107. minFilter: this.minFilter,
  108. magFilter: this.magFilter,
  109. anisotropy: this.anisotropy,
  110. flipY: this.flipY,
  111. premultiplyAlpha: this.premultiplyAlpha,
  112. unpackAlignment: this.unpackAlignment
  113. };
  114. if ( this.image !== undefined ) {
  115. // TODO: Move to THREE.Image
  116. const image = this.image;
  117. if ( image.uuid === undefined ) {
  118. image.uuid = MathUtils.generateUUID(); // UGH
  119. }
  120. if ( ! isRootObject && meta.images[ image.uuid ] === undefined ) {
  121. let url;
  122. if ( Array.isArray( image ) ) {
  123. // process array of images e.g. CubeTexture
  124. url = [];
  125. for ( let i = 0, l = image.length; i < l; i ++ ) {
  126. url.push( ImageUtils.getDataURL( image[ i ] ) );
  127. }
  128. } else {
  129. // process single image
  130. url = ImageUtils.getDataURL( image );
  131. }
  132. meta.images[ image.uuid ] = {
  133. uuid: image.uuid,
  134. url: url
  135. };
  136. }
  137. output.image = image.uuid;
  138. }
  139. if ( ! isRootObject ) {
  140. meta.textures[ this.uuid ] = output;
  141. }
  142. return output;
  143. }
  144. dispose() {
  145. this.dispatchEvent( { type: 'dispose' } );
  146. }
  147. transformUv( uv ) {
  148. if ( this.mapping !== UVMapping ) return uv;
  149. uv.applyMatrix3( this.matrix );
  150. if ( uv.x < 0 || uv.x > 1 ) {
  151. switch ( this.wrapS ) {
  152. case RepeatWrapping:
  153. uv.x = uv.x - Math.floor( uv.x );
  154. break;
  155. case ClampToEdgeWrapping:
  156. uv.x = uv.x < 0 ? 0 : 1;
  157. break;
  158. case MirroredRepeatWrapping:
  159. if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {
  160. uv.x = Math.ceil( uv.x ) - uv.x;
  161. } else {
  162. uv.x = uv.x - Math.floor( uv.x );
  163. }
  164. break;
  165. }
  166. }
  167. if ( uv.y < 0 || uv.y > 1 ) {
  168. switch ( this.wrapT ) {
  169. case RepeatWrapping:
  170. uv.y = uv.y - Math.floor( uv.y );
  171. break;
  172. case ClampToEdgeWrapping:
  173. uv.y = uv.y < 0 ? 0 : 1;
  174. break;
  175. case MirroredRepeatWrapping:
  176. if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
  177. uv.y = Math.ceil( uv.y ) - uv.y;
  178. } else {
  179. uv.y = uv.y - Math.floor( uv.y );
  180. }
  181. break;
  182. }
  183. }
  184. if ( this.flipY ) {
  185. uv.y = 1 - uv.y;
  186. }
  187. return uv;
  188. }
  189. set needsUpdate( value ) {
  190. if ( value === true ) this.version ++;
  191. }
  192. }
  193. Texture.prototype.isTexture = true;
  194. Texture.prototype.DEFAULT_IMAGE = undefined;
  195. Texture.prototype.DEFAULT_MAPPING = UVMapping;
  196. export { Texture };
粤ICP备19079148号