I have a simple 3d obj viewer that I try to add SSAO with.
Normal rendering works just fine but when I add SSAO (composer renderer), the whole screen turns all white.
I’ve tried making the value of light smaller, radius smaller, etc, but nothing seems to work. Kindly seeking your advise. Many thanks! Below is my code
import * as THREE from ‘three’;
import { OrbitControls } from ‘three/examples/jsm/controls/OrbitControls.js’;
import { OBJLoader } from ‘three/examples/jsm/loaders/OBJLoader.js’;
import { EffectComposer } from ‘three/examples/jsm/postprocessing/EffectComposer.js’;
import { RenderPass } from ‘three/examples/jsm/postprocessing/RenderPass.js’;
import { SSAOPass } from ‘three/examples/jsm/postprocessing/SSAOPass.js’;// Scene, Camera, Renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 500, 800, 1000 );
camera.lookAt( 0, 0, 0 );const renderer = new THREE.WebGLRenderer();
//renderer.outputEncoding = THREE.sRGBEncoding
renderer.setClearColor(0x000000, 1); // Set black background
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true; // Enable shadow mapping
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // Choose shadow type (soft shadows)document.body.appendChild(renderer.domElement);
// Orbit Controls
const controls = new OrbitControls(camera, renderer.domElement);// Lighting
const ambientLight = new THREE.AmbientLight( 0xffffff, 0.6 );
scene.add( ambientLight );const pointLight = new THREE.PointLight( 0xffffff, 1 );
camera.add( pointLight );const directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
//directionalLight.position.set( 1, 0.75, 0.5 ).normalize();
directionalLight.position.set(5, 5, 5); // Position the light
directionalLight.castShadow = true; // Enable shadow casting
directionalLight.shadow.radius = 8;// Configure shadow settings for the light
directionalLight.shadow.mapSize.width = 1024; // Higher value improves shadow quality
directionalLight.shadow.mapSize.height = 1024;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 20;scene.add( directionalLight );
const composer = new EffectComposer(renderer);
composer.addPass(new RenderPass(scene, camera));const ssaoPass = new SSAOPass(scene, camera, window.innerWidth, window.innerHeight);
ssaoPass.kernelRadius = 4;
ssaoPass.minDistance = 0.01;
ssaoPass.maxDistance = 0.05;
composer.addPass(ssaoPass);// Enable tone mapping and gamma correction
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.0; // Adjust as needed
renderer.outputEncoding = THREE.sRGBEncoding;// Load OBJ Model
const loader = new OBJLoader();
loader.load(
‘//some url to obj model’,
(object) => {const geometry = object.children[0].geometry; geometry.rotateY(Math.PI); geometry.scale( 0.02,0.02,0.02 ); geometry.translate(-320, 60, -100); ////all white, not shadow related //const material = new THREE.MeshBasicMaterial({ color: 0xffffff }); const material = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.9, // Higher roughness for a more diffuse appearance metalness: 0 // Non-metallic for a purely diffuse look }); const mesh = new THREE.Mesh(geometry, material); mesh.castShadow = true; // Enable shadow casting scene.add( mesh ); //scene.add(object);
},
(xhr) => {
console.log((xhr.loaded / xhr.total) * 100 + ‘% loaded’);
},
(error) => {
console.error(‘An error occurred:’, error);
}
);// Animation Loop
function animate() {
requestAnimationFrame(animate);
controls.update(); // Update OrbitControls
//renderer.render(scene, camera);
composer.render();
}
animate();// Handle Window Resize
window.addEventListener(‘resize’, () => {renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();composer.setSize(window.innerWidth, window.innerHeight);
ssaoPass.setSize(window.innerWidth, window.innerHeight);
});