Texture.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author szimek / https://github.com/szimek/
  5. */
  6. import { EventDispatcher } from '../core/EventDispatcher.js';
  7. import {
  8. MirroredRepeatWrapping,
  9. ClampToEdgeWrapping,
  10. RepeatWrapping,
  11. LinearEncoding,
  12. UnsignedByteType,
  13. RGBAFormat,
  14. LinearMipmapLinearFilter,
  15. LinearFilter,
  16. UVMapping
  17. } from '../constants.js';
  18. import { MathUtils } from '../math/MathUtils.js';
  19. import { Vector2 } from '../math/Vector2.js';
  20. import { Matrix3 } from '../math/Matrix3.js';
  21. import { ImageUtils } from '../extras/ImageUtils.js';
  22. var textureId = 0;
  23. function Texture( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) {
  24. Object.defineProperty( this, 'id', { value: textureId ++ } );
  25. this.uuid = MathUtils.generateUUID();
  26. this.name = '';
  27. this.image = image !== undefined ? image : Texture.DEFAULT_IMAGE;
  28. this.mipmaps = [];
  29. this.mapping = mapping !== undefined ? mapping : Texture.DEFAULT_MAPPING;
  30. this.wrapS = wrapS !== undefined ? wrapS : ClampToEdgeWrapping;
  31. this.wrapT = wrapT !== undefined ? wrapT : ClampToEdgeWrapping;
  32. this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
  33. this.minFilter = minFilter !== undefined ? minFilter : LinearMipmapLinearFilter;
  34. this.anisotropy = anisotropy !== undefined ? anisotropy : 1;
  35. this.format = format !== undefined ? format : RGBAFormat;
  36. this.internalFormat = null;
  37. this.type = type !== undefined ? type : UnsignedByteType;
  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 !== undefined ? encoding : LinearEncoding;
  53. this.version = 0;
  54. this.onUpdate = null;
  55. }
  56. Texture.DEFAULT_IMAGE = undefined;
  57. Texture.DEFAULT_MAPPING = UVMapping;
  58. Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
  59. constructor: Texture,
  60. isTexture: true,
  61. updateMatrix: function () {
  62. this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y );
  63. },
  64. clone: function () {
  65. return new this.constructor().copy( this );
  66. },
  67. copy: function ( source ) {
  68. this.name = source.name;
  69. this.image = source.image;
  70. this.mipmaps = source.mipmaps.slice( 0 );
  71. this.mapping = source.mapping;
  72. this.wrapS = source.wrapS;
  73. this.wrapT = source.wrapT;
  74. this.magFilter = source.magFilter;
  75. this.minFilter = source.minFilter;
  76. this.anisotropy = source.anisotropy;
  77. this.format = source.format;
  78. this.internalFormat = source.internalFormat;
  79. this.type = source.type;
  80. this.offset.copy( source.offset );
  81. this.repeat.copy( source.repeat );
  82. this.center.copy( source.center );
  83. this.rotation = source.rotation;
  84. this.matrixAutoUpdate = source.matrixAutoUpdate;
  85. this.matrix.copy( source.matrix );
  86. this.generateMipmaps = source.generateMipmaps;
  87. this.premultiplyAlpha = source.premultiplyAlpha;
  88. this.flipY = source.flipY;
  89. this.unpackAlignment = source.unpackAlignment;
  90. this.encoding = source.encoding;
  91. return this;
  92. },
  93. toJSON: function ( meta ) {
  94. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  95. if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) {
  96. return meta.textures[ this.uuid ];
  97. }
  98. var output = {
  99. metadata: {
  100. version: 4.5,
  101. type: 'Texture',
  102. generator: 'Texture.toJSON'
  103. },
  104. uuid: this.uuid,
  105. name: this.name,
  106. mapping: this.mapping,
  107. repeat: [ this.repeat.x, this.repeat.y ],
  108. offset: [ this.offset.x, this.offset.y ],
  109. center: [ this.center.x, this.center.y ],
  110. rotation: this.rotation,
  111. wrap: [ this.wrapS, this.wrapT ],
  112. format: this.format,
  113. type: this.type,
  114. encoding: this.encoding,
  115. minFilter: this.minFilter,
  116. magFilter: this.magFilter,
  117. anisotropy: this.anisotropy,
  118. flipY: this.flipY,
  119. premultiplyAlpha: this.premultiplyAlpha,
  120. unpackAlignment: this.unpackAlignment
  121. };
  122. if ( this.image !== undefined ) {
  123. // TODO: Move to THREE.Image
  124. var image = this.image;
  125. if ( image.uuid === undefined ) {
  126. image.uuid = MathUtils.generateUUID(); // UGH
  127. }
  128. if ( ! isRootObject && meta.images[ image.uuid ] === undefined ) {
  129. var url;
  130. if ( Array.isArray( image ) ) {
  131. // process array of images e.g. CubeTexture
  132. url = [];
  133. for ( var i = 0, l = image.length; i < l; i ++ ) {
  134. url.push( ImageUtils.getDataURL( image[ i ] ) );
  135. }
  136. } else {
  137. // process single image
  138. url = ImageUtils.getDataURL( image );
  139. }
  140. meta.images[ image.uuid ] = {
  141. uuid: image.uuid,
  142. url: url
  143. };
  144. }
  145. output.image = image.uuid;
  146. }
  147. if ( ! isRootObject ) {
  148. meta.textures[ this.uuid ] = output;
  149. }
  150. return output;
  151. },
  152. dispose: function () {
  153. this.dispatchEvent( { type: 'dispose' } );
  154. },
  155. transformUv: function ( uv ) {
  156. if ( this.mapping !== UVMapping ) return uv;
  157. uv.applyMatrix3( this.matrix );
  158. if ( uv.x < 0 || uv.x > 1 ) {
  159. switch ( this.wrapS ) {
  160. case RepeatWrapping:
  161. uv.x = uv.x - Math.floor( uv.x );
  162. break;
  163. case ClampToEdgeWrapping:
  164. uv.x = uv.x < 0 ? 0 : 1;
  165. break;
  166. case MirroredRepeatWrapping:
  167. if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {
  168. uv.x = Math.ceil( uv.x ) - uv.x;
  169. } else {
  170. uv.x = uv.x - Math.floor( uv.x );
  171. }
  172. break;
  173. }
  174. }
  175. if ( uv.y < 0 || uv.y > 1 ) {
  176. switch ( this.wrapT ) {
  177. case RepeatWrapping:
  178. uv.y = uv.y - Math.floor( uv.y );
  179. break;
  180. case ClampToEdgeWrapping:
  181. uv.y = uv.y < 0 ? 0 : 1;
  182. break;
  183. case MirroredRepeatWrapping:
  184. if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
  185. uv.y = Math.ceil( uv.y ) - uv.y;
  186. } else {
  187. uv.y = uv.y - Math.floor( uv.y );
  188. }
  189. break;
  190. }
  191. }
  192. if ( this.flipY ) {
  193. uv.y = 1 - uv.y;
  194. }
  195. return uv;
  196. }
  197. } );
  198. Object.defineProperty( Texture.prototype, "needsUpdate", {
  199. set: function ( value ) {
  200. if ( value === true ) this.version ++;
  201. }
  202. } );
  203. export { Texture };