这就是我想要完成的
- 带有主着色器的纹理几何图形
- 按照法线的投影量在第一个纹理上分层第二个纹理。
这是我当前的着色器代码:
Shader "Custom/FurShader"
{
Properties
{
_MainTex( "Main Texture", 2D ) = "white" {}
_MaxHairLength( "Max Hair Length", Float ) = 0.5
_NoOfPasses( "Number of Passes", Float ) = 2.0
}
CGINCLUDE
//includes
#include "UnityCG.cginc"
//structures
struct vertexInput
{
float4 vertex : POSITION;
float4 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct fragmentInput
{
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
};
//uniforms
uniform float _MaxHairLength;
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform sampler2D _SecondTex;
uniform float4 _SecondTex_ST;
uniform float _NoOfPasses;
//function
inline fragmentInput LevelFragmentShader( vertexInput i, int level )
{
fragmentInput o;
float movementDist = ( _MaxHairLength / _NoOfPasses ) * level;
float4 pos = ( i.vertex + ( i.normal * movementDist ) );
o.pos = mul( UNITY_MATRIX_MVP, pos );
o.uv = TRANSFORM_TEX( i.texcoord, _SecondTex );
return o;
}
half4 frag( fragmentInput i ) : COLOR
{
return tex2D( _SecondTex, i.uv );
}
ENDCG
SubShader {
Tags { "Queue" = "Transparent"}
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
#pragma exclude_renderers gles
#pragma vertex vert
#pragma fragment frag_unique
fragmentInput vert( vertexInput i )
{
fragmentInput o;
o.pos = mul( UNITY_MATRIX_MVP, i.vertex );
o.uv = TRANSFORM_TEX( i.texcoord, _MainTex );
return o;
}
half4 frag_unique( fragmentInput i ) : COLOR
{
return tex2D( _MainTex, i.uv );
}
ENDCG
}
Pass
{
CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
#pragma exclude_renderers gles
#pragma vertex vert
#pragma fragment frag
fragmentInput vert( vertexInput i )
{
fragmentInput o = LevelFragmentShader( i, 1 );
return o;
}
ENDCG
}
}
FallBack "Diffuse"
}
但正如您所看到的结果,第二个纹理没有垂直投影到边缘。任何建议都会很棒,我确定我的数学是正确的 vertxPos + ( Normal * projectionDistance)。这可能与我使用统一模型视图投影矩阵的方式有关吗?
图片显示结果 http://i1265.photobucket.com/albums/jj508/maxfire1/Capture_zpsc8db2b1f.png