-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
189 lines (158 loc) · 5.14 KB
/
main.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
'use strict';
import './style.css';
import * as dat from 'dat.gui';
import * as THREE from 'three';
import gsap from 'https://cdn.skypack.dev/gsap';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
const mouse = {
x: undefined,
y: undefined,
};
// dat.GUI
const gui = new dat.GUI();
const world = {
plane: {
width: 400,
height: 400,
widthSegments: 50,
heightSegments: 50,
},
rotateSpeed: 1,
};
const randomValues = [];
const genPlane = () => {
planeMesh.geometry.dispose();
planeMesh.geometry = new THREE.PlaneGeometry(
world.plane.width,
world.plane.height,
world.plane.widthSegments,
world.plane.heightSegments
);
const planePosition = planeMesh.geometry.attributes.position;
const { array } = planePosition;
const arrayLth = array.length;
for (let i = 0; i < arrayLth; i++) {
if (i % 3 === 0) {
const x = array[i];
const y = array[i + 1];
const z = array[i + 2];
array[i] = x + (Math.random() - 0.5) * 3;
array[i + 1] = y + (Math.random() - 0.5) * 3;
array[i + 2] = z + (Math.random() - 0.5) * 3;
}
randomValues.push(Math.random() * Math.PI * 2);
}
planePosition.randomValues = randomValues;
planePosition.originalPosition = planePosition.array;
const colors = [];
for (let i = 0; i < planePosition.count; i++) colors.push(0, 0.19, 0.4);
planeMesh.geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3));
};
gui.add(world.plane, 'width', 1, 500).onChange(genPlane);
gui.add(world.plane, 'height', 1, 500).onChange(genPlane);
gui.add(world.plane, 'widthSegments', 1, 100).onChange(genPlane);
gui.add(world.plane, 'heightSegments', 1, 100).onChange(genPlane);
gui.add(world, 'rotateSpeed', 0, 4).onChange(() => (controls.rotateSpeed = world.rotateSpeed));
// Event Handle
window.onmousemove = (e) => {
mouse.x = (e.clientX / innerWidth) * 2 - 1;
mouse.y = -(e.clientY / innerHeight) * 2 + 1;
};
// ThreeJS Handle
const raycaster = new THREE.Raycaster();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, innerWidth / innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
const controls = new OrbitControls(camera, renderer.domElement);
camera.position.z = 20;
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(devicePixelRatio);
document.body.appendChild(renderer.domElement);
// Plane Handle
const planeGeometry = new THREE.PlaneGeometry(
world.plane.width,
world.plane.height,
world.plane.widthSegments,
world.plane.heightSegments
);
const planeMaterial = new THREE.MeshPhongMaterial({
// color: 0xff0000,
side: THREE.DoubleSide,
flatShading: THREE.FlatShading,
vertexColors: true,
});
const planeMesh = new THREE.Mesh(planeGeometry, planeMaterial);
scene.add(planeMesh);
genPlane();
// Light
const light = new THREE.DirectionalLight(0xffffff, 1);
const backLight = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(0, -1, 1);
backLight.position.set(0, 0, -1);
scene.add(light);
scene.add(backLight);
// Animation Loop
let frame = 0;
const animation = () => {
requestAnimationFrame(animation);
renderer.render(scene, camera);
raycaster.setFromCamera(mouse, camera);
frame += 0.01;
const { array, originalPosition, randomValues } = planeMesh.geometry.attributes.position;
for (let i = 0; i < array.length; i += 3) {
// x
array[i] = originalPosition[i] + Math.cos(frame + randomValues[i]) * 0.01;
// y
array[i + 1] = originalPosition[i + 1] + Math.sin(frame + randomValues[i + 1]) * 0.001;
}
planeMesh.geometry.attributes.position.needsUpdate = true;
const intersects = raycaster.intersectObject(planeMesh);
if (intersects.length > 0) {
const { color } = intersects[0].object.geometry.attributes;
// vertice 1
color.setX(intersects[0].face.a, 0.1);
color.setY(intersects[0].face.a, 0.5);
color.setZ(intersects[0].face.a, 1);
// vertice 2
color.setX(intersects[0].face.b, 0.1);
color.setY(intersects[0].face.b, 0.5);
color.setZ(intersects[0].face.b, 1);
// vertice 3
color.setX(intersects[0].face.c, 0.1);
color.setY(intersects[0].face.c, 0.5);
color.setZ(intersects[0].face.c, 1);
intersects[0].object.geometry.attributes.color.needsUpdate = true;
const initialColor = {
r: 0,
g: 0.19,
b: 0.4,
};
const hoverColor = {
r: 0.1,
g: 0.5,
b: 1,
};
gsap.to(hoverColor, {
r: initialColor.r,
g: initialColor.g,
b: initialColor.b,
duration: 1,
onUpdate: () => {
// vertice 1
color.setX(intersects[0].face.a, hoverColor.r);
color.setY(intersects[0].face.a, hoverColor.g);
color.setZ(intersects[0].face.a, hoverColor.b);
// vertice 2
color.setX(intersects[0].face.b, hoverColor.r);
color.setY(intersects[0].face.b, hoverColor.g);
color.setZ(intersects[0].face.b, hoverColor.b);
// vertice 3
color.setX(intersects[0].face.c, hoverColor.r);
color.setY(intersects[0].face.c, hoverColor.g);
color.setZ(intersects[0].face.c, hoverColor.b);
color.needsUpdate = true;
},
});
}
};
animation();