You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

渲染循环中平行光阴影相机未跟随主相机:能否运行时更新位置?

Can I Update a Directional Light's Shadow Camera Position at Runtime?

Absolutely! You can update a directional light's shadow camera at runtime—your issue is likely tied to how Three.js handles directional light shadows under the hood, not a restriction on dynamic updates. Here's what you need to know to fix it:

  • First, remember that a directional light's shadow camera is an OrthographicCamera, not a PerspectiveCamera. Unlike your main perspective camera, this ortho camera's position is tightly linked to the directional light's own position and direction. If you're directly setting directionalLight.shadow.camera.position without aligning it with the light's properties, it might not behave as expected (Three.js often overrides this to keep the shadow aligned with the light's direction).

  • The proper way to make the shadow camera follow your main camera is to sync the directional light's position to your main camera's position, then ensure the shadow camera's projection matrix is updated. Drop this into your render loop:

// Sync light position to main camera
directionalLight.position.copy(mainCamera.position);
// Optional: Align the light's target with your camera's focus (if needed)
directionalLight.target.copy(mainCamera.target);
// Update the shadow camera's projection matrix to apply changes
directionalLight.shadow.camera.updateProjectionMatrix();
// Ensure shadows are enabled (double-check these if shadows aren't rendering at all)
directionalLight.castShadow = true;
scene.shadowMap.enabled = true;
  • Debug with a Camera Helper: If you're still not seeing movement, add a helper to visualize the shadow camera's frustum—this will confirm if it's actually moving:
const shadowCamHelper = new THREE.CameraHelper(directionalLight.shadow.camera);
scene.add(shadowCamHelper);

You'll see a wireframe outline of the shadow camera's area; if it moves with your main camera, you know the position update is working (any missing shadows might then be due to frustum size or object shadow settings).

  • Don't forget frustum settings: If your shadow camera's left, right, top, bottom, near, or far values are set too narrow/wide, shadows might not render even if the camera moves. Adjust these to fit your scene's scale.

Once you sync the directional light's position to your main camera and update the projection matrix, the shadow camera should move as expected. The key is working with the directional light's core properties rather than directly manipulating the shadow camera's position in most cases.

内容的提问来源于stack exchange,提问作者Joe Morgan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:11:06