- Home /
Unity CG Include not at all working
Hi ,
am not a pro graphic programmer but have some knowledge about them. So if the question sounds too stupid please do excuse me*
I was working on Physically Based Shader system in unity .For which i decided to use an CGinclude file to make the code reusable But no matter what i do i kep getting this error
Shader error in 'CG/Physically Based Shader Test': syntax error, unexpected reserved word "return" at token "return" type name expected at token "return" syntax error, unexpected ',' at token "," type name expected at token "," at line 177
Shader error in 'CG/Physically Based Shader Test': syntax error, unexpected '{', expecting ',' or ';' at token "{" type name expected at token "{" at Assets/Cutom Shaders/cgTest.cginc(24)
Shader error in 'CG/Physically Based Shader Test': function "frag" has no return statement at line 119
and this happens when i use inline elements or float function
The Shader
Shader "CG/Physically Based Shader Test"
{
Properties
{
_Albedo ("Albedo Map ) ",2D) = "white" {}
_SpecularMap ("Specular Texture (Gloss (A)) ",2D) = "white" {}
_SpecularColor ("Specular Color",Color) = (1,1,1,1)
_BumpMap ("Normal Map",2D) = "bump" {}
_Cube ("Environment Refelction Map",CUBE) = "black" {}
_Metaliness ("Metaliness" ,Color)= (1,1,1,1) // Not using right now
_LUT ("LookUp G(R) D(G) Metaliness(A) ",2D) = ""{} // Not using right now
_Roughness ("Roughness",Range(0,1)) = 0.5
}
SubShader{
Pass
{
Tags {"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#pragma glsl
#pragma only_renderers d3d9 opengl d3d11
//#include "UnityCG.cginc"
// Variables
// Diffuse Params
uniform sampler2D _Albedo;
uniform float4 _Albedo_ST;
// Specular Params
uniform sampler2D _SpecularMap;
uniform half4 _SpecularMap_ST;
uniform fixed4 _SpecularColor;
uniform fixed _Roughness;
// Normal Map
uniform sampler2D _BumpMap;
// IBL params
uniform samplerCUBE _Cube;
uniform fixed4 _Metaliness;
// Look Up Texture
uniform sampler2D _LUT;
uniform fixed4 _LightColor0;
/// Structs
struct vertexInput
{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texCoord:TEXCOORD0;
};
struct vertexOutput
{
float4 pos:SV_POSITION;
float4 uv_Diff:TEXCOORD0;
half4 WorldPos:TEXCOORD3;
fixed3 NormalDirection:TEXCOORD1;
fixed4 LightDirection:TEXCOORD2;
};
vertexOutput vert(vertexInput i)
{
vertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
o.NormalDirection = normalize(mul(half4(i.normal,0f),_World2Object).xyz);
o.uv_Diff = i.texCoord;
o.WorldPos = mul(_Object2World,i.vertex);
// LightDirection and Attenuation
return o;
}
// Fragment Shader
half4 frag (vertexOutput i):COLOR
{
half3 LightDir = lerp(_WorldSpaceLightPos0.xyz,_WorldSpaceLightPos0.xyz - i.WorldPos.xyz,_WorldSpaceLightPos0.w);
half dist = length(LightDir);
i.LightDirection = fixed4(normalize(LightDir),lerp(1,1/dist,_WorldSpaceLightPos0.w));
// Normal Directions
fixed3 normalDirection = i.NormalDirection;
// Necesary Calculation
fixed3 ViewDirection = normalize(_WorldSpaceCameraPos.xyz - i.WorldPos.xyz);
fixed3 HalfVector = normalize(i.LightDirection.xyz + ViewDirection);
fixed NdotL = max(0, dot(normalDirection,i.LightDirection.xyz));
fixed NdotH = saturate(dot(normalDirection,HalfVector));
fixed LdotH = saturate(dot(i.LightDirection.xyz,HalfVector));
fixed VdotN = saturate(dot(ViewDirection,normalDirection));
// DLambert Calculation
fixed4 texD = tex2D(_Albedo,_Albedo_ST.xy * i.uv_Diff.xy + _Albedo_ST.zw);
fixed3 LambertDiffuse = pow(NdotL * 0.5 + 0.5,2) * texD.rgb * _LightColor0.xyz ;
// Specular Calculation (Cook Torrence Model)
//Fresenl Term
fixed3 Fresnel_Spec = _SpecularColor.rgb + (1 - _SpecularColor.rgb) * pow((1 - dot(ViewDirection.xyz,HalfVector)),5); // Fres = Cs + ( 1- Cs) (a - l.h)^5
// Geometry Factor and Visualisation Term
// V(l,v,h) = 1/(l.h)^2 = G(l,h)/(n.v)(n.l)
fixed Shadow_Mask = 1 / pow(LdotH,2); // Sigraph 2013 Math Slides
// Normalization fator (Dh)
half alpha = pow(8192,_Roughness);
half DepthFactor = ( (alpha + 2) / (8 * 3.14) ) * pow(NdotH,alpha); // 1 / 8 phi = 0.39285714
/// Final Specular
// Sub Specular
// Reflection Calculation
fixed3 AmbientMap = texCUBElod(_Cube, half4( reflect(ViewDirection,normalDirection) ,(( 1- _Roughness))*3)).rgb;
// fixed3 CubeMapFresnel = _SpecularColor.xyz + (1 - _SpecularColor.xyz) * ( pow( (1 - VdotN) ,5) ); // Rf0 + ( 1- Rf0) * (1 - N.V)^5
#include "cgTest.cginc";
half3 FinalSpecular = (Fresnel_Spec * (GeometericAttenuation) * BeckamannNDF )/(NdotL * VdotN);
// fixed3 FresMetal = fixed3(_Metaliness,_Metaliness,_Metaliness) + ( 1- fixed3(_Metaliness,_Metaliness,_Metaliness)) * ( pow( (1 - VdotN) ,5) );
fixed3 FinalEnvMap= AmbientMap / (4 - 3*_Roughness) * CubeMapFresnel_rough;
// Rim light
fixed Rim = 1 - VdotN;
fixed FinalRim = pow(Rim,5) * NdotH;
fixed3 FinalGather = (LambertDiffuse / 3.14) * (1 - _SpecularColor.rgb)+ ( FinalSpecular /(3.14) * _LightColor0.xyz )+ FinalEnvMap ;// (FinalRim * _LightColor0.xyz) ;
return fixed4(FinalGather,1);
}
ENDCG
}
}
// Fallback "Specular"
}
The CG include file
#ifndef CG_TEST_INCLUDED
#define CG_TEST_INCLUDED
#define PI 3.14159265359
// Specular Cook Torrence
half g_min = min(VdotN,NdotL);
//half GeometericAttenuation = min(1, min((2 * NdotH * VdotN) / saturate(dot(ViewDirection,HalfVector)) , (2 * NdotH * NdotL)/saturate(dot(ViewDirection,HalfVector))));
half GeometericAttenuation = saturate( 2* NdotH * g_min/dot(ViewDirection,HalfVector));
half3 FresnelModified = _SpecularColor.xyz + (1 - _SpecularColor.xyz) * pow(2,((-5.55473 *saturate(dot(ViewDirection,HalfVector)) ) - 6.98316) * saturate(dot(ViewDirection,HalfVector)) );
fixed a= pow(_Roughness,2);
fixed DepthNormaizsation = pow(a,2)/ (3.14* pow( ( (pow(a,2) -1) * pow(NdotH,2) ) + 1 ,2)); //
half BeckamannNDF = ( 1/ ( 3.14 * pow(a,2) * pow(NdotH,4)) ) * exp( (pow(NdotH,2) - 1) / (pow(a,2) * pow(NdotH,2)));
//CubeMap Spcuar
fixed3 CubeMapFresnelModified = _SpecularColor + (max(_Roughness,_SpecularColor) - _SpecularColor) * pow(1 - saturate(dot(ViewDirection,normalDirection)),5);
fixed3 CubeMapFresnel = _SpecularColor.xyz + (1 - _SpecularColor.xyz) * ( pow( (1 - VdotN) ,5) ); // Rf0 + ( 1- Rf0) * (1 - N.V)^5
fixed3 CubeMapFresnel_rough = _SpecularColor.xyz + (max(_Roughness,_SpecularColor.xyz) - _SpecularColor.xyz) * pow(1 - saturate(dot(ViewDirection,normalDirection)),5);
// Here is where I get weired error when i use the following line of code (Note this code is not used anywher as it is returning erros)
inline float D_Beckmann ( float Roughness, float NoH )
{
return Roughness; // Just for testing
}
#endif
So what should i do to make it work. Any help is really appreciated
Thanks
Answer by tanoshimi · Jul 20, 2014 at 02:25 PM
If you had included your full shader it would make it a lot easier to help you, as the line numbers in your error messages would actually mean something, and we could also see what things like m
, NoH
, etc were...
The return value of your D_Beckmann function is NdotH
, for example. Where is this defined?
-- edit --
Ok, now I see you have several unrelated errors in your code, but you'd probably find it a lot easier to spot them if you didn't have so many badly-named/unused variables (DepthFactor? DepthNormaizsation?) and a little bit more formatting...!
#include "cgTest.cginc";
on line 146 should not have a semicolon.The point at which you're including cgTest.cginc is from within the frag function. Your include file is then trying to define the DBeckmann() function inside that function. Move DBeckmann() out of your cginc and into your main code file.
You appear to have some strange characters or encoding in your cginc file. This can happen (as described at http://forum.unity3d.com/threads/include-custom-cginc-files.94518/) if you create your cginc file using Visual Studio, or other programs that add a BOM to the UTF encoding. Create a new file (or duplicate one of the existing cginc files) and ensure that it is saved as UTF-8 without BOM.
Sort those out and you should be good to go.
Thanks @tanoshimi I will upload the full shader and NdotH was a typo that ocurred while i was editing the code in the web
thanks @tanoshimi I will give it a try . yeah i am realy really bad at na$$anonymous$$g stuff
I did encode and save it as UTF-8 without BO$$anonymous$$. but still facing same problem
And one more thing Why is my specular highlight so dull and patchy even though it is in fragment program.
I used Cook Torrance BRDF but when i set the specular color to a bit grey(Plastic ) the specular is almost 0 but it should be a bit brighter(saw demo of similar approximation) No matter what i do its is the same result
Thanks for your patience I really appreciate it
Thanks
If you're using Notepad++ go to the encoding menu and select Encode in UTF-8 without BO$$anonymous$$ (second option down on my version)
Thanks @tanoshimi
Edit
Thanks to you .I took your advice of moving the code (moved everything execpt the inline elements) and now it works flawlessly
Thank you
Could You please please look into my specular it is acting really strange
used Cook Torrance BRDF but when i set the specular color to a bit grey(Plastic ) the specular is almost 0 but it should be a bit brighter(saw demo of similar approximation) No matter what i do its is the same result
Thank you I really appreciate your help
@tanoshimi would you please help me solving the specular problem
Answer by aditya · Jul 20, 2014 at 02:32 PM
bro in your cginc file your returning an undefined variable as your defined a variable NdotH2 but returning NdotH, the "2" is missing from variable name
Thanks for the reply but it was a typo that occurred while entering it into the website as i didnt want to upload the full shader