本来想上张图一目了然,奈何我不懂这CSDN的编辑,就如此吧
顺便吐槽下,镜面反射,油管上几下就找到了,国内我反正没搜到过靠谱的
镜面反射的面的材质(地板,玻璃上挂载的shader)
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry+1"}
LOD 200
Stencil{
Ref 1
Comp always
Pass replace
}
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float _reflecFact;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
// o.Alpha = c.a;
o.Alpha=(1.0-_reflecFact);
}
ENDCG
}
FallBack "Diffuse"
相机上挂载的脚本
Camera m_reCam;
public Camera m_MainCam;
public GameObject m_rePlane;
RenderTexture m_Render;
public Material m_FloorMateria;
[Range(0.0f,1f)]
public float m_AFactor=0.5f;
// Start is called before the first frame update
void Start()
{
#region 反射相机渲染投影
GameObject reflectionCam = new GameObject("ReflectionCamera");
m_reCam = reflectionCam.AddComponent<Camera>();
m_reCam.enabled = false;
m_MainCam = Camera.main;
#endregion
m_Render = new RenderTexture(Screen.width, Screen.height, 24);
}
// Update is called once per frame
void Update()
{
Shader.SetGlobalFloat("_reflecFact", m_AFactor);
}
//主相机渲染完毕后调用
public void OnPostRender()
{
RenderReflection();
}
void RenderReflection()
{
#region 反射相机渲染投影
m_reCam.CopyFrom(m_MainCam);
//记录相机位置
Vector3 cameraDirectionWorldSpace = m_MainCam.transform.forward;
Vector3 cameraUpWorldSpace = m_MainCam.transform.up;
Vector3 cameraPositionWorldSpace = m_MainCam.transform.position;
//可能是投影相机的投影运算,什么摄像机的矢量,投影到地面的向量之类的玩意(我他妈也不知道的嘛,科学上网的嘛)
Vector3 cameraDirectionPlaneSpace = m_rePlane.transform.InverseTransformDirection(cameraDirectionWorldSpace);
Vector3 cameraUpPlaenSpace = m_rePlane.transform.InverseTransformDirection(cameraUpWorldSpace);
Vector3 cameraPositionPlaneSpace = m_rePlane.transform.InverseTransformPoint(cameraPositionWorldSpace);
//Mirror the Vector3 感觉是投影在下方,所以降低Y轴
cameraDirectionPlaneSpace.y *= -1.0f;
cameraUpPlaenSpace.y *= -1.0f;
cameraPositionPlaneSpace.y *= -1.0f;
//将反射摄像机的坐标转换为世界坐标
cameraDirectionWorldSpace = m_rePlane.transform.TransformDirection(cameraDirectionPlaneSpace);
cameraUpWorldSpace = m_rePlane.transform.TransformDirection(cameraUpPlaenSpace);
cameraPositionWorldSpace = m_rePlane.transform.TransformPoint(cameraPositionPlaneSpace);
//设置反射相机的位置和旋转
m_reCam.transform.position = cameraPositionWorldSpace;
m_reCam.transform.LookAt(cameraPositionWorldSpace + cameraDirectionWorldSpace, cameraUpWorldSpace);
#endregion
//将反射摄像机的影像渲染到rendertexture上
m_reCam.targetTexture = m_Render;
m_reCam.Render();
DrawQuad();
}
//画个四边形,将纹理附在画的四边形上
void DrawQuad()
{
GL.PushMatrix();
m_FloorMateria.SetPass(0);
m_FloorMateria.SetTexture("_ReflectionTex", m_Render);
GL.LoadOrtho();
//画个四边形
GL.Begin(GL.QUADS);
GL.TexCoord2(1.0f, 0.0f);
GL.Vertex3(0.0f, 0.0f, 0.0f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, 0.0f);
GL.TexCoord2(0.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 0.0f);
GL.TexCoord2(0.0f, 0.0f);
GL.Vertex3(1.0f, 0.0f, 0.0f);
GL.End();
GL.PopMatrix();
}
这个脚本需要赋值3个。 1,主摄像机,2,反射的面(就是挂载上面那个shader的平面)。3,摄像机需再赋值一个material(下面这个shader)
Properties
{
_ReflectionTex ("Texture", 2D) = "white" {}
//_reflecFact("AFacctor",Range(0f,1f))=0.5f
}
SubShader
{
Tags { "RenderType"="Transparent" }
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Stencil
{
ref 1
Comp Equal
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _ReflectionTex;
float _reflecFact;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_ReflectionTex, i.uv);
col.a=_reflecFact;
return col;
}
ENDCG
}
}
Over