• Runtimes
  • Unity + Sprite Lamp - BumpMapped Spine texture regions

Hello, this is an explanation of how I integrated Bump mapping for texture regions in the Spine Unity Runtime, to take profit from the normal maps generated with Sprite Lamp. This may be not the best way to do it, it is only how I done it, of course there may be better implementations. If you don't know what I'm talking about, you can see this video: http://www.youtube.com/watch?v=PyfMK6QSaVA or read the thread about this topic: http://www.esotericsoftware.com/forum/v ... f=5&t=2017

What I have done is implementing a shader material that receives the necessary information to transform the normals from the texture map and use it to shade the pixel, the material can be downloaded here:

http://pastebin.com/FPxm5GYt#

Feel free to improve or modfy it to meet your needs. The main issue while trying to achieve this feature was to pass the rotation in texture region space to each vertex of the mesh generated by the runtime, to do so, I modified the runtime creating a new array for an unused vertex property in the Mesh object, the tangents array. All those modifications are done in the SkeletalComponent.cs file from the official Unity Spine runtime.

First, I declared an array of float4 to save the extra information like this:

//line 55:
private Vector4[] vertexExtraInfo;

This will be another vertex attribute, just like the colors and UVs. Then I initialized this array like the other ones before doing the mesh setup.

//line 172
this.vertexExtraInfo = new Vector4[vertexCount];

//line 188
Vector4[] vertexExtraInfo = this.vertexExtraInfo;

Inside the "for" loop that transforms each vertex, I fill this extra info array with the vertex rotation and scale.

//line 193
for (int i = 0, n = drawOrder.Count; i < n; i++) {
{
	//[...]
	uvs[vertexIndex + 3] = new Vector2(regionUVs[RegionAttachment.X3], 1 - regionUVs[RegionAttachment.Y3]);

//line 222
//vertex rotation and scale
float angle = (slot.Bone.WorldRotation + regionAttachment.Rotation) * (float)Math.PI / 180.0f;
float scale = slot.Bone.WorldScaleX;
	vertexExtraInfo[vertexIndex] = new Vector4(angle, scale, 0, 0);
vertexExtraInfo[vertexIndex + 1] = new Vector4(angle, scale, 0, 0);
vertexExtraInfo[vertexIndex + 2] = new Vector4(angle, scale, 0, 0);
vertexExtraInfo[vertexIndex + 3] = new Vector4(angle, scale, 0, 0);

vertexIndex += 4;
}

Finally pass this information as the tangents array to the Mesh object, of course this is not a real tangent.

//just after the previous loop
mesh.vertices = vertices;
mesh.colors32 = colors;
mesh.uv = uvs;
//add this
mesh.tangents = vertexExtraInfo;

And that's the trick, assigning the shader to the material and doing this little modifications to the runtime you can achieve the bump effect. I'm passing only one scale supposing unified scaling scaleX == scaleY, but you can figure out how to pass scales for X and Y.

Enjoy!

Related Discussions
...
11 dana kasnije

This is really neat! 🙂 I'm jealous, I hope to play with this stuff at some point!

It's too bad you have to stuff it into the tangents. You might be able to use the second set of texture coordinates. Some info here.

Gallo, this is quite impressive. I'm an artist so I was looking for any information on this. I'm happy I found your post. Thank you for sharing. 🙂

4 mjeseci kasnije

Thanks for writing the shader Gallo.

I'm trying to use sprite lamp exactly like you are in your videos, but instead of using the character you created, I am using the goblin from the demo by adding a normal map to his material. I took the goblin sprite atlas, ran it through some filters until I got a rough normal map. The problem is that the goblin looks the exact same with or without my normal map. Is there something I'm missing for it to work? 🙁

10 dana kasnije

The warning that says "Shader wants tangents..." it's very suspicious, maybe the tangent data is not reaching the shader for some reason.

You need to check "Calculate Tangents" on the SkeletonAnimation inspector.

Hi Gallo, very nice work! I'm part of a team who are keen to use normal mapping with Spine, so we made the changes you recommended to the Spine Unity runtime and used your shader on our character. However we are having issues with Z-fighting on the sprites. Any idea what's causing it, or how to resolve it?

Cheers,
Ronan

3 mjeseci kasnije

Here is my attempt at a Sprite Lamp Spine shader for anyone interested

https://drive.google.com/a/foureyedcat. ... sp=sharing

The code is still a mess and the performance can still be improved. But it works.

In the latest runtime you need to add changes to SkeletonRenderer.cs instead of SkeletonComponent.cs

To prevent z-fighting you need to set the variable ZSpacing in the inspector of Skeleton Animation script to -0.001. The inspector needs to be in debug mode, otherwise the variable is hidden. ZSpacing needs to be smaller than 0 because the first pass in the shader is written to the z-buffer.

Thanks for sharing! 🙂