Three.js中如何围绕中心点旋转正交阴影相机?
Hey there! Let's work through this shadow camera rotation issue you're dealing with. It's super frustrating when the camera doesn't behave like you expect, so let's break down the possible fixes based on your setup:
Case 1: Using Built-in Light Shadow System (e.g., Unity)
If you're relying on the engine's automatic shadow camera for your directional light, here's the key thing to know: the auto-generated shadow camera gets reset every frame by the engine. That means directly modifying its rotation will get overwritten immediately.
You mentioned you rotated the directional light but the shadow camera didn't follow—first check these settings:
- Make sure your directional light's Shadow Type is set to either
Hard ShadowsorSoft Shadows(notNo Shadows). If shadows are disabled, the shadow camera won't even generate. - Verify that shadow resolution and other shadow-related settings are enabled in your project quality settings.
Once those are set, rotating the directional light's transform should automatically adjust the shadow camera's orientation to match the light's direction. If it still isn't working, double-check that no custom scripts are overriding the light's shadow camera settings.
Case 2: Manually Controlling an Orthographic Shadow Camera
If you've set up a custom orthographic camera for shadows, rotating it around its own center requires targeting the right pivot point. Here's how to do it properly:
Option 1: Rotate Around the Camera's View Frustum Center
Orthographic cameras have a view frustum (the area they can see), and you might want to rotate around the center of this frustum instead of the camera's transform position. Use this code snippet (example for Unity C#):
Camera shadowCam = GetComponent<Camera>(); // Calculate the center of the camera's view frustum float frustumMidpoint = shadowCam.nearClipPlane + (shadowCam.farClipPlane - shadowCam.nearClipPlane) / 2f; Vector3 frustumCenter = shadowCam.transform.position + shadowCam.transform.forward * frustumMidpoint; // Rotate 45 degrees around the Y-axis (adjust axis as needed) shadowCam.transform.RotateAround(frustumCenter, Vector3.up, 45f);
Option 2: Rotate Around the Camera's Transform Center
If you just need to rotate around the camera's own position (transform center), use this simpler approach—just make sure you're using local space rotation:
// Rotate 45 degrees around the local Y-axis transform.Rotate(Vector3.up, 45f, Space.Self); // Or set a direct rotation in world space transform.rotation = Quaternion.Euler(0, 45, 0);
If this still doesn't work, check if the camera has a parent object whose rotation is overriding it, or if another script is resetting the camera's rotation every frame.
内容的提问来源于stack exchange,提问作者Joe Morgan

