一日一Shader·几何渲染之爆炸【SS_9】

今天温习一下几何渲染,先拿个简单的效果热个身:

一日一Shader·几何渲染之爆炸【SS_9】_第1张图片

这个是三角面沿法线偏移产生的效果,并用cs脚本控制动画播放。

Shader "MyShader/SS_9"  
{  
    Properties{          
        _MainTex("Texture", 2D) = ""{}  
		_Power("Power",Range(0,10))=0.5		
    }  
    SubShader
    {		
		CGINCLUDE
		#include "UnityCG.cginc"	
		sampler2D _MainTex;
		float4 _MainTex_ST;
		float _Power;
		struct appdata
        {
			float4 vertex : POSITION;
			float2 uv : TEXCOORD0;			
			float3 normal : NORMAL;
		};
		struct v2f
        {
			float4 vertex : SV_POSITION;
			float2 uv : TEXCOORD0;
			float4 worldPos   : TEXCOORD1;			
			float3 normal : TEXCOORD2;
        };
			
		ENDCG
		Pass
        {  	
            CGPROGRAM 			
			#pragma vertex vert
			#pragma fragment frag
			#pragma geometry geom

			v2f vert(appdata v)
			{
				v2f o;
				o.vertex = v.vertex;				
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex);
				o.normal = v.normal;
				return o;
			}
			[maxvertexcount(21)]
			void geom(triangle v2f v[3], inout TriangleStream  tristream)
			{
				float3 normal = normalize(v[0].normal + v[1].normal + v[2].normal);
				for (int i = 0; i < 3; i++) {					
					v2f o = v[i];
					v[i].vertex.xyz += normal*_Power;				
					o.vertex = UnityObjectToClipPos(v[i].vertex.xyz);
					tristream.Append(o);
				}
				tristream.RestartStrip();
			}

			fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);				
                return col;
            }
            ENDCG
        }
    }
}  
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SS_9 : MonoBehaviour
{    
    private Material[] mats;
    private Transform[] objs;
    public float cdTime = 1;
    public float maxValue = 0.7f;
    public float speed = 1;
    public float rotSpeed = 1;

    public Material[] Mats
    {
        get
        {
            if (mats == null)
            {
                mats = new Material[Objs.Length];
                for(var i = 0; i < mats.Length; i++)
                {
                    mats[i] = Objs[i].GetComponent().material;
                }
            }
            return mats;
        }

        set
        {
            mats = value;
        }
    }

    public Transform[] Objs
    {
        get
        {
            if (objs == null)
            {
                objs = transform.GetChildrenToArray();               
            }
            return objs;
        }

        set
        {
            objs = value;
        }
    }

    IEnumerator Start()
    {
        for (var i = 0; i < Mats.Length; i++)
        {
            StartCoroutine(IE_OnceAnim(i));
            StartCoroutine(IE_Rot(i));
            yield return new WaitForSeconds(cdTime);
        }
    }
    IEnumerator IE_Rot(int _index)
    {
        while (true)
        {
            Objs[_index].Rotate(Vector3.forward, rotSpeed * Time.deltaTime);
            yield return null;
        }
    }
    IEnumerator IE_OnceAnim(int _index)
    {
        //当一个球消失时      
        var _cur = 0f;
        while (true)
        {
            Mats[_index].SetFloat("_Power", _cur);
            if (_cur > maxValue)
            {
                _cur = 0;
                Mats[_index].SetFloat("_Power", _cur);
                yield return new WaitForSeconds(cdTime);
            }            
            _cur += Time.deltaTime * speed;           
            yield return null;
        }
    }
}

 

你可能感兴趣的:(Shader)