這是我在《游戲架構-核心技術與面試精粹》看的蟆技,記錄一下~
泛光和輝光不一樣么汰现?
那是肯定的
輝光(Glow):是全屏泛光的升級版本
區(qū)別就是輝光要求場景內(nèi)只有部分物體泛光,而不是全部泛光
所以需要區(qū)分出忽略那些物體
大體思路:
1.用一個額外的攝像機給,在這個攝像機下宴合,所有的非泛光物體都是黑色的,泛光物體保持原樣
2.再將這個結果渲染到一個 RT 上迹鹅,對他進行模糊
3.最后將模糊后的反光圖疊加到主攝像機的渲染圖像上
創(chuàng)建一個 LightGeometry.shader
默認的surface shader卦洽,改了 RenderType
RenderType 可以使用自定義名稱,并不會對改 shader 的使用和著色器產(chǎn)生影響
需要自己區(qū)別場景中不同渲染對象使用的 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"
}
在相機那邊斜棚,可以通過該函數(shù) 動態(tài)替換攝像機下物體的 shader
GetComponent<Camera>().RenderWithShader(replaceShader, "RenderType");
當調(diào)用 RenderWithShader 是阀蒂,會檢查攝像機下的每個物體的 RenderTag
(通常選取 Tag 的條件都是 RenderType)
替換的過程:
1.遍歷所有要顯示的物體
2.如果物體的 Shader 與傳入的 replaceShader 有相同的 RenderType
3.則用 replaceShader 的響應 subshader 替換
下面的 shader该窗,增加了一個 subshader
用來將 RenderType 為 Opaque 的物體替換成為黑色,而為 Glow 的內(nèi)容保持不變
(不發(fā)光的物體要繪制呈黑色蚤霞,避免遮擋關系會出錯)
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"
}
再創(chuàng)建一個 CustomGlow.cs 腳本酗失,負責:
1.在主攝像機下創(chuàng)建了參數(shù)相同的子攝像機
2.并把這個攝像機的渲染目標設置為內(nèi)存中的一個 RenderTextrue
3.渲染時,通過 RenderImage 將其與主攝像機的內(nèi)容進行混合
4.使用 RenderWithShader 實現(xiàn)特效時争便,應該將相機設為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);
}
}
最后創(chuàng)建一個 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
}
}
}
最后把它掛到相機上,掛到相機上: