Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    792
    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:
    792
    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:
    1,121
    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,910
    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,788
    But it also introducing another problem...
     
  11. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,364
    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...
     
    Ricollwy likes this.
  13. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    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:
    4,364
    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,788
    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:
    4,364
    @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,788
    Uuuhhh no thanks then, thankfully i'm not doing any fps game at the moment. So i'm good :D
     
    tatoforever likes this.
  18. rightrain0

    rightrain0

    Joined:
    Aug 9, 2019
    Posts:
    2
    I seem to have discoverd an almost perfect solution
    1. Download Standard shader from unity archive
    2. Open the shader and add a tag "Queue" = "Geometry+1" and line "ZTest Off"
    Code (CSharp):
    1. SubShader
    2.     {
    3.         Tags { "Queue" = "Geometry+1" "RenderType"="Opaque" "PerformanceChecks"="False" }
    4.         //Tags { "Queue" = "Overlay" }
    5.         LOD 300
    6.  
    7.  
    8.         // ------------------------------------------------------------------
    9.         //  Base forward pass (directional light, emission, lightmaps, ...)
    10.         Pass
    11.         {
    12.             Name "FORWARD"
    13.             Tags { "LightMode" = "ForwardBase" }
    14.  
    15.             Blend [_SrcBlend] [_DstBlend]
    16.             ZWrite [_ZWrite]
    17.             //ZTest Always
    18.             ZTest Off
    Like so
    3.Change all materials on weapon to use this shader
    4. To render certain materials after another (fex. gun mag should be renderd after the gun so it doesnt clip trough the gun). Change inspector to debug mode and change "custom render queue to a higher value".
    5. Now all weapons should recieve proper shadows and never clip.

    The reason this is an almost perfect solution is because one of my handguns became semi transparent. Almost like the normals are missing from certain camera angels. Worked perfectly for my other 2 weapons though
     
    Kuklach likes this.
  19. rightrain0

    rightrain0

    Joined:
    Aug 9, 2019
    Posts:
    2
    Here is the whole code for the shader

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Standard_no_clip"
    4. {
    5.     Properties
    6.     {
    7.         _Color("Color", Color) = (1,1,1,1)
    8.         _MainTex("Albedo", 2D) = "white" {}
    9.  
    10.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    11.  
    12.         _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
    13.         _GlossMapScale("Smoothness Scale", Range(0.0, 1.0)) = 1.0
    14.         [Enum(Metallic Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel ("Smoothness texture channel", Float) = 0
    15.  
    16.         [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    17.         _MetallicGlossMap("Metallic", 2D) = "white" {}
    18.  
    19.         [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
    20.         [ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
    21.  
    22.         _BumpScale("Scale", Float) = 1.0
    23.         [Normal] _BumpMap("Normal Map", 2D) = "bump" {}
    24.  
    25.         _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
    26.         _ParallaxMap ("Height Map", 2D) = "black" {}
    27.  
    28.         _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
    29.         _OcclusionMap("Occlusion", 2D) = "white" {}
    30.  
    31.         _EmissionColor("Color", Color) = (0,0,0)
    32.         _EmissionMap("Emission", 2D) = "white" {}
    33.  
    34.         _DetailMask("Detail Mask", 2D) = "white" {}
    35.  
    36.         _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
    37.         _DetailNormalMapScale("Scale", Float) = 1.0
    38.         [Normal] _DetailNormalMap("Normal Map", 2D) = "bump" {}
    39.  
    40.         [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0
    41.  
    42.  
    43.         // Blending state
    44.         [HideInInspector] _Mode ("__mode", Float) = 0.0
    45.         [HideInInspector] _SrcBlend ("__src", Float) = 1.0
    46.         [HideInInspector] _DstBlend ("__dst", Float) = 0.0
    47.         [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    48.     }
    49.  
    50.     CGINCLUDE
    51.         #define UNITY_SETUP_BRDF_INPUT MetallicSetup
    52.     ENDCG
    53.  
    54.     SubShader
    55.     {
    56.         Tags { "Queue" = "Geometry+1" "RenderType"="Opaque" "PerformanceChecks"="False" }
    57.         //Tags { "Queue" = "Overlay" }
    58.         LOD 300
    59.  
    60.  
    61.         // ------------------------------------------------------------------
    62.         //  Base forward pass (directional light, emission, lightmaps, ...)
    63.         Pass
    64.         {
    65.             Name "FORWARD"
    66.             Tags { "LightMode" = "ForwardBase" }
    67.  
    68.             Blend [_SrcBlend] [_DstBlend]
    69.             ZWrite [_ZWrite]
    70.             //ZTest Always
    71.             ZTest Off
    72.  
    73.             CGPROGRAM
    74.             #pragma target 3.0
    75.  
    76.             // -------------------------------------
    77.  
    78.             #pragma shader_feature _NORMALMAP
    79.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    80.             #pragma shader_feature _EMISSION
    81.             #pragma shader_feature_local _METALLICGLOSSMAP
    82.             #pragma shader_feature_local _DETAIL_MULX2
    83.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    84.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    85.             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
    86.             #pragma shader_feature_local _PARALLAXMAP
    87.  
    88.             #pragma multi_compile_fwdbase
    89.             #pragma multi_compile_fog
    90.             #pragma multi_compile_instancing
    91.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    92.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    93.  
    94.             #pragma vertex vertBase
    95.             #pragma fragment fragBase
    96.             #include "UnityStandardCoreForward.cginc"
    97.  
    98.             ENDCG
    99.         }
    100.         // ------------------------------------------------------------------
    101.         //  Additive forward pass (one light per pass)
    102.         Pass
    103.         {
    104.             Name "FORWARD_DELTA"
    105.             Tags { "LightMode" = "ForwardAdd" }
    106.             Blend [_SrcBlend] One
    107.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    108.             ZWrite Off
    109.             ZTest LEqual
    110.  
    111.             CGPROGRAM
    112.             #pragma target 3.0
    113.  
    114.             // -------------------------------------
    115.  
    116.  
    117.             #pragma shader_feature _NORMALMAP
    118.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    119.             #pragma shader_feature_local _METALLICGLOSSMAP
    120.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    121.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    122.             #pragma shader_feature_local _DETAIL_MULX2
    123.             #pragma shader_feature_local _PARALLAXMAP
    124.  
    125.             #pragma multi_compile_fwdadd_fullshadows
    126.             #pragma multi_compile_fog
    127.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    128.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    129.  
    130.             #pragma vertex vertAdd
    131.             #pragma fragment fragAdd
    132.             #include "UnityStandardCoreForward.cginc"
    133.  
    134.             ENDCG
    135.         }
    136.         // ------------------------------------------------------------------
    137.         //  Shadow rendering pass
    138.         Pass {
    139.             Name "ShadowCaster"
    140.             Tags { "LightMode" = "ShadowCaster" }
    141.  
    142.             ZWrite On ZTest LEqual
    143.  
    144.             CGPROGRAM
    145.             #pragma target 3.0
    146.  
    147.             // -------------------------------------
    148.  
    149.  
    150.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    151.             #pragma shader_feature_local _METALLICGLOSSMAP
    152.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    153.             #pragma shader_feature_local _PARALLAXMAP
    154.             #pragma multi_compile_shadowcaster
    155.             #pragma multi_compile_instancing
    156.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    157.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    158.  
    159.             #pragma vertex vertShadowCaster
    160.             #pragma fragment fragShadowCaster
    161.  
    162.             #include "UnityStandardShadow.cginc"
    163.  
    164.             ENDCG
    165.         }
    166.         // ------------------------------------------------------------------
    167.         //  Deferred pass
    168.         Pass
    169.         {
    170.             Name "DEFERRED"
    171.             Tags { "LightMode" = "Deferred" }
    172.  
    173.             CGPROGRAM
    174.             #pragma target 3.0
    175.             #pragma exclude_renderers nomrt
    176.  
    177.  
    178.             // -------------------------------------
    179.  
    180.             #pragma shader_feature _NORMALMAP
    181.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    182.             #pragma shader_feature _EMISSION
    183.             #pragma shader_feature_local _METALLICGLOSSMAP
    184.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    185.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    186.             #pragma shader_feature_local _DETAIL_MULX2
    187.             #pragma shader_feature_local _PARALLAXMAP
    188.  
    189.             #pragma multi_compile_prepassfinal
    190.             #pragma multi_compile_instancing
    191.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    192.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    193.  
    194.             #pragma vertex vertDeferred
    195.             #pragma fragment fragDeferred
    196.  
    197.             #include "UnityStandardCore.cginc"
    198.  
    199.             ENDCG
    200.         }
    201.  
    202.         // ------------------------------------------------------------------
    203.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    204.         // This pass it not used during regular rendering.
    205.         Pass
    206.         {
    207.             Name "META"
    208.             Tags { "LightMode"="Meta" }
    209.  
    210.             Cull Off
    211.  
    212.             CGPROGRAM
    213.             #pragma vertex vert_meta
    214.             #pragma fragment frag_meta
    215.  
    216.             #pragma shader_feature _EMISSION
    217.             #pragma shader_feature_local _METALLICGLOSSMAP
    218.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    219.             #pragma shader_feature_local _DETAIL_MULX2
    220.             #pragma shader_feature EDITOR_VISUALIZATION
    221.  
    222.             #include "UnityStandardMeta.cginc"
    223.             ENDCG
    224.         }
    225.     }
    226.  
    227.     SubShader
    228.     {
    229.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    230.         LOD 150
    231.  
    232.         // ------------------------------------------------------------------
    233.         //  Base forward pass (directional light, emission, lightmaps, ...)
    234.         Pass
    235.         {
    236.             Name "FORWARD"
    237.             Tags { "LightMode" = "ForwardBase" }
    238.  
    239.             Blend [_SrcBlend] [_DstBlend]
    240.             ZWrite [_ZWrite]
    241.  
    242.             CGPROGRAM
    243.             #pragma target 2.0
    244.  
    245.             #pragma shader_feature _NORMALMAP
    246.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    247.             #pragma shader_feature _EMISSION
    248.             #pragma shader_feature_local _METALLICGLOSSMAP
    249.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    250.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    251.             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
    252.             // SM2.0: NOT SUPPORTED shader_feature_local _DETAIL_MULX2
    253.             // SM2.0: NOT SUPPORTED shader_feature_local _PARALLAXMAP
    254.  
    255.             #pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED
    256.  
    257.             #pragma multi_compile_fwdbase
    258.             #pragma multi_compile_fog
    259.  
    260.             #pragma vertex vertBase
    261.             #pragma fragment fragBase
    262.             #include "UnityStandardCoreForward.cginc"
    263.  
    264.             ENDCG
    265.         }
    266.         // ------------------------------------------------------------------
    267.         //  Additive forward pass (one light per pass)
    268.         Pass
    269.         {
    270.             Name "FORWARD_DELTA"
    271.             Tags { "LightMode" = "ForwardAdd" }
    272.             Blend [_SrcBlend] One
    273.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    274.             ZWrite Off
    275.             ZTest LEqual
    276.  
    277.             CGPROGRAM
    278.             #pragma target 2.0
    279.  
    280.             #pragma shader_feature _NORMALMAP
    281.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    282.             #pragma shader_feature_local _METALLICGLOSSMAP
    283.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    284.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    285.             #pragma shader_feature_local _DETAIL_MULX2
    286.             // SM2.0: NOT SUPPORTED shader_feature_local _PARALLAXMAP
    287.             #pragma skip_variants SHADOWS_SOFT
    288.  
    289.             #pragma multi_compile_fwdadd_fullshadows
    290.             #pragma multi_compile_fog
    291.  
    292.             #pragma vertex vertAdd
    293.             #pragma fragment fragAdd
    294.             #include "UnityStandardCoreForward.cginc"
    295.  
    296.             ENDCG
    297.         }
    298.         // ------------------------------------------------------------------
    299.         //  Shadow rendering pass
    300.         Pass {
    301.             Name "ShadowCaster"
    302.             Tags { "LightMode" = "ShadowCaster" }
    303.  
    304.             ZWrite On ZTest LEqual
    305.  
    306.             CGPROGRAM
    307.             #pragma target 2.0
    308.  
    309.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    310.             #pragma shader_feature_local _METALLICGLOSSMAP
    311.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    312.             #pragma skip_variants SHADOWS_SOFT
    313.             #pragma multi_compile_shadowcaster
    314.  
    315.             #pragma vertex vertShadowCaster
    316.             #pragma fragment fragShadowCaster
    317.  
    318.             #include "UnityStandardShadow.cginc"
    319.  
    320.             ENDCG
    321.         }
    322.  
    323.         // ------------------------------------------------------------------
    324.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    325.         // This pass it not used during regular rendering.
    326.         Pass
    327.         {
    328.             Name "META"
    329.             Tags { "LightMode"="Meta" }
    330.  
    331.             Cull Off
    332.  
    333.             CGPROGRAM
    334.             #pragma vertex vert_meta
    335.             #pragma fragment frag_meta
    336.  
    337.             #pragma shader_feature _EMISSION
    338.             #pragma shader_feature_local _METALLICGLOSSMAP
    339.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    340.             #pragma shader_feature_local _DETAIL_MULX2
    341.             #pragma shader_feature EDITOR_VISUALIZATION
    342.  
    343.             #include "UnityStandardMeta.cginc"
    344.             ENDCG
    345.         }
    346.     }
    347.  
    348.  
    349.     FallBack "VertexLit"
    350.     CustomEditor "StandardShaderGUI"
    351. }
    352.  
     
    Kuklach and Stardog like this.