SkeletonRenderer update.

This commit is contained in:
pharan 2016-03-24 00:37:52 +08:00
parent aea0c1a923
commit c0eb1a5237

View File

@ -51,30 +51,41 @@ public class SkeletonRenderer : MonoBehaviour {
public String initialSkinName; public String initialSkinName;
#region Advanced #region Advanced
#if SPINE_OPTIONAL_NORMALS // Submesh Separation
public bool calculateNormals, calculateTangents; [UnityEngine.Serialization.FormerlySerializedAs("submeshSeparators")]
#endif [SpineSlot]
public string[] separatorSlotNames = new string[0];
[System.NonSerialized]
public readonly List<Slot> separatorSlots = new List<Slot>();
public float zSpacing; public float zSpacing;
public bool renderMeshes = true, immutableTriangles; public bool renderMeshes = true, immutableTriangles;
public bool pmaVertexColors = true; public bool pmaVertexColors = true;
#if SPINE_OPTIONAL_NORMALS
public bool calculateNormals, calculateTangents;
#endif
#if SPINE_OPTIONAL_FRONTFACING #if SPINE_OPTIONAL_FRONTFACING
public bool frontFacing; public bool frontFacing;
#endif #endif
public bool logErrors = false;
#if SPINE_OPTIONAL_RENDEROVERRIDE #if SPINE_OPTIONAL_RENDEROVERRIDE
public bool disableMeshRendererOnOverride = true; public bool disableRenderingOnOverride = true;
public delegate void InstructionDelegate (SkeletonRenderer.SmartMesh.Instruction instruction); public delegate void InstructionDelegate (SkeletonRenderer.SmartMesh.Instruction instruction);
event InstructionDelegate MeshOverrideDelegate; event InstructionDelegate generateMeshOverride;
public event InstructionDelegate GenerateMeshOverride { public event InstructionDelegate GenerateMeshOverride {
add { add {
MeshOverrideDelegate += value; generateMeshOverride += value;
if (disableMeshRendererOnOverride && MeshOverrideDelegate != null) { if (disableRenderingOnOverride && generateMeshOverride != null) {
Initialize(false);
meshRenderer.enabled = false; meshRenderer.enabled = false;
} }
} }
remove { remove {
MeshOverrideDelegate -= value; generateMeshOverride -= value;
if (disableMeshRendererOnOverride && MeshOverrideDelegate == null) { if (disableRenderingOnOverride && generateMeshOverride == null) {
Initialize(false);
meshRenderer.enabled = true; meshRenderer.enabled = true;
} }
} }
@ -82,15 +93,6 @@ public class SkeletonRenderer : MonoBehaviour {
#endif #endif
public bool logErrors = false;
// Submesh Separation
[UnityEngine.Serialization.FormerlySerializedAs("submeshSeparators")]
[SpineSlot]
public string[] separatorSlotNames = new string[0];
[System.NonSerialized]
public List<Slot> separatorSlots = new List<Slot>();
// Custom Slot Material // Custom Slot Material
[System.NonSerialized] readonly Dictionary<Slot, Material> customSlotMaterials = new Dictionary<Slot, Material>(); [System.NonSerialized] readonly Dictionary<Slot, Material> customSlotMaterials = new Dictionary<Slot, Material>();
public Dictionary<Slot, Material> CustomSlotMaterials { get { return customSlotMaterials; } } public Dictionary<Slot, Material> CustomSlotMaterials { get { return customSlotMaterials; } }
@ -200,15 +202,19 @@ public class SkeletonRenderer : MonoBehaviour {
return; return;
if ( if (
(
!meshRenderer.enabled !meshRenderer.enabled
)
#if SPINE_OPTIONAL_RENDEROVERRIDE #if SPINE_OPTIONAL_RENDEROVERRIDE
&& MeshOverrideDelegate == null && this.generateMeshOverride == null
#endif #endif
) )
return; return;
// STEP 1. Determine a SmartMesh.Instruction. Split up instructions into submeshes. // STEP 1. Determine a SmartMesh.Instruction. Split up instructions into submeshes.
// This method caches several .Items arrays. // This method caches several .Items arrays.
@ -298,8 +304,8 @@ public class SkeletonRenderer : MonoBehaviour {
#endif #endif
// Create a new SubmeshInstruction when material changes. (or when forced to separate by a submeshSeparator) // Create a new SubmeshInstruction when material changes. (or when forced to separate by a submeshSeparator)
bool separatedBySlot = (separatorSlotCount > 0 && separatorSlots.Contains(slot)); bool forceSeparate = (separatorSlotCount > 0 && separatorSlots.Contains(slot));
if ((vertexCount > 0 && lastMaterial.GetInstanceID() != material.GetInstanceID()) || separatedBySlot) { if ((vertexCount > 0 && lastMaterial.GetInstanceID() != material.GetInstanceID()) || forceSeparate) {
workingSubmeshInstructions.Add( workingSubmeshInstructions.Add(
new Spine.Unity.MeshGeneration.SubmeshInstruction { new Spine.Unity.MeshGeneration.SubmeshInstruction {
skeleton = this.skeleton, skeleton = this.skeleton,
@ -309,7 +315,7 @@ public class SkeletonRenderer : MonoBehaviour {
triangleCount = submeshTriangleCount, triangleCount = submeshTriangleCount,
firstVertexIndex = submeshFirstVertex, firstVertexIndex = submeshFirstVertex,
vertexCount = submeshVertexCount, vertexCount = submeshVertexCount,
separatedBySlot = separatedBySlot forceSeparate = forceSeparate
} }
); );
@ -334,7 +340,7 @@ public class SkeletonRenderer : MonoBehaviour {
triangleCount = submeshTriangleCount, triangleCount = submeshTriangleCount,
firstVertexIndex = submeshFirstVertex, firstVertexIndex = submeshFirstVertex,
vertexCount = submeshVertexCount, vertexCount = submeshVertexCount,
separatedBySlot = false forceSeparate = false
} }
); );
@ -345,8 +351,12 @@ public class SkeletonRenderer : MonoBehaviour {
#endif #endif
#if SPINE_OPTIONAL_RENDEROVERRIDE #if SPINE_OPTIONAL_RENDEROVERRIDE
if (MeshOverrideDelegate != null) { if (this.generateMeshOverride != null) {
MeshOverrideDelegate(workingInstruction); this.generateMeshOverride(workingInstruction);
if (disableRenderingOnOverride) {
return;
}
} }
#endif #endif
@ -367,6 +377,14 @@ public class SkeletonRenderer : MonoBehaviour {
for (int i = 0; i < vertexCount; i++) for (int i = 0; i < vertexCount; i++)
localNormals[i] = normal; localNormals[i] = normal;
} }
// For dynamic tangent calculation, you can remove the tangent-filling logic and add tangent calculation logic below.
if (calculateTangents) {
Vector4[] localTangents = this.tangents = new Vector4[vertexCount];
Vector4 tangent = new Vector4(1, 0, 0, -1);
for (int i = 0; i < vertexCount; i++)
localTangents[i] = tangent;
}
#endif #endif
} else { } else {
Vector3 zero = Vector3.zero; Vector3 zero = Vector3.zero;
@ -591,23 +609,13 @@ public class SkeletonRenderer : MonoBehaviour {
var currentSmartMeshInstructionUsed = currentSmartMesh.instructionUsed; var currentSmartMeshInstructionUsed = currentSmartMesh.instructionUsed;
#if SPINE_OPTIONAL_NORMALS #if SPINE_OPTIONAL_NORMALS
if (currentSmartMeshInstructionUsed.vertexCount < vertexCount) { if (currentSmartMeshInstructionUsed.vertexCount < vertexCount) {
if (calculateNormals) { if (calculateNormals)
currentMesh.normals = normals; currentMesh.normals = normals;
}
// For dynamic calculated tangents, this needs to be moved out of the if block when replacing the logic.
if (calculateTangents) {
if (tangents == null || vertexCount > tangents.Length) {
Vector4[] localTangents = this.tangents = new Vector4[vertexCount];
Vector4 tangent = new Vector4(1, 0, 0, -1);
for (int i = 0; i < vertexCount; i++)
localTangents[i] = tangent;
}
// For dynamic calculated tangents, this needs to be moved out of the vertexCount check block when replacing the logic, also ensuring the size.
if (calculateTangents)
currentMesh.tangents = this.tangents; currentMesh.tangents = this.tangents;
} }
}
#endif #endif
// Check if the triangles should also be updated. // Check if the triangles should also be updated.