Hello Guest

Author Topic: 2D Sprites with Normals and shadows  (Read 18697 times)

pyrobon

  • Newbie
  • *
  • Posts: 3
    • View Profile
2D Sprites with Normals and shadows
« on: December 05, 2013, 11:31:54 pm »
Hello!

I've been struggling with Unity 4.3 sprites. The default shaders can't take normals and after a few days of struggle, a friend shared a shader he wrote to make them compatible (Transparent-cutout-Bumped Diffuse wont work with Sprites gameobjects).
Happy days, my 2D Sprites can now take normals.  The thing is they won't cast shadows no matter what shader they use.
I'm thinking on acquiring 2D toolkit to see If I can have both things, but It's more prudent to ask first.

Does 2D Toolkit sprites have shader that takes normal maps and can it cast shadows from point lights (using deferred lighting, of course)? 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #1 on: December 06, 2013, 10:48:00 am »
No it doesn't, but this will work. Remember that the shadow is cutoff only, so it will almost certainly have a jagged edge to it.
Code: [Select]
Shader "Transparent/Bumped Diffuse with Shadow" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Cutoff("Cutoff", Float) = 0.01
}

SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
LOD 300
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf Lambert addshadow alphatest:_Cutoff

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;

struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};

void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}

Fallback "Transparent/Diffuse"
}

pyrobon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #2 on: December 07, 2013, 10:49:44 am »
Thank you!
I went for it, tried your shader. The normal map works but the 2D toolkit sprite doesn't cast any shadows. Any further suggestions?
« Last Edit: December 07, 2013, 11:46:51 am by pyrobon »

pyrobon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #3 on: December 07, 2013, 11:59:50 am »
Never mind, got it working.
Made some modifications comparing it to the Transparent/CutOut/Bumped Diffuse shader. The final code below. Thanks!

Code: [Select]
Shader "Transparent/Bumped Diffuse with Shadow" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Cutoff("Cutoff", Float) = 0.01
}

SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutOut" }
LOD 300

CGPROGRAM
#pragma surface surf Lambert addshadow alphatest:_Cutoff

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;

struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};

void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}

Fallback "Transparent/Diffuse"
}

couger

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #4 on: January 29, 2014, 11:40:11 am »
Hi

Does this shader work with deferred lighting? I've tried it and I just get a black sprite. I'm also trying to get a sprite to work the same as a quad with a texture and a transparent cutout bumped with shadow.

Is there a shader for 2dtk that does the same thing?


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #5 on: January 29, 2014, 12:05:15 pm »
Have you tried this with a normal camera instead of tk2dCamera? There is a known Unity bug in 4.3 where it doesn't work correctly with tk2dCamera, or indeed any camera that uses a custom projection matrix.

couger

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #6 on: January 29, 2014, 12:18:37 pm »
I am using the normal camera, works great on a quad just not a unity sprite or a 2dtk sprite

I have got it almost working on a 2dtk sprite using this shader

Code: [Select]
Shader "Sprites/Bumped Diffuse with Shadows"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Cutoff ("Alpha Cutoff", Range (0,1)) = 0.5

}

SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="TransparentCutOut"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"

}
LOD 300


Cull Off
Lighting On
ZWrite Off
Fog { Mode Off }


CGPROGRAM
#pragma surface surf Lambert alpha vertex:vert addshadow alphatest:_Cutoff
#pragma multi_compile DUMMY PIXELSNAP_ON

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;

struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
fixed4 color;
};

void vert (inout appdata_full v, out Input o)
{
#if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
v.vertex = UnityPixelSnap (v.vertex);
#endif
v.normal = float3(0,0,-1);
v.tangent =  float4(1, 0, 0, 1);

UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = _Color;
}

void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
o.Albedo = c.rgb;
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}

Fallback "Transparent/Cutout/Diffuse"
}

But the normal map map doesn't seem quite correct when compared to the quad. Not a shader guy so not sure what to change. Also when using the above with deferred lighting the sprite appears almost unlit

Not sure how to post a pic but Ive attached 2 images, the skeleton on the right is the quad in both, the deferred on is how it looks with deferred lighing


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #7 on: January 29, 2014, 12:52:49 pm »
First thing to check - can you get this to work with a solid shader?

couger

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #8 on: January 29, 2014, 01:00:08 pm »
Not sure what you mean, not really a shader guy, do you mean change the shader to solid, if so not sure how, sorry

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #9 on: January 29, 2014, 01:06:52 pm »
No I mean switching to one of the built in unity shaders, Diffuse Bump or one of those.

couger

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #10 on: January 29, 2014, 02:00:48 pm »
No the unity bump diffuse doesn't work, fills the sprite with the texture, both unity and 2dtk sprites

There must be a way to make this work, the shader I posted above is almost working except for the normal map not quite being applied properly. I don't have an understanding of shaders so not sure what needs changing

Given that unity sprites don't support normals it might be a good feature for 2dtk to support them natively, I know you had mentioned this before as not a priority but seems to be quite a few people trying to do this now



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Sprites with Normals and shadows
« Reply #11 on: January 30, 2014, 10:41:17 am »
I know it will get filled, should've been a bit more specific - does it light correctly and do normal maps look correct?

The feature we were talking about is synchronised atlas generation. i.e. you have a normal map for each of your sprites and tk2d will generate 2 atlases which can be used together.