赞
踩
看了冯乐乐老师的《Unity Shader入门精要》在感叹Shader牛逼的同时也对图形学产生了莫大的兴趣,推荐各位在学Unity已及对Shader有兴趣的小伙伴一定要看看。因为很基础,所以代码没什么注释。
话不多说,直接上图:
我实现的是一个呼吸效果,只截了在变化中的图。
然后上代码:
public class BloodScreen : PostEffectsBase { //本代码是放在摄像机上的,而且继承了《Unity Shader入门精要》中的 PostEffectsBase类 public Shader m_BloodScreenShader; private Material m_BloodScreenMaterial; bool down = false; public Material material { get { return m_BloodScreenMaterial= CheckShaderAndCreateMaterial (m_BloodScreenShader,m_BloodScreenMaterial); } } public Texture2D m_BloodTexture; [Range(0.0f, 1.0f)] public float m_Alpha=0.5f; void OnRenderImage(RenderTexture src,RenderTexture dest) { if (material!=null&&m_BloodTexture!=null) { //实现呼吸效果的判断,写得不太好 if (!down) { m_Alpha += Time.deltaTime; if (m_Alpha >= 1.0) down = true; } else { m_Alpha -= Time.deltaTime; if (m_Alpha <= 0) down = false; } material.SetTexture("_BloodTex", m_BloodTexture); material.SetFloat("_Alpha", m_Alpha); Graphics.Blit(src, dest,material); } else { Graphics.Blit(src, dest); } } }
PostEffectsBase类,怕大家找不着我就直接放上来吧
因为是我自己照着写的所以没有注释,如果看过书的小伙伴一定知道作用
using UnityEngine; using System.Collections; [ExecuteInEditMode] [RequireComponent (typeof(Camera))] public class PostEffectsBase : MonoBehaviour { // Called when start protected void CheckResources() { bool isSupported = CheckSupport(); if (isSupported == false) { NotSupported(); } } // Called in CheckResources to check support on this platform protected bool CheckSupport() { if (SystemInfo.supportsImageEffects == false) { Debug.LogWarning("This platform does not support image effects or render textures."); return false; } return true; } // Called when the platform doesn't support this effect protected void NotSupported() { enabled = false; } protected void Start() { CheckResources(); } // Called when need to create the material used by this effect protected Material CheckShaderAndCreateMaterial(Shader shader, Material material) { if (shader == null) { return null; } if (shader.isSupported && material && material.shader == shader) return material; if (!shader.isSupported) { return null; } else { material = new Material(shader); material.hideFlags = HideFlags.DontSave; if (material) return material; else return null; } }
接下来是Shader的代码,非常简单的代码,起最大作用的是后面的混合模式。
有关混合模式,链接: link.
Shader "Learn/BloodScreen" { Properties { _MainTex ("Texture", 2D) = "white" {} _BloodTex ("Blood Texture", 2D) = "white" {} _Alpha("Alpha",Range(0.0,1.0))=0.5 } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; sampler2D _BloodTex; float4 _BloodTex_ST; fixed _Alpha; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex); o.uv.zw = TRANSFORM_TEX(v.uv, _BloodTex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 base = tex2D(_MainTex, i.uv.xy); fixed4 blood=tex2D(_BloodTex,i.uv.zw); blood*=_Alpha; fixed4 col=base*blood+base*(1-_Alpha);//手动混合模式 return col; } ENDCG } } }
差点忘了,图片,可以在爱给网下载http://www.aigei.com/s?q=%E8%A1%80%E6%B6%B2&type=game&page=2
好了,我的第一篇博客到此结束,有不对的地方欢迎指正,感谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。