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!