Hi, I wrote a real-time rendering mirror in Unity 6.0.23f1 with Poly Spatial version 2.1.2. It works well in the Unity editor, but when Build to AVP or viewed in AVP using Play to Device, the mirror object appears completely white. The RenderTexture format is R8G8B8A8_UNORM
. Could you help me figure out what might be causing this issue? Here 's my code, Thx!
using System;
using UnityEngine;
//[ExecuteAlways]
public class PlannarReflectionTest1 : MonoBehaviour
{
public LayerMask reflectMask = -1;
[SerializeField] private MeshRenderer m_targetPlaneRenderer;
[SerializeField] private int m_targetTextureWidth = 1024;
[SerializeField] private string m_reflectTexName = "_MirrorReflectTex";
[SerializeField] private float m_scale = 1.0f;
[HideInInspector] private float m_clipPlaneOffset = 0.01f;
private Camera m_cameraMain;
private Transform m_cameraMainTransform;
private Camera m_cameraReflection;
private Transform m_cameraReflectionTransform;
public RenderTexture m_targetTexture;
private int m_reflectTexInt;
private Vector3 m_planeNormal;
private Vector3 m_planePosition;
private Transform m_normalTransform;
private Vector4 m_reflectionPlane;
private void Awake()
{
// 查找主相机
m_cameraMain = Camera.main;
m_cameraMainTransform = m_cameraMain.transform;
// 创建一个反射相机
var go = new GameObject("ReflectionCamera");
m_cameraReflection = go.AddComponent<Camera>();
m_cameraReflection.aspect = m_cameraMain.aspect;
m_cameraReflection.fieldOfView = m_cameraMain.fieldOfView;
m_cameraReflection.enabled = false; // 禁用自动渲染
m_cameraReflectionTransform = m_cameraReflection.transform;
// 创建贴图,赋值到反摄像机的 targetTexture
m_cameraReflection.targetTexture = m_targetTexture;
m_cameraReflection.cullingMask = reflectMask.value;
// 创建法线
m_normalTransform = new GameObject("Normal").transform;
var planeTransform = m_targetPlaneRenderer.transform;
m_normalTransform.SetPositionAndRotation(planeTransform.position, planeTransform.rotation);
m_normalTransform.SetParent(planeTransform);
m_planePosition = m_normalTransform.position;
m_planeNormal = m_normalTransform.up;
m_reflectTexInt = Shader.PropertyToID(m_reflectTexName);
Shader.SetGlobalTexture(m_reflectTexName, m_targetTexture);
}
private void Update()
{
RenderReflection();
}
private void RenderReflection()
{
// 判断相机是否在法线下方,如果在下方就不做渲染
Vector3 localPos = m_normalTransform.worldToLocalMatrix.MultiplyPoint3x4(m_cameraMainTransform.position);
if (localPos.y < 0) return;
// 调整位置
Vector3 normal = m_normalTransform.up;
Vector3 pos = m_normalTransform.position;
float d = -Vector3.Dot(normal, pos) - m_clipPlaneOffset;
m_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, ref m_reflectionPlane);
// 设置反射相机的视图矩阵
Matrix4x4 worldToCameraMatrix = m_cameraMain.worldToCameraMatrix * reflection;
m_cameraReflection.worldToCameraMatrix = worldToCameraMatrix;
// 计算投影矩阵
Vector3 offsetPos = pos + normal * m_clipPlaneOffset;
Vector3 cpos = worldToCameraMatrix.MultiplyPoint3x4(offsetPos);
Vector3 cnormal = worldToCameraMatrix.MultiplyVector(normal).normalized;
Vector4 clipPlane = new Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos, cnormal));
m_cameraReflection.projectionMatrix = m_cameraMain.CalculateObliqueMatrix(clipPlane);
// 渲染到目标 RenderTexture
GL.invertCulling = true;
m_cameraReflection.Render();
GL.invertCulling = false;
Shader.SetGlobalTexture(m_reflectTexName, m_targetTexture);
m_cameraReflection.targetTexture = m_targetTexture;
Unity.PolySpatial.PolySpatialObjectUtils.MarkDirty(m_targetTexture);
}
private static void CalculateReflectionMatrix(ref Matrix4x4 reflectionMat, ref 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;
}
}