-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathindex.html
More file actions
285 lines (203 loc) · 8.35 KB
/
index.html
File metadata and controls
285 lines (203 loc) · 8.35 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - ObjectControls example</title>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name="description"
content="three.js webgl - ObjectControls example">
<meta name="keywords"
content="threejs, three, object, controls, 3D, object rotation">
<meta name="author"
content="Alberto Piras">
<meta name="contact"
content="a.piras.ict@gmail.com">
<style>
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px;
width: 100%;
color: #ffffff;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: center;
}
a {
color: #ff0080;
text-decoration: none;
}
a:hover {
color: #0080ff;
}
</style>
</head>
<body>
<a href="https://github.com/albertopiras/threeJS-object-controls"><img width="149" height="149"
src="https://github.blog/wp-content/uploads/2008/12/forkme_left_green_007200.png?resize=149%2C149"
class="attachment-full size-full" alt="Fork me on GitHub" data-recalc-dims="1"></a>
<div id="info">
<a href="http://threejs.org"
target="_blank">three.js</a> webgl - <a
href="https://github.com/Alberto-/threeJS-object-controls"
target="_blank">ObjectControls</a> example, with spotlight by <a
href="http://master-domain.com"
target="_blank">Master James</a><br />
</div>
<script src="libs/three.min.js"></script>
<script src="libs/dat.gui.min.js"></script>
<script src="libs/Detector.js"></script>
<script type="module">
import {ObjectControls} from './ObjectControls.js';
if (!Detector.webgl) Detector.addGetWebGLMessage();
var renderer = new THREE.WebGLRenderer();
var camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 1000);
var scene = new THREE.Scene();
var matFloor = new THREE.MeshPhongMaterial({ dithering: true });
var matBox = new THREE.MeshPhongMaterial({ color: 0xebe20d, dithering: true });
var geoFloor = new THREE.BoxGeometry(1000, 1, 1000);
var geoBox = new THREE.BoxGeometry(2, 2, 2);
var mshFloor = new THREE.Mesh(geoFloor, matFloor);
var mshBox = new THREE.Mesh(geoBox, matBox);
var mshBox2 = new THREE.Mesh(geoBox, matBox);
var ambient = new THREE.AmbientLight(0xffffff, 0.1);
var spotLight = new THREE.SpotLight(0xffffff, 1);
var lightHelper;
var gui, guiElements, param = { color: '0xffffff' };
var controls;
function init() {
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.gammaInput = true;
renderer.gammaOutput = true;
camera.position.set(0, 5, 50);
camera.position.z = 30;
spotLight.position.set(15, 40, 35);
spotLight.castShadow = true;
spotLight.angle = 0.18;
spotLight.penumbra = 0;
spotLight.decay = 2;
spotLight.distance = 200;
spotLight.intensity = 2;
spotLight.shadow.mapSize.width = 1024;
spotLight.shadow.mapSize.height = 1024;
spotLight.shadow.camera.near = 1;
spotLight.shadow.camera.far = 200;
lightHelper = new THREE.SpotLightHelper(spotLight);
matFloor.color.set(0x808080);
mshFloor.receiveShadow = true;
mshFloor.position.set(0, - 0.05, 0);
mshBox.castShadow = true;
mshBox2.castShadow = true;
mshBox.position.set(-3, 2.3, 3);
mshBox2.position.set(3, 2.3, 3);
camera.lookAt(mshBox.position);
scene.add(camera);
scene.add(mshFloor);
scene.add(mshBox);
scene.add(mshBox2);
scene.add(ambient);
scene.add(spotLight);
scene.add(new THREE.AxisHelper(10));
scene.add(lightHelper);
document.body.appendChild(renderer.domElement);
renderer.setSize(window.innerWidth, window.innerHeight);
/** instantiate ObjectControls**/
controls = new ObjectControls(camera, renderer.domElement, mshBox);
controls.setDistance(8, 200); // set min - max distance for zoom
controls.setZoomSpeed(0.5); // set zoom speed
controls.enableVerticalRotation();
controls.setMaxVerticalRotationAngle(Math.PI / 4, Math.PI / 4);
controls.setRotationSpeed(0.05);
window.addEventListener('resize', onResize, false);
}
function onResize() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = (window.innerWidth / window.innerHeight);
camera.updateProjectionMatrix();
}
function animate() {
/* Auto rotation usage example
* the auto rotation for mshBox2 is enabled if the user is not interacting or if the selected mesh is not mshBox2
*/
if(!controls.isUserInteractionActive() || controls.getObjectToMove() != mshBox2){
//mshBox2 auto rotation
mshBox2.rotation.y += 0.01;
}
requestAnimationFrame(animate);
render();
}
function render() {
lightHelper.update(); // required
renderer.render(scene, camera);
}
function clearGui() {
if (gui) gui.destroy();
gui = new dat.GUI();
gui.open();
}
function buildGui() {
clearGui();
addGui('light color', spotLight.color.getHex(), function (val) {
spotLight.color.setHex(val);
}, true);
addGui('intensity', spotLight.intensity, function (val) {
spotLight.intensity = val;
}, false, 0, 2);
addGui('distance', spotLight.distance, function (val) {
spotLight.distance = val;
}, false, 0, 200);
addGui('angle', spotLight.angle, function (val) {
spotLight.angle = val;
}, false, 0, Math.PI / 3);
addGui('penumbra', spotLight.penumbra, function (val) {
spotLight.penumbra = val;
}, false, 0, 1);
addGui('decay', spotLight.decay, function (val) {
spotLight.decay = val;
}, false, 1, 2);
var changeMeshConfig = {
useMesh1: function () {
controls.setObjectToMove(mshBox);
},
useMesh2: function () {
controls.setObjectToMove(mshBox2);
},
useBoth: function () {
controls.setObjectToMove([mshBox,mshBox2]);
}
};
gui.add(changeMeshConfig, 'useMesh1');
gui.add(changeMeshConfig, 'useMesh2');
gui.add(changeMeshConfig, 'useBoth');
}
function addGui(name, value, callback, isColor, min, max) {
var node;
param[name] = value;
if (isColor) {
node = gui.addColor(param, name).onChange(function () {
callback(param[name]);
});
} else if (typeof value == 'object') {
node = gui.add(param, name, value).onChange(function () {
callback(param[name]);
});
} else {
node = gui.add(param, name, min, max).onChange(function () {
callback(param[name]);
});
}
return node;
}
init();
animate();
buildGui();
</script>
</body>
</html>