Jump to content
  • Advertisement
Sign in to follow this  
maxfire

OpenGL Unity CG Custom Sader for Texture Layering using Passes

This topic is 2094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!