UI and masking 3d meshes in a scrollrect

Hey so i got a shader from Tim thought not sure its been fully tested but i’m sure you guys will tell us if anything is wrong.

2669388–188334–UI3DMesh.shader (2.42 KB)

2 Likes

WhoooHooo @phil-Unity I’ll give that a whirl. Thanks.
Will run this through the UI Extensions controls I had lined up for this and let you know. Thanks for chasing this down.

Anyone get this working with that shader? I think I am close… got the shader, applying to all mesh & shaders, still not masking tho.

Correction - it’s working but in reverse… showing up outside the mask and not within it… any thoughts?

@seansteezy Hi, how did you got it working (even in reverse)? Im stuck, canvas images just occlude the mesh

It seems to show up way below the rendering canvas, but I have my main cam, and UI is set to screen space camera, with the plane at 5. For each object in my menu (scroll rect → grid layout → menu item prefab → ui → 3d object), they have a modified mesh script attached (from the above) that will find one of the meshes in the child menu object and change its shader to the one provided above. While it doesn’t look particularly good, it does show up outside the clipping mask, but doesn’t show up within it, when that shader is applied to the game objects.

Yup same here, but can you please test to remove all clipping masks? I dont have any and still the images in canvas are occluding that mesh. So then the problem would be not clipping but mesh drawn too early? (Btw: when you set ZWrite On in shader the mesh will pop above, but still no clipping works)

That may be a z value or layer/cam clipping issue. I had probs with my camera near clipping plane, it was clipping my models, even though it was at 10 and the ui was at 0.

Stencil
{
    Ref 1
    Comp LEqual
    Pass Keep
    ReadMask [_StencilReadMask]
    WriteMask [_StencilWriteMask]
}

This stencil settings solved the inversion problem for me :). Hope it will help you too

Edit: Oh and i forgot you haveto set SubShader tags as

Tags { "RenderType"="UI""Queue"="Transparent" }

How lucky am I? Finding this one day after it got figured out after almost a year.
Great job everyone, this should be part of next Unity update.

I attached everything you need to get this working. Remember to set the Canvas Render Mode to “Screen Space - Camera”.

2804413–203640–UI3DMesh.zip (2.86 KB)

7 Likes

i can"t get this to work , sorry guys i’m an artist trying to learn programming , and i need to get this to work for my project ,
what i have is skinnedmesh , a character with some IDLE animation and turning around in a loop
i have done a small modification to the c# code in order to adapt it to the skinned mesh ,i have multiple materials on the character, i attache the UI3DMesh script to the Mask , when i try to change the size of the materials array, unity crashs ,

PS :i have applied the shader provided to all the sub materials of the skinned mesh

please help know if i’m doing this the right way
thanx

my modified script

using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(CanvasRenderer))]
[ExecuteInEditMode]
public class UI3DMesh : MonoBehaviour
{
    public  Mesh TheMesh;
    public SkinnedMeshRenderer Frank;
    public List<Material> Materials;
    void ResetData ()
    {
        var cr = GetComponent<CanvasRenderer>();
        TheMesh= Frank.sharedMesh;
        cr.SetMesh (TheMesh);
        cr.materialCount = Materials.Count;
        for (int i = 0; i < Materials.Count; i++)
            cr.SetMaterial (Materials[i], i);
    }
    void OnEnable()
    {
        ResetData();
    }
    void OnValidate()
    {
        ResetData();
    }
}

Thanks for all the info that you’ve posted here guys! It’s super helpful. I’ve modified the script slightly so that you don’t need to constantly assign the script that was put up on here by Allen0012. All you need to do now is use the shader and material provided in Allen0012’s zip file and place my modified script on a GameObject.

In the inspector you’ll be able to see a list of MeshFilters on the script. Fill that up with all of the GameObjects/Meshes you want to hide in a Mask in Unity’s UI and make sure that all of the GameObjects with the MeshFilters on have CanvasRenderer components too and are parented within a UI Canvas.

Here’s the script:

using UnityEngine;

using System.Collections.Generic;
using System.Reflection;
using System.Linq;


// 3D Masking Original Script from:  https://forum.unity3d.com/threads/ui-and-masking-3d-meshes-in-a-scrollrect.358475/
[ExecuteInEditMode]
public class UI3DMesh : MonoBehaviour
{
    public List <MeshFilter>    meshFilters = new List <MeshFilter> ();

    void ResetData ()
    {
        meshFilters.ForEach (delegate (MeshFilter meshFilter)
        {
            var cr = meshFilter.transform.GetComponent <CanvasRenderer> ();
            cr.SetMesh (meshFilter.sharedMesh);

            List <Material> materials = meshFilter.transform.GetComponent <Renderer> ().sharedMaterials.ToList ();

            for (int i = 0; i < materials.Count; i++)
            {
                cr.materialCount = materials.Count;
                cr.SetMaterial  (materials[i], i);
            }
        });
    }

    void OnEnable()
    {
        ResetData();
    }

    void OnValidate()
    {
        ResetData();
    }
}
7 Likes

Guys is it still been worked on ???

@TechDevTom do you have an example scene with your approach. Doesn’t seem to work or I’m not configuring it correctly

*Edit, ok, got a mesh renderering but with no materials and looks more like a shadow that a mesh.

*Edit 2, scratch that. You have to play with the new “Shader Channels” option on the canvas for it to recognise it properly. Scaling is awful though, as meshes in the canvas are scaled right down.

*Edit 3. Completely scratch that. Seems the UI Canvas system does indeed support 3D objects natively now and the script wasn’t actually doing anything.

Hey Simon, did you get it fixed then? What version of Unity are you using? I think in the end I used a different approach, but it seemed to be working fine for me. If you still need a scene I can probably get you one at some point today. Is this kind of thing actually native to Unity?

Edit 1: It doesn’t seem to work for me now. I’m not sure why… If you want I can take a look into it. I haven’t worked on the project I was using it in for a while now, so it might take a bit of head scratching. I think I might have ended up just using the DepthMask shader to hide certain 3D elements when they went past a certain boundary and changed the RenderQueue value on meshes.

If you note from my last comment @TechDevTom , 3D mesh rendering is not natively supported by the UI system (provided you enable a Light and use either SS-Camera or worldspace canvas.
Although, you may note the scale is really small on a worldspace canvas :S

I’m also interested!

HI @Allen0012, I can’t get the sample you provided to work - do you happen to have the scene/project still? Trying to get this to work is the bane of my existence.

follow this question

I need this to work and it doesn’t, i have a ScrollView on a SS Camera Canvas and the mask masks all the 2D elements but not my 3D objects.

1 Like