-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
135 lines (113 loc) · 3.79 KB
/
main.ts
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
/**
* Example based on the original example of threex.htmlmixer basic demo.
*
* @see https://github.com/jeromeetienne/threex.htmlmixer/blob/master/examples/basic.html
*/
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/Addons.js";
import THREEx from "threex.htmlmixer-continued";
const renderer = new THREE.WebGLRenderer({
alpha: true,
});
renderer.autoClear = false;
renderer.setClearAlpha(0);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const updateFcts: ((delta: number, now: number) => unknown)[] = [];
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.01,
1000,
);
camera.position.z = 3;
// Create THREEx.HtmlMixer
const mixerContext = new THREEx.htmlMixer.HtmlMixerContext(renderer, camera);
// Handle window resize for mixerContext
window.addEventListener(
"resize",
function () {
mixerContext.rendererCss.setSize(window.innerWidth, window.innerHeight);
},
false,
);
// MixerContext configuration and dom attachment
// set up rendererCss
const rendererCss = mixerContext.rendererCss;
rendererCss.setSize(window.innerWidth, window.innerHeight);
// set up rendererWebgl
const rendererWebgl = mixerContext.rendererWebgl;
const css3dElement = rendererCss.domElement;
css3dElement.style.position = "absolute";
css3dElement.style.top = "0px";
css3dElement.style.width = "100%";
css3dElement.style.height = "100%";
document.body.appendChild(css3dElement);
const webglCanvas = rendererWebgl.domElement;
webglCanvas.style.position = "absolute";
webglCanvas.style.top = "0px";
webglCanvas.style.width = "100%";
webglCanvas.style.height = "100%";
webglCanvas.style.pointerEvents = "none";
css3dElement.appendChild(webglCanvas);
// Create a Plane for THREEx.HtmlMixer
// Create the iframe element
const url = "http://threejs.org/";
const domElement = document.createElement("iframe");
domElement.src = url;
domElement.style.border = "none";
// Create the plane
const mixerPlane = new THREEx.htmlMixer.HtmlMixerPlane(
mixerContext,
domElement,
);
mixerPlane.object3d.scale.multiplyScalar(2);
scene.add(mixerPlane.object3d);
// Make it move / Update it
updateFcts.push((delta: number) => {
mixerPlane.object3d.rotation.y += Math.PI * 2 * delta * 0.1;
});
// Add objects in the scene
const material = new THREE.MeshNormalMaterial();
const torusKnotGeometry = new THREE.TorusKnotGeometry(0.5 - 0.125, 0.125);
const torusKnotMesh = new THREE.Mesh(torusKnotGeometry, material);
torusKnotMesh.position.set(+1, 0, +0.5);
const torusKnotGeometry2 = new THREE.TorusKnotGeometry(0.5 - 0.125, 0.125);
const torusKnotGeometry2Mesh = new THREE.Mesh(torusKnotGeometry2, material);
torusKnotGeometry2Mesh.position.set(-1, 0, -0.5);
scene.add(torusKnotMesh, torusKnotGeometry2Mesh);
// Camera Controls
new OrbitControls(camera, rendererCss.domElement);
// Handle resize
function onResize() {
// notify the renderer of the size change
renderer.setSize(window.innerWidth, window.innerHeight);
// update the camera
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", onResize, false);
// Render the css3d
updateFcts.push(function () {
// NOTE: it must be after camera mode
mixerContext.update();
});
// Render the webgl
updateFcts.push(function () {
renderer.render(scene, camera);
});
// Loop runner
let lastTimeMsec: null | number = null;
requestAnimationFrame(function animate(nowMsec) {
// Keep looping
requestAnimationFrame(animate);
// Measure time
lastTimeMsec = lastTimeMsec || nowMsec - 1000 / 60;
const deltaMsec = Math.min(200, nowMsec - lastTimeMsec);
lastTimeMsec = nowMsec;
// Call each update function
updateFcts.forEach(function (updateFn) {
updateFn(deltaMsec / 1000, nowMsec / 1000);
});
});