three.js SpotLightShadow

2023-02-16 17:43 更新

這在SpotLights內部用于計算陰影。

代碼示例

//Create a WebGLRenderer and turn on shadows in the renderer
const renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap

//Create a SpotLight and turn on shadows for the light
const light = new THREE.SpotLight( 0xffffff );
light.castShadow = true;            // default false
scene.add( light );

//Set up shadow properties for the light
light.shadow.mapSize.width = 512;  // default
light.shadow.mapSize.height = 512; // default
light.shadow.camera.near = 0.5;    // default
light.shadow.camera.far = 500      // default
light.shadow.focus = 1;            // default

//Create a sphere that cast shadows (but does not receive them)
const sphereGeometry = new THREE.SphereGeometry( 5, 32, 32 );
const sphereMaterial = new THREE.MeshStandardMaterial( { color: 0xff0000 } );
const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
sphere.castShadow = true; //default is false
sphere.receiveShadow = false; //default
scene.add( sphere );

//Create a plane that receives shadows (but does not cast them)
const planeGeometry = new THREE.PlaneGeometry( 20, 20, 32, 32 );
const planeMaterial = new THREE.MeshStandardMaterial( { color: 0x00ff00 } )
const plane = new THREE.Mesh( planeGeometry, planeMaterial );
plane.receiveShadow = true;
scene.add( plane );

//Create a helper for the shadow camera (optional)
const helper = new THREE.CameraHelper( light.shadow.camera );
scene.add( helper );

構造函數

構造函數創(chuàng)建一個 PerspectiveCamera : PerspectiveCamera 來管理陰影的世界視圖

屬性

有關常用屬性,請參閱基礎LightShadow類。

.camera : Camera

在光的世界里。這用于生成場景的深度圖;從光的角度來看,其他物體背后的物體將處于陰影中。默認值為PerspectiveCamera,近剪裁平面為0.5。 fov將通過更新方法跟蹤擁有SpotLight的角度屬性。同樣,aspect屬性將跟蹤mapSize的方面。如果設置了燈光的距離屬性,則遠剪裁平面將跟蹤該值,否則默認為500。

.focus : Number

用于聚焦陰影相機。相機的視野設置為聚光燈視野的百分比。范圍是 [0, 1]。默認值為 1.0。

.isSpotLightShadow : Boolean

只讀標志,用于檢查給定對象是否屬于 SpotLightShadow 類型。

方法

有關常用方法,請參閱基礎LightShadow類。

源碼

src/lights/SpotLightShadow.js


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號