Di chuyển kết cấu theo vị trí trong shader


8

Divine Original Sin có hiệu ứng hạt tuyệt đẹp, Khi tôi di chuyển xung quanh trò chơi, tôi thấy thiên hà thông qua các hạt di chuyển theo vị trí của mình. Làm thế nào tôi có thể làm như vậy?

bạn có thể thấy hiệu ứng này tại đây: https://youtu.be/4fIpQZ2sIAY

2017-12-28_11-42-01

Câu trả lời:


13

hồ sơ_2017_12_28_20_34_11_392

thường phối hợp uv được sử dụng cho uvmapping nhưng ở đây Thay vì sử dụng vị trí sử dụng uvcoordine!

Shader "Custom/ViewMode1"
{ 
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _Color ("_Color", Color) = (1,1,1,1)
        _Size("Size",Vector) = (1,1,1,1)
    }

    SubShader
    {
        Tags {"Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}
        ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest
            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                half2 texcoord  : TEXCOORD0;
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
            };

            sampler2D _MainTex;
            fixed4 _Color;
            float _Speed;
            float4 _Size;


            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color;
                return OUT;
            }

            float4 frag (v2f i) : COLOR
            {
                float2 screen = i.vertex.xy/_ScreenParams.xy;
                float4 tex = tex2D(_MainTex, screen /_Size)*i.color;
                return tex*_Color;
            }
            ENDCG
        }
    }
}

bạn cũng có thể sử dụng Tex2Dprojthử điều này:

Shader "Custom/ViewMode2"
{
    Properties{
        _MainTex("MainTex",2D) = "white"{}
        _Scale("Scale",Vector) = (1,1,1,1)
    }
    SubShader
    {
        // Draw ourselves after all opaque geometry
        Tags{ "Queue" = "Transparent" }

        // Grab the screen behind the object into _BackgroundTexture
        GrabPass
        {
            "_BackgroundTexture"
        }

        Blend SrcAlpha OneMinusSrcAlpha
        // Render the object with the texture generated above, and invert the colors
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"



            struct v2f
            {
                float4 grabPos : TEXCOORD0;
                float4 pos : SV_POSITION;
            };

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            sampler2D _MainTex;
            float4 _Scale;

            v2f vert(appdata v) {
                v2f o;
                // use UnityObjectToClipPos from UnityCG.cginc to calculate 
                // the clip-space of the vertex
                o.pos = UnityObjectToClipPos(v.vertex);
                // use ComputeGrabScreenPos function from UnityCG.cginc
                // to get the correct texture coordinate
                o.grabPos = ComputeGrabScreenPos(o.pos);

                return o;
            }

            sampler2D _BackgroundTexture;

            half4 frag(v2f i) : SV_Target
            {
                half4 bgcolor = tex2Dproj(_MainTex, i.grabPos/_Scale);
                return bgcolor;
            }
            ENDCG
        }
    }
}

hồ sơ_2017_12_28_20_19_04_91

Shader "Smkgames/Particles/OriginalSin" {
    Properties {
        _MainTex ("Particle Texture", 2D) = "white" {}
        _BackGround("BackGround",2D) = "white"{}
        _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
        _Scale("Scale",Vector) = (1,1,1,1)

        _Hue ("Hue", Range(0, 1.0)) = 0
        _Saturation ("Saturation", Range(0, 1.0)) = 0.5
        _Brightness ("Brightness", Range(0, 1.0)) = 0.5
        _Contrast ("Contrast", Range(0, 1.0)) = 0.5
    }

    Category {
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
        Blend One OneMinusSrcColor
        ColorMask RGB
        Cull Off Lighting Off ZWrite Off


        SubShader {
            GrabPass
            {
                "_BackgroundTexture"
            }
            Pass {

                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma target 2.0
                #pragma multi_compile_particles
                #pragma multi_compile_fog

                #include "UnityCG.cginc"

                sampler2D _MainTex,_BackGround;
                fixed4 _TintColor;

                struct appdata_t {
                    float4 vertex : POSITION;
                    fixed4 color : COLOR;
                    float2 texcoord : TEXCOORD0;

                };

                struct v2f {
                    float4 grabPos : TEXCOORD3;
                    float4 vertex : SV_POSITION;
                    fixed4 color : COLOR;
                    fixed4 hsbc : COLOR1;
                    float2 texcoord : TEXCOORD0;
                    UNITY_FOG_COORDS(1)
                    #ifdef SOFTPARTICLES_ON
                    float4 projPos : TEXCOORD2;
                    #endif
                };

                float4 _MainTex_ST;
                float4 _Scale;

                //__________Hue Saturation Brightness Contrast___________

                fixed _Hue, _Saturation, _Brightness, _Contrast;


                inline float3 applyHue(float3 aColor, float aHue)
                {
                    float angle = radians(aHue);
                    float3 k = float3(0.57735, 0.57735, 0.57735);
                    float cosAngle = cos(angle);

                    return aColor * cosAngle + cross(k, aColor) * sin(angle) + k * dot(k, aColor) * (1 - cosAngle);
                }

                inline float4 applyHSBCEffect(float4 startColor, fixed4 hsbc)
                {
                    float hue = 360 * hsbc.r;
                    float saturation = hsbc.g * 2;
                    float brightness = hsbc.b * 2 - 1;
                    float contrast = hsbc.a * 2;

                    float4 outputColor = startColor;
                    outputColor.rgb = applyHue(outputColor.rgb, hue);
                    outputColor.rgb = (outputColor.rgb - 0.5f) * contrast + 0.5f;
                    outputColor.rgb = outputColor.rgb + brightness;
                    float3 intensity = dot(outputColor.rgb, float3(0.39, 0.59, 0.11));
                    outputColor.rgb = lerp(intensity, outputColor.rgb, saturation);

                    return outputColor;
                }
                //_______________________________________________________________________

                v2f vert (appdata_t v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    #ifdef SOFTPARTICLES_ON
                    o.projPos = ComputeScreenPos (o.vertex);
                    COMPUTE_EYEDEPTH(o.projPos.z);
                    #endif
                    o.color = v.color;
                    o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
                    UNITY_TRANSFER_FOG(o,o.vertex);
                    o.grabPos = ComputeGrabScreenPos(o.vertex);
                    o.hsbc = fixed4(_Hue, _Saturation, _Brightness, _Contrast);

                    return o;
                }

                sampler2D_float _CameraDepthTexture;
                float _InvFade;

                fixed4 frag (v2f i) : SV_Target
                {
                    #ifdef SOFTPARTICLES_ON
                    float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
                    float partZ = i.projPos.z;
                    float fade = saturate (_InvFade * (sceneZ-partZ));
                    i.color.a *= fade;
                    #endif

                    half4 col = i.color * tex2D(_MainTex, i.texcoord);
                    col.rgb *= col.a;
                    UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode
                    half4 bgcolor = tex2Dproj(_BackGround, i.grabPos/_Scale);
                    float4 hsbcColor = applyHSBCEffect(bgcolor, i.hsbc);
                    hsbcColor.rgb *= col.a;

                    return hsbcColor;
                }
                ENDCG 
            }
        } 
    }
}
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.