three.js中聚光燈及其屬性介紹小結(jié)
一、聚光燈及其屬性介紹
Three.js中的聚光燈(SpotLight)是一種用于在場景中創(chuàng)建聚焦光照的光源類型。它有以下屬性:
- color:聚光燈的顏色。
- intensity:聚光燈的強(qiáng)度。
- distance:聚光燈的有效距離。
- angle:聚光燈的光錐角度。
- penumbra:聚光燈錐形光圈的模糊半徑。
- decay:聚光燈的衰減系數(shù)。
- position:聚光燈的位置。
- target:聚光燈的目標(biāo)位置(用于確定聚光燈的方向)。
以下是一個應(yīng)用到聚光燈的所有屬性的例子:
var spotLight = new THREE.SpotLight(0xffffff, 1, 100, Math.PI/4, 0, 2); //創(chuàng)建一個白色聚光燈,強(qiáng)度為1,有效距離為100,光錐角度為45度,模糊半徑為0,衰減系數(shù)為2 spotLight.position.set(0, 10, 0); //設(shè)置聚光燈的位置 var spotLightTarget = new THREE.Object3D(); //創(chuàng)建一個對象作為聚光燈的目標(biāo) spotLightTarget.position.set(0, 0, -10); //設(shè)置目標(biāo)位置 scene.add(spotLightTarget); //將目標(biāo)對象添加到場景中 spotLight.target = spotLightTarget; //設(shè)置聚光燈的目標(biāo)為目標(biāo)對象 scene.add(spotLight); //將聚光燈添加到場景中
二、結(jié)合dat.gui和立方體查看聚光燈屬性設(shè)置效果案例
1. 效果如圖:

2. 完整代碼如下:
import * as THREE from "three";
// 導(dǎo)入軌道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// 導(dǎo)入動畫庫
import gsap from "gsap";
// 導(dǎo)入dat.gui
import * as dat from "dat.gui";
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";
const gui = new dat.GUI();
// 1、創(chuàng)建場景
const scene = new THREE.Scene();
// 2、創(chuàng)建相機(jī)
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
// 設(shè)置相機(jī)位置
camera.position.set(0, 0, 10);
scene.add(camera);
const sphereGeometry = new THREE.BoxGeometry( 1, 1, 1 ); // 立方體幾何體 參數(shù):長、寬、高
const material = new THREE.MeshStandardMaterial({color: 0x00ff00});
const sphere = new THREE.Mesh(sphereGeometry, material);
// 投射陰影
sphere.castShadow = true; // 設(shè)置立方體投射陰影
scene.add(sphere); // 將立方體添加到場景中
// // 創(chuàng)建平面
const planeGeometry = new THREE.PlaneBufferGeometry(50, 50);
// 創(chuàng)建標(biāo)準(zhǔn)材質(zhì)
const materialplane = new THREE.MeshStandardMaterial({color: 0xf0fff0});
const plane = new THREE.Mesh(planeGeometry, materialplane);
plane.position.set(0, -1, 0);
plane.rotation.x = -Math.PI / 2;
// 接收陰影
plane.receiveShadow = true;
scene.add(plane);
// 燈光
// 環(huán)境光
const light = new THREE.AmbientLight(0xffffff, 0.5); // soft white light
scene.add(light);
// 創(chuàng)建聚光燈
const spotLight = new THREE.SpotLight(0xffffff, 1);
spotLight.position.set(5, 5, 5); // 設(shè)置聚光燈位置
spotLight.castShadow = true; // 設(shè)置聚光燈投射陰影
spotLight.intensity = 2; // 設(shè)置聚光燈強(qiáng)度
// 設(shè)置陰影貼圖模糊度
spotLight.shadow.radius = 20;
// 設(shè)置陰影貼圖的分辨率
spotLight.shadow.mapSize.set(512, 512);
// console.log(directionalLight.shadow);
spotLight.target = sphere; // 設(shè)置聚光燈的目標(biāo)為立方體 會自動對準(zhǔn)目標(biāo)
spotLight.angle = Math.PI / 6; // 設(shè)置聚光燈的角度
spotLight.distance = 0; // 設(shè)置聚光燈的距離
spotLight.penumbra = 0; // 設(shè)置聚光燈的邊緣
spotLight.decay = 0; // 設(shè)置聚光燈的衰減
// 設(shè)置透視相機(jī)的屬性
scene.add(spotLight);
gui.add(sphere.position, "x").min(-5).max(5).step(0.1);
gui
.add(spotLight, "angle")
.min(0)
.max(Math.PI / 2)
.step(0.01);
gui.add(spotLight, "distance").min(0).max(10).step(0.01);
gui.add(spotLight, "penumbra").min(0).max(1).step(0.01);
gui.add(spotLight, "decay").min(0).max(5).step(0.01);
// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
// 設(shè)置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
// 開啟場景中的陰影貼圖
renderer.shadowMap.enabled = true;
renderer.physicallyCorrectLights = true; // 設(shè)置渲染器的物理正確性
// console.log(renderer);
// 將webgl渲染的canvas內(nèi)容添加到body
document.body.appendChild(renderer.domElement);
// 創(chuàng)建軌道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 設(shè)置控制器阻尼,讓控制器更有真實效果,必須在動畫循環(huán)里調(diào)用.update()。
controls.enableDamping = true;
// 添加坐標(biāo)軸輔助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// 設(shè)置時鐘
const clock = new THREE.Clock();
function render() {
controls.update();
renderer.render(scene, camera);
// 渲染下一幀的時候就會調(diào)用render函數(shù)
requestAnimationFrame(render);
}
render();
// 監(jiān)聽畫面變化,更新渲染畫面
window.addEventListener("resize", () => {
// console.log("畫面變化了");
// 更新攝像頭
camera.aspect = window.innerWidth / window.innerHeight;
// 更新攝像機(jī)的投影矩陣
camera.updateProjectionMatrix();
// 更新渲染器
renderer.setSize(window.innerWidth, window.innerHeight);
// 設(shè)置渲染器的像素比
renderer.setPixelRatio(window.devicePixelRatio);
});3. 更改屬性效果
1. 更改位置
我們這里設(shè)置了spotLight.target = sphere; // 設(shè)置聚光燈的目標(biāo)為立方體 會自動對準(zhǔn)目標(biāo),此時我們移動立方體,燈光應(yīng)該會跟著立方體移動,如圖:
x : 0

x : 2.7

2. 更改聚光燈的光錐角度
angle:0.52

- angle:0.22

3. 更改聚光燈的有效距離
distance: 0

distance: 10

4. 更改聚光燈錐形光圈的模糊半徑
penumbra:0

penumbra:0.3

5. 更改聚光燈的衰減系數(shù)
decay: 0

decay: 0.31

到此這篇關(guān)于three.js中聚光燈及其屬性介紹小結(jié)的文章就介紹到這了,更多相關(guān)three.js中聚光燈內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js編碼之encodeURIComponent使用介紹(asp,php)
因此對于JS腳本又重新研究了一下。在對新的URL編碼的時候發(fā)現(xiàn),網(wǎng)頁編碼的格式對于JS的影響很大,在這里書寫一點(diǎn)2012-03-03
JavaScript正則表達(dá)式替換字符串中圖片地址(img src)的方法
這篇文章主要介紹了JavaScript正則表達(dá)式替換字符串中圖片地址(img src)的方法,結(jié)合實例形式分析了JS正則替換的常用技巧與注意事項,需要的朋友可以參考下2017-01-01
JS實現(xiàn)將手機(jī)號中間的幾位數(shù)字變成****功能
這篇文章主要介紹了用js如何實現(xiàn)將手機(jī)號中間的幾位數(shù)字變成****?_,今天,我們要實現(xiàn)一個很常見并且簡單的功能如何將手機(jī)號中間的幾位數(shù)變成****,需要的朋友可以參考下2023-09-09
html中通過JS獲取JSON數(shù)據(jù)并加載的方法
本篇內(nèi)容主要給大家講了如何通過javascript解析JSON并得到數(shù)據(jù)后添加到HTML中的方法,需要的朋友參考下。2017-11-11
原生JavaScript實現(xiàn)的簡單省市縣三級聯(lián)動功能示例
這篇文章主要介紹了原生JavaScript實現(xiàn)的簡單省市縣三級聯(lián)動功能,結(jié)合完整實例形式分析了javascript聯(lián)動菜單的實現(xiàn)方法,涉及javascript事件響應(yīng)及頁面元素動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-05-05

