当前位置:网站首页>Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
2022-08-01 02:49:00 【ttod】
首先是要创建一个反向摄像机,同时让该反向摄像机渲染的图像发送给反射平面的Shader来与原来的颜色叠加,代码参考如下:
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);
}
}
其次是用于叠加反射颜色的Shader,对应的材质是添加给反射平面的。代码如下:
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"
}
边栏推荐
- Completely closed Chrome updated and in the top right corner of the tip
- 指定set 'execution.savepoint.path'后,重启flinksql报这个错是啥
- 数据中台建设(七):数据资产管理
- The kernel of the decompression process steps
- "Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
- 彻底关闭Chrome浏览器更新及右上角的更新提示
- Simple vim configuration
- RTL8762DK RTC (5)
- 开源项目站点必备&交流区功能
- Chinese version of Pylint inspection rules
猜你喜欢
[cellular automata] based on matlab interface aggregation cellular automata simulation [including Matlab source code 2004]
By CSDN, torn
ARM cross compilation
LeetCode每日一练 —— 环形链表问题(面试四连问)
【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
Flink 部署和提交job
【分层强化学习】HIRO:Data-Efficient Hierarchical Reinforcement Learning
The bigger and bigger the project is, I split it like this
Solve the problem that when IDEA creates a new file by default, right-click, new, there is no XML file
带wiringPi库在unbutu 编译 并且在树莓派运行
随机推荐
[Data analysis] Based on matlab GUI student achievement management system [including Matlab source code 1981]
一个service层需要调用另两个service层获取数据,并组装成最后的数据,数据都是list,缓存如何设计?
【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)
Nmap 操作手册 - 完整版
High dimensional Gaussian distribution basics
Unity3D study notes 10 - texture array
测试
win10 固定本机IP
Data Middle Office Construction (VII): Data Asset Management
HCIP(14)
Soft Exam Senior System Architect Series: Basic Knowledge of Information Systems
Soft Exam Senior System Architect Series: Basic Knowledge of System Development
Basic usage concepts of vim
The fledgling Xiao Li's 114th blog project notes: Wisdom cloud intelligent flower watering device combat (3) - basic Demo implementation
高维高斯分布基础
内核的解压缩过程详解
机器学习初学者可以学哪些实战项目?
787. 归并排序
opencv 缩小放大用哪种插值更好??
The fledgling Xiao Li's 112th blog project notes: Wisdom cloud intelligent flower watering device actual combat (1) - basic Demo implementation