-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-lighting-basics.html
More file actions
468 lines (408 loc) · 17.1 KB
/
Copy path05-lighting-basics.html
File metadata and controls
468 lines (408 loc) · 17.1 KB
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>05 - Lighting Basics: Types and Effects</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: 'Courier New', monospace;
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
}
#canvas-container {
width: 100vw;
height: 100vh;
}
#info {
position: absolute;
top: 20px;
left: 20px;
color: white;
background: rgba(0, 0, 0, 0.85);
padding: 18px;
border-radius: 10px;
max-width: 300px;
line-height: 1.4;
font-size: 12px;
}
h1 {
margin: 0 0 12px 0;
font-size: 18px;
color: #f1c40f;
}
.step {
margin: 8px 0;
padding: 8px;
background: rgba(255, 255, 255, 0.1);
border-radius: 5px;
border-left: 3px solid #f1c40f;
}
.light-demo {
margin: 6px 0;
padding: 6px;
background: rgba(255, 255, 255, 0.1);
border-radius: 4px;
cursor: pointer;
border-left: 3px solid;
transition: background 0.2s;
}
.light-demo:hover { background: rgba(255, 255, 255, 0.2); }
.light-demo.ambient { border-left-color: #3498db; }
.light-demo.directional { border-left-color: #f39c12; }
.light-demo.point { border-left-color: #e74c3c; }
.light-demo.spot { border-left-color: #9b59b6; }
.light-demo.hemisphere { border-left-color: #2ecc71; }
.controls {
margin-top: 12px;
padding: 10px;
background: rgba(255, 255, 255, 0.1);
border-radius: 5px;
}
.controls button {
margin: 2px;
padding: 6px 12px;
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 4px;
color: white;
cursor: pointer;
font-family: inherit;
font-size: 11px;
}
.controls button:hover {
background: rgba(255, 255, 255, 0.3);
}
.controls button.active {
background: #f39c12;
border-color: #f39c12;
}
.light-info {
position: absolute;
bottom: 20px;
right: 20px;
color: white;
background: rgba(0, 0, 0, 0.8);
padding: 15px;
border-radius: 8px;
font-size: 11px;
line-height: 1.5;
}
code {
background: rgba(0, 0, 0, 0.5);
padding: 1px 4px;
border-radius: 3px;
color: #f1c40f;
}
</style>
</head>
<body>
<div id="info">
<h1>💡 Tutorial 05: Lighting</h1>
<div class="step">
<strong>Goal:</strong> Master different light types and their effects
</div>
<div class="step">
<strong>Light Types:</strong>
<div class="light-demo ambient" onclick="toggleLight('ambient')">
<strong>Ambient</strong> - Overall illumination<br>
<small>Like sunlight on a cloudy day</small>
</div>
<div class="light-demo directional" onclick="toggleLight('directional')">
<strong>Directional</strong> - Sun-like parallel rays<br>
<small>Parallel light, like the sun</small>
</div>
<div class="light-demo point" onclick="toggleLight('point')">
<strong>Point</strong> - Light bulb effect<br>
<small>Emits light in all directions</small>
</div>
<div class="light-demo spot" onclick="toggleLight('spot')">
<strong>Spot</strong> - Flashlight cone<br>
<small>Directional cone of light</small>
</div>
<div class="light-demo hemisphere" onclick="toggleLight('hemisphere')">
<strong>Hemisphere</strong> - Sky/ground reflection<br>
<small>Two-colored ambient light</small>
</div>
</div>
<div class="controls">
<strong>Scene Controls:</strong><br>
<button id="toggle-shadows" onclick="toggleShadows()">Toggle Shadows</button><br>
<button id="reset-lights" onclick="resetLights()">Reset Lights</button><br>
<button id="animate-lights" onclick="animateLights()" class="active">Animate Lights</button>
</div>
</div>
<div class="light-info">
<strong>Active Lights:</strong> <span id="active-lights">3</span><br>
<strong>Shadows:</strong> <span id="shadow-status">Enabled</span><br>
<strong>Animation:</strong> <span id="animation-status">Running</span>
</div>
<div id="canvas-container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
/**
* ==========================================
* TUTORIAL 05: LIGHTING BASICS
* ==========================================
*
* Lighting is crucial for creating realistic 3D scenes.
* Different light types create different moods and effects.
*
* LIGHT TYPES:
* 1. AmbientLight - Even illumination from all directions
* 2. DirectionalLight - Parallel rays (like the sun)
* 3. PointLight - Light source that emits in all directions
* 4. SpotLight - Conical beam of light
* 5. HemisphereLight - Sky/ground color mixing
*/
// ==========================================
// GLOBAL VARIABLES
// ==========================================
let scene, camera, renderer;
let lights = {};
let lightHelpers = {};
let animationId;
let animationEnabled = true;
let shadowEnabled = true;
let objects = [];
// ==========================================
// BASIC SCENE SETUP
// ==========================================
function setupScene() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x1a1a1a);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 8, 15);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputEncoding = THREE.sRGBEncoding;
document.getElementById('canvas-container').appendChild(renderer.domElement);
}
// ==========================================
// CREATE TEST OBJECTS
// ==========================================
function createTestObjects() {
// Create objects at different heights to show light falloff
const positions = [
{ x: -6, y: 0, z: 0 },
{ x: -3, y: 2, z: -2 },
{ x: 0, y: 0, z: -4 },
{ x: 3, y: 3, z: -2 },
{ x: 6, y: 1, z: 0 }
];
positions.forEach((pos, index) => {
const geometry = new THREE.SphereGeometry(0.8, 16, 16);
const material = new THREE.MeshStandardMaterial({
color: new THREE.Color().setHSL(index / positions.length, 0.7, 0.6),
roughness: 0.3,
metalness: 0.1
});
const sphere = new THREE.Mesh(geometry, material);
sphere.position.set(pos.x, pos.y, pos.z);
sphere.castShadow = true;
sphere.receiveShadow = true;
scene.add(sphere);
objects.push(sphere);
});
// Add a torus knot for complex shadows
const torusGeometry = new THREE.TorusKnotGeometry(0.5, 0.2, 100, 16);
const torusMaterial = new THREE.MeshStandardMaterial({
color: 0x8b5cf6,
roughness: 0.2,
metalness: 0.8
});
const torus = new THREE.Mesh(torusGeometry, torusMaterial);
torus.position.set(0, -2, -4);
torus.castShadow = true;
scene.add(torus);
objects.push(torus);
// Ground plane
const groundGeometry = new THREE.PlaneGeometry(20, 20);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x333333,
roughness: 0.8
});
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
}
// ==========================================
// CREATE DIFFERENT LIGHT TYPES
// ==========================================
function createLights() {
// 1. Ambient Light - Base illumination
lights.ambient = new THREE.AmbientLight(0x404040, 0.3);
scene.add(lights.ambient);
// 2. Directional Light - Sun-like
lights.directional = new THREE.DirectionalLight(0xffffff, 0.8);
lights.directional.position.set(5, 10, 5);
lights.directional.castShadow = true;
lights.directional.shadow.mapSize.width = 1024;
lights.directional.shadow.mapSize.height = 1024;
lights.directional.shadow.camera.near = 0.5;
lights.directional.shadow.camera.far = 50;
lights.directional.shadow.camera.left = -10;
lights.directional.shadow.camera.right = 10;
lights.directional.shadow.camera.top = 10;
lights.directional.shadow.camera.bottom = -10;
scene.add(lights.directional);
// 3. Point Light - Light bulb
lights.point = new THREE.PointLight(0xff6b6b, 1, 20);
lights.point.position.set(0, 5, 5);
lights.point.castShadow = true;
lights.point.shadow.mapSize.width = 512;
lights.point.shadow.mapSize.height = 512;
scene.add(lights.point);
// 4. Spot Light - Flashlight cone
lights.spot = new THREE.SpotLight(0x9b59b6, 0.8, 30, Math.PI / 6, 0.3);
lights.spot.position.set(-5, 8, 8);
lights.spot.castShadow = true;
lights.spot.shadow.mapSize.width = 512;
lights.spot.shadow.mapSize.height = 512;
scene.add(lights.spot);
lights.spot.target.position.set(0, 0, 0);
scene.add(lights.spot.target);
// 5. Hemisphere Light - Sky/ground colors
lights.hemisphere = new THREE.HemisphereLight(0x87ceeb, 0x8b4513, 0.4);
scene.add(lights.hemisphere);
// Add light helpers for visualization
createLightHelpers();
}
// ==========================================
// LIGHT HELPERS (VISUAL AIDS)
// ==========================================
function createLightHelpers() {
// Directional light helper
lightHelpers.directional = new THREE.DirectionalLightHelper(lights.directional, 1);
// Point light helper
lightHelpers.point = new THREE.PointLightHelper(lights.point, 0.5);
// Spot light helper
lightHelpers.spot = new THREE.SpotLightHelper(lights.spot);
Object.values(lightHelpers).forEach(helper => {
scene.add(helper);
});
}
// ==========================================
// LIGHT TOGGLE FUNCTIONS
// ==========================================
function toggleLight(lightType) {
const light = lights[lightType];
if (light) {
light.visible = !light.visible;
if (lightHelpers[lightType]) {
lightHelpers[lightType].visible = light.visible;
}
updateLightInfo();
}
}
// ==========================================
// SCENE CONTROL FUNCTIONS
// ==========================================
function toggleShadows() {
shadowEnabled = !shadowEnabled;
Object.values(lights).forEach(light => {
if (light.shadow) {
light.castShadow = shadowEnabled;
}
});
renderer.shadowMap.enabled = shadowEnabled;
document.getElementById('shadow-status').textContent =
shadowEnabled ? 'Enabled' : 'Disabled';
}
function resetLights() {
// Reset light positions and properties
lights.directional.position.set(5, 10, 5);
lights.point.position.set(0, 5, 5);
lights.spot.position.set(-5, 8, 8);
lights.spot.angle = Math.PI / 6;
Object.values(lights).forEach(light => {
if (!light.visible) {
light.visible = true;
}
});
updateLightInfo();
}
function animateLights() {
animationEnabled = !animationEnabled;
document.getElementById('animation-status').textContent =
animationEnabled ? 'Running' : 'Stopped';
}
// ==========================================
// UPDATE LIGHT INFORMATION
// ==========================================
function updateLightInfo() {
const activeCount = Object.values(lights).filter(light => light.visible).length;
document.getElementById('active-lights').textContent = activeCount;
}
// ==========================================
// ANIMATION LOOP
// ==========================================
function animate() {
animationId = requestAnimationFrame(animate);
const time = Date.now() * 0.001;
// Animate objects
objects.forEach((obj, index) => {
obj.rotation.x += 0.01;
obj.rotation.y += 0.02;
});
// Animate lights if enabled
if (animationEnabled) {
// Move point light in a circle
lights.point.position.x = Math.cos(time * 0.5) * 5;
lights.point.position.z = Math.sin(time * 0.5) * 5;
lights.point.position.y = 3 + Math.sin(time * 2) * 2;
// Move spot light
lights.spot.position.x = Math.sin(time * 0.3) * 8;
lights.spot.position.z = Math.cos(time * 0.3) * 8;
// Update spot light target to look at center
lights.spot.target.position.set(
Math.sin(time * 0.2) * 2,
0,
Math.cos(time * 0.2) * 2
);
lights.spot.target.updateMatrixWorld();
// Update helpers
if (lightHelpers.point) {
lightHelpers.point.update();
}
if (lightHelpers.spot) {
lightHelpers.spot.update();
}
}
renderer.render(scene, camera);
}
// ==========================================
// RESIZE HANDLING
// ==========================================
function handleResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// ==========================================
// INITIALIZATION
// ==========================================
function init() {
console.log('💡 Starting Lighting Demo...');
setupScene();
createTestObjects();
createLights();
updateLightInfo();
window.addEventListener('resize', handleResize);
animate();
console.log('✅ Lighting demo initialized!');
}
// Make functions globally available
window.toggleLight = toggleLight;
window.toggleShadows = toggleShadows;
window.resetLights = resetLights;
window.animateLights = animateLights;
init();
</script>
</body>
</html>