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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Shadergraph Normal Vector DOTS

Discussion in 'Entity Component System' started by Chmyke, May 28, 2022.

  1. Chmyke

    Chmyke

    Joined:
    Aug 1, 2012
    Posts:
    34
    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...

    Screenshot_1.png



    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.

    Screenshot_3.png



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


    Screenshot_2.png

    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.
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Is this with Hybrid Renderer V2 or Entities 0.50 ?
     
  3. Chmyke

    Chmyke

    Joined:
    Aug 1, 2012
    Posts:
    34
    It's with Entities 0.50
     
    MNNoxMortem likes this.
  4. Chmyke

    Chmyke

    Joined:
    Aug 1, 2012
    Posts:
    34
    Update

    I'have recreated this bug in a new clean project.
    2020.31f1
    Screenshot_1.png

    I spawn one simple Entity with a RenderMesh:


    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using Unity.Rendering;
    4. using Unity.Transforms;
    5. using UnityEngine;
    6.  
    7. public class ManagerECS : MonoBehaviour
    8. {
    9.     public Mesh mesh;
    10.     public Material material;
    11.  
    12.     EntityManager   entityManager;
    13.     EntityArchetype mainArchetype;
    14.     EntityArchetype childMeshArchetype;
    15.     Entity          mainEntity;
    16.  
    17.     void Start()
    18.     {
    19.         Init();
    20.         CreateNewEntity();
    21.     }
    22.  
    23.     void Init()
    24.     {
    25.         entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    26.  
    27.         SetMainArchetype();
    28.     }
    29.  
    30.     void SetMainArchetype()
    31.     {
    32.         mainArchetype = entityManager.CreateArchetype(
    33.             typeof(Translation),
    34.             typeof(Rotation),
    35.             typeof(LocalToWorld),
    36.             typeof(RenderMesh),
    37.             typeof(RenderBounds)
    38.         );
    39.     }
    40.  
    41.     void CreateNewEntity()
    42.     {
    43.         mainEntity = entityManager.CreateEntity(mainArchetype);
    44.         SetMainComponentData();
    45.     }
    46.  
    47.     void SetMainComponentData()
    48.     {
    49.         entityManager.SetComponentData
    50.             (mainEntity, new Translation { Value = new float3(0,0,0) });
    51.  
    52.         entityManager.SetComponentData
    53.             (mainEntity, new Rotation { Value = quaternion.identity });
    54.  
    55.         entityManager.SetSharedComponentData(mainEntity, new RenderMesh
    56.         {
    57.             mesh      =mesh,
    58.             material  = material,
    59.             layerMask = 1,
    60.         });
    61.  
    62.         entityManager.SetComponentData(mainEntity, new RenderBounds
    63.         {
    64.             Value = new AABB
    65.             {
    66.                 Center = float3.zero, Extents = new float3(1, 1, 1)
    67.             }
    68.         });
    69.     }
    70. }
    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.

    Code (CSharp):
    1. Shader "Universal Render Pipeline/Custom/NormalViewer"
    2. {
    3.     Properties {}
    4.  
    5.     SubShader
    6.     {
    7.         Tags
    8.         {
    9.             "RenderPipeline"="UniversalPipeline" "Queue"="Geometry"
    10.         }
    11.  
    12.         Pass
    13.         {
    14.             Name "Forward"
    15.             Tags
    16.             {
    17.                 "LightMode"="UniversalForward"
    18.             }
    19.  
    20.             Cull Back
    21.  
    22.             HLSLPROGRAM
    23.             #pragma exclude_renderers gles gles3 glcore
    24.             #pragma target 4.5
    25.             #pragma vertex vert
    26.             #pragma fragment frag
    27.             #pragma multi_compile_instancing
    28.             #pragma instancing_options renderinglayer
    29.             #pragma multi_compile _ DOTS_INSTANCING_ON
    30.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    31.  
    32.  
    33.             struct Attributes
    34.             {
    35.                 float4 positionOS : POSITION;
    36.                 half3 normal : NORMAL;
    37.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    38.             };
    39.  
    40.             struct Varyings
    41.             {
    42.                 float4 positionHCS : SV_POSITION;
    43.                 half3 normal : TEXCOORD0;
    44.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    45.             };
    46.  
    47.             Varyings vert(Attributes IN)
    48.             {
    49.                 Varyings OUT;
    50.  
    51.                 UNITY_SETUP_INSTANCE_ID(IN);
    52.                 UNITY_TRANSFER_INSTANCE_ID(IN, OUT);
    53.  
    54.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
    55.                 OUT.normal = TransformObjectToWorldNormal(IN.normal);
    56.                 return OUT;
    57.             }
    58.  
    59.             half4 frag(Varyings IN) : SV_Target
    60.             {
    61.                 UNITY_SETUP_INSTANCE_ID(IN);
    62.                 half4 color = 0;
    63.                 color.rgb = IN.normal;
    64.                 return color;
    65.             }
    66.             ENDHLSL
    67.         }
    68.     }
    69. }
    Screenshot_2.png

    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.
     
    bb8_1 likes this.
  5. Chmyke

    Chmyke

    Joined:
    Aug 1, 2012
    Posts:
    34
    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.
     
    tmonestudio and bb8_1 like this.