1
0

Source.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { ImageUtils } from '../extras/ImageUtils.js';
  2. import * as MathUtils from '../math/MathUtils.js';
  3. class Source {
  4. constructor( data = null ) {
  5. this.isSource = true;
  6. this.uuid = MathUtils.generateUUID();
  7. this.data = data;
  8. this.version = 0;
  9. }
  10. set needsUpdate( value ) {
  11. if ( value === true ) this.version ++;
  12. }
  13. toJSON( meta ) {
  14. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  15. if ( ! isRootObject && meta.images[ this.uuid ] !== undefined ) {
  16. return meta.images[ this.uuid ];
  17. }
  18. const output = {
  19. uuid: this.uuid,
  20. url: ''
  21. };
  22. const data = this.data;
  23. if ( data !== null ) {
  24. let url;
  25. if ( Array.isArray( data ) ) {
  26. // cube texture
  27. url = [];
  28. for ( let i = 0, l = data.length; i < l; i ++ ) {
  29. if ( data[ i ].isDataTexture ) {
  30. url.push( serializeImage( data[ i ].image ) );
  31. } else {
  32. url.push( serializeImage( data[ i ] ) );
  33. }
  34. }
  35. } else {
  36. // texture
  37. url = serializeImage( data );
  38. }
  39. output.url = url;
  40. }
  41. if ( ! isRootObject ) {
  42. meta.images[ this.uuid ] = output;
  43. }
  44. return output;
  45. }
  46. }
  47. function serializeImage( image ) {
  48. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  49. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
  50. ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
  51. // default images
  52. return ImageUtils.getDataURL( image );
  53. } else {
  54. if ( image.data ) {
  55. // images of DataTexture
  56. return {
  57. data: Array.prototype.slice.call( image.data ),
  58. width: image.width,
  59. height: image.height,
  60. type: image.data.constructor.name
  61. };
  62. } else {
  63. console.warn( 'THREE.Texture: Unable to serialize Texture.' );
  64. return {};
  65. }
  66. }
  67. }
  68. export { Source };
粤ICP备19079148号