Hello Guest

Author Topic: Custom Shaders  (Read 5066 times)

Pulluxx

  • Newbie
  • *
  • Posts: 9
    • View Profile
Custom Shaders
« on: May 15, 2013, 01:08:30 am »
Hey there  :)

In our game we have a custom shader we want to use on our animated sprites. What we want to have is a detail texture which are multiplied with the animated sprites texture to simulate that the creature that is the animated sprite is sick. What happens when we use it, is that the detail texture changes position for every frame and the dots which we have as detail appear in a lot of different places. We think this problem could be solved if we could get a new UV2 map for each frame in the animation and then use it in the shader. Is there a way to get this?

This is how the shader looks:

Code: [Select]
Shader "Custom/DiffuseDetailCuttoff"
{
Properties
{
_DiseaseColor ("Disease Color", Color) = (1,1,1,1)
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_DetailTex ("Detail Texture", 2D) = "white" {}
_DetailFade ("Defail Fade", Range(0, 1)) = 1.0
_DetailTiling ("Detail Tiling", Float) = 1.0


}

SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
LOD 110

Pass
{
CGPROGRAM
#pragma vertex vert_vct
#pragma fragment frag_mult
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

sampler2D _MainTex;
half4 _MainTex_ST;
sampler2D _DetailTex;
fixed4 _DiseaseColor;
fixed4 _Color;
half _DetailTiling;
fixed _DetailFade;

struct vin_vct
{
half4 vertex : POSITION;
half4 color : COLOR;
fixed2 texcoord : TEXCOORD0;
};

struct v2f_vct
{
half4 vertex : POSITION;
fixed4 color : COLOR;
fixed2 texcoord : TEXCOORD0;

};

v2f_vct vert_vct(vin_vct v)
{
v2f_vct o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.texcoord = v.texcoord;
return o;
}

fixed4 frag_mult(v2f_vct i) : COLOR
{
fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
fixed4 det = tex2D(_DetailTex, i.texcoord * _DetailTiling);

fixed3 minusDet = 1 - det;
col *= _Color;
col.rgb += lerp(det.rgb * _DiseaseColor.rgb, (0,0,0), _DetailFade);
return col;
}
ENDCG
}
}
 
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off Fog { Mode Off }
LOD 100

BindChannels
{
Bind "Vertex", vertex
Bind "TexCoord", texcoord
Bind "Color", color
}

Pass
{
Lighting Off
SetTexture [_MainTex] { combine texture * primary }
}
}
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Custom Shaders
« Reply #1 on: May 15, 2013, 10:31:53 am »
Easiest way to do this is to create a new derived tk2dSprite class, and add the UV2 data you need. You would probably want to scale and position it relative to the anchor in some way so it "stays" in the same location. Alternatively, you can add tangents & normals to the class (its already available as an option), and use that to derive UVs but it'll be a bit more tricky that way.

Pulluxx

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Custom Shaders
« Reply #2 on: May 21, 2013, 05:01:43 pm »
I'm unsure how to do this. I need to have a Mesh.uv attached to my GameObject that the shader reads from, and then I need to update that Mesh.uv with data from each frame right? And the Mesh.uv needs to take in anchor and scale and position into this. So I need to make a calculation for each frame to adjust the scale and position to the anchor? What variables do I need in the tk2dAnimatedSprite class?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Custom Shaders
« Reply #3 on: May 21, 2013, 07:20:08 pm »
This is easier in 2D Toolkit 2.0, as the animator there is separate from the sprite class, i.e. you can create your own sprite class and animate it independently. You can import the sprite animator alone from tk2d 2.0 beta and use that, if you want to.

Once you've duplicated the tk2dSprite class - you want to calculate and set mesh.uv2 wherever mesh.uv is set.
uv2 should contain the uniform UVs relative to the base class which you're going to use to position the detail texture. I suggest starting with just a planar mapping, and then working from there.