6. 灯光对象
灯光¶
VRP支持三种光源类型,点光源、平行光和聚光灯。
点光源:点光源的照亮空间则是有限的,它是由空间中的一个球体定义的。点光源可以表示由一个点发出的、向所有方向延伸的光。点光源是有位置属性的,对于方向属性,我们需要用点光源的位置减去某点的位置来得到它到该点的方向。同时,点光源也是会衰减的,随着物体逐渐远离点光源,它接收到的光照强度也会逐渐减小。点光源球心处的光照强度最强,球体边界处的最弱,值为0。其中间的衰减值可以由一个函数定义。
平行光:平行光可以照亮的范围是没有限制的,它通常是作为太阳这样的角色在场景中出现的。平行光之所以简单,是因为它没有一个唯一的位置,它可以放在场景中的任意位置。它的几何属性只有方向,我们可以调整平行光的属性来改变它的光源方向, 而且平行光到场景中所有点的方向都是一样的,这也是平行光名字的由来。除此之外,由于平行光没有一个具体的位置,因此也没有衰减的概念,也就是说,光照强度不会随着距离而发生改变。
聚光灯:聚光灯是这3种光源类型中最复杂的一种。它的照亮空间同样是有限的,但不再是简单的球体,而是由空间中的一块锥形区域定义的。聚光灯可以用于表示由一个特定位置出发、向特定方向延伸的光。对于方向属性,我们需要用聚光灯的位置减去某点的位置来得到它到该点的方向。聚光灯的衰减也是随着物体逐渐远离点光源而逐渐减小,在锥形的顶点处光照强度最强,在锥形的边界处强度为0。其中间的衰减值可以由一个函数定义,这个函数相对于点光源衰减计算公式要更加复杂,因为我们需要判断一个点是否在锥体的范围内。
属性列表¶
属性 | 属性名 | 类型 | 默认值/可选值 | 说明 |
---|---|---|---|---|
光源类型 | mode | string | 0、1、2 | 显示当前光源的类型。 0=平行光,1=点光源,2=聚光灯。 |
光照颜色 | color | Color | { r: 1, g: 0, b: 1, a: 1 } | 场景中光源的颜色,可根据需要进行修改。 |
强度 | intensity | number | 0~1000 | 光源的强度。 |
投射阴影 | castShadow | boolean | true、false | 可选择是否开启投射阴影。 |
模拟光源尺寸 | size | number | 可调节模拟光源的尺寸,当光源类型为方向光时有该属性。 该参数可调节平行光阴影软化程度。根据需要输入尺寸。 | |
阴影采样质量 | pcssSampleQuality | number | 0~3 | 可调节阴影采样质量,当光源类型为方向光时有该属性。 |
阴影强度 | shadowIntensity | number | 0~1 | 可调节阴影强度,当光源类型为方向光时有该属性。 |
阴影偏移 | shadowBias | number | 1~100 | 可调节阴影偏移,当光源类型为方向光时有该属性。 通过调节该参数可避免阴影距离偏差和面交叉部分漏光的问题。 |
投影偏移 | lightOrthoNearOffset | number | ||
阴影距离层级 | csmLevel | number | 1、2、3、4 | 可调节阴影精细度,分为0-4级,但是为了渲染需求渲染时可以选择1-4级渲染。 |
阴影0级距离 | CSM0 Distance | number | ||
阴影1级距离 | CSM1 Distance | number | ||
阴影2级距离 | CSM2 Distance | number | ||
阴影3级距离 | CSM3 Distance | number | ||
阴影4级距离 | CSM4 Distance | number | ||
光照范围 | range | number | 0~1000 | 可调节光照范围,当光源类型为点光源和聚光灯时有该属性。 |
点角度 | spotAngle | number | 1~180 | 可调节点角度,当光源类型为聚光灯时有该属性。 |
示例代码¶
// 修改灯光类型|mode
directionalLight.mode = "0";
// 修改平行光光照颜色|color
directionalLight.color = { r: 0.8, g: 0, b: 0.2, a: 0.5 };
// 修改平行光强度|intensity
directionalLight.intensity = 3;
// 是否关闭平行光投射阴影|castShadow
directionalLight.castShadow = true;
// 修改模拟光源尺寸|size
directionalLight.size = 0.1;
// 修改阴影采样质量|pcssSampleQuality
directionalLight.pcssSampleQuality = 2;
// 修改阴影强度|shadowIntensity
directionalLight.shadowIntensity = 1;
// 修改阴影偏移|shadowBias
directionalLight.shadowBias = 20;
示例案例¶
// 1-平行光
directionalLight_Demo() {
// 获取平行光
let directionalLight_actor = gVrpManager.Scene().FindActor("Directional Light");
// 修改灯光属性的位置/旋转/缩放
directionalLight_actor.localPosition = { x: 0, y: 4, z: 0 };
directionalLight_actor.localEulerAngles = { x: 60, y: 30, z: 0 };
directionalLight_actor.localScale = { x: 1, y: 1, z: 1 };
// 获取平行光组件
let directionalLight = directionalLight_actor.GetComponent("Light");
// 修改灯光类型|mode
directionalLight.mode = "0";
// 修改平行光光照颜色|color
directionalLight.color = { r: 0.8, g: 0, b: 0.2, a: 0.5 };
// 修改平行光强度|intensity
directionalLight.intensity = 3;
// 是否关闭平行光投射阴影|castShadow
directionalLight.castShadow = true;
// 修改模拟光源尺寸|size
directionalLight.size = 0.1;
// 修改阴影采样质量|pcssSampleQuality
directionalLight.pcssSampleQuality = 2;
// 修改阴影强度|shadowIntensity
directionalLight.shadowIntensity = 1;
// 修改阴影偏移|shadowBias
directionalLight.shadowBias = 20;
// 调节阴影距离层级
// 修改投影偏移|lightOrthoNearOffset
directionalLight.lightOrthoNearOffset = 500;
// 选择阴影距离层级|csmLevel
directionalLight.csmLevel = 1;
// 修改阴影0级距离|CSM0 Distance
directionalLight.csm0Dis = 0.1;
// 修改阴影1级距离|CSM1 Distance
directionalLight.csm1Dis = 10;
// 修改阴影2级距离|CSM2 Distance
directionalLight.csm2Dis = 20;
// 修改阴影3级距离|CSM3 Distance
directionalLight.csm3Dis = 45;
// 修改阴影4级距离|CSM4 Distance
directionalLight.csm4Dis = 150;
}
// 2-点光源
pointLight_Demo() {
// 获取点光源
let pointLight_Actor = gVrpManager.Scene().FindActor("Point Light");
// 修改灯光属性的位置/旋转/缩放X,Y,Z
pointLight_Actor.localPosition = { x: -0.5, y: 1.12, z: 0 };
pointLight_Actor.localEulerAngles = { x: 180, y: 0, z: 0 };
pointLight_Actor.localScale = { x: 1, y: 1, z: 1 };
// 获取点光源组件
let pointLight = pointLight_Actor.GetComponent("Light");
// 修改灯光类型|mode
pointLight.mode = "1";
// 修改点光源光照颜色|color
pointLight.color = { r: 1, g: 0, b: 1, a: 1 };
// 修改点光源强度|intensity
pointLight.intensity = 3;
// 是否关闭点光源投射阴影|castShadow
pointLight.castShadow = false;
// 修改点光源光照范围|range
pointLight.range = 10;
}
// 3-聚光灯
spotLight_Demo() {
// 获取聚光灯
let spotLight_Actor = gVrpManager.Scene().FindActor("Spot Light");
// 修改灯光属性的位置/旋转/缩放X,Y,Z
spotLight_Actor.localPosition = { x: 0.2, y: 1.32, z: 0.2 };
spotLight_Actor.localEulerAngles = { x: 270, y: 170, z: 0 };
spotLight_Actor.localScale = { x: 1, y: 1, z: 1 };
// 获取点光源组件
let spotLight = spotLight_Actor.GetComponent("Light");
// 修改灯光类型|mode value: 0/1/2
spotLight.mode = "2";
// 修改聚光灯光照颜色|color
spotLight.color = { r: 1, g: 0, b: 1, a: 1 };
// 修改聚光灯强度|intensity
spotLight.intensity = 79;
// 是否关闭聚光灯投射阴影|castShadow
spotLight.castShadow = true;
// 修改点聚光灯照范围|range
spotLight.range = 14;
// 修改点聚光灯点角度|spotAngle
spotLight.spotAngle = 30;
}