准备1:
加入以下脚本到摄像机让depth texture 有效,水面整体颜色变化基于深度。
using UnityEngine;
public class CameraDepthTextureMode : MonoBehaviour
{
[SerializeField]
DepthTextureMode depthTextureMode;
private void OnValidate()
{
SetCameraDepthTextureMode();
}
private void Awake()
{
SetCameraDepthTextureMode();
}
private void SetCameraDepthTextureMode()
{
GetComponent<Camera>().depthTextureMode = depthTextureMode;
}
}
准备2:
加入以下脚本到摄像机,得到一张在view空间的normal纹理
using UnityEngine;
public class NormalsReplacementShader : MonoBehaviour
{
[SerializeField]
Shader normalsShader;
private RenderTexture renderTexture;
private new Camera camera;
private void Start()
{
Camera thisCamera = GetComponent<Camera>();
// Create a render texture matching the main camera's current dimensions.
renderTexture = new RenderTexture(thisCamera.pixelWidth, thisCamera.pixelHeight, 24);
// Surface the render texture as a global variable, available to all shaders.
Shader.SetGlobalTexture("_CameraNormalsTexture", renderTexture);
// Setup a copy of the camera to render the scene using the normals shader.
GameObject copy = new GameObject("Normals camera");
camera = copy.AddComponent<Camera>();
camera.CopyFrom(thisCamera);
camera.transfor