I’m trying to convert a CGFX shader that a colleague wrote into ShaderLab. Now I’m stuck. All Unity tells me is that there is no subshader that can be run on my graphics card. Since I know my graphics card can handle this, I’m guessing there is just some small error that I’m just not seeing. Any help would be appreciated!
Shader "Protein_Phong_Bump_Specular_Transparency" {
Properties {
_ColorTexture ("Diffuse Texture", 2D) = "black" {}
_NormalTexture ("Normal-Map Texture", 2D) = "white" {}
_SpecularTexture ("Specular Texture", 2D) = "white" {}
_TransparencyTexture ("Transparency Texture", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex std_VS
#pragma fragment std_PS
#pragma target 3.0
#include "UnityCG.cginc"
//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////
float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >;
float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
float4x4 WorldXf : World < string UIWidget="None"; >;
float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >;
float gTimer : TIME < string UIWidget = "None"; >;
#define BSIZE 32
#define FULLSIZE 66
#define NOISEFRAC 0.03125
const float4 NTab[FULLSIZE] <string UIWdget="None";> = {
{-0.569811,0.432591,-0.698699,0},
{0.78118,0.163006,0.60265,1},
{0.436394,-0.297978,0.848982,2},
{0.843762,-0.185742,-0.503554,3},
{0.663712,-0.68443,-0.301731,4},
{0.616757,0.768825,0.168875,5},
{0.457153,-0.884439,-0.093694,6},
{-0.956955,0.110962,-0.268189,7},
{0.115821,0.77523,0.620971,8},
{-0.716028,-0.477247,-0.50945,9},
{0.819593,-0.123834,0.559404,10},
{-0.522782,-0.586534,0.618609,11},
{-0.792328,-0.577495,-0.196765,12},
{-0.674422,0.0572986,0.736119,13},
{-0.224769,-0.764775,-0.60382,14},
{0.492662,-0.71614,0.494396,15},
{0.470993,-0.645816,0.600905,16},
{-0.19049,0.321113,0.927685,17},
{0.0122118,0.946426,-0.32269,18},
{0.577419,0.408182,0.707089,19},
{-0.0945428,0.341843,-0.934989,20},
{0.788332,-0.60845,-0.0912217,21},
{-0.346889,0.894997,-0.280445,22},
{-0.165907,-0.649857,0.741728,23},
{0.791885,0.124138,0.597919,24},
{-0.625952,0.73148,0.270409,25},
{-0.556306,0.580363,0.594729,26},
{0.673523,0.719805,0.168069,27},
{-0.420334,0.894265,0.153656,28},
{-0.141622,-0.279389,0.949676,29},
{-0.803343,0.458278,0.380291,30},
{0.49355,-0.402088,0.77119,31},
{-0.569811,0.432591,-0.698699,0},
{0.78118,0.163006,0.60265,1},
{0.436394,-0.297978,0.848982,2},
{0.843762,-0.185742,-0.503554,3},
{0.663712,-0.68443,-0.301731,4},
{0.616757,0.768825,0.168875,5},
{0.457153,-0.884439,-0.093694,6},
{-0.956955,0.110962,-0.268189,7},
{0.115821,0.77523,0.620971,8},
{-0.716028,-0.477247,-0.50945,9},
{0.819593,-0.123834,0.559404,10},
{-0.522782,-0.586534,0.618609,11},
{-0.792328,-0.577495,-0.196765,12},
{-0.674422,0.0572986,0.736119,13},
{-0.224769,-0.764775,-0.60382,14},
{0.492662,-0.71614,0.494396,15},
{0.470993,-0.645816,0.600905,16},
{-0.19049,0.321113,0.927685,17},
{0.0122118,0.946426,-0.32269,18},
{0.577419,0.408182,0.707089,19},
{-0.0945428,0.341843,-0.934989,20},
{0.788332,-0.60845,-0.0912217,21},
{-0.346889,0.894997,-0.280445,22},
{-0.165907,-0.649857,0.741728,23},
{0.791885,0.124138,0.597919,24},
{-0.625952,0.73148,0.270409,25},
{-0.556306,0.580363,0.594729,26},
{0.673523,0.719805,0.168069,27},
{-0.420334,0.894265,0.153656,28},
{-0.141622,-0.279389,0.949676,29},
{-0.803343,0.458278,0.380291,30},
{0.49355,-0.402088,0.77119,31},
{-0.569811,0.432591,-0.698699,0},
{0.78118,0.163006,0.60265,1}};
//// TWEAKABLE PARAMETERS ////
//// NOISE PARAMETERS ////
float gDisplacement <
string UIWidget = "slider";
string UIName = "Noise Displacement";
float UIMin = 0.0;
float UIMax = 2.0;
float UIStep = 0.01;
> = 1.6f;
float gSharpness <
string UIWidget = "slider";
string UIName = "Noise Sharpness";
float UIMin = 0.1;
float UIMax = 5.0;
float UIStep = 0.1;
> = 1.90f;
float gSpeed <
string UIWidget = "slider";
string UIName = "Noise Speed";
float UIMin = 0.01;
float UIMax = 1.0;
float UIStep = 0.001;
> = 0.3f;
float gTurbDensity <
string UIWidget = "slider";
string UIName = "Noise Turbulence";
float UIMin = 0.01;
float UIMax = 8.0;
float UIStep = 0.001;
> = 2.27f;
float4x4 gNoiseXf = {{1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}};
//// LIGHTS ////
float3 PointlightPos <
string Object = "PointLight0";
string UIName = "Point Light Position";
string Space = "World";
> = {0,0,0};
float3 PointlightColor : SPECULAR <
string UIName = "Point Light Color";
string Object = "Pointlight";
string UIWidget = "Color";
> = {1.0f,1.0f,1.0f};
float3 SpotlightPos <
string Object = "SpotlightPos";
string UIName = "Spotlight Position";
string Space = "World";
> = {15.0f,15.0f,15.0f};
// Spotlight points at the origin
float3 SpotlightDir : DIRECTION <
string Object = "SpotlightDir";
string UIName = "Spotlight Direction";
string Space = "World";
> = {0.0f,0.0f,0.0f};
float3 SpotlightColor : SPECULAR <
string UIName = "Spotlight Color";
string Object = "SpotlightColor";
string UIWidget = "Color";
> = {1.0f,1.0f,1.0f};
float SpotlightExponent <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 100.0;
float UIStep = 1.0;
string UIName = "Spotlight Exponent";
> = 2.0;
float SpotlightAngle : FALLOFFANGLE <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.05;
string UIName = "Spotlight Angle Cosine";
> = 0.5;
// Ambient Light
float3 AmbiColor : Ambient <
string UIName = "Ambient Light";
string UIWidget = "Color";
> = {0.07f,0.07f,0.07f};
float Ks <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.05;
string UIName = "Specular";
> = 0.4;
float SpecExpon : SpecularPower <
string UIWidget = "slider";
float UIMin = 1.0;
float UIMax = 128.0;
float UIStep = 1.0;
string UIName = "Specular Power";
> = 55.0;
float Bump <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 3.0;
float UIStep = 0.01;
string UIName = "Bumpiness";
> = 1.0;
float Kr <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.01;
string UIName = "Reflection Strength";
> = 0.5;
float TransparencyIntensity <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.05;
string UIName = "Transparency Intensity";
> = 1.0;
int RepeatU <
string UIWidget = "slider";
int UIMin = 1;
int UIMax = 100;
int UIStep = 1;
string UIName = "Repeat U";
> = 1;
int RepeatV <
string UIWidget = "slider";
int UIMin = 1;
int UIMax = 100;
int UIStep = 1;
string UIName = "Repeat V";
> = 1;
//// COLOR TEXTURE ////
sampler2D _ColorTexture;
sampler2D _NormalTexture;
sampler2D _SpecularTexture;
sampler2D _TransparencyTexture;
sampler2D ColorSampler = sampler_state {
Texture = <_ColorTexture>;
MinFilter = LinearMipMapLinear;
MagFilter = Linear;
WrapS = Repeat;
WrapT = Repeat;
};
sampler2D NormalSampler = sampler_state {
Texture = <_NormalTexture>;
MinFilter = LinearMipMapLinear;
MagFilter = Linear;
WrapS = Repeat;
WrapT = Repeat;
};
sampler2D SpecularSampler = sampler_state {
Texture = <_SpecularTexture>;
MinFilter = LinearMipMapLinear;
MagFilter = Linear;
WrapS = Repeat;
WrapT = Repeat;
};
sampler2D TransparencySampler = sampler_state {
Texture = <_TransparencyTexture>;
MinFilter = LinearMipMapLinear;
MagFilter = Linear;
WrapS = Repeat;
WrapT = Repeat;
};
//// functions ////
// this is the smoothstep function f(t) = 3t^2 - 2t^3, without the normalization
float3 s_curve(float3 t) { return t*t*( float3(3,3,3) - float3(2,2,2)*t); }
float2 s_curve(float2 t) { return t*t*( float2(3,3) - float2(2,2)*t); }
float s_curve(float t) { return t*t*(3.0-2.0*t); }
// 3D version
float noise(float3 v, const uniform float4 pg[FULLSIZE])
{
v = v + (10000.0f).xxx; // hack to avoid negative numbers
float3 i = frac(v * NOISEFRAC) * BSIZE; // index between 0 and BSIZE-1
float3 f = frac(v); // fractional position
// lookup in permutation table
float2 p;
p.x = pg[ i[0] ].w;
p.y = pg[ i[0] + 1 ].w;
p = p + i[1];
float4 b;
b.x = pg[ p[0] ].w;
b.y = pg[ p[1] ].w;
b.z = pg[ p[0] + 1 ].w;
b.w = pg[ p[1] + 1 ].w;
b = b + i[2];
// compute dot products between gradients and vectors
float4 r;
r[0] = dot( pg[ b[0] ].xyz, f );
r[1] = dot( pg[ b[1] ].xyz, f - float3(1.0f, 0.0f, 0.0f) );
r[2] = dot( pg[ b[2] ].xyz, f - float3(0.0f, 1.0f, 0.0f) );
r[3] = dot( pg[ b[3] ].xyz, f - float3(1.0f, 1.0f, 0.0f) );
float4 r1;
r1[0] = dot( pg[ b[0] + 1 ].xyz, f - float3(0.0f, 0.0f, 1.0f) );
r1[1] = dot( pg[ b[1] + 1 ].xyz, f - float3(1.0f, 0.0f, 1.0f) );
r1[2] = dot( pg[ b[2] + 1 ].xyz, f - float3(0.0f, 1.0f, 1.0f) );
r1[3] = dot( pg[ b[3] + 1 ].xyz, f - float3(1.0f, 1.0f, 1.0f) );
// interpolate
f = s_curve(f);
r = lerp( r, r1, f[2] );
r = lerp( r.xyyy, r.zwww, f[1] );
return lerp( r.x, r.y, f[0] );
}
//// CONNECTOR DATA STRUCTURES ////
/* data from application vertex buffer */
struct appdata {
float4 vertex;
float4 texcoord;
float3 normal;
float4 tangent;
};
/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 HPosition : POSITION;
float2 UV : TEXCOORD0;
float3 WorldNormal : TEXCOORD1;
float3 WorldTangent : TEXCOORD2;
float3 WorldBinormal : TEXCOORD3;
float3 WorldView : TEXCOORD4;
float3 PtLtVec : TEXCOORD5;
float3 SptLtVec : TEXCOORD6;
};
//// VERTEX SHADING ////
vertexOutput std_VS(appdata IN,
uniform float4x4 WvpXf,
uniform float Timer,
uniform float Speed,
uniform float Displacement,
uniform float Sharpness,
uniform float TurbDensity,
uniform float4x4 NoiseXf
) {
vertexOutput OUT = (vertexOutput)0;
OUT.WorldNormal = normalize(mul(WorldITXf,float4(IN.normal,0)).xyz);
OUT.WorldTangent = normalize(mul(WorldITXf,IN.tangent).xyz);
float3 binormal = cross(IN.normal, IN.tangent.xyz ) * IN.tangent.w;
OUT.WorldBinormal = normalize(mul(WorldITXf,float4(binormal,0)).xyz);
// code for displacing the vertex
float4 noisePos = TurbDensity*mul(NoiseXf, float4(IN.vertex.xyz+(Speed*Timer),0));
float i = (noise(noisePos.xyz, NTab) + 1.0f) * 0.5f;
float ni = pow(abs(i),Sharpness);
i -= 0.5;
float4 Nn = float4(IN.normal.xyz,0);
float4 NewPos = float4(IN.vertex.xyz,0) - (Nn * (ni-0.5) * Displacement);
float4 Po = float4(NewPos.xyz,1);
float3 Pw = mul(WorldXf,Po).xyz;
OUT.PtLtVec = normalize(PointlightPos - Pw);
OUT.SptLtVec = normalize(SpotlightPos - Pw);
OUT.WorldView = normalize(float3(ViewIXf[0].w,ViewIXf[1].w,ViewIXf[2].w) - Pw);
OUT.HPosition = mul(WvpXf,Po);
OUT.UV = IN.texcoord.xy;
return OUT;
}
//// PIXEL SHADING ////
// Utility function for blinn shading
void phong_shading(vertexOutput IN,
float3 LightColor,
float3 Nn,
float3 Ln,
float3 Vn,
out float3 DiffuseContrib,
out float3 SpecularContrib)
{
float3 Hn = normalize(Vn + Ln);
float4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
DiffuseContrib = litV.y * LightColor;
SpecularContrib = litV.y * litV.z * Ks * LightColor;
}
float4 std_PS(vertexOutput IN) : COLOR {
float3 SptLtDiff,SptLtSpec,PtLtDiff,PtLtSpec;
float spotAtten,spotDot;
IN.UV.x *= RepeatU;
IN.UV.y *= RepeatV;
//normalize direction vectors to lights
float3 PtLtN = IN.PtLtVec;
float3 SptLtN = IN.SptLtVec;
// normalize ws nomals
float3 Vn = IN.WorldView;
float3 Nn = IN.WorldNormal;
float3 Tn = IN.WorldTangent;
float3 Bn = IN.WorldBinormal;
float3 bump = Bump * (tex2D(NormalSampler,IN.UV).rgb - float3(0.5,0.5,0.5));
Nn = Nn + bump.x*Tn + bump.y*Bn;
Nn = normalize(Nn);
// spotlight calculations
spotDot = dot(-SptLtN,normalize(SpotlightDir));
if(spotDot < SpotlightAngle)
spotAtten = 0.0f;
else
spotAtten = pow(spotDot,SpotlightExponent);
// blinn lighting
phong_shading(IN,SpotlightColor,Nn,SptLtN,Vn,SptLtDiff,SptLtSpec);
phong_shading(IN,PointlightColor,Nn,PtLtN,Vn,PtLtDiff,PtLtSpec);
float3 diffuseColor = tex2D(ColorSampler,IN.UV).rgb;
float3 specularColor = tex2D(SpecularSampler,IN.UV).rgb;
float3 specContrib = (specularColor*PtLtSpec) + (specularColor * SptLtSpec * spotAtten);
float3 result = specContrib +(diffuseColor*((SptLtDiff*spotAtten)+PtLtDiff+AmbiColor));
float3 transparentColor = tex2D(TransparencySampler,IN.UV).rgb * TransparencyIntensity; //
float transValue = (transparentColor.r + transparentColor.g + transparentColor.b) / 3.0;
return float4(result,transValue);
}
//// TECHNIQUES ////
/*
technique Main <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexProgram = compile vp40 std_VS(WvpXf,gTimer,
gSpeed,gDisplacement,
gSharpness,gTurbDensity,
gNoiseXf);
DepthTestEnable = true;
DepthMask = true;
CullFaceEnable = true;
CullFace = Back;
BlendEnable = true;
BlendFunc = int2(SrcAlpha, OneMinusSrcAlpha);
DepthFunc = LEqual;
FragmentProgram = compile fp40 std_PS();
}
}*/
ENDCG
}
}
}