-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
238 lines (208 loc) · 6.69 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
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
import "./style.css";
import * as THREE from "three";
import { Pane } from "tweakpane";
import Stats from "stats.js";
import { CinematicRenderer } from "@renderers/CinematicRenderer";
import { CinematicCamera } from "@cameras/CinematicCamera";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { HDRIEnvironment } from "@components/environments/HDRIEnvironment";
import { FOG_PRESETS } from "@presets/fogPresets"; // Ensure path is correct
import {
CAMERA_PRESETS,
CAMERA_MOVEMENT_PRESETS,
CAMERA_COMPATIBILITY,
} from "@presets/cameraPresets"; // Adjust the path if necessary
// Initialize Tweakpane
const pane = new Pane();
// Fog parameters with initial description
const fogParams = {
enabled: true,
preset: "SOFT_BLUE",
color: FOG_PRESETS.SOFT_BLUE.color,
density: FOG_PRESETS.SOFT_BLUE.density,
description: FOG_PRESETS.SOFT_BLUE.description, // Initial description
};
// Fog controls within a folder
const fogFolder = pane.addFolder({ title: "Fog Controls" });
// Dropdown options for fog presets
const options = Object.fromEntries(
Object.keys(FOG_PRESETS).map((key) => [key, key]),
);
// Enable/disable fog
fogFolder
.addBinding(fogParams, "enabled", { label: "Enable Fog" })
.on("change", ({ value }) => {
scene.fog = value
? new THREE.FogExp2(fogParams.color, fogParams.density)
: null;
});
// Fog preset dropdown with description
fogFolder
.addBinding(fogParams, "preset", {
options: options,
label: "Preset",
})
.on("change", ({ value }) => {
const selectedPreset = FOG_PRESETS[value];
fogParams.color = selectedPreset.color;
fogParams.density = selectedPreset.density;
fogParams.description = selectedPreset.description; // Update description
if (scene.fog) {
scene.fog.color.set(fogParams.color);
scene.fog.density = fogParams.density;
}
pane.refresh(); // Refresh the pane to update description
});
// Display the description of the selected fog preset
fogFolder.addBinding(fogParams, "description", {
view: "text", // Set view to text for description
label: "Description",
readonly: true,
});
// Camera movement parameters with `shake` properties initially set to null
const movementParams = {
preset: "STATIC",
description: CAMERA_MOVEMENT_PRESETS.STATIC.name,
movement: CAMERA_MOVEMENT_PRESETS.STATIC.movement,
stabilization: CAMERA_MOVEMENT_PRESETS.STATIC.stabilization,
shake: { intensity: null, frequency: null }, // Initialize shake properties
};
const movementFolder = pane.addFolder({ title: "Camera Movement" });
const movementOptions = Object.fromEntries(
Object.keys(CAMERA_MOVEMENT_PRESETS).map((key) => [
key,
CAMERA_MOVEMENT_PRESETS[key].name,
]),
);
// Dynamic UI elements for `shake`
let shakeIntensityBinding = null;
let shakeFrequencyBinding = null;
// Update movement parameters based on preset selection
movementFolder
.addBinding(movementParams, "preset", {
options: movementOptions,
label: "Movement Preset",
})
.on("change", ({ value }) => {
const selectedPreset = CAMERA_MOVEMENT_PRESETS[value];
// Update basic parameters
movementParams.description = selectedPreset.name;
movementParams.movement = selectedPreset.movement;
movementParams.stabilization = selectedPreset.stabilization;
movementParams.shake = selectedPreset.shake || {
intensity: null,
frequency: null,
}; // Handle shake conditionally
// Remove previous `shake` bindings if they exist
if (shakeIntensityBinding) {
movementFolder.remove(shakeIntensityBinding);
shakeIntensityBinding = null;
}
if (shakeFrequencyBinding) {
movementFolder.remove(shakeFrequencyBinding);
shakeFrequencyBinding = null;
}
// Conditionally add `shake` controls if shake exists in the selected preset
if (selectedPreset.shake) {
shakeIntensityBinding = movementFolder.addBinding(
movementParams.shake,
"intensity",
{
label: "Shake Intensity",
min: 0,
max: 1,
},
);
shakeFrequencyBinding = movementFolder.addBinding(
movementParams.shake,
"frequency",
{
label: "Shake Frequency",
min: 0,
max: 10,
},
);
}
pane.refresh(); // Refresh to apply changes
});
// Add static bindings for non-conditional fields
movementFolder.addBinding(movementParams, "description", {
view: "text",
label: "Description",
readonly: true,
});
movementFolder.addBinding(movementParams, "movement", { label: "Movement" });
movementFolder.addBinding(movementParams, "stabilization", {
label: "Stabilization",
});
// Initialize Scene and Renderer
const scene = new THREE.Scene();
const canvas = document.querySelector("#webgl");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.0;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// Camera
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000,
);
camera.position.set(0, 2, 5);
scene.add(camera);
// Orbit Controls
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
controls.enablePanning = true;
// Infinite Ground
const groundGeometry = new THREE.PlaneGeometry(1000, 1000);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x999999,
roughness: 0.8,
metalness: 0.2,
});
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
// Environment
const environment = new HDRIEnvironment({
path: "/src/assets/environmentMaps/hdr/kloofendal_48d_partly_cloudy_puresky_2k.hdr",
intensity: 1.0,
});
environment.load(renderer).then((envMap) => {
scene.environment = envMap;
scene.background = envMap;
});
// Fog Setup with Default Preset
// Set initial fog
scene.fog = new THREE.FogExp2(fogParams.color, fogParams.density);
// Stats
const stats = new Stats();
stats.showPanel(0);
document.body.appendChild(stats.dom);
// Animation loop
function animate() {
requestAnimationFrame(animate);
stats.begin();
controls.update();
renderer.render(scene, camera);
stats.end();
}
animate();
// Handle resize
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});