Unity CG Custom Sader for Texture Layering using Passes

Started by
0 comments, last by maxfire 10 years, 5 months ago

This is what I’m trying to accomplish

  1. Texture geometry with primary shader
  2. Layer a second texture over the first one by a projected amount following the normal.

Here is my current shader code:


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"

} 

But as you can see the result second texture is not projecting perpendicularly edge to edge. Any suggestions would be great, im sure my maths is correct vertxPos + ( Normal * projectionDistance). Could it be something to do with how im using unitys ModelViewProjection Matrix?

Image showing result http://i1265.photobucket.com/albums/jj508/maxfire1/Capture_zpsc8db2b1f.png

Thanks in advanced

Advertisement

Ok solved this problem, it was the unity cubes Normal deforming under a specific rotation. When I created a flat plane it worked perfectly

This topic is closed to new replies.

Advertisement