光照估计
现实环境照明复杂。要增强虚实融合,可根据光照估计对虚拟对象着色,包括:
- 最主要的方向光,含旋转、强度和颜色。
- L3 球谐形式的环境光。
- 立方体贴图形式的反射,目前尚未集成。
支持
检查系统是否支持:
if (app.xr.lightEstimation.supportedColor) {
// light estimation access is supported
}
app.xr.lightEstimation.on('available', () => {
// light estimation becomes available
});
方向光
最基本的信息是主方向光的旋转、强度和颜色:
const lightEstimation = app.xr.lightEstimation;
// check if light estimation is available
if (lightEstimation.available) {
// rotate entity
entity.setRotation(lightEstimation.rotation());
// set light parameters
entity.light.intensity = lightEstimation.intensity;
entity.light.color = lightEstimation.color;
}
环境光
环境通常比单一方向光复杂,因此提供 L3 SH 球谐环境光。
使用时,材质需要预滤波立方体贴图,场景天空盒也可以;否则需修改 ambientConstantPS 着色器片段。
可逐材质设置球谐数据:
if (app.xr.lightEstimation.available) {
material.setParameter('ambientSH[0]', app.xr.lightEstimation.sphericalHarmonics);
}
没有预滤波贴图或天空盒时,可更新材质片段:
material.chunks.ambientConstantPS = chunkCode;
material.update();
着色器片段代码:
uniform vec3 ambientSH[9];
void addAmbient(vec3 worldNormal) {
vec3 n = worldNormal;
vec3 color =
ambientSH[0] +
ambientSH[1] * n.x +
ambientSH[2] * n.y +
ambientSH[3] * n.z +
ambientSH[4] * n.x * n.z +
ambientSH[5] * n.z * n.y +
ambientSH[6] * n.y * n.x +
ambientSH[7] * (3.0 * n.z * n.z - 1.0) +
ambientSH[8] * (n.x * n.x - n.y * n.y);
dDiffuseLight += color;
}
反射
WebXR 也以立方体贴图估计环境反射,但目前尚未集成到 PlayCanvas。