This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
misc_controls_arcball.html
298 lines (192 loc) · 8.77 KB
/
misc_controls_arcball.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webgl - arcball controls</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> - arcball controls<br/>
<a href="http://www.polycount.com/forum/showthread.php?t=130641" target="_blank" rel="noopener">Cerberus(FFVII Gun) model</a> by Andrew Maximov.
</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": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
import { ArcballControls } from 'v3d/addons/controls/ArcballControls.js';
import { OBJLoader } from 'v3d/addons/loaders/OBJLoader.js';
import { RGBELoader } from 'v3d/addons/loaders/RGBELoader.js';
const cameras = ['Orthographic', 'Perspective'];
const cameraType = { type: 'Perspective' };
const perspectiveDistance = 2.5;
const orthographicDistance = 120;
let camera, controls, scene, renderer, gui;
let folderOptions, folderAnimations;
const arcballGui = {
gizmoVisible: true,
setArcballControls: function() {
controls = new ArcballControls(camera, renderer.domElement, scene);
controls.addEventListener('change', render);
this.gizmoVisible = true;
this.populateGui();
},
populateGui: function() {
folderOptions.add(controls, 'enabled').name('Enable controls');
folderOptions.add(controls, 'enableGrid').name('Enable Grid');
folderOptions.add(controls, 'enableRotate').name('Enable rotate');
folderOptions.add(controls, 'enablePan').name('Enable pan');
folderOptions.add(controls, 'enableZoom').name('Enable zoom');
folderOptions.add(controls, 'cursorZoom').name('Cursor zoom');
folderOptions.add(controls, 'adjustNearFar').name('adjust near/far');
folderOptions.add(controls, 'scaleFactor', 1.1, 10, 0.1).name('Scale factor');
folderOptions.add(controls, 'minDistance', 0, 50, 0.5).name('Min distance');
folderOptions.add(controls, 'maxDistance', 0, 50, 0.5).name('Max distance');
folderOptions.add(controls, 'minZoom', 0, 50, 0.5).name('Min zoom');
folderOptions.add(controls, 'maxZoom', 0, 50, 0.5).name('Max zoom');
folderOptions.add(arcballGui, 'gizmoVisible').name('Show gizmos').onChange(function() {
controls.setGizmosVisible(arcballGui.gizmoVisible);
});
folderOptions.add(controls, 'copyState').name('Copy state(ctrl+c)');
folderOptions.add(controls, 'pasteState').name('Paste state(ctrl+v)');
folderOptions.add(controls, 'reset').name('Reset');
folderAnimations.add(controls, 'enableAnimations').name('Enable anim.');
folderAnimations.add(controls, 'dampingFactor', 0, 100, 1).name('Damping');
folderAnimations.add(controls, 'wMax', 0, 100, 1).name('Angular spd');
}
};
init();
function init() {
const container = document.createElement('div');
document.body.appendChild(container);
renderer = new v3d.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = v3d.sRGBEncoding;
renderer.toneMapping = v3d.ReinhardToneMapping;
renderer.toneMappingExposure = 3;
renderer.domElement.style.background = 'linear-gradient(180deg, rgba(0,0,0,1) 0%, rgba(128,128,255,1) 100%)';
container.appendChild(renderer.domElement);
//
scene = new v3d.Scene();
scene.add(new v3d.HemisphereLight(0x443333, 0x222233, 4));
camera = makePerspectiveCamera();
camera.position.set(0, 0, perspectiveDistance);
const material = new v3d.MeshStandardMaterial();
new OBJLoader()
.setPath('models/obj/cerberus/')
.load('Cerberus.obj', function(group) {
const textureLoader = new v3d.TextureLoader().setPath('models/obj/cerberus/');
material.roughness = 1;
material.metalness = 1;
const diffuseMap = textureLoader.load('Cerberus_A.jpg', render);
diffuseMap.encoding = v3d.sRGBEncoding;
material.map = diffuseMap;
material.metalnessMap = material.roughnessMap = textureLoader.load('Cerberus_RM.jpg', render);
material.normalMap = textureLoader.load('Cerberus_N.jpg', render);
material.map.wrapS = v3d.RepeatWrapping;
material.roughnessMap.wrapS = v3d.RepeatWrapping;
material.metalnessMap.wrapS = v3d.RepeatWrapping;
material.normalMap.wrapS = v3d.RepeatWrapping;
group.traverse(function(child) {
if (child.isMesh) {
child.material = material;
}
});
group.rotation.y = Math.PI / 2;
group.position.x += 0.25;
scene.add(group);
render();
new RGBELoader()
.setPath('textures/equirectangular/')
.load('venice_sunset_1k.hdr', function(hdrEquirect) {
hdrEquirect.mapping = v3d.EquirectangularReflectionMapping;
scene.environment = hdrEquirect;
render();
});
window.addEventListener('keydown', onKeyDown);
window.addEventListener('resize', onWindowResize);
//
gui = new GUI();
gui.add(cameraType, 'type', cameras).name('Choose Camera').onChange(function() {
setCamera(cameraType.type);
});
folderOptions = gui.addFolder('Arcball parameters');
folderAnimations = folderOptions.addFolder('Animations');
arcballGui.setArcballControls();
render();
});
}
function makeOrthographicCamera() {
const halfFovV = v3d.MathUtils.DEG2RAD * 45 * 0.5;
const halfFovH = Math.atan((window.innerWidth / window.innerHeight) * Math.tan(halfFovV));
const halfW = perspectiveDistance * Math.tan(halfFovH);
const halfH = perspectiveDistance * Math.tan(halfFovV);
const near = 0.01;
const far = 2000;
const newCamera = new v3d.OrthographicCamera(-halfW, halfW, halfH, - halfH, near, far);
return newCamera;
}
function makePerspectiveCamera() {
const fov = 45;
const aspect = window.innerWidth / window.innerHeight;
const near = 0.01;
const far = 2000;
const newCamera = new v3d.PerspectiveCamera(fov, aspect, near, far);
return newCamera;
}
function onWindowResize() {
if (camera.type == 'OrthographicCamera') {
const halfFovV = v3d.MathUtils.DEG2RAD * 45 * 0.5;
const halfFovH = Math.atan((window.innerWidth / window.innerHeight) * Math.tan(halfFovV));
const halfW = perspectiveDistance * Math.tan(halfFovH);
const halfH = perspectiveDistance * Math.tan(halfFovV);
camera.left = -halfW;
camera.right = halfW;
camera.top = halfH;
camera.bottom = -halfH;
} else if (camera.type == 'PerspectiveCamera') {
camera.aspect = window.innerWidth / window.innerHeight;
}
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
render();
}
function render() {
renderer.render(scene, camera);
}
function onKeyDown(event) {
if (event.key === 'c') {
if (event.ctrlKey || event.metaKey) {
controls.copyState();
}
} else if (event.key === 'v') {
if (event.ctrlKey || event.metaKey) {
controls.pasteState();
}
}
}
function setCamera(type) {
if (type == 'Orthographic') {
camera = makeOrthographicCamera();
camera.position.set(0, 0, orthographicDistance);
} else if (type == 'Perspective') {
camera = makePerspectiveCamera();
camera.position.set(0, 0, perspectiveDistance);
}
controls.setCamera(camera);
render();
}
</script>
</body>