Three.js中实现场景雾化效果
生活随笔
收集整理的這篇文章主要介紹了
Three.js中实现场景雾化效果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
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> 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Three.js中实现场景雾化效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Three.js中实现点击按钮添加删除旋
- 下一篇: MapReduce Java API-多