Texture.js 5.9 KB

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