Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Unity CG Custom Sader for Texture Layering using Passes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 MaxFire   Members   -  Reputation: 187

Like
0Likes
Like

Posted 28 October 2013 - 04:01 AM

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



Sponsor:

#2 MaxFire   Members   -  Reputation: 187

Like
0Likes
Like

Posted 28 October 2013 - 06:25 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS