当前位置:网站首页>Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
2022-08-01 03:15:00 【ttod】
The first is to create a reverse camera,At the same time let the reverse camera rendered image sent to the reflecting planeShaderCome with the original color overlay,代码参考如下:
using UnityEngine;
public class PlanarReflection : MonoBehaviour
{
Camera m_ReflectionCamera;
Camera m_MainCamera;
RenderTexture m_RenderTarget;
[SerializeField]
GameObject m_ReflectionPlane;
[SerializeField]
Material m_FloorMaterial;
[SerializeField]
[Range(0f, 1f)]
float m_ReflectionFactor = 0.5f;
void Start()
{
GameObject reflectionCameraGo = new GameObject("ReflectionCamera");
m_ReflectionCamera = reflectionCameraGo.AddComponent<Camera>();
m_ReflectionCamera.enabled = false;
m_MainCamera = Camera.main;
m_RenderTarget = new RenderTexture(Screen.width, Screen.height, 24);
}
void Update()
{
m_FloorMaterial.SetFloat("_ReflectionFactor", m_ReflectionFactor);
}
void OnPreRender()
{
RenderReflection();
}
void RenderReflection()
{
m_ReflectionCamera.CopyFrom(m_MainCamera);
Vector3 cameraDirectionWorldSpace = m_MainCamera.transform.forward;
Vector3 cameraUpWorldSpace = m_MainCamera.transform.up;
Vector3 cameraPositionWorldSpace = m_MainCamera.transform.position;
Vector3 cameraDirectionPlaneSpace = m_ReflectionPlane.transform.InverseTransformDirection(cameraDirectionWorldSpace);
Vector3 cameraUpPlaneSpace = m_ReflectionPlane.transform.InverseTransformDirection(cameraUpWorldSpace);
Vector3 cameraPositionPlaneSpace = m_ReflectionPlane.transform.InverseTransformPoint(cameraPositionWorldSpace);
cameraDirectionPlaneSpace.y *= -1f;
cameraUpPlaneSpace.y *= -1f;
cameraPositionPlaneSpace.y *= -1f;
cameraDirectionWorldSpace = m_ReflectionPlane.transform.TransformDirection(cameraDirectionPlaneSpace);
cameraUpWorldSpace = m_ReflectionPlane.transform.TransformDirection(cameraUpPlaneSpace);
cameraPositionWorldSpace = m_ReflectionPlane.transform.TransformPoint(cameraPositionPlaneSpace);
m_ReflectionCamera.transform.position = cameraPositionWorldSpace;
m_ReflectionCamera.transform.LookAt(cameraPositionWorldSpace + cameraDirectionWorldSpace, cameraUpWorldSpace);
m_ReflectionCamera.targetTexture = m_RenderTarget;
m_ReflectionCamera.Render();
m_FloorMaterial.SetTexture("_ReflectionTex", m_RenderTarget);
}
}The second is that used to overlay reflection colorShader,The corresponding material is added to the reflection plane.代码如下:
Shader "Custom/FloorTotal"
{
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
_ReflectionFactor("ReflectionFactor",Range(0,1)) = 0.5
[Hide]_ReflectionTex("ReflectionTex",2D) = "White"{}
}
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;
sampler2D _ReflectionTex;
struct Input
{
float2 uv_MainTex;
float4 screenPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float _ReflectionFactor;
// 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
float2 uv = IN.screenPos.xy / IN.screenPos.w;
uv.x = 1 - uv.x;
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color * (1 - _ReflectionFactor) + _ReflectionFactor * tex2D(_ReflectionTex,uv);
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}边栏推荐
- Parse the bootargs from the device tree (dtb format data)
- New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment
- HCIP(15)
- 被 CSDN,伤透了心
- 指定set 'execution.savepoint.path'后,重启flinksql报这个错是啥
- Handwritten binary search tree and test
- MYSQL Keyword Explain Analysis
- Solve the problem that Excel opens very slowly after installing MySQL
- 【uniCloud】云对象的应用与提升
- IDEA无法识别module(module右下角没有蓝色小方块)
猜你喜欢

Introduction to Oracle

初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现

Which interpolation is better for opencv to zoom in and out??

Solve the problem that Excel opens very slowly after installing MySQL

【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)

解决IDEA默认情况下新建文件时,右击,new,没有XML文件的问题

ARM 交叉编译

MYSQL-Batch insert data

Summary of mobile page optimization in seconds

每周小结(*67):为什么不敢发表观点
随机推荐
pdb drug comprehensive database
One service layer needs to call the other two service layers to obtain data and assemble it into the final data. The data is all lists. How to design the cache?
彻底关闭Chrome浏览器更新及右上角的更新提示
在打开MYSQL表时,有的可以显示编辑,有的没有,如何设置。
Flutter “Hello world“ 程代码
Four implementations of
batch insert: have you really got it? Summary of MVCC
Introduction to Oracle
Dart named parameter syntax
pdb药物综合数据库
By CSDN, torn
如何下载Keil包
【uniCloud】云对象的应用与提升
2. # 代码注释
The 16th day of the special assault version of the sword offer
IDEA修改注释字体
Introduction to the Elastic Stack
从设备树(dtb格式数据)中解析出bootargs
HCIP(14)
项目越写越大,我是这样做拆分的