Code.js 4.8 KB

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