这是我在《游戏架构-核心技术与面试精粹》看的,记录一下~
泛光和辉光不一样么?
那是肯定的
辉光(Glow):是全屏泛光的升级版本
区别就是辉光要求场景内只有部分物体泛光,而不是全部泛光
所以需要区分出忽略那些物体
大体思路:
1.用一个额外的摄像机给,在这个摄像机下,所有的非泛光物体都是黑色的,泛光物体保持原样
2.再将这个结果渲染到一个 RT 上,对他进行模糊
3.最后将模糊后的反光图叠加到主摄像机的渲染图像上
创建一个 LightGeometry.shader
默认的surface shader,改了 RenderType
RenderType 可以使用自定义名称,并不会对改 shader 的使用和着色器产生影响
需要自己区别场景中不同渲染对象使用的 Shader 的 RenderType 的类型名称不同
Shader "Custom/LightGeometry"
{
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"="Glow" } //只改了这里
LOD 200
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;
// 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;
}
ENDCG
}
FallBack "Diffuse"
}
在相机那边,可以通过该函数 动态替换摄像机下物体的 shader
GetComponent<Camera>().RenderWithShader(replaceShader, "RenderType");
当调用 RenderWithShader 是,会检查摄像机下的每个物体的 RenderTag
(通常选取 Tag 的条件都是 RenderType)
替换的过程:
1.遍历所有要显示的物体
2.如果物体的 Shader 与传入的 replaceShader 有相同的 RenderType
3.则用 replaceShader 的响应 subshader 替换
下面的 shader,增加了一个 subshader
用来将 RenderType 为 Opaque 的物体替换成为黑色,而为 Glow 的内容保持不变
(不发光的物体要绘制呈黑色,避免遮挡关系会出错)
Shader "Custom/RenderGlowRT"
{
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"="Glow" }
LOD 200
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;
// 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;
}
ENDCG
}
SubShader
{
Tags {"RenderType" = "Opaque"}
LOD 200
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_TARGET
{
fixed4 col = fixed4(0, 0, 0, 1);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
再创建一个 CustomGlow.cs 脚本,负责:
1.在主摄像机下创建了参数相同的子摄像机
2.并把这个摄像机的渲染目标设置为内存中的一个 RenderTextrue
3.渲染时,通过 RenderImage 将其与主摄像机的内容进行混合
4.使用 RenderWithShader 实现特效时,应该将相机设为disable,防止影响原图的绘制
public class CustomGlow : MonoBehaviour
{
public Shader renderGlowShader;
public Material blurMaterial;
public Material mixMaterial;
private RenderTexture rt;
private void Start()
{
Camera mainCam = GetComponent<Camera>();
this.rt = new RenderTexture(Screen.width, Screen.height, (int)mainCam.depth);
GameObject go = new GameObject("CameraRT");
go.transform.SetParent(transform, false);
Camera cam = go.AddComponent<Camera>();
cam.enabled = false;
cam.clearFlags = CameraClearFlags.SolidColor;
cam.backgroundColor = Color.black;
cam.orthographic = mainCam.orthographic;
cam.orthographicSize = mainCam.orthographicSize;
cam.nearClipPlane = mainCam.nearClipPlane;
cam.farClipPlane = mainCam.farClipPlane;
cam.fieldOfView = mainCam.fieldOfView;
cam.fieldOfView = mainCam.fieldOfView;
cam.targetTexture = rt;
RenderPostEffect bloomTex = go.AddComponent<RenderPostEffect>();
bloomTex.replaceShader = renderGlowShader;
bloomTex.renderMaterial = blurMaterial;
mixMaterial.SetTexture("_BlurTex", rt);
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination, mixMaterial);
}
}
最后创建一个 mixTexture.shader 文件
Shader "Custom/MixTexture"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BlurTex ("Blur Textrue", 2D) = "White" {}
_BlurFactor ("Blur Factor", Range(0, 1)) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
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 _MainTex;
float4 _MainTex_ST;
sampler2D _BlurTex;
float4 _BlurTex_ST;
float _BlurFactor;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 blur = tex2D(_BlurTex, i.uv);
fixed4 final = col + blur * _BlurFactor;
// apply fog
UNITY_APPLY_FOG(i.fogCoord, final);
return final ;
}
ENDCG
}
}
}
最后把它挂到相机上,挂到相机上: