Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Error on build with custom node with Shader Graph

Discussion in 'Graphics Experimental Previews' started by TSDrake, May 24, 2019.

  1. TSDrake

    TSDrake

    Joined:
    Nov 13, 2017
    Posts:
    1
    The title say almost all that it is but here is the code of the custom node (not mine)
    and all the errors i get only on build not in play mode.

    Errors:
    Assets/Scripts/ShaderScritps/MainLightNode.cs(4,19): error CS0234: The type or namespace name `ShaderGraph' does not exist in the namespace `UnityEditor'. Are you missing an assembly reference?
    Assets/Scripts/ShaderScritps/MainLightNode.cs(8,30): error CS0246: The type or namespace name `CodeFunctionNode' could not be found. Are you missing an assembly reference?
    Assets/Scripts/ShaderScritps/MainLightNode.cs(10,26): error CS0115: `MainLightNode.hasPreview' is marked as an override but no suitable property found to override
    Assets/Scripts/ShaderScritps/MainLightNode.cs(59,35): error CS0115: `MainLightNode.GetFunctionToConvert()' is marked as an override but no suitable method found to override
    Assets/Scripts/ShaderScritps/MainLightNode.cs(65,47): error CS0246: The type or namespace name `FunctionRegistry' could not be found. Are you missing an assembly reference?
    Assets/Scripts/ShaderScritps/MainLightNode.cs(65,74): error CS0246: The type or namespace name `GraphContext' could not be found. Are you missing an assembly reference?
    Assets/Scripts/ShaderScritps/MainLightNode.cs(65,101): error CS0246: The type or namespace name `GenerationMode' could not be found. Are you missing an assembly reference?
    Assets/Scripts/ShaderScritps/MainLightNode.cs(76,33): error CS0246: The type or namespace name `Vector1' could not be found. Are you missing an assembly reference?
    Error building Player because scripts had compiler errors
    Build completed with a result of 'Failed'
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
    UnityEditor.BuildPlayerWindow+BuildMethodException: 9 errors
    at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (BuildPlayerOptions options) [0x0021f] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:187
    at UnityEditor.BuildPlayerWindow.CallBuildMethods (Boolean askForBuildLocation, BuildOptions defaultBuildOptions) [0x0007f] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:94
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)



    Custom node:
    [Title("Custom", "Main Light")]
    public class MainLightNode : CodeFunctionNode
    {
    public override bool hasPreview { get { return false; } }

    //This is the string that's passed into the code of the real, final shader code that is used in the Scene when you hit Apply in the ShaderGraph window.
    //As such, it will use real light data from the Scene (for instance, GetMainLight()).
    private static string functionBodyForReals = @"{
    Light mainLight = GetMainLight();
    Color = mainLight.color;
    Direction = mainLight.direction;
    float4 shadowCoord;
    #ifdef _SHADOWS_ENABLED
    #if SHADOWS_SCREEN
    float4 clipPos = TransformWorldToHClip(WorldPos);
    shadowCoord = ComputeShadowCoord(clipPos);
    #else
    shadowCoord = TransformWorldToShadowCoord(WorldPos);
    #endif
    mainLight.attenuation = MainLightRealtimeShadowAttenuation(shadowCoord);
    #endif
    Attenuation = mainLight.attenuation;
    }";

    //This string is passed to the node to generate the shader that's used in the ShaderGraph for preview.
    //Since the graph has no conception of what's the main light, we fake the data by hardcoding it in.
    private static string functionBodyPreview = @"{
    Color = 1;
    Direction = float3(-0.5, -.5, 0.5);
    Attenuation = 1;
    }";

    private static bool isPreview;

    //Returns a different string depending on whether we are in the graph or not.
    private static string functionBody
    {
    get
    {
    if (isPreview)
    return functionBodyPreview;
    else
    return functionBodyForReals;
    }
    }

    //Constructor
    public MainLightNode()
    {
    name = "Main Light";
    }

    protected override MethodInfo GetFunctionToConvert()
    {
    return GetType().GetMethod("CustomFunction", BindingFlags.Static | BindingFlags.NonPublic);
    }

    //Will calculate the boolean isPreview which is used to decide which of the 2 strings to use (see above)
    public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
    isPreview = generationMode == GenerationMode.Preview;

    base.GenerateNodeFunction(registry, graphContext, generationMode);
    }

    //The definition of the ports. 3 go out, 1 goes in
    //(which also has a default binding of WorldSpacePosition, so it doesn't need to be connected)
    private static string CustomFunction(
    [Slot(0, Binding.None)] out Vector3 Direction,
    [Slot(1, Binding.None)] out Vector1 Attenuation,
    [Slot(2, Binding.None)] out Vector3 Color,
    [Slot(3, Binding.WorldSpacePosition)] Vector3 WorldPos)
    {
    //These default values are needed otherwise Unity will complain that the Vector3s are not initialised.
    //They won't be zero in the shader.
    Direction = Vector3.zero;
    Color = Vector3.zero;

    return functionBody;
    }
    }
     
  2. nathonion

    nathonion

    Joined:
    May 12, 2016
    Posts:
    3
    I'm getting the same exact issue, with the same code. An answer to this would be much appreciated!
     
    ansjuan likes this.
  3. ansjuan

    ansjuan

    Joined:
    May 15, 2018
    Posts:
    21
    Same here, any cues?
     
  4. Jason-King

    Jason-King

    Joined:
    Oct 28, 2010
    Posts:
    56
    I had this issue when I moved the source out of the Plugins folder into a separate folder - and only could be resolved by moving the code back into the Plugins folder.
     
  5. Holoday

    Holoday

    Joined:
    May 15, 2020
    Posts:
    8
    Can you elaborate on what you mean by that? I haven't moved any plugins anywhere...