UI.three.js 3.3 KB

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