-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-post-processing.html
More file actions
505 lines (436 loc) · 18.6 KB
/
Copy path08-post-processing.html
File metadata and controls
505 lines (436 loc) · 18.6 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post-Processing Effects Demo</title>
<style>
body { margin: 0; overflow: hidden; font-family: Arial, sans-serif; }
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0,0,0,0.7);
padding: 15px;
border-radius: 5px;
font-size: 14px;
max-width: 300px;
}
#controls {
position: absolute;
top: 10px;
right: 10px;
background: rgba(0,0,0,0.7);
padding: 15px;
border-radius: 5px;
color: white;
max-width: 250px;
}
.control-group { margin: 10px 0; }
label { display: block; margin-bottom: 5px; font-size: 12px; }
input[type="range"] { width: 100%; }
button {
margin: 2px;
padding: 5px 10px;
background: #444;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 12px;
}
button:hover { background: #666; }
button.active { background: #007acc; }
canvas { display: block; }
</style>
</head>
<body>
<div id="info">
<strong>Post-Processing Effects Demo</strong><br>
Real-time image effects pipeline<br>
- Bloom glow effects<br>
- Chromatic aberration<br>
- Film grain and vignette<br>
- Color grading and LUTs
</div>
<div id="controls">
<div class="control-group">
<button onclick="setEffectPreset('none')" class="active">Original</button>
<button onclick="setEffectPreset('cinematic')">Cinematic</button>
<button onclick="setEffectPreset('vintage')">Vintage</button>
<button onclick="setEffectPreset('neon')">Neon</button>
<button onclick="setEffectPreset('cartoon')">Cartoon</button>
</div>
<div class="control-group">
<label>Bloom Intensity: <span id="bloom-val">0.8</span></label>
<input type="range" id="bloom-intensity" min="0" max="2" step="0.1" value="0.8">
</div>
<div class="control-group">
<label>Contrast: <span id="contrast-val">1.2</span></label>
<input type="range" id="contrast" min="0.5" max="2" step="0.1" value="1.2">
</div>
<div class="control-group">
<label>Saturation: <span id="saturation-val">1.1</span></label>
<input type="range" id="saturation" min="0" max="2" step="0.1" value="1.1">
</div>
<div class="control-group">
<button onclick="toggleEffect('bloom')">Toggle Bloom</button>
<button onclick="toggleEffect('chromatic')">Toggle CA</button>
<button onclick="toggleEffect('film')">Toggle Film</button>
</div>
</div>
<script type="importmap">
{
"imports": {
"three": "../node_modules/three/build/three.module.js",
"three/addons/": "../node_modules/three/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
// Scene setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a0a0a);
// Camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(10, 8, 10);
camera.lookAt(0, 0, 0);
// Renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
document.body.appendChild(renderer.domElement);
// Controls
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
// Post-processing setup
const composer = new EffectComposer(renderer);
const renderPass = new RenderPass(scene, camera);
composer.addPass(renderPass);
// Custom shaders
const ChromaticAberrationShader = {
uniforms: {
'tDiffuse': { value: null },
'amount': { value: 0.005 }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform sampler2D tDiffuse;
uniform float amount;
varying vec2 vUv;
void main() {
vec2 offset = amount * vec2(1.0, 1.0);
vec4 cr = texture2D(tDiffuse, vUv + offset);
vec4 cga = texture2D(tDiffuse, vUv);
vec4 cb = texture2D(tDiffuse, vUv - offset);
gl_FragColor = vec4(cr.r, cga.g, cb.b, cga.a);
}
`
};
const FilmShader = {
uniforms: {
'tDiffuse': { value: null },
'time': { value: 0.0 },
'intensity': { value: 0.3 },
'vignette': { value: 0.3 },
'grainScale': { value: 1.0 },
'grainAmount': { value: 0.05 }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform sampler2D tDiffuse;
uniform float time;
uniform float intensity;
uniform float vignette;
uniform float grainScale;
uniform float grainAmount;
varying vec2 vUv;
float rand(vec2 co) {
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
}
void main() {
vec4 color = texture2D(tDiffuse, vUv);
// Vignette
vec2 pos = (vUv - 0.5) * 2.0;
float vignetteFactor = 1.0 - dot(pos, pos) * vignette;
// Film grain
float grain = rand(vUv * time * grainScale) * grainAmount;
// Scanlines
float scanline = sin(vUv.y * 800.0) * 0.02;
color.rgb += grain + scanline;
color.rgb *= vignetteFactor;
color.rgb *= intensity;
gl_FragColor = color;
}
`
};
const ColorGradingShader = {
uniforms: {
'tDiffuse': { value: null },
'contrast': { value: 1.2 },
'saturation': { value: 1.1 },
'brightness': { value: 0.0 },
'hueShift': { value: 0.0 },
'temperature': { value: 0.0 }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform sampler2D tDiffuse;
uniform float contrast;
uniform float saturation;
uniform float brightness;
uniform float hueShift;
uniform float temperature;
varying vec2 vUv;
vec3 rgb2hsv(vec3 c) {
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main() {
vec4 color = texture2D(tDiffuse, vUv);
// Adjust brightness
color.rgb += brightness;
// Adjust contrast
color.rgb = (color.rgb - 0.5) * contrast + 0.5;
// Convert to HSV for saturation and hue shift
vec3 hsv = rgb2hsv(color.rgb);
hsv.y *= saturation; // Saturation
hsv.x += hueShift; // Hue shift
hsv.x = fract(hsv.x); // Wrap hue
color.rgb = hsv2rgb(hsv);
// Temperature adjustment
color.r += temperature * 0.1;
color.b -= temperature * 0.1;
gl_FragColor = color;
}
`
};
// Create post-processing passes
const bloomPass = new UnrealBloomPass(
new THREE.Vector2(window.innerWidth, window.innerHeight),
0.8, 0.4, 0.85
);
composer.addPass(bloomPass);
const chromaticPass = new ShaderPass(ChromaticAberrationShader);
chromaticPass.uniforms['amount'].value = 0.005;
composer.addPass(chromaticPass);
const filmPass = new ShaderPass(FilmShader);
filmPass.uniforms['time'].value = 0.0;
composer.addPass(filmPass);
const colorGradingPass = new ShaderPass(ColorGradingShader);
colorGradingPass.uniforms['contrast'].value = 1.2;
colorGradingPass.uniforms['saturation'].value = 1.1;
composer.addPass(colorGradingPass);
// Scene content - glowing objects
const objects = [];
const geometries = [
new THREE.SphereGeometry(1, 32, 16),
new THREE.TorusGeometry(1, 0.3, 16, 100),
new THREE.OctahedronGeometry(1),
new THREE.DodecahedronGeometry(0.8)
];
const colors = [0xff6b35, 0x4a90e2, 0x2ecc71, 0xf39c12, 0x9b59b6, 0xe74c3c];
geometries.forEach((geometry, i) => {
const material = new THREE.MeshStandardMaterial({
color: colors[i % colors.length],
emissive: colors[i % colors.length],
emissiveIntensity: 0.3,
metalness: 0.8,
roughness: 0.2
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(
Math.sin(i * Math.PI / 2) * 6,
(i % 2) * 3,
Math.cos(i * Math.PI / 2) * 6
);
mesh.castShadow = true;
scene.add(mesh);
objects.push(mesh);
});
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.4);
scene.add(ambientLight);
const pointLight1 = new THREE.PointLight(0xffffff, 1.0, 50);
pointLight1.position.set(10, 10, 10);
scene.add(pointLight1);
const pointLight2 = new THREE.PointLight(0xff00ff, 0.5, 30);
pointLight2.position.set(-10, 5, -10);
scene.add(pointLight2);
// Post-processing manager
class PostProcessingManager {
constructor() {
this.effects = {
bloom: true,
chromatic: false,
film: false
};
this.currentPreset = 'none';
}
setEffectPreset(preset) {
this.currentPreset = preset;
switch(preset) {
case 'none':
this.setBloomIntensity(0);
this.setContrast(1.0);
this.setSaturation(1.0);
this.enableEffect('chromatic', false);
this.enableEffect('film', false);
break;
case 'cinematic':
this.setBloomIntensity(1.2);
this.setContrast(1.3);
this.setSaturation(0.9);
this.enableEffect('chromatic', true);
this.enableEffect('film', true);
filmPass.uniforms['intensity'].value = 0.8;
break;
case 'vintage':
this.setBloomIntensity(0.5);
this.setContrast(0.8);
this.setSaturation(1.4);
this.enableEffect('chromatic', false);
this.enableEffect('film', true);
filmPass.uniforms['intensity'].value = 0.9;
filmPass.uniforms['vignette'].value = 0.5;
filmPass.uniforms['grainAmount'].value = 0.1;
break;
case 'neon':
this.setBloomIntensity(2.0);
this.setContrast(1.5);
this.setSaturation(2.0);
this.enableEffect('chromatic', true);
this.enableEffect('film', false);
chromaticPass.uniforms['amount'].value = 0.01;
break;
case 'cartoon':
this.setBloomIntensity(0.3);
this.setContrast(1.8);
this.setSaturation(0.7);
this.enableEffect('chromatic', false);
this.enableEffect('film', false);
break;
}
// Update button states
document.querySelectorAll('button').forEach(btn => {
btn.classList.remove('active');
});
event.target.classList.add('active');
}
setBloomIntensity(intensity) {
bloomPass.strength = intensity;
document.getElementById('bloom-val').textContent = intensity.toFixed(1);
}
setContrast(contrast) {
colorGradingPass.uniforms['contrast'].value = contrast;
document.getElementById('contrast-val').textContent = contrast.toFixed(1);
}
setSaturation(saturation) {
colorGradingPass.uniforms['saturation'].value = saturation;
document.getElementById('saturation-val').textContent = saturation.toFixed(1);
}
enableEffect(effectName, enabled) {
this.effects[effectName] = enabled;
const passMap = {
'chromatic': chromaticPass,
'film': filmPass,
'bloom': bloomPass
};
if (passMap[effectName]) {
passMap[effectName].enabled = enabled;
}
}
}
const postProcessingManager = new PostProcessingManager();
// Global functions
window.setEffectPreset = (preset) => {
postProcessingManager.setEffectPreset(preset);
};
window.toggleEffect = (effectName) => {
postProcessingManager.enableEffect(
effectName,
!postProcessingManager.effects[effectName]
);
};
// Control bindings
const bloomSlider = document.getElementById('bloom-intensity');
bloomSlider.addEventListener('input', (e) => {
postProcessingManager.setBloomIntensity(parseFloat(e.target.value));
});
const contrastSlider = document.getElementById('contrast');
contrastSlider.addEventListener('input', (e) => {
postProcessingManager.setContrast(parseFloat(e.target.value));
});
const saturationSlider = document.getElementById('saturation');
saturationSlider.addEventListener('input', (e) => {
postProcessingManager.setSaturation(parseFloat(e.target.value));
});
// Animation loop
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsedTime = clock.getElapsedTime();
// Update film grain time
filmPass.uniforms['time'].value = elapsedTime;
// Rotate objects
objects.forEach((obj, index) => {
obj.rotation.x = elapsedTime * (0.5 + index * 0.1);
obj.rotation.y = elapsedTime * (0.3 + index * 0.1);
});
// Move lights
pointLight1.position.x = Math.sin(elapsedTime * 0.5) * 15;
pointLight1.position.z = Math.cos(elapsedTime * 0.5) * 15;
pointLight2.position.x = Math.sin(elapsedTime * 0.8 + Math.PI / 2) * 12;
pointLight2.position.z = Math.cos(elapsedTime * 0.8 + Math.PI / 2) * 12;
controls.update();
composer.render();
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
composer.setSize(window.innerWidth, window.innerHeight);
bloomPass.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>