ui.three.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Texture
  2. UI.Texture = function () {
  3. UI.Element.call( this );
  4. var scope = this;
  5. var canvas = document.createElement( 'canvas' );
  6. canvas.width = 64;
  7. canvas.height = 16;
  8. canvas.style.cursor = 'pointer';
  9. canvas.addEventListener( 'click', function ( event ) {
  10. input.click();
  11. }, false );
  12. var context = canvas.getContext( '2d' );
  13. context.fillStyle = 'black';
  14. context.fillRect( 0, 0, canvas.width, canvas.height );
  15. var input = document.createElement( 'input' );
  16. input.type = 'file';
  17. input.addEventListener( 'change', function ( event ) {
  18. var file = event.target.files[ 0 ];
  19. if ( file.type.match( 'image.*' ) ) {
  20. var reader = new FileReader();
  21. reader.addEventListener( 'load', function ( event ) {
  22. var image = document.createElement( 'img' );
  23. image.addEventListener( 'load', function( event ) {
  24. var scale = 64 / this.width;
  25. context.drawImage( this, 0, 0, this.width * scale, this.height * scale );
  26. scope.texture = new THREE.Texture( this );
  27. scope.texture.needsUpdate = true;
  28. if ( scope.onChangeCallback ) scope.onChangeCallback();
  29. }, false );
  30. image.src = event.target.result;
  31. }, false );
  32. reader.readAsDataURL( file );
  33. }
  34. } );
  35. this.dom = canvas;
  36. this.texture = null;
  37. this.onChangeCallback = null;
  38. return this;
  39. };
  40. UI.Texture.prototype = Object.create( UI.Element.prototype );
  41. UI.Texture.prototype.textureNameMap = {};
  42. UI.Texture.prototype.getValue = function () {
  43. return this.texture;
  44. };
  45. UI.Texture.prototype.setValue = function ( value ) {
  46. this.texture = value;
  47. };
  48. UI.Texture.prototype.onChange = function ( callback ) {
  49. this.onChangeCallback = callback;
  50. return this;
  51. };
  52. // CubeTexture
  53. UI.CubeTexture = function () {
  54. UI.Element.call( this );
  55. var scope = this;
  56. var canvas = document.createElement( 'canvas' );
  57. canvas.width = 64;
  58. canvas.height = 16;
  59. canvas.style.cursor = 'pointer';
  60. canvas.addEventListener( 'click', function ( event ) {
  61. input.click();
  62. }, false );
  63. var context = canvas.getContext( '2d' );
  64. context.fillStyle = 'black';
  65. context.fillRect( 0, 0, canvas.width, canvas.height );
  66. var input = document.createElement( 'input' );
  67. input.type = 'file';
  68. input.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. var scale = 64 / this.width;
  76. context.drawImage( this, 0, 0, this.width * scale, this.height * scale );
  77. scope.texture = new THREE.Texture( [ this, this, this, this, this, this ], new THREE.CubeReflectionMapping() )
  78. scope.texture.needsUpdate = true;
  79. if ( scope.onChangeCallback ) scope.onChangeCallback();
  80. }, false );
  81. image.src = event.target.result;
  82. }, false );
  83. reader.readAsDataURL( file );
  84. }
  85. }, false );
  86. this.dom = canvas;
  87. this.texture = null;
  88. this.onChangeCallback = null;
  89. return this;
  90. };
  91. UI.CubeTexture.prototype = Object.create( UI.Element.prototype );
  92. UI.CubeTexture.prototype.getValue = function () {
  93. return this.texture;
  94. };
  95. UI.CubeTexture.prototype.setValue = function ( value ) {
  96. this.texture = value;
  97. };
  98. UI.CubeTexture.prototype.onChange = function ( callback ) {
  99. this.onChangeCallback = callback;
  100. return this;
  101. };
粤ICP备19079148号