一,cubemap
通过采样一张cubemap模拟反射效果。
v2f vert (appdata v)
{
v2f o = (v2f) 0;
o.vertex = TransformObjectToHClip(v.vertex.xyz);
o.worldNormal = TransformObjectToWorldNormal(v.normal.xyz);
return o;
}
half4 frag (v2f i) : SV_Target
{
Light light=GetMainLight();
float3 worldLightDir= light.direction;
float3 reflectDir=normalize(reflect(-worldLightDir,i.worldNormal));
half4 envCol = texCUBE(_CubeMap, reflectDir);
half3 envHDRCol = DecodeHDREnvironment(envCol, unity_SpecCube0_HDR);
return half4(envHDRCol,1);
}
CubeMapReflection(默认天空球)
二,ReflectionProbe
ReflectionProbe反射探头会采样周围生成一张CubeMap,在Shader中通过变量访问。ReflectionProbe有三种模式,baked,custom,realtime。baked提前烘培,custom可以烘培,也可以在定义一张CubeMap
v2f vert (appdata v)
{
v2f o = (v2f) 0;
o.vertex = TransformObjectToHClip(v.vertex.xyz);
o.worldNormal = TransformObjectToWorldNormal(v.normal.xyz);
return o;
}
half4 frag (v2f i) : SV_Target
{
Light light=GetMainLight();
float3 worldLightDir= light.direction;
float3 reflectDir=normalize(reflect(-worldLightDir,i.worldNormal));
half4 envCol = SAMPLE_TEXTURECUBE(unity_SpecCube0, samplerunity_SpecCube0, reflectDir);
half3 envHDRCol = DecodeHDREnvironment(envCol, unity_SpecCube0_HDR);
return half4(envHDRCol,1);
}
三,PlanarReflection
适用于表明平整物体的反射,镜子,湖面等。通过反射矩阵将需要反射的物体再渲染一遍。
C#部分
using System;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Serialization;
using Unity.Mathematics;
namespace UnityEngine.Rendering.Universal
{
[ExecuteAlways]
public class PlanarReflections : MonoBehaviour
{
[Serializable]
public enum ResolutionMulltiplier
{
Full,
Half,
Third,
Quarter
}
[Serializable]
public class PlanarReflectionSettings
{
public ResolutionMulltiplier m_ResolutionMultiplier = ResolutionMulltiplier.Third;
public float m_ClipPlaneOffset = 0.07f;
public LayerMask m_ReflectLayers = -1;
public bool m_Shadows;
}
[SerializeField]
public PlanarReflectionSettings m_settings = new PlanarReflectionSettings();
public GameObject target;
[FormerlySerializedAs("camOffset")] public float m_planeOffset;
private static Camera _reflectionCamera;
private RenderTexture _reflectionTexture;
private readonly int _planarReflectionTextureId = Shader.PropertyToID("_PlanarReflectionTexture");
private int2 _oldReflectionTextureSize;
public static event Action<ScriptableRenderContext, Camera> BeginPlanarReflections;
private void OnEnable()
{
RenderPipelineManager.beginCameraRendering += ExecutePlanarReflections;
}
private void OnDisable()
{
Cleanup();
}
private void OnDestroy()
{
Cleanup();
}
private void Cleanup()
{
RenderPipelineManager.beginCameraRendering -= ExecutePlanarReflections;
if(_reflectionCamera)
{
_reflectionCamera.targetTexture = null;
SafeDestroy(_reflectionCamera.gameObject);
}
if (_reflectionTexture)
{
RenderTexture.ReleaseTemporary(_reflectionTexture);
}
}
private static void SafeDestroy(Object obj)
{
if (Application.isEditor)
{
DestroyImmediate(obj);
}
else
{
Destroy(obj);
}
}
private void UpdateCamera(Camera src, Camera dest)
{
if (dest == null) return;
dest.CopyFrom(src);
dest.useOcclusionCulling = false;
if (dest.gameObject.TryGetComponent(out UniversalAdditionalCameraData camData))
{
camData.renderShadows = m_settings.m_Shadows;
}
}
private void UpdateReflectionCamera(Camera realCamera)
{
if (_reflectionCamera == null)
_reflectionCamera = CreateMirrorObjects();
Vector3 pos = Vector3.zero;
Vector3 normal = Vector3.up;
if (target != null)
{
pos = target.transform.position + Vector3.up * m_planeOffset;
normal = target.transform.up;
}
UpdateCamera(realCamera, _reflectionCamera);
var d = -Vector3.Dot(normal, pos) - m_settings.m_ClipPlaneOffset;
var reflectionPlane = new Vector4(normal.x, normal.y, normal.z, d);
var reflection = Matrix4x4.identity;
reflection *= Matrix4x4.Scale(new Vector3(1, -1, 1));
CalculateReflectionMatrix(ref reflection, reflectionPlane);
var oldPosition = realCamera.transform.position - new Vector3(0, pos.y * 2, 0);
var newPosition = ReflectPosition(oldPosition);
_reflectionCamera.transform.forward = Vector3.Scale(realCamera.transform.forward, new Vector3(1, -1, 1));
_reflectionCamera.worldToCameraMatrix = realCamera.worldToCameraMatrix * reflection;
var clipPlane = CameraSpacePlane(_reflectionCamera, pos - Vector3.up * 0.1f, normal, 1.0f);
var projection = realCamera.CalculateObliqueMatrix(clipPlane);
_reflectionCamera.projectionMatrix = projection;
_reflectionCamera.cullingMask = m_settings.m_ReflectLayers;
_reflectionCamera.transform.position = newPosition;
}
private static void CalculateReflectionMatrix(ref Matrix4x4 reflectionMat, Vector4 plane)
{
reflectionMat.m00 = (1F - 2F * plane[0] * plane[0]);
reflectionMat.m01 = (-2F * plane[0] * plane[1]);
reflectionMat.m02 = (-2F * plane[0] * plane[2]);
reflectionMat.m03 = (-2F * plane[3] * plane[0]);
reflectionMat.m10 = (-2F * plane[1] * plane[0]);
reflectionMat.m11 = (1F - 2F * plane[1] * plane[1]);
reflectionMat.m12 = (-2F * plane[1] * plane[2]);
reflectionMat.m13 = (-2F * plane[3] * plane[1]);
reflectionMat.m20 = (-2F * plane[2] * plane[0]);
reflectionMat.m21 = (-2F * plane[2] * plane[1]);
reflectionMat.m22 = (1F -