Search Unity

Trying to mod shader to work with particle color over time

Discussion in 'Shaders' started by SpaceOwlGames, Aug 30, 2017.

  1. SpaceOwlGames

    SpaceOwlGames

    Joined:
    Apr 22, 2016
    Posts:
    61
    Hello,
    Preface: I know nothing about shaders.

    However, I have this shader that came from an asset pack that I like, but it doesn't work with the particle color over time module. Some research says that the color must be _Color and it must be a vertex color (whatever that means), I've tried to modify the shader to match the examples to no avail, could anyone take a quick peek and let me know what exactly needs to change to get the color over time working? Much appreciated!

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    4. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    5.  
    6. Shader "FORGE3D/Billboard Nebula Custom"
    7. {
    8.     Properties
    9.     {
    10.         _CoronaNoise("Texture", 2D) = "black" {}
    11.         _Color("Color", Color) = (0.5,0.5,0.5,0.5)
    12.         _EdgeMaskFalloff("Edge Mask Falloff", float) = 0
    13.         _EdgeMaskPower("Edge Mask Power", float) = 0
    14.         _MinFadeStart("Min Fade Start", float) = 0
    15.         _MinFadeEnd("Min Fade End", float) = 0
    16.         _MaxFadeStart("Max Fade Start", float) = 0
    17.         _MaxFadeEnd("Max Fade End", float) = 0
    18.     }
    19.  
    20.     Category
    21.     {
    22.    
    23.        
    24.        
    25.            
    26.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    27.         Blend One One
    28.         AlphaTest Greater .01
    29.         ColorMask RGB
    30.         Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
    31.         // Tags { "RenderType"="Opaque" }
    32.         SubShader {  
    33.  
    34.         // Disable Dynamic Batching
    35.         //Pass{}
    36.  
    37.         Pass {
    38.  
    39.             CGPROGRAM
    40.              #pragma vertex vert
    41.             #pragma fragment frag
    42.           //  #pragma only_renderers opengl d3d9 d3d11
    43.             #pragma glsl
    44.          //   #pragma target 3.0
    45.             #pragma multi_compile_particles
    46.             #include "UnityCG.cginc"
    47.            
    48.            
    49.  
    50.             float4 _Color;
    51.  
    52.             float _EdgeMaskFalloff, _EdgeMaskPower;
    53.             float _MinFadeStart, _MinFadeEnd;
    54.             float _MaxFadeStart, _MaxFadeEnd;
    55.  
    56.             sampler2D _CoronaNoise;
    57.  
    58.        
    59.  
    60.             struct VertexInput
    61.             {
    62.                 float4 vertex : POSITION;
    63.                 float3 normal : NORMAL;  
    64.                 float2 texcoord0 : TEXCOORD0;
    65.             };
    66.  
    67.             struct VertexOutput
    68.             {                  
    69.                 float4 pos : POSITION;
    70.                 float4 posWorld : TEXCOORD0;
    71.                 float3 normalDir : TEXCOORD1;            
    72.                 float2 uv : TEXCOORD2;  
    73.             };
    74.  
    75.             VertexOutput vert (VertexInput v)
    76.             {
    77.                 VertexOutput o;
    78.                    o.normalDir = mul(float4(v.normal,0), unity_WorldToObject).xyz;              
    79.                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
    80.                 o.pos = UnityObjectToClipPos(v.vertex);
    81.                 o.uv = v.texcoord0;
    82.        
    83.            
    84.                 return o;
    85.             }
    86.  
    87.        
    88.  
    89.          
    90.  
    91.  
    92.             float4 frag(VertexOutput i) : COLOR
    93.             {
    94.                
    95.                 float3 finalColor = tex2D(_CoronaNoise, i.uv.xy);
    96.  
    97.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
    98.                 float viewDot = abs(dot(viewDirection, normalize(i.normalDir)));          
    99.                 viewDot = pow(viewDot, _EdgeMaskFalloff) * _EdgeMaskPower;
    100.                 viewDot = clamp(viewDot, 0 ,1);
    101.  
    102.                 float dist = distance(_WorldSpaceCameraPos.xyz, i.posWorld.xyz);
    103.                 float MinFade = saturate((_MinFadeEnd - dist) / (_MinFadeStart -_MinFadeEnd));
    104.                 float MaxFade = saturate((_MaxFadeEnd - dist) / (_MaxFadeEnd - _MaxFadeStart));
    105.  
    106.                
    107.                 return float4(saturate(pow(finalColor * viewDot *_Color, 1.3) * 1.5 * MaxFade * MinFade), 1);
    108.  
    109.            
    110.             }
    111.  
    112.             ENDCG  
    113.         }
    114.     }    
    115.      
    116. }
    117. }
    118.  
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Your Vertex structs don't contain any color information. You need to assign the vertex color from the mesh to a variable in your VertexOutput struct and then do something with it in the frag.

    _Color is just a color property on your material, it won't have anything to do with vertex color.