UI.three.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Texture
  2. UI.Texture = function ( position ) {
  3. UI.Element.call( this );
  4. var scope = this;
  5. var image = new Image();
  6. this.texture = new THREE.Texture( image );
  7. this.dom = document.createElement( 'input' );
  8. this.dom.type = 'file';
  9. this.dom.style.position = position || 'relative';
  10. this.onChangeCallback = null;
  11. this.dom.addEventListener( 'change', function ( event ) {
  12. var file = event.target.files[ 0 ];
  13. if ( file.type.match( 'image.*' ) ) {
  14. var reader = new FileReader();
  15. reader.addEventListener( 'load', function ( event ) {
  16. var image = document.createElement( 'img' );
  17. image.addEventListener( 'load', function( event ) {
  18. scope.texture.image = this;
  19. scope.texture.needsUpdate = true;
  20. if ( scope.onChangeCallback ) scope.onChangeCallback();
  21. }, false );
  22. image.src = event.target.result;
  23. // remember the original filename (including extension)
  24. // this is used for url field in the scene export
  25. scope.texture.sourceFile = file.name;
  26. // generate unique name per texture
  27. // based on source file name
  28. var chunks = file.name.split( '.' );
  29. var extension = chunks.pop().toLowerCase();
  30. var filename = chunks.join( '.' );
  31. if ( ! ( filename in scope.textureNameMap ) ) {
  32. scope.textureNameMap[ filename ] = true;
  33. scope.texture.name = filename;
  34. } else {
  35. scope.texture.name = filename + "_" + scope.texture.id;
  36. }
  37. }, false );
  38. reader.readAsDataURL( file );
  39. }
  40. }, false );
  41. return this;
  42. };
  43. UI.Texture.prototype = Object.create( UI.Element.prototype );
  44. UI.Texture.prototype.textureNameMap = {};
  45. UI.Texture.prototype.getValue = function () {
  46. return this.texture;
  47. };
  48. UI.Texture.prototype.setValue = function ( value ) {
  49. this.texture = value;
  50. };
  51. UI.Texture.prototype.onChange = function ( callback ) {
  52. this.onChangeCallback = callback;
  53. return this;
  54. };
  55. // CubeTexture
  56. UI.CubeTexture = function ( position ) {
  57. UI.Element.call( this );
  58. var scope = this;
  59. this.texture = new THREE.Texture( [], new THREE.CubeReflectionMapping() );
  60. this.dom = document.createElement( 'input' );
  61. this.dom.type = 'file';
  62. this.dom.style.position = position || 'relative';
  63. this.onChangeCallback = null;
  64. this.dom.addEventListener( 'change', function ( event ) {
  65. var file = event.target.files[ 0 ];
  66. if ( file.type.match( 'image.*' ) ) {
  67. var reader = new FileReader();
  68. reader.addEventListener( 'load', function ( event ) {
  69. var image = document.createElement( 'img' );
  70. image.addEventListener( 'load', function( event ) {
  71. scope.texture.image = [ this, this, this, this, this, this ];
  72. scope.texture.needsUpdate = true;
  73. if ( scope.onChangeCallback ) scope.onChangeCallback();
  74. }, false );
  75. image.src = event.target.result;
  76. }, false );
  77. reader.readAsDataURL( file );
  78. }
  79. }, false );
  80. return this;
  81. };
  82. UI.CubeTexture.prototype = Object.create( UI.Element.prototype );
  83. UI.CubeTexture.prototype.getValue = function () {
  84. return this.texture;
  85. };
  86. UI.CubeTexture.prototype.setValue = function ( value ) {
  87. this.texture = value;
  88. };
  89. UI.CubeTexture.prototype.onChange = function ( callback ) {
  90. this.onChangeCallback = callback;
  91. return this;
  92. };