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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
<div class="world"> <div class="annotation"> </div> </div> <!--<a class="toggle toggle-night"></a><a class="toggle-music"></a>--> <audio class="world-music" src="https://res.cloudinary.com/liza/video/upload/v1492853426/sheep_qr3tus.m4a"></audio>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script> <script src="js/three.js"></script> <script src="js/GLTFLoader.js" type="module"></script>
<script src="js/orbitcontrols.js"></script> <script src="js/simplex_noise.min.js"></script>
<script type="module"> import TWEEN from './js/tween.js';
import { GLTFLoader } from './js/GLTFLoader.js'; 'use strict';
var raycaster = new THREE.Raycaster(); // Needed for object intersection var mouse = new THREE.Vector2(); const annotation = document.querySelector(".annotation"); let objects = []; let mainobj, sprite; let spriteBehindObject; let skybox;
let scene, cubes, cube, group, camera, renderer, controls, mouseDown, world, night = false;
let sheep, cloud, sphere, sky;
let width = window.innerWidth, height = window.innerHeight;
let mouseX = 0; let mouseY = 0;
function init() {
scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000); camera.lookAt(scene.position); camera.position.set(-1300, 1300, 15); camera.zoom = 1;
renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(width, height); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap;
var controls = new THREE.OrbitControls(camera, renderer.domElement); controls.autoRotate = false; controls.autoRotateSpeed = 4; controls.maxDistance = 15; controls.minDistance = 10; controls.enablePan = false;
controls.update();
group = new THREE.Group();
var loader = new GLTFLoader();
const boxWidth = 1; const boxHeight = 1; const boxDepth = 1; const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth); const labelContainerElem = document.querySelector('.annotation');
function makeInstance(geometry, color, x, y, z, html) {
const material = new THREE.MeshPhongMaterial({ color, opacity: 0, transparent: true });
const cube = new THREE.Mesh(geometry, material);
group.add(cube);
cube.position.x = x; cube.position.y = y; cube.position.z = z;
const elem = document.createElement('div');
elem.innerHTML = html; labelContainerElem.appendChild(elem);
return { cube, elem }; } cubes = [ makeInstance(geometry, "", -3, 6, 2, ' <button class="pointBtn zoomIn" ><div class="brandLogoRight"><img src="img/logo/logo02.jpg" class="w-100"></div></button>' ), makeInstance(geometry, "", 1, 6, 2, ' <button class="pointBtn zoomIn"><div class="brandLogoRight"><img src="img/logo/logo02.jpg" class="w-100"></div></button>' ), makeInstance(geometry, "", 4, 6, 2, ' <button class="pointBtn zoomIn"><div class="brandLogoRight"><img src="img/logo/logo02.jpg" class="w-100"></div></button>' ), makeInstance(geometry, "", -3, 6, -3, '<button class="pointBtn zoomIn"><div class="brandLogoLeft"><img src="img/logo/logo02.jpg" class="w-100"></div></button>' ), makeInstance(geometry, "", 1, 6, -3, '<button class="pointBtn zoomIn"><div class="brandLogoLeft"><img src="img/logo/logo02.jpg" class="w-100"></div></button>' ), makeInstance(geometry, "", 4, 6, -3, '<button class="pointBtn zoomIn"><div class="brandLogoLeft"><img src="img/logo/logo02.jpg" class="w-100"></div></button>' ), ];
loader.load("./modal/shop/test.gltf", function(gltf) { mainobj = gltf.scene; mainobj.scale.x = 0.05; mainobj.scale.y = 0.05; mainobj.scale.z = 0.05; mainobj.rotation.y = 0; mainobj.rotation.x = 0; mainobj.rotation.z = 0; mainobj.position.y = 0; group.add(mainobj);
}); scene.add(group); // controls = new THREE.OrbitControls(camera, renderer.domElement); // controls.enableZoom = false;
drawbackground(); addLights();
world = document.querySelector('.world'); world.appendChild(renderer.domElement);
document.addEventListener('mousedown', onMouseDown); document.addEventListener('mouseup', onMouseUp); document.addEventListener('touchstart', onTouchStart); document.addEventListener('touchend', onTouchEnd); window.addEventListener('resize', onResize);
document.addEventListener('mousemove', onMouseMove, false); document.addEventListener('click', onDocumentMouseDown, false);
}
function animateParticles(event) { mouseX = event.clientX mouseY = event.clientY } //light function addLights() { const light = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.6); scene.add(light);
const directLight1 = new THREE.DirectionalLight(0xffd798, 1); directLight1.castShadow = true; directLight1.position.set(9.5, 8.2, 8.3); scene.add(directLight1);
const pointlight = new THREE.PointLight(0xffffff, 2, 30);
pointlight.castShadow = true; pointlight.position.set(10, 5, 1); scene.add(pointlight);
const directLight2 = new THREE.DirectionalLight(0xc9ceff, 0.8); directLight2.castShadow = true; directLight2.position.set(-15.8, 5.2, 8); scene.add(directLight2);
directLight1.position.set(20, 30, 10); directLight1.castShadow = true; directLight1.shadow.camera.top = 20;
}
//sky box function drawbackground() { let materialArray = []; let texture_ft = new THREE.TextureLoader().load('img/skybox/harmony_ft.jpg');
let texture_bk = new THREE.TextureLoader().load('img/skybox/harmony_bk.jpg'); let texture_up = new THREE.TextureLoader().load('img/skybox/harmony_up.jpg'); let texture_dn = new THREE.TextureLoader().load('img/skybox/harmony_dn.jpg'); let texture_rt = new THREE.TextureLoader().load('img/skybox/harmony_rt.jpg');
let texture_lt = new THREE.TextureLoader().load('img/skybox/harmony_lf.jpg');
materialArray.push(new THREE.MeshBasicMaterial({ map: texture_ft })); materialArray.push(new THREE.MeshBasicMaterial({ map: texture_bk })); materialArray.push(new THREE.MeshBasicMaterial({ map: texture_up })); materialArray.push(new THREE.MeshBasicMaterial({ map: texture_dn })); materialArray.push(new THREE.MeshBasicMaterial({ map: texture_rt })); materialArray.push(new THREE.MeshBasicMaterial({ map: texture_lt }));
for (let i = 0; i < 6; i++) materialArray[i].side = THREE.BackSide; let skyboxGeo = new THREE.BoxGeometry(1000, 1000, 1000); skybox = new THREE.Mesh(skyboxGeo, materialArray); scene.add(skybox);
} //point button position function onDocumentMouseDown(event) {
// Welcome to the exciting world of raycasting ! // First let's get some mouse coordinates: mouse.x = (event.clientX / window.innerWidth) * 2 - 1; mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; // This is basically converting 2d coordinates to 3d Space: raycaster.setFromCamera(mouse, camera); // And checking if it intersects with an array object var intersects = raycaster.intersectObjects(objects);
// does your cursor intersect the object on click ? //console.log(intersects.length > 0 ? "yes" : "no");
// And finally change the color: if (intersects.length > 0) { let obj = intersects[0].object;
intersects[0].object.material.color.setHex(Math.random() * 0xffffff);
} } //mouse Move function onMouseMove(event) {
// calculate mouse position in normalized device coordinates // (-1 to +1) for both components
mouse.x = (event.clientX / window.innerWidth) * 2 - 1; mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
function onResize() { width = window.innerWidth; height = window.innerHeight; camera.aspect = width / height; camera.updateProjectionMatrix(); renderer.setSize(width, height); }
function onMouseDown(event) {
theta = -((event.clientX - onMouseDownPosition.x) * 0.5) + onMouseDownTheta; phi = ((event.clientY - onMouseDownPosition.y) * 0.5) + onMouseDownPhi;
phi = Math.min(180, Math.max(0, phi));
camera.position.x = radious * Math.sin(theta * Math.PI / 360) * Math.cos(phi * Math.PI / 360); camera.position.y = radious * Math.sin(phi * Math.PI / 360); camera.position.z = radious * Math.cos(theta * Math.PI / 360) * Math.cos(phi * Math.PI / 360); camera.updateMatrix();
mouseDown = true; }
function onTouchStart(event) { // const targetClass = event.target.classList[0]; // if (targetClass === 'toggle' || targetClass === 'toggle-music') return; event.preventDefault(); mouseDown = true; }
function onMouseUp() { mouseDown = false; }
function onTouchEnd(event) { // const targetClass = event.target.classList[0]; //if (targetClass === 'toggle' || targetClass === 'toggle-music') return; event.preventDefault(); mouseDown = false; }
function rad(degrees) { return degrees * (Math.PI / 180); }
function animate() { requestAnimationFrame(animate); camera.updateProjectionMatrix(); TWEEN.update(); render(); }
function render() {
updateScreenPosition(); // updateScreenPosition(); // mainobj.rotation.y += 0.0009; //skybox.rotation.y += 0.0009;
renderer.render(scene, camera); } /* function updateScreenPosition() { const vector = new THREE.Vector3(1, 1, 1); const canvas = renderer.domElement;
vector.project(camera);
vector.x = Math.round((0.5 + vector.x / 2) * (canvas.width / window.devicePixelRatio)); vector.y = Math.round((0.5 - vector.y / 2) * (canvas.height / window.devicePixelRatio));
console.log(vector.x); annotation.style.top = `${mainobj.position.y }px`; annotation.style.left = `${mainobj.position.x}px`; annotation.style.opacity = spriteBehindObject ? 0.25 : 1; }*/ // onclick
$(document).on("click", ".zoomIn", function() { // call this to make the model larger/ zoom in
var zoom = { value: camera.zoom }; var zoomEnd = { value: 2 }; var tween = new TWEEN.Tween(zoom).to(zoomEnd, 600); tween.onUpdate(function() { camera.zoom = zoom.value; }); tween.start(); setTimeout(function() { $("body").fadeOut(); window.location.href = "shop.php";
}, 1000); // call this to make it smaller / zoom out //zoomModel(false, 4); });
function updateScreenPosition() { const vector = new THREE.Vector3(1, 1, 1);
const canvas = renderer.domElement;
vector.project(camera);
cubes.forEach((cubeInfo, ndx) => { const { cube, elem } = cubeInfo; const speed = 1 + ndx * .1; // cube.position.x = 10; // cube.position.y += 0.001;
// get the position of the center of the cube cube.updateWorldMatrix(true, false); cube.getWorldPosition(vector);
// get the normalized screen coordinate of that position // x and y will be in the -1 to +1 range with x = -1 being // on the left and y = -1 being on the bottom vector.project(camera);
// convert the normalized position to CSS coordinates const x = (vector.x * .5 + .5) * canvas.clientWidth; const y = (vector.y * -.5 + .5) * canvas.clientHeight;
// move the elem to that position elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`; });
} init(); animate(); </script>
|