零時,Unity小實驗 GS部分(一)

@author 小道算法

發現Unity用來作一些小的渲染實驗嗨挺方便。(N年前了,又從新打開試試)app

1、Scene視圖 ,對齊相機到視圖框架

這個UE是自帶了,Unity發現沒,就寫了,有點醜將就。主要用來後面實驗方便點spa

 

 

public class CameraEditor_Tool
{
[MenuItem("DZEditorTool/DZCameraTool/Align/SceneView")]
    static void AlignToSceneView()
    {
if(Selection.gameObjects.Length >0)
{
Camera viewCame = Selection.gameObjects[0].GetComponent<Camera>();
if( viewCame != null)
{
Camera cas = UnityEditor.SceneView.lastActiveSceneView.camera;
if(cas == null || cas == viewCame)
return;
Transform camT = cas.transform;
viewCame.gameObject.transform.position = camT.position;
viewCame.gameObject.transform.LookAt(camT.position + camT.forward*10);
}
}
    }
 
 
 
    [InitializeOnLoadMethod]
     static void StartInitializeOnLoadMethod()
     {
     EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
     }
 
     static void OnHierarchyGUI(int instanceID, Rect selectionRect)
     {
     if (Event.current != null && selectionRect.Contains(Event.current.mousePosition)
     && Event.current.button == 1 && Event.current.type <= EventType.MouseUp)
     {
     GameObject selectedGameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
             //這裏能夠判斷selectedGameObject的條件
     if (selectedGameObject&&selectedGameObject.GetComponent<Camera>()!=null)
     {
                Vector2 mousePosition = Event.current.mousePosition;
 
                EditorUtility.DisplayPopupMenu(new Rect(mousePosition.x, mousePosition.y, 0, 0), "DZEditorTool/",null);
     Event.current.Use();
     }          
     }
     }
}
 
2、計算着色器
------Unreal中Shader自己不是很麻煩,主要是按框架寫套路麻煩就懶了
 
原理簡單,只是試試自己(該Shader轉的,其實不少Shader和算法自己是已經存在,看完改改哦)

 

 
Shader "RenderStudio/Geo/Geo_ParticleExp_Beta"
{
Properties
{
    //細分相關變量
    _Level("Level",int)=0
    _DispDir("Displacement Direction",Vector)=(0,0,0)
    _uVelScale("VelScale",float)=2
    //粒子化特效相關變量
        _Speed("Speed",Range(0,1))=1
        _ShaderStartTime("Shader Start Time",float)=0
        _FinalColor("Final Color",color)=(1,1,1,1)
}

SubShader
{
        Tags{"RenderType"="Transparent" "Queue" = "Transparent"}
LOD 100
 
Pass
{
   Blend SrcAlpha OneMinusSrcAlpha // use alpha blending
    cull off

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
 
#include "UnityCG.cginc"
//CPU輸入變量
////細分相關變量
uniform int _Level;
            uniform float3 _DispDir;
            uniform float _uVelScale;
            ////粒子化特效相關變量
            uniform float _Speed;           //粒子位移速度
            uniform float _ShaderStartTime; //粒子化起始時間
            uniform fixed4 _FinalColor;     //粒子顏色

            //內部變量
            float3 V0, V1, V2;
            float3 CG;
            float unityTime;

struct appdata
{
float4 vertex : POSITION;
float3 normal:NORMAL;
};
 
struct v2g
{
float4 vertex : SV_POSITION;
fixed4 color:COLOR;
float3 normal:NORMAL;
};

struct g2f
{
float4 vertex : SV_POSITION;
fixed4 color:COLOR;
};
 
v2g vert (appdata v)
{
v2g o;
o.vertex = v.vertex;
o.normal=UnityObjectToWorldNormal(v.normal);
return o;
}

[maxvertexcount(120)]//v2g input[3]
void geom(inout PointStream<g2f> OutputStream,triangle v2g input[3])
{
float time_SinceBirth=(unityTime-_ShaderStartTime)*0.1f;
            g2f o = (g2f)0;
             V1 = (input[1].vertex - input[0].vertex).xyz;
             V2 = (input[2].vertex - input[0].vertex).xyz;
             V0 = input[0].vertex.xyz;
             CG=(input[0].vertex.xyz + input[1].vertex.xyz+ input[2].vertex.xyz)/3.0f;

             int numLayers =1<<_Level;     //2^_Level
             float dt = 1.0f / float( numLayers );
float t = 1.0f;
for( int it = 0; it < numLayers; it++ )
             {
                float smax = 1.0f - t;
                int nums = it + 1;
                float ds = smax / float( nums - 1 );
                float s = 0;
                for( int is = 0; is < nums; is++ )
                {
                    float3 v = V0 + s*V1 + t*V2;
                    float3 vel = _uVelScale * ( v - CG );
                    v = CG + vel*(_Speed*time_SinceBirth+1.0f) + 0.5f*_DispDir.xyz*sin(it*is)*(_Speed*time_SinceBirth)*(_Speed*time_SinceBirth);
                    o.vertex = UnityObjectToClipPos(float4( v, 1.0f ));
                    o.color=_FinalColor;
                    o.color.w=1.0f-smoothstep(0,1.0f,time_SinceBirth);
                    OutputStream.Append(o);
                 s += ds;
             }
                t -= dt;
             }
}

fixed4 frag (g2f i) : SV_Target
{
          return i.color;
}
ENDCG
}
}
}
相關文章
相關標籤/搜索