Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Shader issue with Shuriken

Discussion in 'Shaders' started by descenderdante, Mar 12, 2013.

  1. descenderdante

    descenderdante

    Joined:
    Sep 3, 2009
    Posts:
    218
    Ok , so i wrote a shader that adds a "bendy" effect to a object ... issue is that it does not work properly with shrunken , when using billboarded shrunken the mesh just disassapers while a mesh type shuriken particle works just fine , let me know if you have any thoughts why it's not working or any walkarounds

    here is the CG program

    PHP:
    CGPROGRAM
                
    #pragma vertex vert
                #pragma fragment frag
                #pragma fragmentoption ARB_precision_hint_fastest
                #pragma multi_compile_particles


                #include "UnityCG.cginc"


                
    sampler2D _MainTex;
                
    fixed4 _TintColor;
                
                
    float _XBend;
                
    float _YBend;
                
                
    struct appdata_t {
                    
    float4 vertex POSITION;
                    
    fixed4 color COLOR;
                    
    float2 texcoord TEXCOORD0;
                } ;


                
    struct v2f {
                    
    float4 vertex POSITION;
                    
    fixed4 color COLOR;
                    
    float2 texcoord TEXCOORD0;
                    
    #ifdef SOFTPARTICLES_ON
                    
    float4 projPos TEXCOORD1;
                    
    #endif
                
    } ;
                
                
    float4 _MainTex_ST;
                
                
    v2f vert (appdata_t v)
                {
                    
    v2f o;
                    
                    
                    
                    
    float4 pos mul(_Object2Worldv.vertex);
                      
                      if(
    pos._WorldSpaceCameraPos.11.0f)
                      {
                          
    float zDiff pos._WorldSpaceCameraPos.x;
                          
    float zDiffSquared zDiff zDiff;
                          
    float bendAmountX zDiffSquared _XBend;
                        
    float bendAmountY zDiffSquared _YBend;
                        
                          
    pos.+= bendAmountX;
                          
    pos.+= bendAmountY;
                      }
                      
                      
    float4 lPos mul(_World2Object pos);
                      
                      
    v.vertex lPos;
                    
                    
    o.vertex mul(UNITY_MATRIX_MVPv.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);
                    return 
    o;
                }


                
    sampler2D _CameraDepthTexture;
                
    float _InvFade;
                
                
    fixed4 frag (v2f i) : COLOR
                
    {
                    
    #ifdef SOFTPARTICLES_ON
                    
    float sceneZ LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTextureUNITY_PROJ_COORD(i.projPos))));
                    
    float partZ i.projPos.z;
                    
    float fade saturate (_InvFade * (sceneZ-partZ));
                    
    i.color.*= fade;
                    
    #endif
                    
                    
    return 2.0f i.color _TintColor * (tex2D(_MainTexi.texcoord).i.color.2.0f);
                }
                
    ENDCG 


    This part actually does the bending
    PHP:
        float4 pos mul(_Object2Worldv.vertex);
                      
                      if(
    pos._WorldSpaceCameraPos.11.0f)
                      {
                          
    float zDiff pos._WorldSpaceCameraPos.x;
                          
    float zDiffSquared zDiff zDiff;
                          
    float bendAmountX zDiffSquared _XBend;
                        
    float bendAmountY zDiffSquared _YBend;
                        
                          
    pos.+= bendAmountX;
                          
    pos.+= bendAmountY;
                      }
                      
                      
    float4 lPos mul(_World2Object pos);
                      
                      
    v.vertex lPos;
                 
    This is what the shader does in game

    $ingamescreem.png
     
    Last edited: Mar 12, 2013
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
  3. descenderdante

    descenderdante

    Joined:
    Sep 3, 2009
    Posts:
    218
    Worked , thank you
     
  4. CPXTom

    CPXTom

    Joined:
    Apr 24, 2010
    Posts:
    113
    Sorry to revive an old thread, but I'm pretty much doing the same thing and using this solution does not work :( Did you have to do anything else to get particles to render properly?

    Is using _World2Object after the transformations still okay?