<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Film Post Effect</title>
<script src="../../three-part/threejs/three.js"></script>
<script src="../../three-part/threejs/post/EffectComposer.js"></script>
<script src="../../three-part/threejs/post/FilmPass.js"></script>
<script src="../../three-part/threejs/post/FilmShader.js"></script>
<script src="../../three-part/threejs/post/RenderPass.js"></script>
<script src="../../three-part/threejs/post/ShaderPass.js"></script>
<script src="../../three-part/threejs/post/CopyShader.js"></script>
<script src="../../three-part/utils/stats.min.js"></script>
<script src="../../three-part/utils/dat.gui.min.js"></script>
<script src="../controls/TrackballControls.js"></script>
<script src="../util/util.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
init();
function init() {
// show FPS
let stats = initStats();
// resize
window.addEventListener('resize', onResize, false);
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.x = -30;
camera.position.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position);
let renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0x000000));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.getElementById("container").appendChild(renderer.domElement);
// init trackball control
let trackballControls = initTrackballControls(camera, renderer);
let clock = new THREE.Clock();
let textureLoader = new THREE.TextureLoader();
let sphereMaterial = new THREE.MeshPhongMaterial({
map: textureLoader.load("../assets/textures/earth/Earth.png"),
normalMap: textureLoader.load("../assets/textures/earth/EarthNormal.png"),
specularMap: textureLoader.load("../assets/textures/earth/EarthSpec.png"),
normalScale: new THREE.Vector2(6,6)
});
let sphereGeometry = new THREE.SphereGeometry(10, 50, 50);
let sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
scene.add(sphere);
let ambientLight = new THREE.AmbientLight(0x343434);
scene.add(ambientLight);
let spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(-10, 30, 40);
spotLight.shadow.mapSize.width = 2048;
spotLight.shadow.mapSize.height = 2048;
spotLight.shadow.camera.fov = 15;
spotLight.castShadow = true;
spotLight.decay = 2;
spotLight.penumbra = 0.05;
scene.add(spotLight);
let renderPass = new THREE.RenderPass(scene, camera);
let effectFilm = new THREE.FilmPass(0.8, 0.325, 256, false);
let composer = new THREE.EffectComposer(renderer);
composer.addPass(renderPass);
composer.addPass(effectFilm);
// attributes which can be modified in GUI
const controls = {
"grayScale" : effectFilm.uniforms.grayscale.value,
"noiseIntensity" : effectFilm.uniforms.nIntensity.value,
"scanlinesIntensity" : effectFilm.uniforms.sIntensity.value,
"scanlinesCount" : effectFilm.uniforms.sCount.value
};
// init GUI
initGUI();
renderScene();
function initGUI(){
let gui = new dat.GUI();
gui.add(controls, "grayScale").onChange(function(e){
effectFilm.uniforms.grayscale.value = e;
});
gui.add(controls, "noiseIntensity", 0, 1, 0.01).onChange(function(e){
effectFilm.uniforms.nIntensity.value = e;
});
gui.add(controls, "scanlinesIntensity", 0, 1, 0.01).onChange(function(e){
effectFilm.uniforms.sIntensity.value = e;
});
gui.add(controls, "scanlinesCount", 0, 500, 1).onChange(function(e){
effectFilm.uniforms.sCount.value = e;
});
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function renderScene(){
trackballControls.update(clock.getDelta());
stats.update();
sphere.rotation.y -= 0.01;
requestAnimationFrame(renderScene);
composer.render(clock.getDelta());
}
}
</script>
</body>
</html>
运行结果:
网友评论