mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
Calculate normals and tangets.
This commit is contained in:
parent
42712a6587
commit
7318ec912a
@ -28,7 +28,8 @@ using UnityEngine;
|
|||||||
|
|
||||||
[CustomEditor(typeof(SkeletonAnimation))]
|
[CustomEditor(typeof(SkeletonAnimation))]
|
||||||
public class SkeletonAnimationInspector : Editor {
|
public class SkeletonAnimationInspector : Editor {
|
||||||
private SerializedProperty skeletonDataAsset, animationName, loop, useAnimationName, initialSkinName, timeScale;
|
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale, normals, tangents;
|
||||||
|
private SerializedProperty animationName, loop, useAnimationName;
|
||||||
|
|
||||||
void OnEnable () {
|
void OnEnable () {
|
||||||
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
|
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
|
||||||
@ -37,6 +38,8 @@ public class SkeletonAnimationInspector : Editor {
|
|||||||
useAnimationName = serializedObject.FindProperty("useAnimationName");
|
useAnimationName = serializedObject.FindProperty("useAnimationName");
|
||||||
initialSkinName = serializedObject.FindProperty("initialSkinName");
|
initialSkinName = serializedObject.FindProperty("initialSkinName");
|
||||||
timeScale = serializedObject.FindProperty("timeScale");
|
timeScale = serializedObject.FindProperty("timeScale");
|
||||||
|
normals = serializedObject.FindProperty("calculateNormals");
|
||||||
|
tangents = serializedObject.FindProperty("calculateTangents");
|
||||||
}
|
}
|
||||||
|
|
||||||
override public void OnInspectorGUI () {
|
override public void OnInspectorGUI () {
|
||||||
@ -104,6 +107,8 @@ public class SkeletonAnimationInspector : Editor {
|
|||||||
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.EndHorizontal();
|
||||||
|
|
||||||
EditorGUILayout.PropertyField(timeScale);
|
EditorGUILayout.PropertyField(timeScale);
|
||||||
|
EditorGUILayout.PropertyField(normals);
|
||||||
|
EditorGUILayout.PropertyField(tangents);
|
||||||
|
|
||||||
if (serializedObject.ApplyModifiedProperties() ||
|
if (serializedObject.ApplyModifiedProperties() ||
|
||||||
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
|
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
|
||||||
|
|||||||
@ -28,12 +28,14 @@ using UnityEngine;
|
|||||||
|
|
||||||
[CustomEditor(typeof(SkeletonComponent))]
|
[CustomEditor(typeof(SkeletonComponent))]
|
||||||
public class SkeletonComponentInspector : Editor {
|
public class SkeletonComponentInspector : Editor {
|
||||||
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale;
|
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale, normals, tangents;
|
||||||
|
|
||||||
void OnEnable () {
|
void OnEnable () {
|
||||||
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
|
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
|
||||||
initialSkinName = serializedObject.FindProperty("initialSkinName");
|
initialSkinName = serializedObject.FindProperty("initialSkinName");
|
||||||
timeScale = serializedObject.FindProperty("timeScale");
|
timeScale = serializedObject.FindProperty("timeScale");
|
||||||
|
normals = serializedObject.FindProperty("calculateNormals");
|
||||||
|
tangents = serializedObject.FindProperty("calculateTangents");
|
||||||
}
|
}
|
||||||
|
|
||||||
override public void OnInspectorGUI () {
|
override public void OnInspectorGUI () {
|
||||||
@ -65,6 +67,8 @@ public class SkeletonComponentInspector : Editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
EditorGUILayout.PropertyField(timeScale);
|
EditorGUILayout.PropertyField(timeScale);
|
||||||
|
EditorGUILayout.PropertyField(normals);
|
||||||
|
EditorGUILayout.PropertyField(tangents);
|
||||||
|
|
||||||
if (serializedObject.ApplyModifiedProperties() ||
|
if (serializedObject.ApplyModifiedProperties() ||
|
||||||
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
|
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
|
||||||
|
|||||||
@ -35,15 +35,18 @@ public class SkeletonComponent : MonoBehaviour {
|
|||||||
public Skeleton skeleton;
|
public Skeleton skeleton;
|
||||||
public String initialSkinName;
|
public String initialSkinName;
|
||||||
public float timeScale = 1;
|
public float timeScale = 1;
|
||||||
|
public bool calculateNormals;
|
||||||
|
public bool calculateTangents;
|
||||||
private Mesh mesh;
|
private Mesh mesh;
|
||||||
|
private float[] vertexPositions = new float[8];
|
||||||
private int lastVertexCount;
|
private int lastVertexCount;
|
||||||
private Vector3[] vertices;
|
private Vector3[] vertices;
|
||||||
private Color32[] colors;
|
private Color32[] colors;
|
||||||
private Vector2[] uvs;
|
private Vector2[] uvs;
|
||||||
private float[] vertexPositions = new float[8];
|
private Material[] sharedMaterials = new Material[0];
|
||||||
private List<Material> submeshMaterials = new List<Material>();
|
private List<Material> submeshMaterials = new List<Material>();
|
||||||
private List<int[]> submeshIndexes = new List<int[]>();
|
private List<int[]> submeshIndexes = new List<int[]>();
|
||||||
private Material[] sharedMaterials = new Material[0];
|
private Vector4[] tangents = new Vector4[0];
|
||||||
|
|
||||||
public virtual void Clear () {
|
public virtual void Clear () {
|
||||||
GetComponent<MeshFilter>().mesh = null;
|
GetComponent<MeshFilter>().mesh = null;
|
||||||
@ -80,6 +83,7 @@ public class SkeletonComponent : MonoBehaviour {
|
|||||||
|
|
||||||
public virtual void Update () {
|
public virtual void Update () {
|
||||||
SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(false);
|
SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(false);
|
||||||
|
|
||||||
// Clear fields if missing information to render.
|
// Clear fields if missing information to render.
|
||||||
if (skeletonDataAsset == null || skeletonData == null) {
|
if (skeletonDataAsset == null || skeletonData == null) {
|
||||||
Clear();
|
Clear();
|
||||||
@ -180,9 +184,22 @@ public class SkeletonComponent : MonoBehaviour {
|
|||||||
mesh.vertices = vertices;
|
mesh.vertices = vertices;
|
||||||
mesh.colors32 = colors;
|
mesh.colors32 = colors;
|
||||||
mesh.uv = uvs;
|
mesh.uv = uvs;
|
||||||
|
|
||||||
mesh.subMeshCount = submeshMaterials.Count;
|
mesh.subMeshCount = submeshMaterials.Count;
|
||||||
for (int i = 0; i < mesh.subMeshCount; ++i)
|
for (int i = 0; i < mesh.subMeshCount; ++i)
|
||||||
mesh.SetTriangles(submeshIndexes[i], i);
|
mesh.SetTriangles(submeshIndexes[i], i);
|
||||||
|
|
||||||
|
if (calculateNormals) {
|
||||||
|
mesh.RecalculateNormals();
|
||||||
|
if (calculateTangents) {
|
||||||
|
Vector4[] tangents = this.tangents;
|
||||||
|
int count = mesh.normals.Length;
|
||||||
|
if (tangents.Length != count) this.tangents = tangents = new Vector4[count];
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
tangents[i] = new Vector4(1, 0, 0, 1);
|
||||||
|
mesh.tangents = tangents;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Adds a material. Adds submesh indexes if existing indexes aren't sufficient. */
|
/** Adds a material. Adds submesh indexes if existing indexes aren't sufficient. */
|
||||||
|
|||||||
@ -22,14 +22,14 @@
|
|||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
[CustomEditor(typeof(SkeletonAnimation))]
|
[CustomEditor(typeof(SkeletonAnimation))]
|
||||||
public class SkeletonAnimationInspector : Editor {
|
public class SkeletonAnimationInspector : Editor {
|
||||||
private SerializedProperty skeletonDataAsset, animationName, loop, useAnimationName, initialSkinName, timeScale;
|
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale, normals, tangents;
|
||||||
|
private SerializedProperty animationName, loop, useAnimationName;
|
||||||
|
|
||||||
void OnEnable () {
|
void OnEnable () {
|
||||||
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
|
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
|
||||||
@ -38,6 +38,8 @@ public class SkeletonAnimationInspector : Editor {
|
|||||||
useAnimationName = serializedObject.FindProperty("useAnimationName");
|
useAnimationName = serializedObject.FindProperty("useAnimationName");
|
||||||
initialSkinName = serializedObject.FindProperty("initialSkinName");
|
initialSkinName = serializedObject.FindProperty("initialSkinName");
|
||||||
timeScale = serializedObject.FindProperty("timeScale");
|
timeScale = serializedObject.FindProperty("timeScale");
|
||||||
|
normals = serializedObject.FindProperty("calculateNormals");
|
||||||
|
tangents = serializedObject.FindProperty("calculateTangents");
|
||||||
}
|
}
|
||||||
|
|
||||||
override public void OnInspectorGUI () {
|
override public void OnInspectorGUI () {
|
||||||
@ -54,7 +56,8 @@ public class SkeletonAnimationInspector : Editor {
|
|||||||
for (int i = 0; i < skins.Length - 1; i++) {
|
for (int i = 0; i < skins.Length - 1; i++) {
|
||||||
String name = component.skeleton.Data.Skins[i].Name;
|
String name = component.skeleton.Data.Skins[i].Name;
|
||||||
skins[i] = name;
|
skins[i] = name;
|
||||||
if (name == initialSkinName.stringValue) skinIndex = i;
|
if (name == initialSkinName.stringValue)
|
||||||
|
skinIndex = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
@ -74,7 +77,8 @@ public class SkeletonAnimationInspector : Editor {
|
|||||||
for (int i = 0; i < animations.Length - 2; i++) {
|
for (int i = 0; i < animations.Length - 2; i++) {
|
||||||
String name = component.skeleton.Data.Animations[i].Name;
|
String name = component.skeleton.Data.Animations[i].Name;
|
||||||
animations[i + 2] = name;
|
animations[i + 2] = name;
|
||||||
if (name == animationName.stringValue) animationIndex = i + 2;
|
if (name == animationName.stringValue)
|
||||||
|
animationIndex = i + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
@ -103,6 +107,8 @@ public class SkeletonAnimationInspector : Editor {
|
|||||||
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.EndHorizontal();
|
||||||
|
|
||||||
EditorGUILayout.PropertyField(timeScale);
|
EditorGUILayout.PropertyField(timeScale);
|
||||||
|
EditorGUILayout.PropertyField(normals);
|
||||||
|
EditorGUILayout.PropertyField(tangents);
|
||||||
|
|
||||||
if (serializedObject.ApplyModifiedProperties() ||
|
if (serializedObject.ApplyModifiedProperties() ||
|
||||||
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
|
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
|
||||||
|
|||||||
@ -22,19 +22,20 @@
|
|||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
[CustomEditor(typeof(SkeletonComponent))]
|
[CustomEditor(typeof(SkeletonComponent))]
|
||||||
public class SkeletonComponentInspector : Editor {
|
public class SkeletonComponentInspector : Editor {
|
||||||
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale;
|
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale, normals, tangents;
|
||||||
|
|
||||||
void OnEnable () {
|
void OnEnable () {
|
||||||
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
|
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
|
||||||
initialSkinName = serializedObject.FindProperty("initialSkinName");
|
initialSkinName = serializedObject.FindProperty("initialSkinName");
|
||||||
timeScale = serializedObject.FindProperty("timeScale");
|
timeScale = serializedObject.FindProperty("timeScale");
|
||||||
|
normals = serializedObject.FindProperty("calculateNormals");
|
||||||
|
tangents = serializedObject.FindProperty("calculateTangents");
|
||||||
}
|
}
|
||||||
|
|
||||||
override public void OnInspectorGUI () {
|
override public void OnInspectorGUI () {
|
||||||
@ -51,7 +52,8 @@ public class SkeletonComponentInspector : Editor {
|
|||||||
for (int i = 0; i < skins.Length - 1; i++) {
|
for (int i = 0; i < skins.Length - 1; i++) {
|
||||||
String name = component.skeleton.Data.Skins[i].Name;
|
String name = component.skeleton.Data.Skins[i].Name;
|
||||||
skins[i] = name;
|
skins[i] = name;
|
||||||
if (name == initialSkinName.stringValue) skinIndex = i;
|
if (name == initialSkinName.stringValue)
|
||||||
|
skinIndex = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
@ -65,6 +67,8 @@ public class SkeletonComponentInspector : Editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
EditorGUILayout.PropertyField(timeScale);
|
EditorGUILayout.PropertyField(timeScale);
|
||||||
|
EditorGUILayout.PropertyField(normals);
|
||||||
|
EditorGUILayout.PropertyField(tangents);
|
||||||
|
|
||||||
if (serializedObject.ApplyModifiedProperties() ||
|
if (serializedObject.ApplyModifiedProperties() ||
|
||||||
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
|
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
|
||||||
|
|||||||
@ -35,15 +35,18 @@ public class SkeletonComponent : MonoBehaviour {
|
|||||||
public Skeleton skeleton;
|
public Skeleton skeleton;
|
||||||
public String initialSkinName;
|
public String initialSkinName;
|
||||||
public float timeScale = 1;
|
public float timeScale = 1;
|
||||||
|
public bool calculateNormals;
|
||||||
|
public bool calculateTangents;
|
||||||
private Mesh mesh;
|
private Mesh mesh;
|
||||||
|
private float[] vertexPositions = new float[8];
|
||||||
private int lastVertexCount;
|
private int lastVertexCount;
|
||||||
private Vector3[] vertices;
|
private Vector3[] vertices;
|
||||||
private Color32[] colors;
|
private Color32[] colors;
|
||||||
private Vector2[] uvs;
|
private Vector2[] uvs;
|
||||||
private float[] vertexPositions = new float[8];
|
private Material[] sharedMaterials = new Material[0];
|
||||||
private List<Material> submeshMaterials = new List<Material>();
|
private List<Material> submeshMaterials = new List<Material>();
|
||||||
private List<int[]> submeshIndexes = new List<int[]>();
|
private List<int[]> submeshIndexes = new List<int[]>();
|
||||||
private Material[] sharedMaterials = new Material[0];
|
private Vector4[] tangents = new Vector4[0];
|
||||||
|
|
||||||
public virtual void Clear () {
|
public virtual void Clear () {
|
||||||
GetComponent<MeshFilter>().mesh = null;
|
GetComponent<MeshFilter>().mesh = null;
|
||||||
@ -59,9 +62,6 @@ public class SkeletonComponent : MonoBehaviour {
|
|||||||
mesh.name = "Skeleton Mesh";
|
mesh.name = "Skeleton Mesh";
|
||||||
mesh.hideFlags = HideFlags.HideAndDontSave;
|
mesh.hideFlags = HideFlags.HideAndDontSave;
|
||||||
mesh.MarkDynamic();
|
mesh.MarkDynamic();
|
||||||
|
|
||||||
// BOZO
|
|
||||||
//renderer.sharedMaterial = skeletonDataAsset.atlasAsset.material;
|
|
||||||
|
|
||||||
vertices = new Vector3[0];
|
vertices = new Vector3[0];
|
||||||
|
|
||||||
@ -80,6 +80,7 @@ public class SkeletonComponent : MonoBehaviour {
|
|||||||
|
|
||||||
public virtual void Update () {
|
public virtual void Update () {
|
||||||
SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(false);
|
SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(false);
|
||||||
|
|
||||||
// Clear fields if missing information to render.
|
// Clear fields if missing information to render.
|
||||||
if (skeletonDataAsset == null || skeletonData == null) {
|
if (skeletonDataAsset == null || skeletonData == null) {
|
||||||
Clear();
|
Clear();
|
||||||
@ -180,9 +181,22 @@ public class SkeletonComponent : MonoBehaviour {
|
|||||||
mesh.vertices = vertices;
|
mesh.vertices = vertices;
|
||||||
mesh.colors32 = colors;
|
mesh.colors32 = colors;
|
||||||
mesh.uv = uvs;
|
mesh.uv = uvs;
|
||||||
|
|
||||||
mesh.subMeshCount = submeshMaterials.Count;
|
mesh.subMeshCount = submeshMaterials.Count;
|
||||||
for (int i = 0; i < mesh.subMeshCount; ++i)
|
for (int i = 0; i < mesh.subMeshCount; ++i)
|
||||||
mesh.SetTriangles(submeshIndexes[i], i);
|
mesh.SetTriangles(submeshIndexes[i], i);
|
||||||
|
|
||||||
|
if (calculateNormals) {
|
||||||
|
mesh.RecalculateNormals();
|
||||||
|
if (calculateTangents) {
|
||||||
|
Vector4[] tangents = this.tangents;
|
||||||
|
int count = mesh.normals.Length;
|
||||||
|
if (tangents.Length != count) this.tangents = tangents = new Vector4[count];
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
tangents[i] = new Vector4(1, 0, 0, 1);
|
||||||
|
mesh.tangents = tangents;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Adds a material. Adds submesh indexes if existing indexes aren't sufficient. */
|
/** Adds a material. Adds submesh indexes if existing indexes aren't sufficient. */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user