Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

UI and masking 3d meshes in a scrollrect

Discussion in 'UGUI & TextMesh Pro' started by saarwii, Oct 1, 2015.

  1. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    35
    You guys may want to try it out
    . Though, I'm not entirely sure how we can add it onto the UI.

    hi phil, can you guys update us on what we can do in the URP and HDRP? the solution posted here doesn't seem to work on the new render pipelines.

    I'm getting an error from the shader:
    Code (CSharp):
    1. Shader error in 'Universal Render Pipeline/Simple Lit': Couldn't open include file 'Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitInput.hlsl'. at line 134
     
    larry2013z likes this.
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Proper support of URP is coming. The basics have worked since day 1 but anything requiring lighting needs to be redone and they've just not gotten around to supporting it yet. I'm working with on of their devs to get URP full support into a release soon.

    As for you issue i'm not 100% sure as i'm not fully informed on how URP shaders are working.
     
    CraftedGaming likes this.
  3. MidnightCoffeeInc

    MidnightCoffeeInc

    Joined:
    Feb 28, 2017
    Posts:
    440
    I would love an update on this for URP, if there's a solution?
    I want to make a scrollrect with 3D objects.
    Thanks!
     
  4. VitaliusSch

    VitaliusSch

    Joined:
    Dec 7, 2013
    Posts:
    1
    Hey All!
    I approached this question in a rather non-standard way. I hide the 3D meshes if they are not visible in the viewport. Maybe someone will come in handy:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ScrollItemCtl : MonoBehaviour
    6. {
    7.     public RectTransform scrollViewViewport;
    8.     public GameObject go3dMesh;
    9.  
    10.     Vector4 offset = new Vector4( 0, 17, 0, 20);
    11.  
    12.     private void Update()
    13.     {
    14.         if (go3dMesh != null)
    15.             go3dMesh.SetActive(RectTransformUtility.RectangleContainsScreenPoint(scrollViewViewport, transform.position, null , offset));
    16.     }
    17. }
    Oh yeah .. you can add offsets to fine tune the hiding of objects.
     
    Last edited: Aug 3, 2021
  5. Jahvan

    Jahvan

    Joined:
    Feb 1, 2017
    Posts:
    8
    Couldn't find any news on this so here's something I made for my needs. It renders a mesh using the Canvas Renderer component and allows said mesh to be masked just like any other UI element, as long as its material shaders are set up correctly. (Tested on URP 2021.3)

    I believe this is everything you need to add to your shader(s) in order for masking to work:
    Code (CSharp):
    1. Shader "UI/Maskable"
    2. {
    3.     Properties
    4.     {
    5.  
    6.         _StencilComp("Stencil Comparison", Float) = 8
    7.         _Stencil("Stencil ID", Float) = 0
    8.         _StencilOp("Stencil Operation", Float) = 0
    9.         _StencilWriteMask("Stencil Write Mask", Float) = 255
    10.         _StencilReadMask("Stencil Read Mask", Float) = 255
    11.  
    12.         _ColorMask("Color Mask", Float) = 15
    13.  
    14.     }
    15.  
    16.     SubShader
    17.         {
    18.  
    19.                 Stencil
    20.                 {
    21.                     Ref[_Stencil]
    22.                     Comp[_StencilComp]
    23.                     Pass[_StencilOp]
    24.                     ReadMask[_StencilReadMask]
    25.                     WriteMask[_StencilWriteMask]
    26.                 }
    27.  
    28.                 ZWrite Off
    29.                 ZTest[unity_GUIZTestMode]
    30.                 ColorMask[_ColorMask]
    31.  
    32.                 Pass
    33.                 {
    34.  
    35.                     // ...
    36.  
    37.                 }
    38.  
    39.         }
    40. }
    Or you can use an existing shader that's already compatible with masking.

    Now simply add the UIConvertMeshRenderer component to a game object that is within a UI canvas hierarchy. Make sure it has a MeshFilter and MeshRenderer component attached, and that they include the desired mesh and material(s).

    A few things to note:

    - You'll probably need to set the transform scale of your mesh pretty high when it's in a canvas.

    - I only tested this on a canvas using Screen Space - Camera.

    - Meshes used for this must have the read/write flag enabled.

    - I didn't need my materials to be lit, so no idea if lighting is possible for this.

    - If you allow the script to create a control parent transform, you should be able to use its anchors and such to manipulate the mesh just like any other UI element.
     

    Attached Files:

    Last edited: Dec 15, 2022
    MidnightCoffeeInc likes this.
  6. MidnightCoffeeInc

    MidnightCoffeeInc

    Joined:
    Feb 28, 2017
    Posts:
    440
    This looks awesome, thanks!
     
  7. Tartilla_

    Tartilla_

    Joined:
    Apr 17, 2021
    Posts:
    1
    upload_2023-2-24_14-14-59.png
    Use such settings and archive with all files from Allen0012. Don't forget to add a Mask component for the parent object
    upload_2023-2-24_14-15-36.png
     

    Attached Files: