Postprocess Ssao
Runnable three.js example: Postprocess Ssao. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
说明:屏幕空间环境光遮蔽按深度与法线估算缝隙遮挡,墙角与物体接触处变暗
核心逻辑:EffectComposer 串 RenderPass → SSAOPass → OutputPass
依赖:three.js 0.160.0、OrbitControls、SSAOPass · jsDelivr CDN(ESM + importmap)
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>环境光遮蔽</title>
<style>
html,body{height:100%;margin:0;background:#0a0f14}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600;font-variant-numeric:tabular-nums}
.bar input[type=range]{width:130px;accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:5px;cursor:pointer;
border:1px solid #303845;background:#1d2531;color:#c8d2e0}
.bar button:hover{background:#253040}
.bar button.on{background:#1f3a5c;border-color:#2f5c8f;color:#cfe4ff}
.bar .sp{flex:1 1 auto}
#stage{flex:1 1 auto;min-height:0;position:relative}
#stage canvas{display:block}
</style>
</head>
<body>
<div class="bar">
<span>采样半径 <b id="kv">8</b></span>
<label>kernelRadius <input type="range" id="kr" min="2" max="24" step="1" value="8"></label>
<button id="m0" class="on">成片</button>
<button id="m1">仅遮蔽</button>
<span class="sp"></span>
<span>拖拽旋转 · 滚轮缩放</span>
</div>
<div id="stage"></div>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/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 { SSAOPass } from 'three/addons/postprocessing/SSAOPass.js';
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
const stage = document.getElementById('stage');
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(stage.clientWidth, stage.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a0f14);
scene.fog = new THREE.Fog(0x0a0f14, 40, 80);
const camera = new THREE.PerspectiveCamera(45, stage.clientWidth / stage.clientHeight, 0.1, 200);
camera.position.set(16, 12, 19);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1.5, 0);
controls.enableDamping = true;
controls.maxPolarAngle = Math.PI * 0.49;
controls.update();
scene.add(new THREE.HemisphereLight(0xc6dcf2, 0x161d24, 1.1));
const sun = new THREE.DirectionalLight(0xffeacb, 1.8);
sun.position.set(-14, 20, 10);
scene.add(sun);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({ color: 0x4a525a, roughness: 0.95 })
);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
/* 高低错落的体块:缝隙越多,环境光遮蔽越明显 */
function hash(n) {
const x = Math.sin(n * 39.1 + 4.4) * 43758.5453;
return x - Math.floor(x);
}
for (let i = 0; i < 34; i++) {
const w = 1.2 + hash(i) * 2.6;
const h = 1.4 + Math.pow(hash(i + 2), 1.4) * 7;
const x = (hash(i + 4) - 0.5) * 16;
const z = (hash(i + 8) - 0.5) * 16;
const b = new THREE.Mesh(
new THREE.BoxGeometry(w, h, w),
new THREE.MeshStandardMaterial({
color: new THREE.Color().setHSL(0.1 + hash(i + 6) * 0.5, 0.14, 0.5 + hash(i + 10) * 0.2),
roughness: 0.7,
metalness: 0.06
})
);
b.position.set(x, h / 2, z);
b.rotation.y = hash(i + 12) * Math.PI;
scene.add(b);
}
const composer = new EffectComposer(renderer);
composer.setSize(stage.clientWidth, stage.clientHeight);
composer.addPass(new RenderPass(scene, camera));
const ssaoPass = new SSAOPass(scene, camera, stage.clientWidth, stage.clientHeight);
ssaoPass.kernelRadius = 8;
ssaoPass.minDistance = 0.005;
ssaoPass.maxDistance = 0.12;
composer.addPass(ssaoPass);
composer.addPass(new OutputPass());
const krEl = document.getElementById('kr');
const kvEl = document.getElementById('kv');
krEl.addEventListener('input', function () {
const v = Number(krEl.value);
ssaoPass.kernelRadius = v;
kvEl.textContent = String(v);
});
const m0 = document.getElementById('m0');
const m1 = document.getElementById('m1');
m0.addEventListener('click', function () {
ssaoPass.output = SSAOPass.OUTPUT.Default;
m0.classList.add('on');
m1.classList.remove('on');
});
m1.addEventListener('click', function () {
ssaoPass.output = SSAOPass.OUTPUT.SSAO;
m1.classList.add('on');
m0.classList.remove('on');
});
renderer.setAnimationLoop(function () {
controls.update();
composer.render();
});
window.addEventListener('resize', function () {
const w = stage.clientWidth;
const h = stage.clientHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
composer.setSize(w, h);
ssaoPass.setSize(w, h);
});
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.