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