I've been working on a project which involves me working with shader coding something I'm not familiar with. I've been provided some HLSL code from another team member and have been trying to implement in unity. Not being able to use the provided code directly I've been trying to convert it into the format unity wants it to be in as seen below. Just wanted to see if there is anywhere obvious i am going wrong it compiles fine and i can attach it to an object but its just a flat black.
Shader "Custom/test2" {
SubShader {
Pass{
CGPROGRAM
#pragma fragment frag
struct PixelInput {
float4 norm : TEXCOORD0;
float4 worldpos : TEXCOORD1;
float4 eyevector : TEXCOORD2;
float4 tangent : TEXCOORD3;
float4 binormal : TEXCOORD4;
float4 objpos : TEXCOORD5;
float4 tex0 : TEXCOORD6;
float4 tex1 : TEXCOORD7;
};
struct PixelOutput {
float4 c : COLOR;
};
uniform sampler2D BumpTex;
uniform sampler2D MarbleCol1;
float4 marblefunc(float4 p, float3 norm, float3 eye, float4 uv){
float3 var;
float3 pos = p.xyz * 0.019500 ;
float3 r = float3(pos.x, pos.y, pos.z);
var[0] = (pos.x +10000 )* 0.045000 + 60 *noise (r);
var[2] = var[0]%17;
if (var[2]<4){
r.r = pos.x/70.0f; r.g = pos.y/50.0f; r.b = pos.z/50.0f;
var[1] = 0.7f+0.2f*noise(r);
}
else {
r.r = pos.x; r.g = pos.y; r.b = pos.z;
if ( var[2] <9 ) {
if (var[2] >=12){
var[0] = abs(var[0]-(var[0]/17.0) *17.0 - 10.5)* 0.1538462;
var[1] = 0.3 + 0.3*var[0] + 0.8*noise(r);
}
} else {
var[1] = 0.3f*(1.0f + noise(r));
}
}
float4 c0 = tex2D(MarbleCol1,uv);
float4 c1 = float4( 0.301961, 0.815686, 0.101961, 1.0);
return (1.0f-var[1])*c0 + var[1]*c1;
}
uniform float4 Specular = float4(0.900000,0.900000,0.900000,1.000000);
uniform float4 Emissive = float4(0.000000,0.000000,0.000000,1.0f);
uniform float SpecularLevel = 0.000000;
uniform float GlossLevel = 9.999999;
uniform float3 LightCol0;
uniform float3 LightDir0;
uniform float LightFallOff0;
uniform float LightHotSpot0;
uniform float Opacity = 1.000000;
PixelOutput fragmentEntry(PixelInput pi)
{
PixelOutput PO;
float3 N;
N = normalize(pi.norm.xyz);
float3 Eye;
Eye.xyz = normalize(pi.eyevector.xyz);
float4 nColor = float4(0.0,0.0,0.0,1.0);
float3 BumpN;
float3 BumpX, BumpY, BumpZ;
BumpN = tex2D(BumpTex, pi.tex0);
BumpN = (BumpN - 0.5);
BumpN = normalize(float3(BumpN.x *0.300000, BumpN.y *0.300000, BumpN.z));
BumpZ = N;
BumpX = pi.binormal.xyz;
BumpY = pi.tangent.xyz;
BumpX = normalize(BumpX);
BumpY = normalize(BumpY);
BumpZ = normalize(BumpZ);
N = BumpX *BumpN.x + BumpY* BumpN.y + BumpZ *BumpN.z;
float4 mDiffuse;
mDiffuse = marblefunc( pi.objpos, N, Eye, pi.tex0);
float4 mSpecular = Specular;
float4 SelfIllum = Emissive;
nColor = nColor + SelfIllum *mDiffuse;
float3 H;
float3 Light;
float f;
float fAtt;
float fDist;
fAtt = 1.0;
nColor = nColor + float4(LightCol0,1.0)* mDiffuse* clamp(dot(N,LightDir0),0,1)* fAtt;
H = normalize(Eye+Light);
f = clamp(dot(N,H),0,1);
f = pow(f, GlossLevel);
f = f *SpecularLevel;
nColor = nColor + float4(LightCol0,1.0)* mSpecular* f *fAtt;
nColor.a = Opacity;
PO.c = nColor;
return PO;
}
ENDCG
}
}
FallBack "Diffuse"
}
Any and all help is greatly appreciated.
You should really try to compile it, and resolve the compiler error step by step. Here's just a few hints:
CG
code, not shaderlab
shaders. That's fine and the syntax is the same documented into Nvidia doc.var[2] = var[0]%17;
var is a float so you should use fmod instead.#pragma vertex vertexFunctionName
)Above there are just a few hints. I think that will be too much completely debug the shader for you. If you have specific questions on specific errors feel free to ask.