-
Notifications
You must be signed in to change notification settings - Fork 0
/
play3d-sandbox.js
104 lines (90 loc) · 2.75 KB
/
play3d-sandbox.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
"use strict";
var RT = (function() {
var camera = null,
scene, renderer;
var animationID = null;
var update = null;
function clear3d() {
if (animationID) {
cancelAnimationFrame(animationID);
animationID = null;
}
//camera = null;
scene = new THREE.Scene();
//renderer = null;
update = null;
}
function create3d() {
// 1 micrometer to 100 billion light years in one scene, with 1 unit = 1 meter? preposterous! and yet...
var NEAR = 1e-6, FAR = 1e27;
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, NEAR, FAR);
camera.position.z = 1;
camera.position.x = 1;
camera.position.y = 1;
camera.lookAt(0, 0, 0);
renderer = new THREE.WebGLRenderer({
antialias: true,
logarithmicDepthBuffer: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render);
controls.update();
}
function render() {
renderer.render(scene, camera);
}
function animate() {
if (renderer && scene && camera) {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
}
clear3d();
create3d();
var throttle = function(type, name, obj) {
obj = obj || window;
var running = false;
var func = function() {
if (running) { return; }
running = true;
requestAnimationFrame(function() {
obj.dispatchEvent(new CustomEvent(name));
running = false;
});
};
obj.addEventListener(type, func);
};
/* init - you can init any event */
throttle("resize", "optimizedResize");
window.addEventListener('optimizedResize', function(e) {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render();
});
var obj = {
clear3d: clear3d,
render: render,
};
Object.defineProperties(obj, {
scene: {
get: function() { return scene; }
}
});
return obj;
})();
window.addEventListener('message', function(e) {
var mainWindow = e.source;
RT.clear3d();
var result = null;
try {
var f = new Function('scene', 'RT', 'e', 'window', 'document', e.data);
f.bind({})(RT.scene);
} catch( err ) {
result = err.stack;
}
mainWindow.postMessage(result, '*');
RT.render();
});