Skip to content
Snippets Groups Projects
Commit 9d754c5f authored by Guerrero, Michael J's avatar Guerrero, Michael J
Browse files

Added working copy of basic planar reflection.

parent ca2a462f
No related branches found
No related tags found
No related merge requests found
...@@ -7,16 +7,22 @@ using UnityEngine; ...@@ -7,16 +7,22 @@ using UnityEngine;
public class PlanarReflection : MonoBehaviour public class PlanarReflection : MonoBehaviour
{ {
public Transform reflectionPlaneObject; public Vector3 planeNormal;
public Vector3 pointOnPlane;
public Material reflectionMaterial; public Material reflectionMaterial;
public LayerMask reflectionLayers;
public float debug;
Camera reflectionCamera; Camera reflectionCamera;
Vector3 reflectionScale;
RenderTexture reflectionTexture; RenderTexture reflectionTexture;
void Start() void Start()
{ {
reflectionTexture = new RenderTexture(Screen.width, Screen.height, 24); reflectionTexture = new RenderTexture(Screen.width, Screen.height, 24);
reflectionMaterial.SetTexture("_reflectionTexture", reflectionTexture); reflectionMaterial.SetTexture("_PlanarReflectionTexture", reflectionTexture);
reflectionCamera = gameObject.AddComponent<Camera>(); // reflectionCamera = gameObject.AddComponent<Camera>();
reflectionCamera = GetComponent<Camera>();
RenderPipelineManager.beginCameraRendering += OnBeginRendering; RenderPipelineManager.beginCameraRendering += OnBeginRendering;
} }
...@@ -27,25 +33,74 @@ public class PlanarReflection : MonoBehaviour ...@@ -27,25 +33,74 @@ public class PlanarReflection : MonoBehaviour
reflectionCamera.CopyFrom(camera); reflectionCamera.CopyFrom(camera);
reflectionCamera.depth -= 1; reflectionCamera.depth -= 1;
reflectionCamera.targetTexture = reflectionTexture; reflectionCamera.targetTexture = reflectionTexture;
reflectionCamera.cullingMask = reflectionLayers;
Vector3 cameraForwardWorld = camera.transform.forward; Vector3 cameraForwardWorld = camera.transform.forward;
Vector3 cameraUpWorld = camera.transform.up; Vector3 cameraUpWorld = camera.transform.up;
Vector3 cameraPosWorld = camera.transform.position; Vector3 cameraPosWorld = camera.transform.position;
Vector3 cameraForwardPlane = reflectionPlaneObject.InverseTransformDirection(cameraForwardWorld);
Vector3 cameraUpPlane = reflectionPlaneObject.InverseTransformDirection(cameraUpWorld);
Vector3 cameraPosPlane = reflectionPlaneObject.InverseTransformPoint(cameraPosWorld);
cameraForwardPlane.y *= -1f; Matrix4x4 refMat = Matrix4x4.identity;
cameraUpPlane.y *= -1f; CalculateReflectionMatrix(ref refMat, planeNormal);
cameraPosPlane.y *= -1f;
reflectionCamera.transform.position = refMat * cameraPosWorld;
reflectionCamera.transform.forward = refMat * cameraForwardWorld;
reflectionMaterial.SetTexture("_PlanarReflectionTexture", reflectionTexture);
// Vector3 test = reflection * cameraForwardWorld;
// Debug.Log("from " + cameraForwardWorld + " -to- " + test);
// Matrix4x4 planeReflection = reflectionPlaneObject.transform.worldToLocalMatrix;
// planeReflection.m30 = 0f;
// planeReflection.m31 = 0f;
// planeReflection.m32 = 0f;
// planeReflection.m33 = 1f;
// var test = planeReflection * reflection;
// var test3 = reflection * planeReflection;
// var test2 = test * Vector3.up;
// Debug.Log(test3);
// Debug.Log(test);
// planeReflection = planeReflection * reflection;
// Vector3 cameraForwardPlane = reflectionPlaneObject.InverseTransformDirection(cameraForwardWorld);
// Vector3 cameraUpPlane = reflectionPlaneObject.InverseTransformDirection(cameraUpWorld);
// Vector3 cameraPosPlane = reflectionPlaneObject.InverseTransformPoint(cameraPosWorld);
// cameraForwardPlane = planeReflection * cameraForwardPlane;
// cameraUpPlane = planeReflection * cameraUpPlane;
// cameraPosPlane = planeReflection * cameraPosPlane;
cameraForwardWorld = reflectionPlaneObject.TransformDirection(cameraForwardPlane); // cameraForwardWorld = reflectionPlaneObject.TransformDirection(cameraForwardPlane);
cameraUpWorld = reflectionPlaneObject.TransformDirection(cameraUpPlane); // cameraUpWorld = reflectionPlaneObject.TransformDirection(cameraUpPlane);
cameraPosWorld = reflectionPlaneObject.TransformPoint(cameraPosPlane); // cameraPosWorld = reflectionPlaneObject.TransformPoint(cameraPosPlane);
reflectionCamera.transform.position = cameraPosWorld; // reflectionCamera.transform.position = cameraPosWorld;
reflectionCamera.transform.LookAt(cameraPosWorld + cameraForwardWorld, cameraUpWorld); // reflectionCamera.transform.LookAt(cameraPosWorld + cameraForwardWorld, cameraUpWorld);
} }
}
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 - 2F * plane[2] * plane[2]);
reflectionMat.m23 = (-2F * plane[3] * plane[2]);
reflectionMat.m30 = 0F;
reflectionMat.m31 = 0F;
reflectionMat.m32 = 0F;
reflectionMat.m33 = 1F;
} }
} }
This diff is collapsed.
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"name": "com.futuretech.shaders", "name": "com.futuretech.shaders",
"displayName": "FutureTech Shaders", "displayName": "FutureTech Shaders",
"description": "Contains Unity Shader Graphs", "description": "Contains Unity Shader Graphs",
"version": "0.1.5", "version": "0.1.6",
"unity": "2019.2", "unity": "2019.2",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment