forked from Soft8Soft/verge3d-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss2d_label.html
230 lines (165 loc) · 6.39 KB
/
css2d_label.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
229
230
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>Verge3D css2d - label</title>
<link type="text/css" rel="stylesheet" href="main.css">
<style>
.label {
color: #FFF;
font-family: sans-serif;
padding: 2px;
background: rgba(0, 0, 0, .6);
}
</style>
</head>
<body>
<div id="info"><a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> css2d - label</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import { OrbitControls } from 'v3d/addons/controls/OrbitControls.js';
import { CSS2DRenderer, CSS2DObject } from 'v3d/addons/renderers/CSS2DRenderer.js';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
let gui;
let camera, scene, renderer, labelRenderer;
const layers = {
'Toggle Name': function() {
camera.layers.toggle(0);
},
'Toggle Mass': function() {
camera.layers.toggle(1);
},
'Enable All': function() {
camera.layers.enableAll();
},
'Disable All': function() {
camera.layers.disableAll();
}
};
const clock = new v3d.Clock();
const textureLoader = new v3d.TextureLoader();
let moon;
init();
animate();
function init() {
const EARTH_RADIUS = 1;
const MOON_RADIUS = 0.27;
camera = new v3d.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(10, 5, 20);
camera.layers.enableAll();
camera.layers.toggle(1);
scene = new v3d.Scene();
const dirLight = new v3d.DirectionalLight(0xffffff);
dirLight.position.set(0, 0, 1);
dirLight.layers.enableAll();
scene.add(dirLight);
const axesHelper = new v3d.AxesHelper(5);
axesHelper.layers.enableAll();
scene.add(axesHelper);
//
const earthGeometry = new v3d.SphereGeometry(EARTH_RADIUS, 16, 16);
const earthMaterial = new v3d.MeshPhongMaterial({
specular: 0x333333,
shininess: 5,
map: textureLoader.load('textures/planets/earth_atmos_2048.jpg'),
specularMap: textureLoader.load('textures/planets/earth_specular_2048.jpg'),
normalMap: textureLoader.load('textures/planets/earth_normal_2048.jpg'),
normalScale: new v3d.Vector2(0.85, 0.85)
});
const earth = new v3d.Mesh(earthGeometry, earthMaterial);
scene.add(earth);
const moonGeometry = new v3d.SphereGeometry(MOON_RADIUS, 16, 16);
const moonMaterial = new v3d.MeshPhongMaterial({
shininess: 5,
map: textureLoader.load('textures/planets/moon_1024.jpg')
});
moon = new v3d.Mesh(moonGeometry, moonMaterial);
scene.add(moon);
//
earth.layers.enableAll();
moon.layers.enableAll();
const earthDiv = document.createElement('div');
earthDiv.className = 'label';
earthDiv.textContent = 'Earth';
earthDiv.style.marginTop = '-1em';
const earthLabel = new CSS2DObject(earthDiv);
earthLabel.position.set(0, EARTH_RADIUS, 0);
earth.add(earthLabel);
earthLabel.layers.set(0);
const earthMassDiv = document.createElement('div');
earthMassDiv.className = 'label';
earthMassDiv.textContent = '5.97237e24 kg';
earthMassDiv.style.marginTop = '-1em';
const earthMassLabel = new CSS2DObject(earthMassDiv);
earthMassLabel.position.set(0, -2 * EARTH_RADIUS, 0);
earth.add(earthMassLabel);
earthMassLabel.layers.set(1);
const moonDiv = document.createElement('div');
moonDiv.className = 'label';
moonDiv.textContent = 'Moon';
moonDiv.style.marginTop = '-1em';
const moonLabel = new CSS2DObject(moonDiv);
moonLabel.position.set(0, MOON_RADIUS, 0);
moon.add(moonLabel);
moonLabel.layers.set(0);
const moonMassDiv = document.createElement('div');
moonMassDiv.className = 'label';
moonMassDiv.textContent = '7.342e22 kg';
moonMassDiv.style.marginTop = '-1em';
const moonMassLabel = new CSS2DObject(moonMassDiv);
moonMassLabel.position.set(0, -2 * MOON_RADIUS, 0);
moon.add(moonMassLabel);
moonMassLabel.layers.set(1);
//
renderer = new v3d.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
labelRenderer = new CSS2DRenderer();
labelRenderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.domElement.style.position = 'absolute';
labelRenderer.domElement.style.top = '0px';
document.body.appendChild(labelRenderer.domElement);
const controls = new OrbitControls(camera, labelRenderer.domElement);
controls.minDistance = 5;
controls.maxDistance = 100;
//
window.addEventListener('resize', onWindowResize);
initGui();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
moon.position.set(Math.sin(elapsed) * 5, 0, Math.cos(elapsed) * 5);
renderer.render(scene, camera);
labelRenderer.render(scene, camera);
}
//
function initGui() {
gui = new GUI();
gui.add(layers, 'Toggle Name');
gui.add(layers, 'Toggle Mass');
gui.add(layers, 'Enable All');
gui.add(layers, 'Disable All');
}
</script>
</body>
</html>