Texture.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.version ++;
  36. },
  37. clone: function ( texture ) {
  38. if ( texture === undefined ) texture = new THREE.Texture();
  39. texture.image = this.image;
  40. texture.mipmaps = this.mipmaps.slice( 0 );
  41. texture.mapping = this.mapping;
  42. texture.wrapS = this.wrapS;
  43. texture.wrapT = this.wrapT;
  44. texture.magFilter = this.magFilter;
  45. texture.minFilter = this.minFilter;
  46. texture.anisotropy = this.anisotropy;
  47. texture.format = this.format;
  48. texture.type = this.type;
  49. texture.offset.copy( this.offset );
  50. texture.repeat.copy( this.repeat );
  51. texture.generateMipmaps = this.generateMipmaps;
  52. texture.premultiplyAlpha = this.premultiplyAlpha;
  53. texture.flipY = this.flipY;
  54. texture.unpackAlignment = this.unpackAlignment;
  55. return texture;
  56. },
  57. toJSON: function ( meta ) {
  58. if ( meta.textures[ this.uuid ] !== undefined ) {
  59. return meta.textures[ this.uuid ];
  60. }
  61. function getDataURL( image ) {
  62. var canvas;
  63. if ( image.toDataURL !== undefined ) {
  64. canvas = image;
  65. } else {
  66. canvas = document.createElement( 'canvas' );
  67. canvas.width = image.width;
  68. canvas.height = image.height;
  69. canvas.getContext( '2d' ).drawImage( image, 0, 0, image.width, image.height );
  70. }
  71. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  72. return canvas.toDataURL( 'image/jpeg', 0.6 );
  73. } else {
  74. return canvas.toDataURL( 'image/png' );
  75. }
  76. }
  77. var output = {
  78. metadata: {
  79. version: 4.4,
  80. type: 'Texture',
  81. generator: 'Texture.toJSON'
  82. },
  83. uuid: this.uuid,
  84. name: this.name,
  85. mapping: this.mapping,
  86. repeat: [ this.repeat.x, this.repeat.y ],
  87. offset: [ this.offset.x, this.offset.y ],
  88. wrap: [ this.wrapS, this.wrapT ],
  89. minFilter: this.minFilter,
  90. magFilter: this.magFilter,
  91. anisotropy: this.anisotropy
  92. };
  93. if ( this.image !== undefined ) {
  94. // TODO: Move to THREE.Image
  95. var image = this.image;
  96. if ( image.uuid === undefined ) {
  97. image.uuid = THREE.Math.generateUUID(); // UGH
  98. }
  99. if ( meta.images[ image.uuid ] === undefined ) {
  100. meta.images[ image.uuid ] = {
  101. uuid: image.uuid,
  102. url: getDataURL( image )
  103. };
  104. }
  105. output.image = image.uuid;
  106. }
  107. meta.textures[ this.uuid ] = output;
  108. return output;
  109. },
  110. dispose: function () {
  111. this.dispatchEvent( { type: 'dispose' } );
  112. }
  113. };
  114. THREE.EventDispatcher.prototype.apply( THREE.Texture.prototype );
  115. THREE.TextureIdCount = 0;