Search Unity

Magnifying Shader for Particle System

Discussion in 'General Discussion' started by ducloc96, May 26, 2021.

  1. ducloc96

    ducloc96

    Joined:
    Mar 18, 2018
    Posts:
    26
    Hello .I’m a beginner in unity. I’m trying to use a magnifying shader for particle system to make effect similar explosion of homescape.
    I found some magnifying shader on youtube but it not run.
    Can you guys tell me what i’m doing wrong in my shader? Thanks

    Shader "Custom/Magnifier"
    {
    Properties
    {
    _Color("Main Color", Color) = (1,1,1,1)
    _Intensity("Intensity", float) = 1
    _MainTex("Alpha (A)", 2D) = "white" {}
    _NoiseTex("Noise Texture (RG)", 2D) = "white" {}
    _ScaleStrength("Scale Strength", Range(-10, 10)) = 0.2
    _NosieStrength("Noise Strength", Range(-10, 10)) = 0.2
    }
    SubShader
    {
    Tags
    {
    "QUEUE" = "Transparent+1"
    "RenderType" = "Transparent"
    }
    Pass
    {
    Tags
    {
    }
    ZClip Off
    ZWrite Off
    Cull Off
    Stencil
    {
    Ref 0
    ReadMask 0
    WriteMask 0
    Pass Keep
    Fail Keep
    ZFail Keep
    PassFront Keep
    FailFront Keep
    ZFailFront Keep
    PassBack Keep
    FailBack Keep
    ZFailBack Keep
    }

    }
    Pass
    {
    Name "BASE"
    Tags
    {
    "LIGHTMODE" = "ALWAYS"
    "QUEUE" = "Transparent+1"
    "RenderType" = "Transparent"
    }
    ZWrite Off
    Cull Off
    Blend SrcAlpha OneMinusSrcAlpha

    CGPROGRAM

    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    #define CODE_BLOCK_VERTEX
    uniform float4 _MainTex_ST;
    uniform float4 _Color;
    uniform float _ScaleStrength;
    uniform float _NosieStrength;
    uniform sampler2D _NoiseTex;
    uniform sampler2D _MainTex;
    uniform sampler2D _GrabTexture;

    struct appdata_t {
    float4 vertex :pOSITION0;
    float4 color :COLOR0;
    float2 texcoord :TEXCOORD0;
    float2 texcoord1 :TEXCOORD1;
    };

    struct OUT_Data_Vert
    {
    float4 color :COLOR0;
    float4 texcoord :TEXCOORD0;
    float4 texcoord1 :TEXCOORD1;
    float4 vertex :SV_POSITION;
    };

    struct v2f
    {
    float4 color :COLOR0;
    float4 texcoord :TEXCOORD0;
    float4 texcoord1 :TEXCOORD1;
    };

    struct OUT_Data_Frag
    {
    float4 color :SV_Target0;
    };

    float4 u_xlat0;
    float4 u_xlat1;
    OUT_Data_Vert vert(appdata_t in_v)
    {
    OUT_Data_Vert out_v;
    //UNITY_INITIALIZE_OUTPUT(OUT_Data_Vert, out_v);
    u_xlat0 = UnityObjectToClipPos(in_v.vertex);
    out_v.vertex = u_xlat0;
    u_xlat0.xy = u_xlat0.xy / u_xlat0.ww;
    out_v.texcoord.xy = (u_xlat0.xy * float2(0.5, 0.5)) + float2(0.5, 0.5);
    u_xlat0 = in_v.color * _Color;
    out_v.color = u_xlat0;
    out_v.texcoord.zw = in_v.texcoord.xy;
    out_v.texcoord1.xy = TRANSFORM_TEX(in_v.texcoord.xy, _MainTex);
    out_v.texcoord1.z = in_v.texcoord1.x;
    return out_v;
    }

    #define CODE_BLOCK_FRAGMENT

    float2 u_xlat0_d;
    float3 u_xlat10_0;
    float u_xlat1_d;
    float2 u_xlat2;
    float2 u_xlat3;
    float u_xlat10_4;
    float u_xlat6;
    OUT_Data_Frag frag(v2f in_f)
    {
    OUT_Data_Frag out_f;
    //UNITY_INITIALIZE_OUTPUT(OUT_Data_Frag, out_f);
    u_xlat10_0.x = tex2D(_NoiseTex, in_f.texcoord1.xy).x;
    u_xlat0_d.x = u_xlat10_0.x * _NosieStrength;
    u_xlat2.xy = (-in_f.texcoord.wz) + float2(0.5, 0.5);
    u_xlat6 = dot(u_xlat2.xy, u_xlat2.xy);
    u_xlat1_d = rsqrt(u_xlat6);
    u_xlat6 = sqrt(u_xlat6);
    u_xlat3.xy = float2(u_xlat6, u_xlat6) * u_xlat2.yx;
    u_xlat2.xy = u_xlat2.xy * float2(u_xlat1_d, u_xlat1_d);
    u_xlat0_d.xy = u_xlat0_d.xx * u_xlat2.xy;
    u_xlat0_d.xy = u_xlat3.xy * float2(float2(_ScaleStrength, _ScaleStrength)) + u_xlat0_d.xy;
    u_xlat10_4 = tex2D(_MainTex, in_f.texcoord1.xy).w;
    u_xlat0_d.xy = float2(u_xlat10_4, u_xlat10_4) * u_xlat0_d.xy;
    u_xlat0_d.xy = u_xlat0_d.xy * in_f.texcoord1.zz + in_f.texcoord.xy;
    u_xlat10_0.xyz = tex2D(_GrabTexture, u_xlat0_d.xy).xyz;
    out_f.color.xyz = u_xlat10_0.xyz;
    out_f.color.w = in_f.color.w;
    return out_f;
    }

    ENDCG

    }
    }
    FallBack Off
    }
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    Explain what you're trying to do and what "Magnifying shader" stands for.

    Use code tags for code so indents are formatted properly.

    Additionally, using names like this:
    Code (csharp):
    1.  
    2. loat2 u_xlat0_d;
    3. float3 u_xlat10_0;
    4. float u_xlat1_d;
    5. float2 u_xlat2;
    6. float2 u_xlat3;
    7. float u_xlat10_4;
    8. float u_xlat6;
    9.  
    Is a bad idea. Names should have descriptive names written in english. For example "particleScalingFactor". Random names makes your code harder for other people to decipher.

    Name length does not affect shader performance in any way.
     
    Last edited: May 26, 2021
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Lol quite ironic :D
    But I agree with above poster, made points.
     
    neginfinity likes this.
  4. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    417
    Funny. Where I work, we have a couple of software products that are very old and relatively stable such that we rarely have to touch the code anymore. But when we do, it's a nightmare. The original developer decided that all the variables should just have names describing the variable type. So it's a million or more lines of code with variables like..

    Code (CSharp):
    1. int int1; //integer for number of messages in queue
    2. int int2; // integer for max number of messages in queue
    3. float float1; // max life of messages in seconds
    It's a nightmare.
     
  5. This is when the unit tests and refactor gets very useful. Too bad unit tests are usually missing when they are needed and are there when no one cares. :D
     
    stain2319 likes this.
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    One of the key got tuck. It happen ometime.

    Ah, yeah, modern refactoring tools are quite miraculous. Key press, and it gets renamed EVERYWHERE. I remember the time where a variable rename meant hunting it through the source code through compile errors.

    It will stil take quite a bit of time to figure out what each variable stands for, though.
     
    Antypodish and Lurking-Ninja like this.