Texture.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author szimek / https://github.com/szimek/
  5. */
  6. THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  7. Object.defineProperty( this, 'id', { value: THREE.TextureIdCount ++ } );
  8. this.uuid = THREE.Math.generateUUID();
  9. this.name = '';
  10. this.sourceFile = '';
  11. this.image = image !== undefined ? image : THREE.Texture.DEFAULT_IMAGE;
  12. this.mipmaps = [];
  13. this.mapping = mapping !== undefined ? mapping : THREE.Texture.DEFAULT_MAPPING;
  14. this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
  15. this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
  16. this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
  17. this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
  18. this.anisotropy = anisotropy !== undefined ? anisotropy : 1;
  19. this.format = format !== undefined ? format : THREE.RGBAFormat;
  20. this.type = type !== undefined ? type : THREE.UnsignedByteType;
  21. this.offset = new THREE.Vector2( 0, 0 );
  22. this.repeat = new THREE.Vector2( 1, 1 );
  23. this.generateMipmaps = true;
  24. this.premultiplyAlpha = false;
  25. this.flipY = true;
  26. this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
  27. this.version = 0;
  28. this.onUpdate = null;
  29. };
  30. THREE.Texture.DEFAULT_IMAGE = undefined;
  31. THREE.Texture.DEFAULT_MAPPING = THREE.UVMapping;
  32. THREE.Texture.prototype = {
  33. constructor: THREE.Texture,
  34. set needsUpdate ( value ) {
  35. if ( value === true ) this.update();
  36. this.version ++;
  37. },
  38. clone: function ( texture ) {
  39. if ( texture === undefined ) texture = new THREE.Texture();
  40. texture.image = this.image;
  41. texture.mipmaps = this.mipmaps.slice( 0 );
  42. texture.mapping = this.mapping;
  43. texture.wrapS = this.wrapS;
  44. texture.wrapT = this.wrapT;
  45. texture.magFilter = this.magFilter;
  46. texture.minFilter = this.minFilter;
  47. texture.anisotropy = this.anisotropy;
  48. texture.format = this.format;
  49. texture.type = this.type;
  50. texture.offset.copy( this.offset );
  51. texture.repeat.copy( this.repeat );
  52. texture.generateMipmaps = this.generateMipmaps;
  53. texture.premultiplyAlpha = this.premultiplyAlpha;
  54. texture.flipY = this.flipY;
  55. texture.unpackAlignment = this.unpackAlignment;
  56. return texture;
  57. },
  58. toJSON: function ( meta ) {
  59. if ( meta.textures[ this.uuid ] !== undefined ) {
  60. return meta.textures[ this.uuid ];
  61. }
  62. function getDataURL( image ) {
  63. var canvas;
  64. if ( image.toDataURL !== undefined ) {
  65. canvas = image;
  66. } else {
  67. canvas = document.createElement( 'canvas' );
  68. canvas.width = image.width;
  69. canvas.height = image.height;
  70. canvas.getContext( '2d' ).drawImage( image, 0, 0, image.width, image.height );
  71. }
  72. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  73. return canvas.toDataURL( 'image/jpeg', 0.6 );
  74. } else {
  75. return canvas.toDataURL( 'image/png' );
  76. }
  77. }
  78. var output = {
  79. metadata: {
  80. version: 4.4,
  81. type: 'Texture',
  82. generator: 'Texture.toJSON'
  83. },
  84. uuid: this.uuid,
  85. name: this.name,
  86. mapping: this.mapping,
  87. repeat: [ this.repeat.x, this.repeat.y ],
  88. offset: [ this.offset.x, this.offset.y ],
  89. wrap: [ this.wrapS, this.wrapT ],
  90. minFilter: this.minFilter,
  91. magFilter: this.magFilter,
  92. anisotropy: this.anisotropy
  93. };
  94. if ( this.image !== undefined ) {
  95. // TODO: Move to THREE.Image
  96. var image = this.image;
  97. if ( image.uuid === undefined ) {
  98. image.uuid = THREE.Math.generateUUID(); // UGH
  99. }
  100. if ( meta.images[ image.uuid ] === undefined ) {
  101. meta.images[ image.uuid ] = {
  102. uuid: image.uuid,
  103. url: getDataURL( image )
  104. };
  105. }
  106. output.image = image.uuid;
  107. }
  108. meta.textures[ this.uuid ] = output;
  109. return output;
  110. },
  111. update: function () {
  112. this.dispatchEvent( { type: 'update' } );
  113. },
  114. dispose: function () {
  115. this.dispatchEvent( { type: 'dispose' } );
  116. }
  117. };
  118. THREE.EventDispatcher.prototype.apply( THREE.Texture.prototype );
  119. THREE.TextureIdCount = 0;
粤ICP备19079148号