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

Question How to use two AR Cameras to provide Renderorder?

Discussion in 'AR' started by jreutelsterz, Oct 2, 2022.

  1. jreutelsterz

    jreutelsterz

    Joined:
    Dec 10, 2021
    Posts:
    31
    Hey, so in my iOS AR App I want to show Modells based on Image tracking. There I want to render one modell on top of the other. For this I tried to use two AR Cameras where the Main Camera renders everything except the model which comes in front and the second camera renders just the model for the frontlayer. The pictures in the second post show the settings I made in the inspector for each camera.



    I also tried it with two different orders in the scene. First I tried to make the secondCamera a Child of the MainCamera and in the second try both cameras are in the same hierarchy level in the ARSessionOrigin.

    My problem shows up because I just see the rendered object from the second camera and the frontlayer. It seems like my second camera overlays the first camera (mainCamera) rendered model completely.
     
    Last edited: Oct 2, 2022
  2. jreutelsterz

    jreutelsterz

    Joined:
    Dec 10, 2021
    Posts:
    31
  3. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,025
    Multiple AR Camera Managers in your scene is liable to cause problems, as per our docs (https://docs.unity3d.com/Packages/c...managers.html#enabling-and-disabling-managers):

    You should only enable one manager of the same type in a scene at any given time. (Enabling more than one instance of a manager can result in undesirable behavior, as each manager instance will interact with the same underlying subsystem.)

    Your app also pays a significant performance cost for every active camera in the scene. I would recommend seeking a single-camera solution if possible, but your idea to use multiple cameras is a possible solution.
     
  4. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,025
  5. jreutelsterz

    jreutelsterz

    Joined:
    Dec 10, 2021
    Posts:
    31
    thanks for the Input! I tried it know with the use of the shader render queue but I don't really understand it. I got my Background and the first AR object for which I used the standard shader with the geometry tag. For the objects I want to render in the front I tried to different queue tags. Geometry+20 (any chosen number) and overlay. Somehow when my background AR element is once drawn I can't track the new image and render the model in the front. When I also track the image for the front model the tracking and rendering for the background one seems not to work. Can I block the tracking method once a Object is rendered? doesn't make sense to my why it behaves like this
     
  6. jreutelsterz

    jreutelsterz

    Joined:
    Dec 10, 2021
    Posts:
    31
    So in the last few days I figured out how to render both models. But I still couldn't make it up to the right order. I used for the AR Modell in the back the Tags { "Queue" = "Geometry-1" } in the Standard (Shader) in Line 56. The Model in the front has the identical shader just with the
    Tags { "Queue" = "Geometry" }. Since I changed it to this I can see both but the Geometry-1 Model is still in the front
     
  7. davidmo_unity

    davidmo_unity

    Unity Technologies

    Joined:
    Jun 18, 2019
    Posts:
    99
    Changing the render order does not necessarily mean that given geometry will always render in front of another object if both materials are using shaders that perform Z-Testing (the standard shader does use ZTest LEqual). If you want to always render one object in front of another you could leverage the Offset ShaderLab Command to allow the two objects to exist at the "same" depth but bias one material over another.
     
  8. jreutelsterz

    jreutelsterz

    Joined:
    Dec 10, 2021
    Posts:
    31
    I used now
    Code (CSharp):
    1. Offset -1, -1
    in the Shader for the models which I want to render in front. There I used this Offset component in the SubShader section to make it work in every Pass. The Shader for the background model has no Offset. I don't know if this is correct but it still doesn't work like wanted. I also tried it with different render queues but had no different behavior.

    I attached a picture of the current Situation. The four skat cards are the tracked images for the plane which contains a map. The Cube unter this plane contain the images for the character models. In this case the frog as a placeholder. This Frog should be on top so that the map is playable
    WhatsApp Image 2022-10-11 at 19.41.25.jpeg
     
  9. jreutelsterz

    jreutelsterz

    Joined:
    Dec 10, 2021
    Posts:
    31
    The following Shader is the one used for the character model. My Offset component is set at line 58

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "CharacterShader"
    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"}
    57.         LOD 300
    58.         Offset -1, -1
    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.  
    71.             CGPROGRAM
    72.             #pragma target 3.0
    73.  
    74.             // -------------------------------------
    75.  
    76.             #pragma shader_feature_local _NORMALMAP
    77.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    78.             #pragma shader_feature _EMISSION
    79.             #pragma shader_feature_local _METALLICGLOSSMAP
    80.             #pragma shader_feature_local _DETAIL_MULX2
    81.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    82.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    83.             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
    84.             #pragma shader_feature_local _PARALLAXMAP
    85.  
    86.             #pragma multi_compile_fwdbase
    87.             #pragma multi_compile_fog
    88.             #pragma multi_compile_instancing
    89.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    90.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    91.  
    92.             #pragma vertex vertBase
    93.             #pragma fragment fragBase
    94.             #include "UnityStandardCoreForward.cginc"
    95.  
    96.             ENDCG
    97.         }
    98.         // ------------------------------------------------------------------
    99.         //  Additive forward pass (one light per pass)
    100.         Pass
    101.         {
    102.             Name "FORWARD_DELTA"
    103.             Tags { "LightMode" = "ForwardAdd" }
    104.             Blend [_SrcBlend] One
    105.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    106.             ZWrite Off
    107.             ZTest LEqual
    108.  
    109.             CGPROGRAM
    110.             #pragma target 3.0
    111.  
    112.             // -------------------------------------
    113.  
    114.  
    115.             #pragma shader_feature_local _NORMALMAP
    116.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    117.             #pragma shader_feature_local _METALLICGLOSSMAP
    118.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    119.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    120.             #pragma shader_feature_local _DETAIL_MULX2
    121.             #pragma shader_feature_local _PARALLAXMAP
    122.  
    123.             #pragma multi_compile_fwdadd_fullshadows
    124.             #pragma multi_compile_fog
    125.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    126.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    127.  
    128.             #pragma vertex vertAdd
    129.             #pragma fragment fragAdd
    130.             #include "UnityStandardCoreForward.cginc"
    131.  
    132.             ENDCG
    133.         }
    134.         // ------------------------------------------------------------------
    135.         //  Shadow rendering pass
    136.         Pass {
    137.             Name "ShadowCaster"
    138.             Tags { "LightMode" = "ShadowCaster" }
    139.  
    140.             ZWrite On ZTest LEqual
    141.  
    142.             CGPROGRAM
    143.             #pragma target 3.0
    144.  
    145.             // -------------------------------------
    146.  
    147.  
    148.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    149.             #pragma shader_feature_local _METALLICGLOSSMAP
    150.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    151.             #pragma shader_feature_local _PARALLAXMAP
    152.             #pragma multi_compile_shadowcaster
    153.             #pragma multi_compile_instancing
    154.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    155.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    156.  
    157.             #pragma vertex vertShadowCaster
    158.             #pragma fragment fragShadowCaster
    159.  
    160.             #include "UnityStandardShadow.cginc"
    161.  
    162.             ENDCG
    163.         }
    164.         // ------------------------------------------------------------------
    165.         //  Deferred pass
    166.         Pass
    167.         {
    168.             Name "DEFERRED"
    169.             Tags { "LightMode" = "Deferred" }
    170.  
    171.             CGPROGRAM
    172.             #pragma target 3.0
    173.             #pragma exclude_renderers nomrt
    174.  
    175.  
    176.             // -------------------------------------
    177.  
    178.             #pragma shader_feature_local _NORMALMAP
    179.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    180.             #pragma shader_feature _EMISSION
    181.             #pragma shader_feature_local _METALLICGLOSSMAP
    182.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    183.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    184.             #pragma shader_feature_local _DETAIL_MULX2
    185.             #pragma shader_feature_local _PARALLAXMAP
    186.  
    187.             #pragma multi_compile_prepassfinal
    188.             #pragma multi_compile_instancing
    189.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    190.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    191.  
    192.             #pragma vertex vertDeferred
    193.             #pragma fragment fragDeferred
    194.  
    195.             #include "UnityStandardCore.cginc"
    196.  
    197.             ENDCG
    198.         }
    199.  
    200.         // ------------------------------------------------------------------
    201.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    202.         // This pass it not used during regular rendering.
    203.         Pass
    204.         {
    205.             Name "META"
    206.             Tags { "LightMode"="Meta" }
    207.  
    208.             Cull Off
    209.  
    210.             CGPROGRAM
    211.             #pragma vertex vert_meta
    212.             #pragma fragment frag_meta
    213.  
    214.             #pragma shader_feature _EMISSION
    215.             #pragma shader_feature_local _METALLICGLOSSMAP
    216.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    217.             #pragma shader_feature_local _DETAIL_MULX2
    218.             #pragma shader_feature EDITOR_VISUALIZATION
    219.  
    220.             #include "UnityStandardMeta.cginc"
    221.             ENDCG
    222.         }
    223.     }
    224.  
    225.     SubShader
    226.     {
    227.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    228.         LOD 150
    229.  
    230.         // ------------------------------------------------------------------
    231.         //  Base forward pass (directional light, emission, lightmaps, ...)
    232.         Pass
    233.         {
    234.             Name "FORWARD"
    235.             Tags { "LightMode" = "ForwardBase" }
    236.  
    237.             Blend [_SrcBlend] [_DstBlend]
    238.             ZWrite [_ZWrite]
    239.  
    240.             CGPROGRAM
    241.             #pragma target 2.0
    242.  
    243.             #pragma shader_feature_local _NORMALMAP
    244.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    245.             #pragma shader_feature _EMISSION
    246.             #pragma shader_feature_local _METALLICGLOSSMAP
    247.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    248.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    249.             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
    250.             // SM2.0: NOT SUPPORTED shader_feature_local _DETAIL_MULX2
    251.             // SM2.0: NOT SUPPORTED shader_feature_local _PARALLAXMAP
    252.  
    253.             #pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED
    254.  
    255.             #pragma multi_compile_fwdbase
    256.             #pragma multi_compile_fog
    257.  
    258.             #pragma vertex vertBase
    259.             #pragma fragment fragBase
    260.             #include "UnityStandardCoreForward.cginc"
    261.  
    262.             ENDCG
    263.         }
    264.         // ------------------------------------------------------------------
    265.         //  Additive forward pass (one light per pass)
    266.         Pass
    267.         {
    268.             Name "FORWARD_DELTA"
    269.             Tags { "LightMode" = "ForwardAdd" }
    270.             Blend [_SrcBlend] One
    271.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    272.             ZWrite Off
    273.             ZTest LEqual
    274.  
    275.             CGPROGRAM
    276.             #pragma target 2.0
    277.  
    278.             #pragma shader_feature_local _NORMALMAP
    279.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    280.             #pragma shader_feature_local _METALLICGLOSSMAP
    281.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    282.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    283.             #pragma shader_feature_local _DETAIL_MULX2
    284.             // SM2.0: NOT SUPPORTED shader_feature_local _PARALLAXMAP
    285.             #pragma skip_variants SHADOWS_SOFT
    286.  
    287.             #pragma multi_compile_fwdadd_fullshadows
    288.             #pragma multi_compile_fog
    289.  
    290.             #pragma vertex vertAdd
    291.             #pragma fragment fragAdd
    292.             #include "UnityStandardCoreForward.cginc"
    293.  
    294.             ENDCG
    295.         }
    296.         // ------------------------------------------------------------------
    297.         //  Shadow rendering pass
    298.         Pass {
    299.             Name "ShadowCaster"
    300.             Tags { "LightMode" = "ShadowCaster" }
    301.  
    302.             ZWrite On ZTest LEqual
    303.  
    304.             CGPROGRAM
    305.             #pragma target 2.0
    306.  
    307.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    308.             #pragma shader_feature_local _METALLICGLOSSMAP
    309.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    310.             #pragma skip_variants SHADOWS_SOFT
    311.             #pragma multi_compile_shadowcaster
    312.  
    313.             #pragma vertex vertShadowCaster
    314.             #pragma fragment fragShadowCaster
    315.  
    316.             #include "UnityStandardShadow.cginc"
    317.  
    318.             ENDCG
    319.         }
    320.  
    321.         // ------------------------------------------------------------------
    322.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    323.         // This pass it not used during regular rendering.
    324.         Pass
    325.         {
    326.             Name "META"
    327.             Tags { "LightMode"="Meta" }
    328.  
    329.             Cull Off
    330.  
    331.             CGPROGRAM
    332.             #pragma vertex vert_meta
    333.             #pragma fragment frag_meta
    334.  
    335.             #pragma shader_feature _EMISSION
    336.             #pragma shader_feature_local _METALLICGLOSSMAP
    337.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    338.             #pragma shader_feature_local _DETAIL_MULX2
    339.             #pragma shader_feature EDITOR_VISUALIZATION
    340.  
    341.             #include "UnityStandardMeta.cginc"
    342.             ENDCG
    343.         }
    344.     }
    345.  
    346.  
    347.     FallBack "VertexLit"
    348.     CustomEditor "StandardShaderGUI"
    349. }
     
  10. jreutelsterz

    jreutelsterz

    Joined:
    Dec 10, 2021
    Posts:
    31
    so I checked my project further and there are some issues with the y coordinate. One of the skat cards has a y coordinate from -0.254 which makes sense. Now the build with the four points of the skat cards the plane for the map which has a y coordinate of 0. But to make it more incomprehensible: the y coordinate from a rendered character is at -23.29 ?!

    A diffrent test gave me the following values:
    empty = skat card, map = plane, frog = character

    -0,3042505 is height of the empty0

    -0,2986501 is height of the empty1

    -0,2942715 is height of the empty2

    -0,2903216 is height of the empty3

    0 is height of the map

    -18,88283 is height of the Frog

    for all values I used the transform.position.y variable
     
    Last edited: Oct 14, 2022
  11. davidmo_unity

    davidmo_unity

    Unity Technologies

    Joined:
    Jun 18, 2019
    Posts:
    99
    I see, my mistake I assumed you were trying to display flat images. For 3D it's not going to work quite right as the a non-planar mesh will have variable depth values. Offset is more for planar images.

    As for your problem, it's hard to say but if the Frog is a child of the map and has an offset in the prefab then that offset could be getting a weird scaling applied to it during tracking. If you want a pure shader solution then the render order approach is actually probably correct except you'll want a stencil mask too. Either render the frog first with a stencil and don't render the map wherever the frog stencil is drawn or render the map first and always render the frog wherever the stencil has been applied. This will always show the frog in front of the map but it can look odd when in the case where you are looking from below (frog will be smaller as it's farther away but will still be in front of the map).

    https://docs.unity3d.com/Manual/SL-Stencil.html
     
  12. jreutelsterz

    jreutelsterz

    Joined:
    Dec 10, 2021
    Posts:
    31
    I could finally solve this problem yesterday. I used the different components for the Render Queue or the zTest but didn't used them at the same time. I now took the standard surface shader and used there the both named values and now its working. Thank you guys for the help!