-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
228 lines (170 loc) · 6.82 KB
/
index.html
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
227
228
<!-- This example is based on the Three.js example of the Reflector -->
<!-- Link: https://threejs.org/examples/#webgl_mirror -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fern Solutions | Three.js mirror</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
html, body {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<div id="container"></div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.152.1/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.152.1/examples/jsm/",
"three-mirror": "./js/three-mirror.esm.min.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { VRButton } from 'three/addons/webxr/VRButton.js';
import { Mirror } from 'three-mirror';
let camera, scene, renderer;
let cameraControls;
let sphereGroup, smallSphere;
let groundMirror, verticalMirror;
init();
animate();
function init() {
const container = document.getElementById( 'container' );
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.xr.enabled = true;
container.appendChild( renderer.domElement );
document.body.appendChild( VRButton.createButton( renderer ) );
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 7.5, 16.0 );
cameraControls = new OrbitControls( camera, renderer.domElement );
cameraControls.target.set( 0, 4.0, 0 );
cameraControls.maxDistance = 40.0;
cameraControls.minDistance = 1.0;
cameraControls.update();
//
const planeGeo = new THREE.PlaneGeometry( 10.1, 10.1 );
// reflectors/mirrors
let geometry, material;
geometry = new THREE.CircleGeometry( 4.0, 64 );
groundMirror = new Mirror( geometry, { mirrorId: 1, renderSelf: false } );
groundMirror.material.color = new THREE.Color('black');
groundMirror.renderOrder = 1000;
groundMirror.position.y = 0.05;
groundMirror.rotateX( - Math.PI / 2 );
scene.add( groundMirror );
geometry = new THREE.PlaneGeometry( 10.0, 10.0 );
verticalMirror = new Mirror( geometry, { mirrorId: 2, renderSelf: false } );
verticalMirror.material.color = new THREE.Color('black');
verticalMirror.renderOrder = 1000;
verticalMirror.position.y = 5.0;
verticalMirror.position.z = -5.0;
scene.add( verticalMirror );
let renderingMirrors = false;
scene.onAfterRender = function(renderer, scene, camera) {
if(renderingMirrors) {
return;
}
renderingMirrors = true;
verticalMirror.visible = false;
groundMirror.render(renderer, scene, camera);
verticalMirror.visible = true;
groundMirror.visible = false;
verticalMirror.render(renderer, scene, camera);
groundMirror.visible = true;
renderingMirrors = false;
}
sphereGroup = new THREE.Object3D();
scene.add( sphereGroup );
geometry = new THREE.CylinderGeometry( 0.01, 1.5 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
const sphereCap = new THREE.Mesh( geometry, material );
sphereCap.position.y = -1.5 * Math.sin( Math.PI / 180 * 30 ) - 0.005;
sphereCap.rotateX( - Math.PI );
geometry = new THREE.SphereGeometry( 1.5, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
const halfSphere = new THREE.Mesh( geometry, material );
halfSphere.add( sphereCap );
halfSphere.rotateX( - Math.PI / 180 * 135 );
halfSphere.rotateZ( - Math.PI / 180 * 20 );
halfSphere.position.y = 0.75 + 1.5 * Math.sin( Math.PI / 180 * 30 );
sphereGroup.add( halfSphere );
geometry = new THREE.IcosahedronGeometry( 0.5, 0 );
material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
smallSphere = new THREE.Mesh( geometry, material );
scene.add( smallSphere );
// walls
const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
planeTop.position.y = 10.0;
planeTop.rotateX( Math.PI / 2 );
scene.add( planeTop );
const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
planeBottom.rotateX( - Math.PI / 2 );
scene.add( planeBottom );
const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
planeFront.position.z = 5.0;
planeFront.position.y = 5.0;
planeFront.rotateY( Math.PI );
scene.add( planeFront );
const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
planeRight.position.x = 5.0;
planeRight.position.y = 5.0;
planeRight.rotateY( - Math.PI / 2 );
scene.add( planeRight );
const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
planeLeft.position.x = -5.0;
planeLeft.position.y = 5.0;
planeLeft.rotateY( Math.PI / 2 );
scene.add( planeLeft );
// lights
const mainLight = new THREE.PointLight( 0xcccccc, 1.5, 25.0 );
mainLight.position.y = 6.0;
scene.add( mainLight );
const greenLight = new THREE.PointLight( 0x00ff00, 0.25, 100.0 );
greenLight.position.set( 55.0, 5.0, 0 );
scene.add( greenLight );
const redLight = new THREE.PointLight( 0xff0000, 0.25, 100.0 );
redLight.position.set( - 55.0, 5.0, 0 );
scene.add( redLight );
const blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 100.0 );
blueLight.position.set( 0, 5.0, 55.0 );
scene.add( blueLight );
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
renderer.setAnimationLoop( render );
}
function render() {
const timer = Date.now() * 0.01;
sphereGroup.rotation.y -= 0.002;
smallSphere.position.set(
Math.cos( timer * 0.1 ) * 3.0,
Math.abs( Math.cos( timer * 0.2 ) ) * 2.0 + 0.5,
Math.sin( timer * 0.1 ) * 3.0
);
smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
smallSphere.rotation.z = timer * 0.8;
renderer.render( scene, camera );
}
</script>
</body>
</html>