Please review any and all PUBLIC repositories, groups and associate files. These allow anyone on the Internet to access without authentication. Repository and group owners are responsible for their content and permission settings. Go to your project(s), click on Settings > General and expand the "Visibility, project features, permissions" to change this setting.

Commit 9d754c5f authored by Guerrero, Michael J's avatar Guerrero, Michael J

Added working copy of basic planar reflection.

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