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

Reflection shading now working. Added example with distortion.

parent 9d754c5f
No related branches found
No related tags found
No related merge requests found
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Rendering;
using UnityEngine.Rendering.LWRP;
using UnityEngine.Rendering;
using UnityEngine;
public class PlanarReflection : MonoBehaviour
{
public Vector3 planeNormal;
......@@ -16,71 +12,46 @@ public class PlanarReflection : MonoBehaviour
Camera reflectionCamera;
Vector3 reflectionScale;
RenderTexture reflectionTexture;
Vector4 reflectionPlane;
//-----------------------------------------------------------------------------
void Start()
{
reflectionTexture = new RenderTexture(Screen.width, Screen.height, 24);
reflectionMaterial.SetTexture("_PlanarReflectionTexture", reflectionTexture);
// reflectionCamera = gameObject.AddComponent<Camera>();
reflectionCamera = GetComponent<Camera>();
RenderPipelineManager.beginCameraRendering += OnBeginRendering;
reflectionPlane = new Vector4(planeNormal.x, planeNormal.y, planeNormal.z, -Vector3.Dot(pointOnPlane, planeNormal.normalized));
}
//-----------------------------------------------------------------------------
void OnBeginRendering(ScriptableRenderContext context, Camera camera)
{
if (camera.tag == "MainCamera")
{
reflectionCamera.CopyFrom(camera);
reflectionCamera.clearFlags = CameraClearFlags.SolidColor;
reflectionCamera.backgroundColor = Color.black;
reflectionCamera.depth -= 1;
reflectionCamera.targetTexture = reflectionTexture;
reflectionCamera.cullingMask = reflectionLayers;
Vector3 cameraForwardWorld = camera.transform.forward;
Vector3 cameraUpWorld = camera.transform.up;
Vector3 cameraPosWorld = camera.transform.position;
// temp - remove this
reflectionPlane = new Vector4(planeNormal.x, planeNormal.y, planeNormal.z, -Vector3.Dot(pointOnPlane, planeNormal.normalized));
Matrix4x4 refMat = Matrix4x4.identity;
CalculateReflectionMatrix(ref refMat, planeNormal);
CalculateReflectionMatrix(ref refMat, reflectionPlane);
reflectionCamera.transform.position = refMat * cameraPosWorld;
reflectionCamera.transform.position = refMat * new Vector4(cameraPosWorld.x, cameraPosWorld.y, cameraPosWorld.z, 1.0f);
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);
// 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]);
......
This diff is collapsed.
fileFormatVersion: 2
guid: f5208cc6de24be840a32b29be1bbd7ab
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 625f186215c104763be7675aa2d941aa, type: 3}
This diff is collapsed.
This diff is collapsed.
fileFormatVersion: 2
guid: ce7f554a81f9a3542890003d01b8427b
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 60072b568d64c40a485e0fc55012dc9f, type: 3}
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