Texture.js 5.7 KB

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