Code.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. temp = temp.replace("js/RequestAnimationFrame.js", "../examples/js/RequestAnimationFrame.js");
  24. console.log('test', temp);
  25. var opener = window.open('','myconsole',
  26. 'width=800,height=400'
  27. +',menubar=1'
  28. +',toolbar=0'
  29. +',status=1'
  30. +',scrollbars=1'
  31. +',resizable=1');
  32. opener.document.writeln( temp );
  33. opener.document.close();
  34. }, false
  35. );
  36. _domElement.appendChild( _preview );
  37. /*
  38. var _checkboxText = document.createElement( 'span' );
  39. _checkboxText.style.fontFamily = 'Monospace';
  40. _checkboxText.innerText = 'HTML';
  41. _domElement.appendChild( _checkboxText );
  42. */
  43. //
  44. var _code = document.createElement( 'pre' );
  45. _code.style.color = '#404040';
  46. _code.style.margin = '20px';
  47. _code.style.fontSize = '13px';
  48. _code.style.whiteSpace = 'pre'; // 'pre-wrap'
  49. _domElement.appendChild( _code );
  50. //
  51. var _list = [];
  52. var _codegen = function (html) {
  53. var string = '';
  54. string += [
  55. 'var camera, scene, renderer;',
  56. '',
  57. 'init();',
  58. 'animate();',
  59. '',
  60. 'function init() {',
  61. '',
  62. '\tcamera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );',
  63. '\tcamera.position.z = 500;',
  64. '',
  65. '\tscene = new THREE.Scene();',
  66. ''
  67. ].join( '\n' );
  68. for ( var i = 0, l = _list.length; i < l; i ++ ) {
  69. string += _list[ i ] + '\n';
  70. }
  71. string += [
  72. '',
  73. '\trenderer = new THREE.WebGLRenderer()',
  74. '\trenderer.setSize( window.innerWidth, window.innerHeight );',
  75. '\tdocument.body.appendChild( renderer.domElement );',
  76. '',
  77. '}',
  78. '',
  79. 'function animate() {',
  80. '',
  81. '\trequestAnimationFrame( animate );',
  82. '\trender();',
  83. '',
  84. '}',
  85. '',
  86. 'function render() {',
  87. '',
  88. '\trenderer.render( scene, camera );',
  89. '',
  90. '}'
  91. ].join( '\n' );
  92. if ( html ) {
  93. string = '&lt;!doctype html&gt;\n&lt;html&gt;\n\t&lt;body&gt;\n\t\t&lt;script src=\"js/Three.js\"&gt;&lt;/script&gt;\n\t\t&lt;script src=\"js/RequestAnimationFrame.js\"&gt;&lt;/script&gt;\n\t\t&lt;script&gt;\n' + ( '\n' + string ).replace( /\n/gi, '\n\t\t\t' ) + '\n\n\t\t&lt;/script&gt;\n\t&lt;/body&gt;\n&lt;/html&gt;';
  94. }
  95. return string;
  96. }
  97. var _update = function () {
  98. _code.innerHTML = _codegen( _html );
  99. }
  100. // signals
  101. signals.updated.add( function ( scene ) {
  102. _list.length = 0;
  103. for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
  104. var object = scene.children[ i ];
  105. if ( object.geometry == undefined || object.geometry.gui == undefined ) {
  106. _list.push( 'TODO' );
  107. continue;
  108. }
  109. if ( object instanceof THREE.Mesh ) {
  110. var string = '';
  111. string += '\n\tvar geometry = ' + object.geometry.gui.getCode() + ';';
  112. string += '\n\tvar material = ' + object.material.gui.getCode() + ';';
  113. string += '\n\tvar mesh = new THREE.Mesh( geometry, material );';
  114. if ( object.position.x != 0 ) string += '\n\tmesh.position.x = ' + object.position.x + ';';
  115. if ( object.position.y != 0 ) string += '\n\tmesh.position.y = ' + object.position.y + ';';
  116. if ( object.position.z != 0 ) string += '\n\tmesh.position.z = ' + object.position.z + ';';
  117. if ( object.rotation.x != 0 ) string += '\n\tmesh.rotation.x = ' + object.rotation.x + ';';
  118. if ( object.rotation.y != 0 ) string += '\n\tmesh.rotation.y = ' + object.rotation.y + ';';
  119. if ( object.rotation.z != 0 ) string += '\n\tmesh.rotation.z = ' + object.rotation.z + ';';
  120. if ( object.scale.x != 1 ) string += '\n\tmesh.scale.x = ' + object.scale.x + ';';
  121. if ( object.scale.y != 1 ) string += '\n\tmesh.scale.y = ' + object.scale.y + ';';
  122. if ( object.scale.z != 1 ) string += '\n\tmesh.scale.z = ' + object.scale.z + ';';
  123. string += '\n\tscene.add( mesh );';
  124. _list.push( string );
  125. }
  126. }
  127. _update();
  128. } );
  129. //
  130. this.getDOMElement = function () {
  131. return _domElement;
  132. }
  133. this.setPosition = function ( x, y ) {
  134. _domElement.style.left = x + 'px';
  135. _domElement.style.top = y + 'px';
  136. }
  137. this.setSize = function ( width, height ) {
  138. _domElement.style.width = width + 'px';
  139. _domElement.style.height = height + 'px';
  140. }
  141. }