forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.js
226 lines (161 loc) · 5.38 KB
/
Code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var Code = function () {
var _domElement = document.createElement( 'div' );
_domElement.style.position = 'absolute';
_domElement.style.backgroundColor = '#f0f0f0';
_domElement.style.overflow = 'auto';
//
var _html = false;
var _checkbox = document.createElement( 'input' );
_checkbox.type = 'checkbox';
_checkbox.style.margin = '20px 6px 0px 20px';
_checkbox.addEventListener( 'click', function () { _html = !_html; _update(); }, false );
_domElement.appendChild( _checkbox );
var _preview = document.createElement( 'a' );
_preview.href = '#';
_preview.innerHTML = 'preview';
_preview.style.margin = '20px 6px 0px 20px';
_preview.addEventListener( 'click', function () {
// Get unescaped code gen
var temp=document.createElement("pre");
temp.innerHTML = _codegen( true );
temp = temp.firstChild.nodeValue;
temp = temp.replace("js/Three.js", "../build/Three.js");
var opener = window.open('','myconsole',
'width=800,height=400'
+',menubar=1'
+',toolbar=0'
+',status=1'
+',scrollbars=1'
+',resizable=1');
opener.document.writeln( temp );
opener.document.close();
}, false
);
_domElement.appendChild( _preview );
/*
var _checkboxText = document.createElement( 'span' );
_checkboxText.style.fontFamily = 'Monospace';
_checkboxText.innerText = 'HTML';
_domElement.appendChild( _checkboxText );
*/
//
var _code = document.createElement( 'pre' );
_code.style.color = '#404040';
_code.style.margin = '20px';
_code.style.fontSize = '13px';
_code.style.whiteSpace = 'pre'; // 'pre-wrap'
_domElement.appendChild( _code );
//
var _list = [];
var _codegen = function (html) {
var string = '';
console.log(_list);
string += [
'var camera, scene, renderer;',
'',
'init();',
'animate();',
'',
'function init() {',
'',
'\tcamera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );',
'\tcamera.position.z = 500;',
'',
'\tscene = new THREE.Scene();',
''
].join( '\n' );
for ( var i = 0, l = _list.length; i < l; i ++ ) {
string += _list[ i ] + '\n';
}
string += [
'',
'\trenderer = new THREE.WebGLRenderer()',
'\trenderer.setSize( window.innerWidth, window.innerHeight );',
'\tdocument.body.appendChild( renderer.domElement );',
'',
'}',
'',
'function animate() {',
'',
'\trequestAnimationFrame( animate );',
'\trender();',
'',
'}',
'',
'function render() {',
'',
'\trenderer.render( scene, camera );',
'',
'}'
].join( '\n' );
if ( html ) {
string = '<!doctype html>\n<html>\n\t<body>\
\n\t\t<style> body {background-color: #f0f0f0;} </style>\
\n\t\t<script src=\"js/Three.js\"></script>\
\n\t\t<script>\n'
+ ( '\n' + string ).replace( /\n/gi, '\n\t\t\t' ) +
'\n\n\t\t</script>\n\t</body>\n</html>';
}
return string;
}
var _update = function () {
_code.innerHTML = _codegen( _html );
}
var _strfor = function(str) {
for (var i=1; i<arguments.length; i++) {
if (arguments[i].toFixed) {
arguments[i] = arguments[i].toFixed(2);
}
str = str.replace('{'+(i-1)+'}', arguments[i]);
}
return str;
}
// signals
signals.updated.add( function ( scene ) {
_list.length = 0;
for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
var object = scene.children[ i ];
if ( object.geometry == undefined || object.geometry.gui == undefined ) {
if (object instanceof THREE.Camera) {
var string = '';
string += _strfor( '\n\tcamera.position.set({0},{1},{2});', object.position.x, object.position.y, object.position.z);
string += _strfor( '\n\tcamera.rotation.set({0},{1},{2});', object.rotation.x, object.rotation.y, object.rotation.z);
_list.push( string );
} else {
_list.push( 'TODO' );
}
continue;
}
if ( object instanceof THREE.Mesh ) {
var string = '';
string += '\n\tvar geometry = ' + object.geometry.gui.getCode() + ';';
string += '\n\tvar material = ' + object.material.gui.getCode() + ';';
string += '\n\tvar mesh = new THREE.Mesh( geometry, material );';
if ( object.position.x != 0 ) string += '\n\tmesh.position.x = ' + object.position.x + ';';
if ( object.position.y != 0 ) string += '\n\tmesh.position.y = ' + object.position.y + ';';
if ( object.position.z != 0 ) string += '\n\tmesh.position.z = ' + object.position.z + ';';
if ( object.rotation.x != 0 ) string += '\n\tmesh.rotation.x = ' + object.rotation.x + ';';
if ( object.rotation.y != 0 ) string += '\n\tmesh.rotation.y = ' + object.rotation.y + ';';
if ( object.rotation.z != 0 ) string += '\n\tmesh.rotation.z = ' + object.rotation.z + ';';
if ( object.scale.x != 1 ) string += '\n\tmesh.scale.x = ' + object.scale.x + ';';
if ( object.scale.y != 1 ) string += '\n\tmesh.scale.y = ' + object.scale.y + ';';
if ( object.scale.z != 1 ) string += '\n\tmesh.scale.z = ' + object.scale.z + ';';
string += '\n\tscene.add( mesh );';
_list.push( string );
}
}
_update();
} );
//
this.getDOMElement = function () {
return _domElement;
}
this.setPosition = function ( x, y ) {
_domElement.style.left = x + 'px';
_domElement.style.top = y + 'px';
}
this.setSize = function ( width, height ) {
_domElement.style.width = width + 'px';
_domElement.style.height = height + 'px';
}
}