Hello Guest

Author Topic: Changing RenderQueue at Runtime  (Read 5350 times)

boehmz

  • Newbie
  • *
  • Posts: 7
    • View Profile
Changing RenderQueue at Runtime
« on: November 28, 2012, 09:13:12 pm »
For my main character, I want him to show up in front of everything else on screen as a black silhouette if he's completely behind an object but not if he's only partially covered.  I'm trying to modify the renderqueue value to be in front of everything else when he is covered and have the same value as everything else when not.

Code: [Select]
Debug.Log("render queue = " + anim.renderer.material.renderQueue);
if(Physics.Raycast(ray, out hit))
{

if(!hit.collider.gameObject.GetComponent<ProtagonistScript>())
        {

       if (anim.color != Color.black)
      {
        Debug.Log("increasing render queue");
anim.renderer.material.renderQueue += 15;
anim.color = Color.black;
}
}
else if (anim.color != Color.white)
{
                Debug.Log("decreasing render queue");
anim.renderer.material.renderQueue -= 15;
anim.color = Color.white;
}

anim is a tk2dAnimatedSprite.
When testing this, when I walk behind an object I see the "increasing render queue" called and the "decreasing render queue" call when I walk out from behind it.  But the main character never gets drawn on top and the renderqueue always gets printed at the same number.

Am I not changing render queue value properly?  My other option is to make another shader with a different renderqueue value and change the shader, but I imagine dynamically switching shaders at runtime would be a performance issue.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing RenderQueue at Runtime
« Reply #1 on: November 28, 2012, 11:14:53 pm »
you're changing the materials renderQueue (make sure you changed sharedMaterial, not material - material will just create a copy each time and leak lots and lots of materials).

Changing renderqueue is the same as changing material though, as you'll need a unique material for the sprite to be able to change the render queue on that alone and not everything else.

However, if you're not using a perspective camera, why dont you just move the sprite closer to the camera (have 2 distances for the sprite, near and far) when you need it to draw it in front of everything and back to the original place otherwise? To me this is a much better solution, as the sprites will still be able to batch.