零时,Unity小实验 GS部分(一)

@author 小道

发现Unity用来做一些小的渲染实验嗨挺方便。(N年前了,又重新打开试试)

一、Scene视图 ,对齐相机到视图

这个UE是自带了,Unity发现没,就写了,有点丑将就。主要用来后面实验方便点

 

零时,Unity小实验 GS部分(一)_第1张图片

 

public class CameraEditor_Tool
{
[MenuItem("DZEditorTool/DZCameraTool/Align/SceneView")]
    static void AlignToSceneView()
    {
if(Selection.gameObjects.Length >0)
{
Camera viewCame = Selection.gameObjects[0].GetComponent();
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()!=null)
     {
                Vector2 mousePosition = Event.current.mousePosition;
 
                EditorUtility.DisplayPopupMenu(new Rect(mousePosition.x, mousePosition.y, 0, 0), "DZEditorTool/",null);
     Event.current.Use();
     }          
     }
     }
}
 
二、计算着色器
------Unreal中Shader本身不是很麻烦,主要是按框架写套路麻烦就懒了
 
原理简单,只是试试本身(该Shader转的,其实很多Shader和算法本身是已经存在,看完改改哦)
零时,Unity小实验 GS部分(一)_第2张图片

 

 
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 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
}
}
}

你可能感兴趣的:(零时,Unity小实验 GS部分(一))