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
  4. Dismiss Notice

Failed to open source file.

Discussion in 'Shaders' started by MayBeArtorias, Jan 14, 2021.

  1. MayBeArtorias

    MayBeArtorias

    Joined:
    Nov 29, 2017
    Posts:
    3
    Hello,

    I'm trying to implement a shader with multiple light sources following catlike tutorial series. As I'm trying to include the shader file ("MyLight") I get the error:
    "Shader error in 'Unlit/EnhancedShader': failed to open source file: 'Shadow_Testing/Assets/ShaderForge/MyLight.cginc' ..."
    But I don't know why Unity can't find the file because all shaders are in the same folder.
    I tried to reimport the shaders, restarting Unity and Rider or to autocreate a new blank Unlit Shader too - with no effects.
     

    Attached Files:

  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,852
    Hi!

    "MyLight" is a shader in your case, and it has a ".shader" extension.
    You are trying to include a file "MyLight.cginc", which doesn't exist.
     
    MayBeArtorias likes this.
  3. MayBeArtorias

    MayBeArtorias

    Joined:
    Nov 29, 2017
    Posts:
    3
    I tried to change the file type to, but then I get the error: Shader error in "Unlit/EnhancedShader": unrecognized identifier 'Shader' at "[Path to MyLigh.shader]".

    Here is my shader Code: (maybe it helps)
    First Shader:


    Shader "Unlit/EnhancedShader"
    {
    Properties
    {
    _Tint ("Tint", Color) = (1, 1, 1, 1)
    _MainTex ("Albedo", 2D) = "white" {}
    _SpecularTint ("Specular", Color) = (0.5, 0.5, 0.5)
    _Smoothness ("Smoothness", Range(0, 1)) = 0.1
    _Metallic ("Metallic", Range(0, 1)) = 0
    }
    SubShader
    {

    LOD 100

    Pass
    {
    Tags {
    "LightMode" = "ForwardBase"
    }
    CGPROGRAM
    //#include "NewUnlitShader.shader"
    #pragma vertex MyVertexProgram
    #pragma fragment MyFragmentProgram
    #pragma target 3.0

    #include "MyLight.shader"
    #include "UnityCG.cginc"
    #include "UnityPBSLighting.cginc"

    //#if !defined(MY_LIGHTING_INCLUDED)
    //#define MY_LIGHTING_INCLUDED
    //#endif

    ENDCG
    }
    Pass
    {
    Tags {
    "LightMode" = "ForwardAdd"
    }
    Blend one one

    CGPROGRAM

    #pragma vertex MyVertexProgram
    #pragma fragment MyFragmentProgram
    #pragma target 3.0
    //#pragma _multi_compile

    #include "Mylight.shader"
    #include "UnityCG.cginc"
    //#include "UnityStandardBRDF.cginc"
    //#include "UnityStandardUtils.cginc"
    #include "UnityPBSLighting.cginc"


    ENDCG
    }
    }}


    Second Shader:


    Shader "MyLight"
    {
    Properties
    {
    //_MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
    //Tags { "RenderType"="Opaque" }
    //LOD 100

    Pass
    {
    CGPROGRAM
    #pragma vertex MyVertexProgram
    #pragma fragment MyFragmentProgram
    #pragma target 3.0


    #include "UnityPBSLighting.cginc"
    #include "AutoLight.cginc"

    struct VertexData {
    float4 position : POSITION;
    float3 normal : NORMAL;
    float2 uv : TEXCOORD0;

    };

    struct Interpolators {
    float4 position : SV_POSITION;
    float2 uv : TEXCOORD0;
    float3 normal : TEXCOORD1;
    float3 worldPos : TEXCOORD2;
    };



    float4 _Tint;
    sampler2D _MainTex;
    float _Smoothness;
    float _Metallic;
    float4 _MainTex_ST;

    Interpolators MyVertexProgram (VertexData v) {
    Interpolators i;
    i.uv = TRANSFORM_TEX(v.uv, _MainTex);
    i.position = UnityObjectToClipPos(v.position);
    i.worldPos = mul(unity_ObjectToWorld, v.position);
    i.normal = i.normal = UnityObjectToWorldNormal(v.normal);
    return i;
    }

    UnityLight CreateLight (Interpolators i) {
    UnityLight light;
    light.dir = normalize(_WorldSpaceLightPos0.xyz - i.worldPos);
    UNITY_LIGHT_ATTENUATION(attenuation, 0, i.worldPos);
    light.color = _LightColor0.rgb * attenuation;
    light.ndotl = DotClamped(i.normal, light.dir);
    return light;
    }

    float4 MyFragmentProgram (Interpolators i) : SV_TARGET {
    i.normal = normalize(i.normal);
    float3 viewDir = normalize(_WorldSpaceCameraPos - i.worldPos);

    //float4 tint = (1, 1, 1, 1);
    float3 albedo = tex2D(_MainTex, i.uv).rgb * _Tint.rgb;

    float3 specularTint;
    float oneMinusReflectivity;
    albedo = DiffuseAndSpecularFromMetallic(
    albedo, _Metallic, specularTint, oneMinusReflectivity
    );


    UnityIndirect indirectLight;
    indirectLight.diffuse = 0;
    indirectLight.specular = 0;

    return UNITY_BRDF_PBS(
    albedo,
    specularTint,
    oneMinusReflectivity,
    _Smoothness,
    i.normal,
    viewDir,
    CreateLight(i),
    indirectLight
    );
    }

    ENDCG
    }
    }
    }
     
    Last edited: Jan 14, 2021
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,852
    That's because an include file is not a shader and it shouldn't contain a shader.
    Please check again with the tutorial you're going through.
     
    MayBeArtorias likes this.
  5. MayBeArtorias

    MayBeArtorias

    Joined:
    Nov 29, 2017
    Posts:
    3
    I kinda read it wrong. I created a new .cginc file and this way it is working as intended :)
    Code (CSharp):
    1.  
    2. #include "UnityPBSLighting.cginc"
    3. #include "AutoLight.cginc"
    4.  
    5. struct VertexData {
    6.     float4 position : POSITION;
    7.     float3 normal : NORMAL;
    8.     float2 uv : TEXCOORD0;
    9.                
    10. };
    11.            
    12. struct Interpolators {
    13.     float4 position : SV_POSITION;
    14.     float2 uv : TEXCOORD0;
    15.     float3 normal : TEXCOORD1;
    16.     float3 worldPos : TEXCOORD2;
    17. };
    18.  
    19.            
    20.  
    21. float4 _Tint;
    22. sampler2D _MainTex;
    23. float _Smoothness;
    24. float _Metallic;
    25. float4 _MainTex_ST;
    26.  
    27. Interpolators MyVertexProgram (VertexData v) {
    28.     Interpolators i;
    29.     i.uv = TRANSFORM_TEX(v.uv, _MainTex);
    30.     i.position = UnityObjectToClipPos(v.position);
    31.     i.worldPos = mul(unity_ObjectToWorld, v.position);
    32.     i.normal = i.normal = UnityObjectToWorldNormal(v.normal);
    33.     return i;
    34. }
    35.  
    36. UnityLight CreateLight (Interpolators i) {
    37.     UnityLight light;
    38.     light.dir = normalize(_WorldSpaceLightPos0.xyz - i.worldPos);
    39.     UNITY_LIGHT_ATTENUATION(attenuation, 0, i.worldPos);
    40.     light.color = _LightColor0.rgb * attenuation;
    41.     light.ndotl = DotClamped(i.normal, light.dir);
    42.     return light;
    43. }
    44.  
    45. float4 MyFragmentProgram (Interpolators i) : SV_TARGET {
    46.     i.normal = normalize(i.normal);
    47.     float3 viewDir = normalize(_WorldSpaceCameraPos - i.worldPos);
    48.  
    49.     //float4 tint = (1, 1, 1, 1);
    50.     float3 albedo = tex2D(_MainTex, i.uv).rgb * _Tint.rgb;
    51.  
    52.     float3 specularTint;
    53.     float oneMinusReflectivity;
    54.     albedo = DiffuseAndSpecularFromMetallic(
    55.         albedo, _Metallic, specularTint, oneMinusReflectivity
    56.     );
    57.                
    58.                
    59.     UnityIndirect indirectLight;
    60.     indirectLight.diffuse = 0;
    61.     indirectLight.specular = 0;
    62.                
    63.     return UNITY_BRDF_PBS(
    64.         albedo,
    65.         specularTint,
    66.         oneMinusReflectivity,
    67.         _Smoothness,
    68.         i.normal,
    69.         viewDir,
    70.         CreateLight(i),
    71.         indirectLight
    72.     );
    73. }
     
    aleksandrk likes this.