ui.three.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // String
  2. UI.ParamString = function ( name ) {
  3. UI.Panel.call( this );
  4. var scope = this;
  5. var row = new UI.Panel();
  6. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  7. this.string = new UI.Input().setWidth( '150px' ).setColor( '#444' ).setFontSize( '12px' );
  8. this.string.dom.name = name;
  9. row.add( this.name, this.string );
  10. this.add( row );
  11. return this;
  12. };
  13. UI.ParamString.prototype = Object.create( UI.Panel.prototype );
  14. UI.ParamString.prototype.setValue = function ( value ) {
  15. this.string.setValue( value );
  16. };
  17. UI.ParamString.prototype.getValue = function ( value ) {
  18. return this.string.getValue( value );
  19. };
  20. // Integer
  21. UI.ParamInteger = function ( name ) {
  22. UI.Panel.call( this );
  23. var scope = this;
  24. var row = new UI.Panel();
  25. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  26. this.integer = new UI.Integer();
  27. this.integer.dom.name = name;
  28. row.add( this.name, this.integer );
  29. this.add( row );
  30. return this;
  31. };
  32. UI.ParamInteger.prototype = Object.create( UI.Panel.prototype );
  33. UI.ParamInteger.prototype.setValue = function ( value ) {
  34. this.integer.setValue( value );
  35. };
  36. UI.ParamInteger.prototype.getValue = function () {
  37. return this.integer.getValue();
  38. };
  39. // Float
  40. UI.ParamFloat = function ( name ) {
  41. UI.Panel.call( this );
  42. var scope = this;
  43. var row = new UI.Panel();
  44. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  45. this.float = new UI.Number();
  46. this.float.dom.name = name;
  47. row.add( this.name, this.float );
  48. this.add( row );
  49. return this;
  50. };
  51. UI.ParamFloat.prototype = Object.create( UI.Panel.prototype );
  52. UI.ParamFloat.prototype.setValue = function ( value ) {
  53. this.float.setValue( value );
  54. };
  55. UI.ParamFloat.prototype.getValue = function () {
  56. return this.float.getValue();
  57. };
  58. // Bool
  59. UI.ParamBool = function ( name ) {
  60. UI.Panel.call( this );
  61. var scope = this;
  62. var row = new UI.Panel();
  63. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' ).setPosition( 'relative' ).setLeft( '25px' );
  64. this.bool = new UI.Checkbox().setPosition( 'absolute' ).setLeft( '10px' );
  65. this.bool.dom.name = name;
  66. row.add( this.name, this.bool );
  67. this.add( row );
  68. return this;
  69. };
  70. UI.ParamBool.prototype = Object.create( UI.Panel.prototype );
  71. UI.ParamBool.prototype.setValue = function ( value ) {
  72. this.bool.setValue( value );
  73. };
  74. UI.ParamBool.prototype.getValue = function () {
  75. return this.bool.getValue();
  76. };
  77. // Vector3
  78. UI.ParamVector3 = function ( name, scaleLock ) {
  79. UI.Panel.call( this );
  80. scaleLock = scaleLock ? scaleLock : false;
  81. var scope = this;
  82. var row = new UI.Panel();
  83. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  84. this.x = new UI.Number().setWidth( '50px' ).onChange( setYZ );
  85. this.y = new UI.Number().setWidth( '50px' ).onChange( setXZ );
  86. this.z = new UI.Number().setWidth( '50px' ).onChange( setXY );
  87. this.x.dom.name = name;
  88. this.y.dom.name = name;
  89. this.z.dom.name = name;
  90. row.add( this.name, this.x, this.y, this.z );
  91. if ( scaleLock ) {
  92. this.scaleLock = new UI.Checkbox().setPosition( 'absolute' ).setLeft( '75px' );
  93. row.add( this.scaleLock );
  94. }
  95. this.add( row );
  96. function setYZ( event ) {
  97. if ( scope.scaleLock && scope.scaleLock.getValue() && event.srcElement.oldValue ) {
  98. var scale = event.srcElement.value / event.srcElement.oldValue;
  99. scope.y.setValue( parseFloat(scope.y.getValue()) * scale );
  100. scope.z.setValue( parseFloat(scope.z.getValue()) * scale );
  101. }
  102. }
  103. function setXZ( event ) {
  104. if ( scope.scaleLock && scope.scaleLock.getValue() && event.srcElement.oldValue ) {
  105. var scale = event.srcElement.value / event.srcElement.oldValue;
  106. scope.x.setValue( parseFloat(scope.x.getValue()) * scale );
  107. scope.z.setValue( parseFloat(scope.z.getValue()) * scale );
  108. }
  109. }
  110. function setXY( event ) {
  111. if ( scope.scaleLock && scope.scaleLock.getValue() && event.srcElement.oldValue ) {
  112. var scale = event.srcElement.value / event.srcElement.oldValue;
  113. scope.x.setValue( parseFloat(scope.x.getValue()) * scale );
  114. scope.y.setValue( parseFloat(scope.y.getValue()) * scale );
  115. }
  116. }
  117. return this;
  118. };
  119. UI.ParamVector3.prototype = Object.create( UI.Panel.prototype );
  120. UI.ParamVector3.prototype.setValue = function ( value ) {
  121. this.x.setValue( value.x );
  122. this.y.setValue( value.y );
  123. this.z.setValue( value.z );
  124. };
  125. UI.ParamVector3.prototype.getValue = function () {
  126. return new THREE.Vector3( this.x.getValue(), this.y.getValue(), this.z.getValue() );
  127. };
  128. // Color
  129. UI.ParamColor = function ( name ) {
  130. UI.Panel.call( this );
  131. var scope = this;
  132. var row = new UI.Panel();
  133. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  134. this.color = new UI.Color();
  135. this.color.dom.name = name;
  136. row.add( this.name, this.color );
  137. this.add( row );
  138. return this;
  139. };
  140. UI.ParamColor.prototype = Object.create( UI.Panel.prototype );
  141. UI.ParamColor.prototype.setValue = function ( color ) {
  142. this.color.setValue( '#' + color.getHexString() );
  143. };
  144. UI.ParamColor.prototype.getValue = function () {
  145. return this.color.getHexValue();
  146. };
  147. // Select
  148. UI.ParamSelect = function ( name ) {
  149. UI.Panel.call( this );
  150. var scope = this;
  151. this.onChangeCallback;
  152. var row = new UI.Panel();
  153. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  154. this.select = new UI.Select().setWidth( '150px' ).setColor( '#444' ).setFontSize( '12px' );
  155. this.select.dom.name = name;
  156. row.add( this.name, this.select );
  157. this.add( row );
  158. return this;
  159. };
  160. UI.ParamSelect.prototype = Object.create( UI.Panel.prototype );
  161. UI.ParamSelect.prototype.setValue = function ( value ) {
  162. this.select.setValue( value );
  163. };
  164. UI.ParamSelect.prototype.getValue = function ( value ) {
  165. return this.select.getValue( value );
  166. };
  167. UI.ParamSelect.prototype.setOptions = function ( value ) {
  168. this.select.setOptions( value );
  169. };
  170. // Texture
  171. UI.Texture = function ( position ) {
  172. UI.Element.call( this );
  173. var scope = this;
  174. var image = new Image();
  175. this.texture = null;
  176. this.dom = document.createElement( 'input' );
  177. this.dom.type = 'file';
  178. this.dom.style.position = position || 'relative';
  179. this.onChangeCallback = null;
  180. this.dom.addEventListener( 'change', function ( event ) {
  181. var file = event.target.files[ 0 ];
  182. if ( file.type.match( 'image.*' ) ) {
  183. var reader = new FileReader();
  184. reader.addEventListener( 'load', function ( event ) {
  185. var image = document.createElement( 'img' );
  186. image.addEventListener( 'load', function( event ) {
  187. scope.texture = new THREE.Texture( this );
  188. scope.texture.needsUpdate = true;
  189. // remember the original filename (including extension)
  190. // this is used for url field in the scene export
  191. scope.texture.sourceFile = file.name;
  192. // generate unique name per texture
  193. // based on source file name
  194. var chunks = file.name.split( '.' );
  195. var extension = chunks.pop().toLowerCase();
  196. var filename = chunks.join( '.' );
  197. if ( ! ( filename in scope.textureNameMap ) ) {
  198. scope.textureNameMap[ filename ] = true;
  199. scope.texture.name = filename;
  200. } else {
  201. scope.texture.name = filename + "_" + scope.texture.id;
  202. }
  203. if ( scope.onChangeCallback ) scope.onChangeCallback();
  204. }, false );
  205. image.src = event.target.result;
  206. }, false );
  207. reader.readAsDataURL( file );
  208. }
  209. }, false );
  210. return this;
  211. };
  212. UI.Texture.prototype = Object.create( UI.Element.prototype );
  213. UI.Texture.prototype.textureNameMap = {};
  214. UI.Texture.prototype.getValue = function () {
  215. return this.texture;
  216. };
  217. UI.Texture.prototype.setValue = function ( value ) {
  218. this.texture = value;
  219. };
  220. UI.Texture.prototype.onChange = function ( callback ) {
  221. this.onChangeCallback = callback;
  222. return this;
  223. };
  224. // CubeTexture
  225. UI.CubeTexture = function ( position ) {
  226. UI.Element.call( this );
  227. var scope = this;
  228. this.texture = new THREE.Texture( [], new THREE.CubeReflectionMapping() );
  229. this.dom = document.createElement( 'input' );
  230. this.dom.type = 'file';
  231. this.dom.style.position = position || 'relative';
  232. this.onChangeCallback = null;
  233. this.dom.addEventListener( 'change', function ( event ) {
  234. var file = event.target.files[ 0 ];
  235. if ( file.type.match( 'image.*' ) ) {
  236. var reader = new FileReader();
  237. reader.addEventListener( 'load', function ( event ) {
  238. var image = document.createElement( 'img' );
  239. image.addEventListener( 'load', function( event ) {
  240. scope.texture.image = [ this, this, this, this, this, this ];
  241. scope.texture.needsUpdate = true;
  242. if ( scope.onChangeCallback ) scope.onChangeCallback();
  243. }, false );
  244. image.src = event.target.result;
  245. }, false );
  246. reader.readAsDataURL( file );
  247. }
  248. }, false );
  249. return this;
  250. };
  251. UI.CubeTexture.prototype = Object.create( UI.Element.prototype );
  252. UI.CubeTexture.prototype.getValue = function () {
  253. return this.texture;
  254. };
  255. UI.CubeTexture.prototype.setValue = function ( value ) {
  256. this.texture = value;
  257. };
  258. UI.CubeTexture.prototype.onChange = function ( callback ) {
  259. this.onChangeCallback = callback;
  260. return this;
  261. };
  262. // Json
  263. UI.ParamJson = function ( name ) {
  264. UI.Panel.call( this );
  265. var scope = this;
  266. var row = new UI.Panel();
  267. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  268. this.json = new UI.TextArea().setWidth( '150px' ).setHeight( '40px' ).setColor( '#444' ).setFontSize( '12px' );
  269. this.json.dom.name = name;
  270. this.json.onKeyUp( function () {
  271. try {
  272. JSON.parse( scope.json.getValue() );
  273. scope.json.setBorderColor( '#ccc' );
  274. scope.json.setBackgroundColor( '' );
  275. } catch ( error ) {
  276. scope.json.setBorderColor( '#f00' );
  277. scope.json.setBackgroundColor( 'rgba(255,0,0,0.25)' );
  278. }
  279. } );
  280. row.add( this.name, this.json );
  281. this.add( row );
  282. return this;
  283. };
  284. UI.ParamJson.prototype = Object.create( UI.Panel.prototype );
  285. UI.ParamJson.prototype.setValue = function ( value ) {
  286. this.json.setValue( value );
  287. };
  288. UI.ParamJson.prototype.getValue = function () {
  289. return this.json.getValue();
  290. };