Search Unity

Mask 3D Object In Scroll Rect

Discussion in 'Shaders' started by mertzorlular95, May 4, 2020.

  1. mertzorlular95

    mertzorlular95

    Joined:
    Sep 11, 2019
    Posts:
    3
    I was trying to use 3D object in scroll rect but 3d object was visible outside the scroll rect. So I found https://forum.unity.com/threads/ui-and-masking-3d-meshes-in-a-scrollrect.358475/ this post and follow the instructions. I managed to get it work but not completely. As you can see in the picture, mask is working but probaby something wrong with the shader. Can you guys take a look at the shader code? Lights and shadows are not working.

    Object on the right side is the original one, left side is the masked one.



    Code (CSharp):
    1. // https://forum.unity3d.com/threads/ui-and-masking-3d-meshes-in-a-scrollrect.358475/
    2. Shader "Custom/NewSurfaceShader" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    7.         _Metallic ("Metallic", Range(0,1)) = 0.0
    8.  
    9.  
    10.         _StencilComp("Stencil Comparison", Float) = 8
    11.         _Stencil("Stencil ID", Float) = 0
    12.         _StencilOp("Stencil Operation", Float) = 0
    13.         _StencilWriteMask("Stencil Write Mask", Float) = 255
    14.         _StencilReadMask("Stencil Read Mask", Float) = 255
    15.  
    16.         _ColorMask("Color Mask", Float) = 15
    17.  
    18.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
    19.     }
    20.  
    21.     SubShader
    22.     {
    23.         Tags { "RenderType"="Opaque""Queue"="Transparent"}
    24.         LOD 200
    25.         ZWrite On
    26.         ZTest[unity_GUIZTestMode]
    27.         Blend SrcAlpha OneMinusSrcAlpha
    28.         ColorMask[_ColorMask]
    29.  
    30.      
    31.         Stencil
    32.         {
    33.             Ref 1
    34.             Comp LEqual
    35.             Pass Keep
    36.             ReadMask [_StencilReadMask]
    37.             WriteMask [_StencilWriteMask]
    38.         }
    39.  
    40.         CGPROGRAM
    41.         // Physically based Standard lighting model, and enable shadows on all light types
    42.         #pragma surface surf Standard vertex:vert fullforwardshadows
    43.         // Use shader model 3.0 target, to get nicer looking lighting
    44.         #pragma target 3.0
    45.  
    46.         #include "UnityUI.cginc"
    47.          
    48.         struct Input
    49.         {
    50.             float2 uv_MainTex;
    51.             float4 worldPosition;
    52.         };
    53.  
    54.         void vert(inout appdata_full v, out Input o)
    55.         {
    56.             UNITY_INITIALIZE_OUTPUT(Input, o);
    57.             o.worldPosition = v.vertex;
    58.         }
    59.      
    60.  
    61.         sampler2D _MainTex;
    62.         half _Glossiness;
    63.         half _Metallic;
    64.         fixed4 _Color;
    65.         // float4 _ClipRect;
    66.  
    67.         void surf (Input IN, inout SurfaceOutputStandard o) {
    68.             // Albedo comes from a texture tinted by color
    69.             fixed4 c = _Color;
    70.             o.Albedo = c.rgb;
    71.             // Metallic and smoothness come from slider variables
    72.             o.Metallic = _Metallic;
    73.             o.Smoothness = _Glossiness;
    74.             o.Alpha = c.a;
    75.          
    76.             #ifdef UNITY_UI_ALPHACLIP
    77.             clip(o.Alpha - 0.001);
    78.             #endif
    79.         }
    80.         ENDCG
    81.     }
    82.     FallBack "Diffuse"
    83. }
     
    Last edited: May 4, 2020