ui.three.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 ( model ) {
  15. this.string.setValue( model );
  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 ( model ) {
  34. this.integer.setValue( model );
  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 ( model ) {
  53. this.float.setValue( model );
  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( 'auto' ).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 ( model ) {
  72. this.bool.setValue( model );
  73. };
  74. UI.ParamBool.prototype.getValue = function () {
  75. return this.bool.getValue();
  76. };
  77. // Vector2
  78. UI.ParamVector2 = function ( name ) {
  79. UI.Panel.call( this );
  80. var scope = this;
  81. var row = new UI.Panel();
  82. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  83. this.x = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.y ] ) } );
  84. this.y = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.x ] ) } );
  85. this.x.dom.name = name;
  86. this.y.dom.name = name;
  87. row.add( this.name, this.x, this.y );
  88. this.scaleLock = new UI.Checkbox().setPosition( 'absolute' ).setRight( '10px' );
  89. row.add( this.scaleLock );
  90. this.add( row );
  91. function scaleProportionately( event, targets ) {
  92. if ( scope.scaleLock && scope.scaleLock.getValue() && event.srcElement.oldValue ) {
  93. var scale = event.srcElement.value / event.srcElement.oldValue;
  94. for ( var i in targets ) {
  95. targets[ i ].setValue( parseFloat(targets[ i ].getValue()) * scale );
  96. }
  97. }
  98. }
  99. return this;
  100. };
  101. UI.ParamVector2.prototype = Object.create( UI.Panel.prototype );
  102. UI.ParamVector2.prototype.setValue = function ( model ) {
  103. this.x.setValue( model.x );
  104. this.y.setValue( model.y );
  105. };
  106. UI.ParamVector2.prototype.getValue = function () {
  107. return new THREE.Vector2( this.x.getValue(), this.y.getValue() );
  108. };
  109. // Vector3
  110. UI.ParamVector3 = function ( name ) {
  111. UI.Panel.call( this );
  112. var scope = this;
  113. var row = new UI.Panel();
  114. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  115. this.x = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.y, scope.z ] ) } );
  116. this.y = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.x, scope.z ] ) } );
  117. this.z = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.x, scope.y ] ) } );
  118. this.x.dom.name = name;
  119. this.y.dom.name = name;
  120. this.z.dom.name = name;
  121. row.add( this.name, this.x, this.y, this.z );
  122. this.scaleLock = new UI.Checkbox().setPosition( 'absolute' ).setRight( '10px' );
  123. row.add( this.scaleLock );
  124. this.add( row );
  125. function scaleProportionately( event, targets ) {
  126. if ( scope.scaleLock && scope.scaleLock.getValue() && event.srcElement.oldValue ) {
  127. var scale = event.srcElement.value / event.srcElement.oldValue;
  128. for ( var i in targets ) {
  129. targets[ i ].setValue( parseFloat(targets[ i ].getValue()) * scale );
  130. }
  131. }
  132. }
  133. return this;
  134. };
  135. UI.ParamVector3.prototype = Object.create( UI.Panel.prototype );
  136. UI.ParamVector3.prototype.setValue = function ( model ) {
  137. this.x.setValue( model.x );
  138. this.y.setValue( model.y );
  139. this.z.setValue( model.z );
  140. };
  141. UI.ParamVector3.prototype.getValue = function () {
  142. return new THREE.Vector3( this.x.getValue(), this.y.getValue(), this.z.getValue() );
  143. };
  144. // Vector4
  145. UI.ParamVector4 = function ( name ) {
  146. UI.Panel.call( this );
  147. var scope = this;
  148. var row = new UI.Panel();
  149. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  150. this.x = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.y, scope.z, scope.w ] ) } );
  151. this.y = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.x, scope.z, scope.w ] ) } );
  152. this.z = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.x, scope.y, scope.w ] ) } );
  153. this.w = new UI.Number().setWidth( '35px' ).onChange( function( event ) { scaleProportionately( event, [ scope.x, scope.y, scope.z ] ) } );
  154. this.x.dom.name = name;
  155. this.y.dom.name = name;
  156. this.z.dom.name = name;
  157. this.w.dom.name = name;
  158. row.add( this.name, this.x, this.y, this.z, this.w );
  159. this.scaleLock = new UI.Checkbox().setPosition( 'absolute' ).setRight( '10px' );
  160. row.add( this.scaleLock );
  161. this.add( row );
  162. function scaleProportionately( event, targets ) {
  163. if ( scope.scaleLock && scope.scaleLock.getValue() && event.srcElement.oldValue ) {
  164. var scale = event.srcElement.value / event.srcElement.oldValue;
  165. for ( var i in targets ) {
  166. targets[ i ].setValue( parseFloat(targets[ i ].getValue()) * scale );
  167. }
  168. }
  169. }
  170. return this;
  171. };
  172. UI.ParamVector4.prototype = Object.create( UI.Panel.prototype );
  173. UI.ParamVector4.prototype.setValue = function ( model ) {
  174. this.x.setValue( model.x );
  175. this.y.setValue( model.y );
  176. this.z.setValue( model.z );
  177. this.w.setValue( model.w );
  178. };
  179. UI.ParamVector4.prototype.getValue = function () {
  180. return new THREE.Vector4( this.x.getValue(), this.y.getValue(), this.z.getValue(), this.w.getValue() );
  181. };
  182. // Color
  183. UI.ParamColor = function ( name ) {
  184. UI.Panel.call( this );
  185. var scope = this;
  186. var row = new UI.Panel();
  187. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  188. this.color = new UI.Color();
  189. this.color.dom.name = name;
  190. row.add( this.name, this.color );
  191. this.add( row );
  192. return this;
  193. };
  194. UI.ParamColor.prototype = Object.create( UI.Panel.prototype );
  195. UI.ParamColor.prototype.setValue = function ( color ) {
  196. this.color.setValue( '#' + color.getHexString() );
  197. };
  198. UI.ParamColor.prototype.getValue = function () {
  199. return this.color.getHexValue();
  200. };
  201. // Select
  202. UI.ParamSelect = function ( name ) {
  203. UI.Panel.call( this );
  204. var scope = this;
  205. this.onChangeCallback;
  206. var row = new UI.Panel();
  207. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  208. this.select = new UI.Select().setWidth( '150px' ).setColor( '#444' ).setFontSize( '12px' );
  209. this.select.dom.name = name;
  210. row.add( this.name, this.select );
  211. this.add( row );
  212. return this;
  213. };
  214. UI.ParamSelect.prototype = Object.create( UI.Panel.prototype );
  215. UI.ParamSelect.prototype.setValue = function ( model ) {
  216. this.select.setValue( model );
  217. };
  218. UI.ParamSelect.prototype.getValue = function ( value ) {
  219. return this.select.getValue( value );
  220. };
  221. UI.ParamSelect.prototype.setOptions = function ( value ) {
  222. this.select.setOptions( value );
  223. };
  224. // Texture
  225. UI.Texture = function ( position ) {
  226. UI.Element.call( this );
  227. var scope = this;
  228. var image = new Image();
  229. this.texture = null;
  230. this.dom = document.createElement( 'input' );
  231. this.dom.type = 'file';
  232. this.dom.style.position = position || 'relative';
  233. this.onChangeCallback = null;
  234. this.dom.addEventListener( 'change', function ( event ) {
  235. var file = event.target.files[ 0 ];
  236. if ( file.type.match( 'image.*' ) ) {
  237. var reader = new FileReader();
  238. reader.addEventListener( 'load', function ( event ) {
  239. var image = document.createElement( 'img' );
  240. image.addEventListener( 'load', function( event ) {
  241. scope.texture = new THREE.Texture( this );
  242. scope.texture.needsUpdate = true;
  243. // remember the original filename (including extension)
  244. // this is used for url field in the scene export
  245. scope.texture.sourceFile = file.name;
  246. // generate unique name per texture
  247. // based on source file name
  248. var chunks = file.name.split( '.' );
  249. var extension = chunks.pop().toLowerCase();
  250. var filename = chunks.join( '.' );
  251. if ( ! ( filename in scope.textureNameMap ) ) {
  252. scope.textureNameMap[ filename ] = true;
  253. scope.texture.name = filename;
  254. } else {
  255. scope.texture.name = filename + "_" + scope.texture.id;
  256. }
  257. if ( scope.onChangeCallback ) scope.onChangeCallback();
  258. }, false );
  259. image.src = event.target.result;
  260. }, false );
  261. reader.readAsDataURL( file );
  262. }
  263. }, false );
  264. return this;
  265. };
  266. UI.Texture.prototype = Object.create( UI.Element.prototype );
  267. UI.Texture.prototype.textureNameMap = {};
  268. UI.Texture.prototype.getValue = function () {
  269. return this.texture;
  270. };
  271. UI.Texture.prototype.setValue = function ( model ) {
  272. this.texture = value;
  273. };
  274. UI.Texture.prototype.onChange = function ( callback ) {
  275. this.onChangeCallback = callback;
  276. return this;
  277. };
  278. // CubeTexture
  279. UI.CubeTexture = function ( position ) {
  280. UI.Element.call( this );
  281. var scope = this;
  282. this.texture = new THREE.Texture( [], new THREE.CubeReflectionMapping() );
  283. this.dom = document.createElement( 'input' );
  284. this.dom.type = 'file';
  285. this.dom.style.position = position || 'relative';
  286. this.onChangeCallback = null;
  287. this.dom.addEventListener( 'change', function ( event ) {
  288. var file = event.target.files[ 0 ];
  289. if ( file.type.match( 'image.*' ) ) {
  290. var reader = new FileReader();
  291. reader.addEventListener( 'load', function ( event ) {
  292. var image = document.createElement( 'img' );
  293. image.addEventListener( 'load', function( event ) {
  294. scope.texture.image = [ this, this, this, this, this, this ];
  295. scope.texture.needsUpdate = true;
  296. if ( scope.onChangeCallback ) scope.onChangeCallback();
  297. }, false );
  298. image.src = event.target.result;
  299. }, false );
  300. reader.readAsDataURL( file );
  301. }
  302. }, false );
  303. return this;
  304. };
  305. UI.CubeTexture.prototype = Object.create( UI.Element.prototype );
  306. UI.CubeTexture.prototype.getValue = function () {
  307. return this.texture;
  308. };
  309. UI.CubeTexture.prototype.setValue = function ( model ) {
  310. this.texture = value;
  311. };
  312. UI.CubeTexture.prototype.onChange = function ( callback ) {
  313. this.onChangeCallback = callback;
  314. return this;
  315. };
  316. // Json
  317. UI.ParamJson = function ( name ) {
  318. UI.Panel.call( this );
  319. var scope = this;
  320. var row = new UI.Panel();
  321. this.name = new UI.Text( name ).setWidth( '90px' ).setColor( '#666' );
  322. this.json = new UI.TextArea().setWidth( '150px' ).setHeight( '40px' ).setColor( '#444' ).setFontSize( '12px' );
  323. this.json.dom.name = name;
  324. this.json.onKeyUp( function () {
  325. try {
  326. JSON.parse( scope.json.getValue() );
  327. scope.json.setBorderColor( '#ccc' );
  328. scope.json.setBackgroundColor( '' );
  329. } catch ( error ) {
  330. scope.json.setBorderColor( '#f00' );
  331. scope.json.setBackgroundColor( 'rgba(255,0,0,0.25)' );
  332. }
  333. } );
  334. row.add( this.name, this.json );
  335. this.add( row );
  336. return this;
  337. };
  338. UI.ParamJson.prototype = Object.create( UI.Panel.prototype );
  339. UI.ParamJson.prototype.setValue = function ( model ) {
  340. this.json.setValue( model );
  341. };
  342. UI.ParamJson.prototype.getValue = function () {
  343. return this.json.getValue();
  344. };