2
0

Code.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. var Code = function () {
  2. var _domElement = document.createElement( 'div' );
  3. _domElement.style.position = 'absolute';
  4. _domElement.style.backgroundColor = '#f0f0f0';
  5. _domElement.style.overflow = 'auto';
  6. //
  7. var _html = false;
  8. var _checkbox = document.createElement( 'input' );
  9. _checkbox.type = 'checkbox';
  10. _checkbox.style.margin = '20px 6px 0px 20px';
  11. _checkbox.addEventListener( 'click', function () { _html = !_html; _update(); }, false );
  12. _domElement.appendChild( _checkbox );
  13. var _preview = document.createElement( 'a' );
  14. _preview.href = '#';
  15. _preview.innerHTML = 'preview';
  16. _preview.style.margin = '20px 6px 0px 20px';
  17. _preview.addEventListener( 'click', function () {
  18. // Get unescaped code gen
  19. var temp=document.createElement("pre");
  20. temp.innerHTML = _codegen( true );
  21. temp = temp.firstChild.nodeValue;
  22. temp = temp.replace("js/Three.js", "../build/Three.js");
  23. var opener = window.open('','myconsole',
  24. 'width=800,height=400'
  25. +',menubar=1'
  26. +',toolbar=0'
  27. +',status=1'
  28. +',scrollbars=1'
  29. +',resizable=1');
  30. opener.document.writeln( temp );
  31. opener.document.close();
  32. }, false
  33. );
  34. _domElement.appendChild( _preview );
  35. /*
  36. var _checkboxText = document.createElement( 'span' );
  37. _checkboxText.style.fontFamily = 'Monospace';
  38. _checkboxText.innerText = 'HTML';
  39. _domElement.appendChild( _checkboxText );
  40. */
  41. //
  42. var _code = document.createElement( 'pre' );
  43. _code.style.color = '#404040';
  44. _code.style.margin = '20px';
  45. _code.style.fontSize = '13px';
  46. _code.style.whiteSpace = 'pre'; // 'pre-wrap'
  47. _domElement.appendChild( _code );
  48. //
  49. var _list = [];
  50. var _codegen = function (html) {
  51. var string = '';
  52. console.log(_list);
  53. string += [
  54. 'var camera, scene, renderer;',
  55. '',
  56. 'init();',
  57. 'animate();',
  58. '',
  59. 'function init() {',
  60. '',
  61. '\tcamera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );',
  62. '\tcamera.position.z = 500;',
  63. '',
  64. '\tscene = new THREE.Scene();',
  65. ''
  66. ].join( '\n' );
  67. for ( var i = 0, l = _list.length; i < l; i ++ ) {
  68. string += _list[ i ] + '\n';
  69. }
  70. string += [
  71. '',
  72. '\trenderer = new THREE.WebGLRenderer()',
  73. '\trenderer.setSize( window.innerWidth, window.innerHeight );',
  74. '\tdocument.body.appendChild( renderer.domElement );',
  75. '',
  76. '}',
  77. '',
  78. 'function animate() {',
  79. '',
  80. '\trequestAnimationFrame( animate );',
  81. '\trender();',
  82. '',
  83. '}',
  84. '',
  85. 'function render() {',
  86. '',
  87. '\trenderer.render( scene, camera );',
  88. '',
  89. '}'
  90. ].join( '\n' );
  91. if ( html ) {
  92. string = '&lt;!doctype html&gt;\n&lt;html&gt;\n\t&lt;body&gt;\
  93. \n\t\t&lt;style&gt; body {background-color: #f0f0f0;} &lt;/style&gt;\
  94. \n\t\t&lt;script src=\"js/Three.js\"&gt;&lt;/script&gt;\
  95. \n\t\t&lt;script&gt;\n'
  96. + ( '\n' + string ).replace( /\n/gi, '\n\t\t\t' ) +
  97. '\n\n\t\t&lt;/script&gt;\n\t&lt;/body&gt;\n&lt;/html&gt;';
  98. }
  99. return string;
  100. }
  101. var _update = function () {
  102. _code.innerHTML = _codegen( _html );
  103. }
  104. var _strfor = function(str) {
  105. for (var i=1; i<arguments.length; i++) {
  106. if (arguments[i].toFixed) {
  107. arguments[i] = arguments[i].toFixed(2);
  108. }
  109. str = str.replace('{'+(i-1)+'}', arguments[i]);
  110. }
  111. return str;
  112. }
  113. // signals
  114. signals.updated.add( function ( scene ) {
  115. _list.length = 0;
  116. for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
  117. var object = scene.children[ i ];
  118. if ( object.geometry == undefined || object.geometry.gui == undefined ) {
  119. if (object instanceof THREE.Camera) {
  120. var string = '';
  121. string += _strfor( '\n\tcamera.position.set({0},{1},{2});', object.position.x, object.position.y, object.position.z);
  122. string += _strfor( '\n\tcamera.rotation.set({0},{1},{2});', object.rotation.x, object.rotation.y, object.rotation.z);
  123. _list.push( string );
  124. } else {
  125. _list.push( 'TODO' );
  126. }
  127. continue;
  128. }
  129. if ( object instanceof THREE.Mesh ) {
  130. var string = '';
  131. string += '\n\tvar geometry = ' + object.geometry.gui.getCode() + ';';
  132. string += '\n\tvar material = ' + object.material.gui.getCode() + ';';
  133. string += '\n\tvar mesh = new THREE.Mesh( geometry, material );';
  134. if ( object.position.x != 0 ) string += '\n\tmesh.position.x = ' + object.position.x + ';';
  135. if ( object.position.y != 0 ) string += '\n\tmesh.position.y = ' + object.position.y + ';';
  136. if ( object.position.z != 0 ) string += '\n\tmesh.position.z = ' + object.position.z + ';';
  137. if ( object.rotation.x != 0 ) string += '\n\tmesh.rotation.x = ' + object.rotation.x + ';';
  138. if ( object.rotation.y != 0 ) string += '\n\tmesh.rotation.y = ' + object.rotation.y + ';';
  139. if ( object.rotation.z != 0 ) string += '\n\tmesh.rotation.z = ' + object.rotation.z + ';';
  140. if ( object.scale.x != 1 ) string += '\n\tmesh.scale.x = ' + object.scale.x + ';';
  141. if ( object.scale.y != 1 ) string += '\n\tmesh.scale.y = ' + object.scale.y + ';';
  142. if ( object.scale.z != 1 ) string += '\n\tmesh.scale.z = ' + object.scale.z + ';';
  143. string += '\n\tscene.add( mesh );';
  144. _list.push( string );
  145. }
  146. }
  147. _update();
  148. } );
  149. //
  150. this.getDOMElement = function () {
  151. return _domElement;
  152. }
  153. this.setPosition = function ( x, y ) {
  154. _domElement.style.left = x + 'px';
  155. _domElement.style.top = y + 'px';
  156. }
  157. this.setSize = function ( width, height ) {
  158. _domElement.style.width = width + 'px';
  159. _domElement.style.height = height + 'px';
  160. }
  161. }