場景
Three.js中實現點擊按鈕添加刪除旋轉立方體:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119452536
在上面實現的基礎上,可以為整個場景添加一種霧化效果。
一個物體離得越遠,就越模糊。
霧化與否對比

注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
在定義完場景之后加上如下代碼
// 創建一個場景,它將包含我們所有的元素,如物體,攝像機和燈光 var scene = new THREE.Scene(); //添加霧化效果 scene.fog = new THREE.Fog(0xffffff,0.015,100);
注意:
參數一表示場景中霧的顏色,具體霧的顏色你可以根據具體環境設置
參數二表示受霧化影響的最近距離(以相機位置為准)
參數三1000表示受霧化影響的最遠距離(以相機位置為准)
完整示例代碼
<!DOCTYPE html>
<html>
<head>
<title>場景中添加刪除方塊</title>
<script type="text/javascript" src="./js/three.js"></script>
<script type="text/javascript" src="./js/dat.gui.js"></script>
<style>
body {
/* 將邊距設置為0,溢出設置為隱藏,以實現全屏顯示 */
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<!-- 顯示的div -->
<div id="WebGL-output">
</div>
<script type="text/javascript">
// 初始化的方法
function init() {
// 創建一個場景,它將包含我們所有的元素,如物體,攝像機和燈光
var scene = new THREE.Scene();
//添加霧化效果
scene.fog = new THREE.Fog(0xffffff,0.015,100);
// 創建一個相機,它定義了我們正在看的地方
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// 創建渲染器並設置大小
var renderer = new THREE.WebGLRenderer();
//將renderer的背景色設置為接近白色
renderer.setClearColor(new THREE.Color(0xEEEEEE,1.0));
//設置大小
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;
// 創建平面,並定義平面的尺寸
var planeGeometry = new THREE.PlaneGeometry(80, 40,1,1);
//創建一個基本材質,並設置顏色
var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc});
//把兩個對象合並到Mesh網格對象
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;
// 設置平面繞x軸旋轉90度
plane.rotation.x = -0.5 * Math.PI;
// 設置平面的坐標位置
plane.position.x = 15;
plane.position.y = 0;
plane.position.z = 0;
// 將平面添加進場景
scene.add(plane);
// 定義相機的坐標,即懸掛在場景的上方
camera.position.x = -30;
camera.position.y = 40;
camera.position.z = 30;
//為了確保相機能夠拍攝到這些物體,使用lookat函數指向場景的中心
camera.lookAt(scene.position);
// 添加環境光
var ambientLight = new THREE.AmbientLight(0x0c0c0c);
scene.add(ambientLight);
// spotLight光源是聚光燈光源,類似手電筒,會形成一種錐形效果的光,可以產生陰影
var spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(-40, 60, -10);
spotLight.castShadow = true;
scene.add(spotLight);
// 將renderer的輸出掛接到HTML終點div元素
document.getElementById("WebGL-output").appendChild(renderer.domElement);
var step = 0;
var controls = new function () {
//速度值
this.rotationSpeed = 0.02;
//立方體個數
this.numberOfObjects = scene.children.length;
this.removeCube = function () {
var allChildren = scene.children;
var lastObject = allChildren[allChildren.length - 1];
//防止移除相機和光源
if (lastObject instanceof THREE.Mesh) {
scene.remove(lastObject);
this.numberOfObjects = scene.children.length;
}
};
this.addCube = function () {
//隨機大小尺寸
var cubeSize = Math.ceil((Math.random() * 3));
// 生成立方體
var cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
//創建新的MeshLambertMaterial實例,顏色隨機
var cubeMaterial = new THREE.MeshLambertMaterial({color: Math.random() * 0xffffff});
//合並
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
cube.name = "cube-"+scene.children.length;
// 生成隨機坐標
cube.position.x = -30 + Math.round((Math.random() * planeGeometry.parameters.width));
cube.position.y = Math.round((Math.random() * 5));
cube.position.z = -20 + Math.round((Math.random() * planeGeometry.parameters.height));
// 方塊添加進場景
scene.add(cube);
this.numberOfObjects = scene.children.length;
};
//輸出場景中的對象
this.outputObjects = function () {
console.log(scene.children);
}
};
var gui = new dat.GUI();
gui.add(controls, 'rotationSpeed', 0, 0.5);
gui.add(controls, 'addCube');
gui.add(controls, 'removeCube');
gui.add(controls, 'outputObjects');
gui.add(controls, 'numberOfObjects').listen();
render();
function render() {
// 循環每個對象
scene.traverse(function (e) {
if (e instanceof THREE.Mesh && e != plane) {
e.rotation.x += controls.rotationSpeed;
e.rotation.y += controls.rotationSpeed;
e.rotation.z += controls.rotationSpeed;
}
});
// 動畫實現
requestAnimationFrame(render);
renderer.render(scene, camera);
}
}
window.onload = init;
</script>
</body>
</html>
