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

Frustum Culling acting up (probably)

Discussion in 'General Graphics' started by skarik, Feb 8, 2015.

  1. skarik

    skarik

    Joined:
    Jan 4, 2015
    Posts:
    3
    I'm a Unity Free pleb. Can I post here?

    So I made this material. Pretty nifty.


    I have a script where I apply it to the world.
    Code (csharp):
    1.     void AddChildPulseMaterial(Transform t_transform)
    2.     {
    3.         foreach (Transform child in t_transform)
    4.         {
    5.             AddChildPulseMaterial(child);
    6.         }
    7.         if (t_transform.renderer)
    8.         {
    9.             //Bounds t_bounds = t_transform.renderer.bounds;
    10.             Material [] t_materialList = new Material[t_transform.renderer.materials.Length+1];
    11.             for (int i = 0; i < t_materialList.Length - 1; ++i)
    12.             {
    13.                 t_materialList[i] = t_transform.renderer.materials[i];
    14.             }
    15.             t_materialList[t_materialList.Length - 1] = m_pulseMaterial;
    16.             t_transform.renderer.materials = t_materialList;
    17.             //Debug.Log(t_transform.renderer.bounds);
    18.             //Debug.Log("original:");
    19.             //Debug.Log(t_bounds);
    20.         }
    21.     }
    The bounds are totally still valid, yo, so why are my peeps floatin? I feel like the issue's in that line 13, but I'm not so clever, ya feel me? It keeps making an instance of what's already in there, and maaan, I don't want no instance!

    In game:


    In the viewport:



    But when I use the inspector and I do it by hand, it totally works! Totes cray, mang.

    I could totes cave in an just make an editor script, but I live for that challenge. Except nobody got no time for overcoming challenges, man! What, graphics? I got some gameplay to make, y'know? But, like, for future reference: an answer would be cool.

    Here's the shader as some tax because I'm a dirty pleb
    Code (csharp):
    1. Shader "Custom/PulseEffect"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Glow Color", Color) = (0.5,0.5,0.5,0.5)
    6.         _PulseWidth ("Pulse Width", float) = 2.0
    7.         _PulsePosition ("Pulse Radius", float) = 15.0
    8.         _PulseDistance ("Pulse Distance", float) = 300.0
    9.     }
    10.     Category
    11.     {
    12.         Tags
    13.         {
    14.             "RenderType"="Opaque"
    15.             "Queue"="Geometry+1"
    16.         }
    17.         Cull Off
    18.         Lighting Off
    19.         ZWrite Off
    20.         ZTest Always
    21.  
    22.         Blend SrcAlpha OneMinusSrcAlpha
    23.    
    24.         SubShader
    25.         {
    26.             Pass
    27.             {
    28.                 CGPROGRAM
    29.                 #pragma vertex vert
    30.                 #pragma fragment frag
    31.  
    32.                 #include "UnityCG.cginc"
    33.  
    34.                 fixed4 _Color;
    35.                 fixed _PulseWidth;
    36.                 fixed _PulsePosition;
    37.                 fixed _PulseRadius;     // Set via global
    38.                 fixed _PulseDistance;   // Set via panel for debug purposes (never actually used)
    39.  
    40.                 struct appdata_t {
    41.                     float4 vertex : POSITION;
    42.                     float3 normal : NORMAL;
    43.                 };
    44.                 struct v2f {
    45.                     float4 vertex : SV_POSITION; // todo: find the documentation for these colon attrb thingies
    46.                     float4 vertCamDelta : TEXCOORD0;
    47.                     float3 normal : TEXCOORD1;
    48.                 };
    49.  
    50.                 // Vertex routine
    51.                 v2f vert (appdata_t v)
    52.                 {
    53.                     v2f o;
    54.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    55.  
    56.                     // math in viewspace? want my worldspace, I can't handle this
    57.                     float4 t_camPos         = mul(UNITY_MATRIX_V, float4(0,0,0,0));
    58.                     float4 t_vertPosition   = mul(UNITY_MATRIX_MV, v.vertex);
    59.                     float3 t_vertNormal     = mul(float3x3(UNITY_MATRIX_MV), v.normal);
    60.  
    61.                     o.vertCamDelta = t_camPos - t_vertPosition;
    62.                     o.vertCamDelta.w = 0;
    63.  
    64.                     o.normal = normalize(t_vertNormal); // We normalize because 3x3 gets scaled down yo. Stop forgettin that
    65.  
    66.                     return o;
    67.                 }
    68.            
    69.                 // Fragment/Pixel routine
    70.                 fixed4 frag (v2f i) : SV_Target
    71.                 {
    72.                     float t_distance = length(i.vertCamDelta);
    73.                     float t_width = _PulseWidth * (t_distance/_PulseDistance);
    74.                     if ( abs( t_distance - _PulsePosition ) > t_width && abs( t_distance - _PulseRadius ) > t_width )
    75.                     {
    76.                         discard;
    77.                     }
    78.  
    79.                     // Brighten if the normal is...uh...
    80.                     // Not exactly facing the camera, but it helps show the shape
    81.                     float t_colorStrength = 0;
    82.                     t_colorStrength = dot(i.normal,float3(0,0,1)) + 1;
    83.  
    84.                     // Set the color
    85.                     float4 t_result = _Color;
    86.                     t_result.rgb *= t_colorStrength; // STRIKE IT DOWN
    87.                     // Fade out at the max distance
    88.                     t_result.a *= max( 0, 1-t_distance/_PulseDistance );
    89.  
    90.                     return t_result;
    91.                 }
    92.                 ENDCG
    93.             }
    94.         }
    95.     }
    96. }

    But seriously, some insight appreciated.
    Summary:
    -When I add a material pass (forward renderer) to a bunch of objects, the scaled ones' frustum culling doesn't work correctly on the instanced passes.
    -Suspected something about making instances of original materials, but not sure why that would kill the pass.
    -When I add the same material pass manually, everything is cool.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    No...Unity Free users should be seen but not heard. Wellllll, OK, maybe this once. But don't bother the Pro users. Don't even look at them.

    renderer.sharedMaterials.

    --Eric