포그

Fog

카메라에서 멀어질수록 물체를 배경색에 섞어 흐릿하게 만드는 기법. 깊이감을 주고 먼 오브젝트의 팝인을 감춥니다.

다른 이름: THREE.FogTHREE.FogExp2Distance fog
···
js
import * as THREE from 'three';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const bg = 0x0c0e16;
scene.background = new THREE.Color(bg);
scene.fog = new THREE.FogExp2(bg, 0.085);
const camera = new THREE.PerspectiveCamera(55, 1, 0.1, 100);
camera.position.set(0, 1.1, 0);

const dLight = new THREE.DirectionalLight(0xffffff, 1.4);
dLight.position.set(2, 5, -3);
scene.add(dLight);
scene.add(new THREE.HemisphereLight(0xaeccff, 0x0c0e16, 2.0));

const group = new THREE.Group();
const COLS = 14;
for (let i = 0; i < COLS; i++) {
  const side = i % 2 === 0 ? -1 : 1;
  const geo = new THREE.CylinderGeometry(0.18, 0.22, 3.4, 8);
  const mat = new THREE.MeshStandardMaterial({ color: 0x5b74c9, roughness: 0.55 });
  const pillar = new THREE.Mesh(geo, mat);
  pillar.position.set(side * 1.6, 0.5, -(Math.floor(i / 2)) * 2.6);
  group.add(pillar);
}
scene.add(group);

function resize() { renderer.setSize(innerWidth, innerHeight); camera.aspect = innerWidth / innerHeight; camera.updateProjectionMatrix(); }
addEventListener('resize', resize); resize();
const totalZ = 2.6 * Math.ceil(COLS / 2);
renderer.setAnimationLoop((t) => {
  const z = (t * 0.0009) % totalZ;
  camera.position.z = -z + 6;
  renderer.render(scene, camera);
});

scene.fog에 THREE.Fog(color, near, far)를 넣으면 near 거리부터 far 거리까지 선형으로, THREE.FogExp2(color, density)를 넣으면 거리에 따라 지수적으로 물체 색과 fog color를 섞습니다. 계산은 내장 머티리얼 셰이더 안에서 자동으로 처리되므로 별도 코드가 필요 없습니다.

핵심은 fog color와 scene.background(또는 렌더러 clear color)를 반드시 같은 색으로 맞추는 것입니다. 색이 다르면 물체가 뿌옇게 사라지는 지점과 실제 배경 사이에 경계선이 보여 착시가 깨집니다.

데모는 같은 색의 원기둥 기둥들이 복도처럼 늘어선 장면에서 카메라가 계속 앞으로 나아갑니다. FogExp2로 먼 기둥은 배경 속으로 자연스럽게 사라지고, 카메라가 순간적으로 처음 위치로 돌아가는 지점도 안개가 가려줍니다.

언제 쓰나

오픈월드·터널 씬에서 먼 지오메트리의 로딩/팝인을 감추거나, 거리감과 분위기(안개·황사·심해)를 만들 때.