Search Unity

Weapon Clipping Methods

Discussion in 'Editor & General Support' started by MarkBenitez, Oct 5, 2013.

  1. MarkBenitez

    MarkBenitez

    Joined:
    Oct 5, 2013
    Posts:
    44
    Anybody got any methods on weapon clipping besides using a second camera?Reason why i ask is because when using a second camera it does not render shadows on an object

    How hard of a task would this be to script? I feel like using a second camera takes away more fps, is that true?
     
  2. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    791
    One other way is to make the player's collider bigger to encompass the weapon. Then when shooting, just tell the shooting script (and any projectiles that it may spawn) to ignore the player's collider so you don't end up shooting yourself. In the case of firing instantiated projectiles that spawn inside the player, use Physics.IgnoreCollision to solve that.
     
    Last edited: Oct 6, 2013
  3. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    There's enlarging the player's collider and altering some code like GibTreaty suggests. As for not rendering shadows, I wonder why. I've had no problems in the past with using two cameras. I can't think of an easy way of scripting it but I more so just messing with stuff in the Inspector. And it wouldn't necessarily lower the frames it depends on certain factors involved like the platform, how many objects are getting drawn or redrawn, the complexity of the object or scene rather, the ability of your computer or developing environment and stuff like that.
     
  4. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    791
    Well if you're weapon camera only renders the weapon and nothing else that means it won't receive shadows from buildings, players and other things. Likewise, the main camera won't render weapon shadows onto the ground because the weapon is not being rendered on the main camera.
     
  5. MarkBenitez

    MarkBenitez

    Joined:
    Oct 5, 2013
    Posts:
    44
    I need it to render shadows and etc on the gun,

    is there any thing in code that makes the camera render a certain layer or tag first? (on top of everything else getting rendered)
     
  6. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    999
    an old game called Tresspasser had a mechanic that if you bumped your weapon into the wall hard enough, it would fall out of your hand and you had to kneel down and pick it up again
     
  7. Divinux

    Divinux

    Joined:
    Jul 5, 2011
    Posts:
    205
    Hang on guys isn't a second camera how most games do this? How do they handle weapon shadows?

    I am not aware of any way to do this other than 2nd camera or colliders. From investigating z-clipping I learned about offsetting the rendering right in the shader, could that somehow be utilized for this problem?
     
  8. MarkBenitez

    MarkBenitez

    Joined:
    Oct 5, 2013
    Posts:
    44
    Right now all i can think of is a shader that can change the rendering order
    http://docs.unity3d.com/Documentation/Components/SL-SubshaderTags.html

    im looking in to how to code it, but im also updating unity cause it kept on crashing, probably something in script

    so its gonna take 30+ min but heres where im looking if you want to test it just reply the solution if you find out:
    http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=camera

    Im not sure if this is it but its worth a try if you got unity open right now:
    http://docs.unity3d.com/Documentation/ScriptReference/Camera.OnRenderObject.html
     
    Last edited: Oct 7, 2013
  9. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,260
    Rust does it this way - http://garry.tv/2014/04/07/unity-viewmodels/
    • Custom Shaders for all viewmodel meshes
    • Use only the projection matrix with modified znear/zfar (custom projection, no inaccuracy issues)
    • Pass 1 – clear the depth buffer (renders the mesh, but only clears the depth)
    • Pass 2 – render (renders the mesh as normal, depth isn’t an issue)
    Bumping because this was the top result on Google.
     
    GibTreaty likes this.
  10. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,355
    But it also introducing another problem...
     
  11. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    3,802
    This solution is not perfect but can make your objects render on top of others without the need of re-rendering anything and wont affect shadows casting/receiving. Maybe It's not the perfect solution but won't hurt trying. ;)
    Edit your weapon shader and add this line (before or after the Tags) inside your pass:
    Offset -3000, -3000
    You may also have to add this same line to other shaders (like the hand holding the weapon) with the same exact value so they both can render on top of everything.
    EDIT -3000 could be a huge value in your case, so try it out higher values and lower it till it fits your clipping needs. Lower values means closer to the camera.
     
    Last edited: Mar 17, 2015
  12. 3agle

    3agle

    Joined:
    Jul 9, 2012
    Posts:
    508
    Now Unity 5 has introduced the option to render 'shadows only', this is much easier.
    Use the 2 camera approach suggested earlier, but render the weapon in both camera views, one normally and another with 'shadows only' enabled.

    Potentially you may not get shadows cast onto the weapon from this approach, so you could resolve that by also rendering the shadow casting items twice, once normally and once with 'shadows only'.
    I haven't tried this but it should be perfectly feasible. I'm unsure if the shadow pass would be shared between cameras by default, or if you could make the cameras share a shadow pass, so performance may be an issue with that particular implementation/

    Of course there are other clipping methods that can lead to really nice visual effects, such as detecting collisions with walls and moving the weapon accordingly, like pulling the weapon up so it doesn't hit the wall etc. Though I can't find any examples of this I have definitely played at least one game that does it...
     
  13. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,355
    Kinda surprising that Unity still doesn't have a proper solution for this problem

    Didn't Offset gonna messed up the object depth??
     
    Last edited: Mar 17, 2015
  14. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    3,802
    Depends on how far/close you want to clip. As I've stated It's not a perfect solution but it's one of the most cheapest.
     
  15. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,355
    ah yes, i've tested your solution and it works with lighting. The only problem is AmbientOcclusion doesn't affected it since the depth are changed. But hey the lighting works :). Still there's a couple of small problem, but it's a start
     
    tatoforever likes this.
  16. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    3,802
    @rea,
    You can also achieve what you are looking for with stencil tests without touching the weapon depth. But this method requires change to all the shaders in your scenes. :D
     
  17. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,355
    Uuuhhh no thanks then, thankfully i'm not doing any fps game at the moment. So i'm good :D
     
    tatoforever likes this.