Texture.js 6.4 KB

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