浏览代码

[PlanarReflection] Capture projection

/main
Frédéric Vauchelles 7 年前
当前提交
e1efe09f
共有 4 个文件被更改,包括 150 次插入4 次删除
  1. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/PlanarReflectionProbe.cs
  2. 73
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/ReflectionSystemInternal.cs
  3. 69
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/CameraUtils.cs
  4. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/CameraUtils.cs.meta

1
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/PlanarReflectionProbe.cs


set { m_CaptureMirrorPlaneLocalNormal = value; }
}
public Vector3 captureMirrorPlaneNormal { get { return transform.TransformDirection(m_CaptureMirrorPlaneLocalNormal); } }
public Vector4 captureMirrorPlane { get { return CameraUtils.Plane(captureMirrorPlanePosition, captureMirrorPlaneNormal); } }
#region Proxy Properties
public Matrix4x4 proxyToWorld

73
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/ReflectionSystemInternal.cs


ctr.position = captureToWorld.GetColumn(3);
ctr.rotation = captureToWorld.rotation;
camera.ResetProjectionMatrix();
camera.ResetWorldToCameraMatrix();
if (viewerCamera == null)
{
camera.fieldOfView = GetCaptureCameraFOVFor(probe, viewerCamera);

}
else
{
camera.fieldOfView = GetCaptureCameraFOVFor(probe, viewerCamera);
camera.aspect = 1;
camera.nearClipPlane = probe.captureNearPlane;
camera.farClipPlane = probe.captureFarPlane;
camera.farClipPlane = viewerCamera.farClipPlane;
camera.nearClipPlane = viewerCamera.nearClipPlane;
camera.orthographic = viewerCamera.orthographic;
camera.fieldOfView = viewerCamera.fieldOfView;
camera.aspect = viewerCamera.aspect;
camera.orthographicSize = viewerCamera.orthographicSize;
camera.clearFlags = viewerCamera.clearFlags;
camera.backgroundColor = viewerCamera.backgroundColor;
var plane = probe.captureMirrorPlane;
var reflectionMatrix = CalculateReflectionMatrix(plane);
camera.worldToCameraMatrix = (viewerCamera.worldToCameraMatrix * reflectionMatrix) * Matrix4x4.Scale(new Vector3(-1, 1, 1));
var clipPlane = CameraSpacePlane(camera, probe.captureMirrorPlanePosition, probe.captureMirrorPlaneNormal, 1.0f, 0);
var proj = camera.CalculateObliqueMatrix(clipPlane);
camera.projectionMatrix = proj;
var newPos = reflectionMatrix.MultiplyPoint(viewerCamera.transform.position);
camera.transform.position = newPos;
var forward = reflectionMatrix.MultiplyVector(viewerCamera.transform.forward);
var up = reflectionMatrix.MultiplyVector(viewerCamera.transform.up);
camera.transform.rotation = Quaternion.LookRotation(forward, up);
//camera.fieldOfView = GetCaptureCameraFOVFor(probe, viewerCamera);
//camera.aspect = 1;
//camera.nearClipPlane = probe.captureNearPlane;
//camera.farClipPlane = probe.captureFarPlane;
}
// Given position/normal of the plane, calculates plane in camera space.
static Vector4 CameraSpacePlane(Camera cam, Vector3 pos, Vector3 normal, float sideSign, float clipPlaneOffset)
{
Vector3 offsetPos = pos + normal * clipPlaneOffset;
Matrix4x4 m = cam.worldToCameraMatrix;
Vector3 cpos = m.MultiplyPoint(offsetPos);
Vector3 cnormal = m.MultiplyVector(normal).normalized * sideSign;
return new Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos, cnormal));
}
static Matrix4x4 CalculateReflectionMatrix(Vector4 plane)
{
var reflectionMat = new Matrix4x4();
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 - 2F * plane[2] * plane[2]);
reflectionMat.m23 = (-2F * plane[3] * plane[2]);
reflectionMat.m30 = 0F;
reflectionMat.m31 = 0F;
reflectionMat.m32 = 0F;
reflectionMat.m33 = 1F;
return reflectionMat;
}
static HDCamera GetRenderHDCamera(PlanarReflectionProbe probe)

69
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/CameraUtils.cs


namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public static class CameraUtils
{
public static Vector4 Plane(Vector3 position, Vector3 normal)
{
var n = normal;
var d = -Vector3.Dot(n, position);
var plane = new Vector4(n.x, n.y, n.z, d);
return plane;
}
public static Vector4 Plane(Matrix4x4 basis, Vector3 pos, Vector3 normal, float sideSign = 1, float clipPlaneOffset = 0)
{
var offsetPos = pos + normal * clipPlaneOffset;
var cpos = basis.MultiplyPoint(offsetPos);
var cnormal = basis.MultiplyVector(normal).normalized * sideSign;
return new Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos, cnormal));
}
public static Matrix4x4 CalculateObliqueMatrix(Matrix4x4 sourceProjection, Vector4 clipPlane)
{
var projection = sourceProjection;
var inversion = sourceProjection.inverse;
var cps = new Vector4(
(clipPlane.x > 0 ? 1 : 0) - (clipPlane.x < 0 ? 1 : 0),
(clipPlane.y > 0 ? 1 : 0) - (clipPlane.y < 0 ? 1 : 0),
1.0f,
1.0f);
var q = inversion * cps;
var c = clipPlane * (2.0f / Vector4.Dot(clipPlane, q));
projection[2] = c.x - projection[3];
projection[6] = c.y - projection[7];
projection[10] = c.z - projection[11];
projection[14] = c.w - projection[15];
return projection;
}
public static Matrix4x4 CalculateReflectionMatrix(Vector4 plane)
{
var reflectionMat = new Matrix4x4();
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 - 2F * plane[2] * plane[2]);
reflectionMat.m23 = (-2F * plane[3] * plane[2]);
reflectionMat.m30 = 0F;
reflectionMat.m31 = 0F;
reflectionMat.m32 = 0F;
reflectionMat.m33 = 1F;
return reflectionMat;
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/CameraUtils.cs.meta


fileFormatVersion: 2
guid: 2c8e36e6456734643b4b2bde3c6c4c25
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存