Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Thruster Effect

Discussion in 'Shaders' started by Tabb, Feb 19, 2018.

  1. Tabb

    Tabb

    Joined:
    Jan 2, 2015
    Posts:
    40
    Hi,

    I'm trying to create a thruster effect for a space game.
    Like this :


    I managed to make the texture translate thanks to tutorial.
    This is my result :


    Now, I need to fade the texture so that on one end of the cylinder, the texture opacity is 100% and on the opposite side of the cylinder it is 0%.
    It will not exactly be from the extreme end but you get the idea.

    Can you help me with this ?

    Here the current shader for the texture translation :

    Code (CSharp):
    1. Shader "Unlit/Exhaust"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _ScrollSpeeds ("Scroll Speeds", vector) = (0, -20, 0, 0)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             // make fog work
    19.             #pragma multi_compile_fog
    20.          
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 UNITY_FOG_COORDS(1)
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             float4 _MainTex_ST;
    38.             // Declare our new parameter here so it's visible to the CG shader
    39.             float4 _ScrollSpeeds;
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    46.  
    47.                 // Shift the uvs over time.
    48.                 o.uv += _ScrollSpeeds * _Time.x;
    49.  
    50.                 UNITY_TRANSFER_FOG(o,o.vertex);
    51.                 return o;
    52.             }
    53.          
    54.             fixed4 frag (v2f i) : SV_Target
    55.             {
    56.                 // sample the texture
    57.                 fixed4 col = tex2D(_MainTex, i.uv);
    58.                 // apply fog
    59.                 UNITY_APPLY_FOG(i.fogCoord, col);
    60.                 return col;
    61.             }
    62.             ENDCG
    63.         }
    64.     }
    65. }
    66.  
     
  2. RepoGames

    RepoGames

    Joined:
    Apr 15, 2016
    Posts:
    69
    Just create ramp gradient texture and sample from it like tex2D(ramp, uv.y) if the uv increases with the height of the cylinder. If you want to make it simpler just multiply the color by the (1.0 - i.uv.y) in fragment shader, because the further we go with texture the uv increases.

    EDIT: I see you are modyfing uv so just copy the default uv to another TEXCOORD
     
  3. Tabb

    Tabb

    Joined:
    Jan 2, 2015
    Posts:
    40
    Thank you for your help.
    I understand the idea, but I don't know how to code this. I'm really not good with shaders.

    Do you mean I should do something like
    Code (CSharp):
    1. col = col * (1.0 - i.uv.y);


    Honestly I have no idea how to do so.

    Thank you again.
     
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,995
    use TEXCOORD1 instead of TEXCOORD0
     
  5. Tabb

    Tabb

    Joined:
    Jan 2, 2015
    Posts:
    40
    omg I feel so stupid. I don't really understand how shaders works so I really don't know how using another texcoord could do the trick :/
     
  6. Tabb

    Tabb

    Joined:
    Jan 2, 2015
    Posts:
    40
    I still doesn't made it work.
    Can you give me more details about how I can apply your solutions please ?