I wanna try to create a POM shader for Unity, buthave some problems:
Shader "POM" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_BumpMap ("Bumpmap (RGB)", 2D) = "bump" {}
_HeightScale("Height Scale",Range (0,1)) = 0.05
_MaxSamples("Max Samples",Range (0,128)) = 50
_MinSamples("Min Samples",Range (0,128)) = 4
}
SubShader {
Pass{
Tags {"LightMode" = "PixelOrNone"}
Blend AppSrcAdd AppDstAdd
Color [_PPLAmbient]
CGPROGRAM
// profiles arbfp1
#pragma fragmentoption ARB_fragment_program
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "AutoLight.cginc"
sampler2D _MainTex;
sampler2D _BumpMap;
float _HeightScale;
int _MinSamples;
int _MaxSamples;
struct v2f{
V2F_POS_FOG;
float2 uv;
float3 tangentSpaceLightDir;
float3 viewDirT;
float2 ParallaxOffset;
};
v2f vert (appdata_tan v)
{
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFORM_UV(0);
float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) );
float3x3 rotation = float3x3( v.tangent.xyz, binormal, v.normal );
o.viewDirT = mul(normalize(ObjSpaceViewDir(v.vertex)),rotation);
float3 viewDir = o.viewDirT;
o.tangentSpaceLightDir = mul(rotation, viewDir);
float2 vParallaxDirection = normalize( o.viewDirT.xy );
float fLength = length( o.viewDirT );
float fParallaxLength = sqrt( fLength * fLength - o.viewDirT.z * o.viewDirT ) / o.viewDirT.z;
o.ParallaxOffset = vParallaxDirection * fParallaxLength;
o.ParallaxOffset *= _HeightScale;
return o;
}
half4 frag (v2f i) : COLOR
{
appdata_tan v;
half4 texcol = tex2D( _MainTex, i.uv );
// get normal from the normal map
half3 normal = tex2D(_BumpMap, i.uv).rgb * 2.0 - 1.0;
float2 dx = ddx(i.uv);
float2 dy = ddy(i.uv);
int nNumSteps = (int) lerp( _MaxSamples, _MinSamples, dot( normalize(i.viewDirT), normalize(v.normal )) );
float fCurrHeight = 0.0;
float fStepSize = 1.0 / (float) nNumSteps;
float fPrevHeight = 1.0;
float fNextHeight = 0.0;
int nStepIndex = 0;
bool bCondition = true;
float2 vTexOffsetPerStep = fStepSize * i.ParallaxOffset;
float2 vTexCurrentOffset = i.uv;
float fCurrentBound = 1.0;
float fParallaxAmount = 0.0;
float2 pt1 = 0;
float2 pt2 = 0;
float2 texOffset2 = 0;
while (nStepIndex < nNumSteps )
{
vTexCurrentOffset -= vTexOffsetPerStep;
// Sample height map which in this case is stored in the alpha channel of the normal map:
fCurrHeight = tex2D( _BumpMap, vTexCurrentOffset, dx, dy ).a;
fCurrentBound -= fStepSize;
if ( fCurrHeight > fCurrentBound )
{
pt1 = float2( fCurrentBound, fCurrHeight );
pt2 = float2( fCurrentBound + fStepSize, fPrevHeight );
texOffset2 = vTexCurrentOffset - vTexOffsetPerStep;
nStepIndex = nNumSteps + 1;
}
else
{
nStepIndex++;
fPrevHeight = fCurrHeight;
}
}
float fDelta2 = pt2.x - pt2.y;
float fDelta1 = pt1.x - pt1.y;
fParallaxAmount = (pt1.x * fDelta2 - pt2.x * fDelta1 ) / ( fDelta2 - fDelta1 );
float2 vParallaxOffset = i.ParallaxOffset * (1 - fParallaxAmount );
// The computed texture offset for the displaced point on the pseudo-extruded surface:
float2 texSample = i.uv - vParallaxOffset;
float2 vLightRayTS = normalize(i.tangentSpaceLightDir) * _HeightScale;
half4 c = texcol;
return c;
}
ENDCG
}
UsePass "Bumped Specular/PPL"
}
FallBack "Parallax Specular", 1
}
and there are 3 errors that appear:
-
Cg in program ‘frag’: error C3004: function “ddx” not supported in this profile at line 67
-
Cg in program ‘frag’: error C3004: function “ddy” not supported in this profile at line 68
-
Cg in program ‘frag’: error C3004: function “tex2D” not supported in this profile at line 94.
anybody can help me???