Search Unity

Resolved Cull Front or Back in ShaderGraph (URP)

Discussion in 'Shader Graph' started by petey, May 2, 2020.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Hi all,

    Just wondering if there is an option somewhere to cull front or back faces in Unity ShaderGraph, I can't seem to find anything. I'm using URP, maybe it's not an option in there?

    Thanks!
    Pete
     
    cxode and Nokobot like this.
  2. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Were you able to figure this out?
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    No! Sadly :( I think it might be one of those things that's just not available. There's an option to have two sided, but I think that's about it. In the end I flipped the polys in Houdini before importing them into Unity.
     
    cxode likes this.
  4. Bordeaux_Fox

    Bordeaux_Fox

    Joined:
    Nov 14, 2018
    Posts:
    589
    You can setup this functionality with "Is Front Face" plus "Branch" node. Then depending what you want, set Albedo, Alpha, whatever, linked by the outcome of the branch node. But this requires a double sided material.

    But if you can avoid it, you would just setup the face direction in your 3D modelling program.
     
    Last edited: Jul 24, 2020
  5. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
  6. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    266
    My solution was to reverse the normals of the object with a simple editor script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6. public class ReverseNormals : MonoBehaviour
    7. {
    8.  
    9.     void Start()
    10.     {
    11.         MeshFilter filter = GetComponent(typeof(MeshFilter)) as MeshFilter;
    12.         if (filter != null)
    13.         {
    14.             Mesh mesh = filter.sharedMesh;
    15.  
    16.             Vector3[] normals = mesh.normals;
    17.             for (int i = 0; i < normals.Length; i++)
    18.                 normals[i] = -normals[i];
    19.             mesh.normals = normals;
    20.  
    21.             for (int m = 0; m < mesh.subMeshCount; m++)
    22.             {
    23.                 int[] triangles = mesh.GetTriangles(m);
    24.                 for (int i = 0; i < triangles.Length; i += 3)
    25.                 {
    26.                     int temp = triangles[i + 0];
    27.                     triangles[i + 0] = triangles[i + 1];
    28.                     triangles[i + 1] = temp;
    29.                 }
    30.                 mesh.SetTriangles(triangles, m);
    31.             }
    32.         }
    33.     }
    34. }
    35.  
     
    dtaddis likes this.
  7. x2stone

    x2stone

    Joined:
    Dec 17, 2014
    Posts:
    22
    Thank you so much man !!! That was exactly what I was looking for !
    After hours / days looking for how to achieve that using shader coding, render pipeline changes etc, the answer is finally as simple as your script ! I didn't thought about that method, doing it directly on the mesh.
     
    Rispat-Momit likes this.
  8. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    266
    I had a hard time figuring it out myself. I am glad it helped out :)
     
  9. ppernigotti

    ppernigotti

    Joined:
    Aug 8, 2020
    Posts:
    1
    For implemented Cull Front without used Script, I used this graph.

    upload_2022-1-1_21-4-49.png
     
    Vocare4 and linosia97 like this.
  10. Lepisto3D

    Lepisto3D

    Joined:
    Oct 6, 2019
    Posts:
    27
    I've had this issue with a Sprite Unlit shader in URP and I couldnt for the life of me figure out why one of my older shaders had no culling, but any new one I made did.

    It turns out that if you switch the shader to a Lit shader, select Both on Render Faces, and then switch back to Sprite Lit, it retains the Cull setting.

    This has been a headache for a very long time for me as a lot of assets I had I couldn't select in the scene view when they were flipped (so they were culled). Just noticed that doing this method disables the culling under the hood.

    This is kind of a bug, but also not - because I would like this as an exposed setting, because it enables me to work in my scene. Here is the code diff on both shaders when one of them is Sprite Unlit without doing this little trick first. Left side is before the trick, right is after. upload_2023-5-7_17-22-19.png
     
  11. ATMLVE

    ATMLVE

    Joined:
    Jun 11, 2023
    Posts:
    75
    Necroing this but I have a solution to do this via C# script, I searched everywhere online and couldn't find an answer to this question with this thread being one of the top results. The solution is as simple as enabling Allow Material Override on the shader graph. You can then edit the render face per material in the now exposed Surface Options, or via C# script with these:

    Code (CSharp):
    1.         material.SetFloat("_Cull", (float)CullMode.Off);
    2.         material.SetFloat("_Cull", (float)CullMode.Front);
    3.         material.SetFloat("_Cull", (float)CullMode.Back);
     
    richard_harrington likes this.
  12. richard_harrington

    richard_harrington

    Unity Technologies

    Joined:
    Sep 29, 2020
    Posts:
    22
    Just adding some updated info here: as of Unity 2021+, you now have the option to render front, back, or both faces in Shader Graph, via the Render Face setting in the main Graph Inspector panel.
    upload_2023-10-18_14-5-28.png
     
    cupcakebandit, ATMLVE and FredMoreau like this.
  13. ATMLVE

    ATMLVE

    Joined:
    Jun 11, 2023
    Posts:
    75
    Sorry, yes that's the way now and I made it seem you need C#. I was searching for a way to do this specifically in C# at runtime, and this thread was one of the first results so I posted here.
     
    richard_harrington likes this.
  14. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    304
    But at runtine is possible to change it? I want to change to Both or Front depending of situation.
    I had try by code mat.SetFloat("_Cull", (float)CullMode.Off); but nops...
     
  15. ATMLVE

    ATMLVE

    Joined:
    Jun 11, 2023
    Posts:
    75
    Are the properties exposed?

     
  16. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    304
    Yes
    Yes, but the material doesnt change. The parament change to Both but material doesnt change. I thought that maybe is because is necessary to active some part of the sahder like EnableKeyword...