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

Displaying texture just from one perspective.

Discussion in 'Shaders' started by steffanPL, Apr 20, 2018.

  1. steffanPL

    steffanPL

    Joined:
    Oct 9, 2014
    Posts:
    40
    Hi guys,
    I have 3d scan mesh that I want to texture dynamically based on the view perspective. One perspective view + background texture at time).

    Problem is that the my texture is visible not only on front side of mesh, but also behind it.

    I would like to make this shader cast texture on mesh just once - on the first visible face. Nothing behind it shouldn't be textured.


    Picture shows front and back side of a model ( front is ok, back should have Background texture).
    Here's what I have so far:

    Code (CSharp):
    1.  
    2. Shader "Custom/Aspect Fix" {
    3.     Properties {
    4.         _MainTex ("Projected texture", 2D) = "white" {}
    5.         _BackgroundTex ("Background Texture", 2D) = "white" {}
    6.     }
    7.    
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.    
    11.         Pass {
    12.             CGPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.    
    16.             #include "UnityCG.cginc"
    17.    
    18.             sampler2D _MainTex;
    19.             sampler2D _BackgroundTex;
    20.             float4 _MainTex_ST;
    21.  
    22.    
    23.             struct v2f {
    24.                 float4 pos : SV_POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.    
    28.             v2f vert (appdata_base v) {
    29.                 v2f o;
    30.                 o.pos = UnityObjectToClipPos (v.vertex);
    31.                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
    32.                 return o;
    33.             }
    34.    
    35.             half4 frag (v2f i) : SV_Target
    36.             {
    37.                 half4 c = tex2D (_MainTex, i.uv);
    38.                 if (i.uv.x <= 0.0f || i.uv.y <= 0.0f || i.uv.x >= 1.0f || i.uv.y >= 1.0f)
    39.                     c = tex2D (_BackgroundTex, i.uv);
    40.                 return c;
    41.             }
    42.             ENDCG
    43.         }
    44.     }
    45.     Fallback "Legacy/Diffuse"
    46. }
    47.  
    Thanks for any help !
     
  2. LukasCh

    LukasCh

    Unity Technologies

    Joined:
    Mar 9, 2015
    Posts:
    102
    Hey, so If I understand correctly you want it to act like projector that projects your texture into some mesh body and as in real life it should respect collisions (Not to go pass the faces).

    Firstly I have question is this case even possible? As you mentioned projector will follow the camera - it means you can't see what is behind.

    In case I misunderstood and camera will not be in parallel with projector, you going to need some kind of depth test, for this you can use same technique that is used in shadows (https://en.wikipedia.org/wiki/Shadow_mapping). Idea is to have depth map and test if the pixel is occluded from perspective of projector.