Texture.js 6.2 KB

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