-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_soy-sauce.js
228 lines (178 loc) · 8.06 KB
/
index_soy-sauce.js
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
// 🪩 fork from Ksenia Kondrashova https://codepen.io/ksenia-k/pen/ZEjJxWQ
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls'
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
const container = document.querySelector('.container');
const canvasEl = document.querySelector('#canvas');
let renderer, clock, texture, scenes = [];
const loader = new OBJLoader();
let objects = [null];
loader.load('models/Soy sauce.obj', function (obj) {
objects[0] =
{
geometry: { ...obj },
mirrorSize: .0012,
isObject: true,
};
init();
});
function init() {
initScene();
window.addEventListener('resize', updateSceneSize);
function initScene() {
renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
canvas: canvasEl
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
clock = new THREE.Clock();
new THREE.TextureLoader().load(
'bg3.png',
(t) => {
texture = t;
for (let i = 0; i < objects.length; i++) {
scenes.push(createScene(i));
}
updateSceneSize();
render();
});
}
function createDiscoBall(idx) {
const dummy = new THREE.Object3D();
const mirrorMaterial = new THREE.MeshMatcapMaterial({
matcap: texture
});
const object = objects[idx];
let geometryOriginal = object.isObject ? object.geometry.children[0].geometry : object.geometry;
geometryOriginal.deleteAttribute('normal');
geometryOriginal.deleteAttribute('uv');
geometryOriginal = BufferGeometryUtils.mergeVertices(geometryOriginal);
geometryOriginal.computeVertexNormals();
const mirrorGeometry = new THREE.PlaneGeometry(objects[idx].mirrorSize, objects[idx].mirrorSize);
let instancedMirrorMesh = new THREE.InstancedMesh(
mirrorGeometry,
mirrorMaterial,
geometryOriginal.attributes.position.count
);
const positions = geometryOriginal.attributes.position.array;
const normals = geometryOriginal.attributes.normal.array;
for (let i = 0; i < positions.length; i += 3) {
dummy.position.set(positions[i], positions[i + 1], positions[i + 2]);
dummy.lookAt(positions[i] + normals[i], positions[i + 1] + normals[i + 1], positions[i + 2] + normals[i + 2]);
dummy.updateMatrix();
instancedMirrorMesh.setMatrixAt(i / 3, dummy.matrix);
}
const obj = new THREE.Group();
const innerGeometry = geometryOriginal.clone();
const ballInnerMaterial = new THREE.MeshBasicMaterial({ color: 0x452624 });
const innerMesh = new THREE.Mesh(
innerGeometry,
ballInnerMaterial
);
const rightEye = new THREE.Mesh(new THREE.SphereGeometry(0.0042, 32, 32), new THREE.MeshStandardMaterial({ color: 0x777777, roughness: 0.2, metalness: 0.5 }));
rightEye.position.set(-0.01, 0.0866, 0.027);
const leftEye = rightEye.clone();
leftEye.position.set(0.013, 0.083, 0.0282);
const nose = new THREE.Mesh(new THREE.SphereGeometry(0.0045, 32, 32), new THREE.MeshStandardMaterial({ color: 0x777777, roughness: 0.5, metalness: 0.7 }));
nose.scale.set(1.4, 1.15, 1.0);
nose.position.y = 0.078;
nose.position.z = 0.04;
nose.position.x = 0.001;
innerMesh.add(rightEye);
innerMesh.add(leftEye);
innerMesh.add(nose);
obj.add(innerMesh, instancedMirrorMesh);
return obj;
}
function createScene(geometryIdx) {
const scene = new THREE.Scene();
scene.position.y = -0.25;
const element = document.createElement('div');
element.className = 'overlay-block';
scene.userData.element = element;
container.appendChild(element);
const camera = new THREE.PerspectiveCamera(22, scene.userData.element.clientWidth / scene.userData.element.clientHeight, 0.5, 10);
// camera.position.y = 3
// camera.position.x = 0;
// camera.position.z = 2;
// camera.up.set(0 , 0, 0 );
let offset = window.innerHeight < 768 ? new THREE.Vector3( 0, 0.1, -2.0 ) : new THREE.Vector3( 0, 0.1, -1.5 )
camera.position.addVectors( scene.position, offset )
scene.userData.camera = camera;
const controls = new OrbitControls(scene.userData.camera, scene.userData.element);
controls.minDistance = 1;
controls.maxDistance = 10;
controls.autoRotate = true;
controls.autoRotateSpeed = 3;
controls.enableZoom = true;
controls.enableDamping = true;
controls.enableRotate = true;
scene.userData.controls = controls;
scene.add(createDiscoBall(geometryIdx));
// var hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.61);
// hemiLight.position.set(0, -5, 0);
// scene.add(hemiLight);
var dirLight = new THREE.DirectionalLight(0xffffff, 1.0);
dirLight.position.set(-5, 12, 5);
// dirLight.castShadow = true;
// dirLight.shadow.mapSize = new THREE.Vector2(5524, 5524);
scene.add(dirLight);
return scene;
}
function render() {
renderer.setScissorTest(true);
const delta = .04 * clock.getDelta();
scenes.forEach((scene, idx) => {
const rect = scene.userData.element.getBoundingClientRect();
const width = rect.right - rect.left;
const height = rect.bottom - rect.top;
const left = rect.left;
const bottom = renderer.domElement.clientHeight - rect.bottom;
renderer.setViewport(left, bottom, width, height * 2);
renderer.setScissor(left, bottom, width, height * 2);
scene.userData.controls.update();
// if (idx === 0) {
// scene.children[0].rotateX(delta);
// scene.children[0].rotateZ(delta);
// } else if (idx === 2) {
// scene.children[0].rotateZ(-3 * delta);
// }
// scene.children[0].position.x = Math.cos(clock.getElapsedTime() / 1 + (Math.sin(idx) * 5)) * 0.05;
// scene.children[0].rotation.z += (Math.cos(clock.getElapsedTime() / 10 + idx / 10) + 1) / 2 * Math.PI / 40;
// scene.children[0].rotation.z += delta * Math.cos(idx)
const scale = window.innerWidth < 768 ? 1 : 1.75;
scene.children[0].scale.x = scale;
scene.children[0].scale.y = scale;
scene.children[0].scale.z = scale;
scene.children[0].rotation.y = Math.PI;
// scene.children[0].position.z = 0.
// scene.children[0].rotation.x = (-Math.PI/2);
// if (idx === 0) {
// scene.children[0].rotateZ(16 * delta);
// scene.children[0].rotateY(6 * delta);
// } else if (idx === 1) {
// scene.children[0].rotateY(-10 * delta);
// scene.children[0].rotateX(-6 * delta);
// } else if (idx === 2) {
// scene.children[0].rotateX(-7 * delta);
// } else if (idx === 3) {
// scene.children[0].rotateY(12 * delta);
// scene.children[0].rotateZ(3 * delta);
// }
// scene.children[0].position.z = Math.sin(delta) * 0.1;
renderer.render(scene, scene.userData.camera);
});
requestAnimationFrame(render);
}
function updateSceneSize() {
scenes.forEach(scene => {
scene.userData.camera.aspect = scene.userData.element.clientWidth / (scene.userData.element.clientHeight * 2);
scene.userData.camera.updateProjectionMatrix();
// scene.position.z = 0.17;
// scene.rotation.x = -Math.PI / 2;
});
renderer.setSize(container.clientWidth, container.clientHeight);
}
};