Shadergraph Normal Vector DOTS

Hello,

I have created some Entities with a RenderMesh and a material that use a Shadergraph shader.
I can use various nodes of Shadergraph with no problems, like Sample Texture, Colors, etc…

But for my shader I need to use the Normal Vector of my Mesh to do some effect.
When I use this shader on a regular GameObject, no problem, I can see the Normal.

But when I use this shader with this particular Input on my Entities, the shader is broken. No color, only pure black and white.

I guess it’s maybe liked with the Space attribute of the node with is not the same in ECS world than in classic… ?

Do you think it’s possible to create a custom function in Shadergraph to fix this problem with the Normal Vector?

Thank you.

Is this with Hybrid Renderer V2 or Entities 0.50 ?

It’s with Entities 0.50

1 Like

Update

I’have recreated this bug in a new clean project.
2020.31f1
8165504--1062248--Screenshot_1.png

I spawn one simple Entity with a RenderMesh:

using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;

public class ManagerECS : MonoBehaviour
{
    public Mesh mesh;
    public Material material;

    EntityManager   entityManager;
    EntityArchetype mainArchetype;
    EntityArchetype childMeshArchetype;
    Entity          mainEntity;

    void Start()
    {
        Init();
        CreateNewEntity();
    }

    void Init()
    {
        entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        SetMainArchetype();
    }

    void SetMainArchetype()
    {
        mainArchetype = entityManager.CreateArchetype(
            typeof(Translation),
            typeof(Rotation),
            typeof(LocalToWorld),
            typeof(RenderMesh),
            typeof(RenderBounds)
        );
    }

    void CreateNewEntity()
    {
        mainEntity = entityManager.CreateEntity(mainArchetype);
        SetMainComponentData();
    }

    void SetMainComponentData()
    {
        entityManager.SetComponentData
            (mainEntity, new Translation { Value = new float3(0,0,0) });

        entityManager.SetComponentData
            (mainEntity, new Rotation { Value = quaternion.identity });

        entityManager.SetSharedComponentData(mainEntity, new RenderMesh
        {
            mesh      =mesh,
            material  = material,
            layerMask = 1,
        });

        entityManager.SetComponentData(mainEntity, new RenderBounds
        {
            Value = new AABB
            {
                Center = float3.zero, Extents = new float3(1, 1, 1)
            }
        });
    }
}

And I still have the problem with the Normal Vector in my Shadergraph shader.
So I made an HLSL shader because I thought the bug was maybe related with Shadergraph. But the problem is still here, all normals are black.

Shader "Universal Render Pipeline/Custom/NormalViewer"
{
    Properties {}

    SubShader
    {
        Tags
        {
            "RenderPipeline"="UniversalPipeline" "Queue"="Geometry"
        }

        Pass
        {
            Name "Forward"
            Tags
            {
                "LightMode"="UniversalForward"
            }

            Cull Back

            HLSLPROGRAM
            #pragma exclude_renderers gles gles3 glcore
            #pragma target 4.5
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #pragma instancing_options renderinglayer
            #pragma multi_compile _ DOTS_INSTANCING_ON
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"


            struct Attributes
            {
                float4 positionOS : POSITION;
                half3 normal : NORMAL;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct Varyings
            {
                float4 positionHCS : SV_POSITION;
                half3 normal : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            Varyings vert(Attributes IN)
            {
                Varyings OUT;

                UNITY_SETUP_INSTANCE_ID(IN);
                UNITY_TRANSFER_INSTANCE_ID(IN, OUT);

                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.normal = TransformObjectToWorldNormal(IN.normal);
                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(IN);
                half4 color = 0; 
                color.rgb = IN.normal;
                return color;
            }
            ENDHLSL
        }
    }
}

So I don’t think it’s a shadergraph bug.

I created a SystemBase that print in the console the value of each normal stored in the renderMesh.mesh. And they were not empty, it was the correct value I expected… So it’s not from the renderMesh neither.

Another strange thing, it’s not just a black color, I think it’s extremely negative because when I try to add it some value it stay black.

1 Like

Update

I found the solution.
In order to normal to work on the shader you need to add the WorldToLocal_Tag to the entity, do not confuse with WorldToLocal because it will not work.

2 Likes