-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
232 lines (204 loc) · 7.85 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
229
230
231
232
<!DOCTYPE html>
<html lang="en">
<head>
<title>Basic Scene</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<!-- <link type="text/css" rel="stylesheet" href="style.css" /> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r126/three.js" crossorigin="anonymous"></script>
</head>
<body>
<script type="module">
import { VRButton } from 'https://unpkg.com/[email protected]/examples/jsm/webxr/VRButton.js';
import { FBXLoader } from 'https://unpkg.com/[email protected]/examples/jsm/loaders/FBXLoader.js';
import { XRControllerModelFactory } from 'https://unpkg.com/[email protected]/examples/jsm/webxr/XRControllerModelFactory.js';
let camera, scene, renderer;
// Controller
let controllerL, controllerR;
//Loader
let textureLoader = new THREE.TextureLoader();
let objects = [];
let checkGrabbingR;
let controllerActionRadius = 0.1;
initConfiguration();
initScene();
function initConfiguration() {
const container = document.createElement('div');
document.body.appendChild(container);
// Scene
scene = new THREE.Scene();
// Add sky
// const textureSky = textureLoader.load(
// 'https://cdnnnnnn.glitch.global/3dfb403a-5c18-4735-b386-004d31d0dbf6/sky.jpg?v=1668363787745'
// );
const textureSky = textureLoader.load(
'assets/rogland_clear_night.jpg'
);
const materialSky = new THREE.MeshBasicMaterial({
map: textureSky,
side: THREE.DoubleSide,
});
const sky = new THREE.Mesh(new THREE.SphereGeometry(30, 32, 32), materialSky);
scene.add(sky);
// Add ground
const textureGround = textureLoader.load(
'assets/coast_sand_rocks_02_diff_1k.jpg',
function (texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(15, 20);
}
);
const materialGround = new THREE.MeshStandardMaterial({
map: textureGround,
});
let plane = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
materialGround
);
plane.rotation.set(-Math.PI / 2, 0, 0);
plane.receiveShadow = true;
scene.add(plane);
// Camera
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 40);
camera.position.set(0, 3, 3);
camera.rotation.set(-0.4, 0, 0);
// three.js renderer
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
var controllerModelFactory = new XRControllerModelFactory();
controllerL = renderer.xr.getControllerGrip(0);
controllerR = renderer.xr.getControllerGrip(1);
const material = new THREE.MeshPhongMaterial({
shininess: 6,
shading: true,
transparent: 1,
opacity: 0.8,
});
const model1 = controllerModelFactory.createControllerModel(controllerL, material);
controllerL.add(model1);
scene.add(controllerL);
const model2 = controllerModelFactory.createControllerModel(controllerR);
controllerR.add(model2);
scene.add(controllerR);
// Sphere that follow the controller
const materialSphereHand = new THREE.MeshPhongMaterial({
color: new THREE.Color('rgb(255,255,255)'),
shininess: 1,
shading: true,
transparent: true,
opacity: 0.2,
});
var sphereR = new THREE.Mesh(
new THREE.SphereGeometry(controllerActionRadius, 20, 20),
materialSphereHand.clone(),
);
controllerR.add(sphereR);
controllerR.addEventListener('selectstart', SelectEventGrab);
controllerR.addEventListener('selectend', SelectEventGrabEnd);
var geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, -0, -50),
]);
var line = new THREE.Line(geometry);
controllerL.add(line);
controllerL.addEventListener('squeezestart', SelectEventRay);
function SelectEventRay() {
var raycaster = new THREE.Raycaster(),
intersected = [];
var tempMatrix = new THREE.Matrix4();
var intersections = getIntersections(controllerL);
for (var i = 0; i < intersections.length; i++) {
var intersection = intersections[i];
if (objects.includes(intersection.object)) {
var object = intersection.object;
object.position.set(controllerL.position.x, controllerL.position.y, controllerL.position.z);
}
}
function getIntersections(controller) {
tempMatrix.identity().extractRotation(controller.matrixWorld);
raycaster.ray.origin.setFromMatrixPosition(controller.matrixWorld);
raycaster.ray.direction.set(0, 0, -1).applyMatrix4(tempMatrix);
return raycaster.intersectObjects(scene.children);
}
}
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// -----> Enable the renderer for WebXR
renderer.xr.enabled = true;
container.appendChild(renderer.domElement);
// Add the VR button to the body of the DOM
document.body.appendChild(VRButton.createButton(renderer));
renderer.setAnimationLoop(render);
}
function initScene() {
var light = new THREE.HemisphereLight(
0xffffbb, // Sky Color
0x080820, // Ground Color
1, // Intensity
);
light.position.set(0.5, 1, 0.25);
scene.add(light);
AddObjects();
function AddObjects() {
const material = new THREE.MeshPhongMaterial({
shininess: 6,
flatShading: true,
transparent: 1,
opacity: 0.8,
});
objects = [
SetObject(
new THREE.CylinderGeometry(0.2, 0.2, 1, 32),
material.clone(),
[-0.5, 1.5, 0],
new THREE.Color('rgb(226,35,0)'),
),
SetObject(
new THREE.BoxGeometry(1, 1, 1),
material.clone(),
[0.5, 1.5, 0],
new THREE.Color('rgb(100,100, 255)'),
),
SetObject(
new THREE.SphereGeometry(0.6, 20, 20),
material.clone(),
[0, 1.5, -0.5],
new THREE.Color('rgb(0,255,0)'),
),
];
function SetObject(geometry, mat, position, color) {
mat.color = color;
let mesh = new THREE.Mesh(geometry, mat.clone());
mesh.position.set(position[0], position[1], position[2]);
mesh.scale.set(0.1, 0.1, 0.1);
scene.add(mesh);
// New
return mesh;
}
}
}
function CheckObjects() {
for (var i = 0; i < objects.length; i++) {
var diferenciaX = controllerR.position.x - objects[i].position.x;
var diferenciaY = controllerR.position.y - objects[i].position.y;
var diferenciaZ = controllerR.position.z - objects[i].position.z;
if (
Math.sqrt(Math.pow(diferenciaX, 2) + Math.pow(diferenciaY, 2) + Math.pow(diferenciaZ, 2)) <=
controllerActionRadius
)
objects[i].position.copy(controllerR.position);
}
}
// New render function
function render() {
if (checkGrabbingR) CheckObjects();
renderer.render(scene, camera);
}
function SelectEventGrab(event) {
checkGrabbingR = true;
}
function SelectEventGrabEnd(event) {
checkGrabbingR = false;
}
</script>
</body>
</html>