Calculate normals and tangets.

This commit is contained in:
NathanSweet 2013-08-04 11:04:22 +02:00
parent 42712a6587
commit 7318ec912a
6 changed files with 66 additions and 16 deletions

View File

@ -28,7 +28,8 @@ using UnityEngine;
[CustomEditor(typeof(SkeletonAnimation))]
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 () {
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
@ -37,6 +38,8 @@ public class SkeletonAnimationInspector : Editor {
useAnimationName = serializedObject.FindProperty("useAnimationName");
initialSkinName = serializedObject.FindProperty("initialSkinName");
timeScale = serializedObject.FindProperty("timeScale");
normals = serializedObject.FindProperty("calculateNormals");
tangents = serializedObject.FindProperty("calculateTangents");
}
override public void OnInspectorGUI () {
@ -104,6 +107,8 @@ public class SkeletonAnimationInspector : Editor {
EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(timeScale);
EditorGUILayout.PropertyField(normals);
EditorGUILayout.PropertyField(tangents);
if (serializedObject.ApplyModifiedProperties() ||
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")

View File

@ -28,12 +28,14 @@ using UnityEngine;
[CustomEditor(typeof(SkeletonComponent))]
public class SkeletonComponentInspector : Editor {
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale;
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale, normals, tangents;
void OnEnable () {
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
initialSkinName = serializedObject.FindProperty("initialSkinName");
timeScale = serializedObject.FindProperty("timeScale");
normals = serializedObject.FindProperty("calculateNormals");
tangents = serializedObject.FindProperty("calculateTangents");
}
override public void OnInspectorGUI () {
@ -65,6 +67,8 @@ public class SkeletonComponentInspector : Editor {
}
EditorGUILayout.PropertyField(timeScale);
EditorGUILayout.PropertyField(normals);
EditorGUILayout.PropertyField(tangents);
if (serializedObject.ApplyModifiedProperties() ||
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")

View File

@ -35,15 +35,18 @@ public class SkeletonComponent : MonoBehaviour {
public Skeleton skeleton;
public String initialSkinName;
public float timeScale = 1;
public bool calculateNormals;
public bool calculateTangents;
private Mesh mesh;
private float[] vertexPositions = new float[8];
private int lastVertexCount;
private Vector3[] vertices;
private Color32[] colors;
private Vector2[] uvs;
private float[] vertexPositions = new float[8];
private Material[] sharedMaterials = new Material[0];
private List<Material> submeshMaterials = new List<Material>();
private List<int[]> submeshIndexes = new List<int[]>();
private Material[] sharedMaterials = new Material[0];
private Vector4[] tangents = new Vector4[0];
public virtual void Clear () {
GetComponent<MeshFilter>().mesh = null;
@ -80,6 +83,7 @@ public class SkeletonComponent : MonoBehaviour {
public virtual void Update () {
SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(false);
// Clear fields if missing information to render.
if (skeletonDataAsset == null || skeletonData == null) {
Clear();
@ -180,9 +184,22 @@ public class SkeletonComponent : MonoBehaviour {
mesh.vertices = vertices;
mesh.colors32 = colors;
mesh.uv = uvs;
mesh.subMeshCount = submeshMaterials.Count;
for (int i = 0; i < mesh.subMeshCount; ++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. */

View File

@ -22,14 +22,14 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
using System;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(SkeletonAnimation))]
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 () {
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
@ -38,6 +38,8 @@ public class SkeletonAnimationInspector : Editor {
useAnimationName = serializedObject.FindProperty("useAnimationName");
initialSkinName = serializedObject.FindProperty("initialSkinName");
timeScale = serializedObject.FindProperty("timeScale");
normals = serializedObject.FindProperty("calculateNormals");
tangents = serializedObject.FindProperty("calculateTangents");
}
override public void OnInspectorGUI () {
@ -54,7 +56,8 @@ public class SkeletonAnimationInspector : Editor {
for (int i = 0; i < skins.Length - 1; i++) {
String name = component.skeleton.Data.Skins[i].Name;
skins[i] = name;
if (name == initialSkinName.stringValue) skinIndex = i;
if (name == initialSkinName.stringValue)
skinIndex = i;
}
EditorGUILayout.BeginHorizontal();
@ -74,7 +77,8 @@ public class SkeletonAnimationInspector : Editor {
for (int i = 0; i < animations.Length - 2; i++) {
String name = component.skeleton.Data.Animations[i].Name;
animations[i + 2] = name;
if (name == animationName.stringValue) animationIndex = i + 2;
if (name == animationName.stringValue)
animationIndex = i + 2;
}
EditorGUILayout.BeginHorizontal();
@ -103,6 +107,8 @@ public class SkeletonAnimationInspector : Editor {
EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(timeScale);
EditorGUILayout.PropertyField(normals);
EditorGUILayout.PropertyField(tangents);
if (serializedObject.ApplyModifiedProperties() ||
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")

View File

@ -22,19 +22,20 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
using System;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(SkeletonComponent))]
public class SkeletonComponentInspector : Editor {
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale;
private SerializedProperty skeletonDataAsset, initialSkinName, timeScale, normals, tangents;
void OnEnable () {
skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
initialSkinName = serializedObject.FindProperty("initialSkinName");
timeScale = serializedObject.FindProperty("timeScale");
normals = serializedObject.FindProperty("calculateNormals");
tangents = serializedObject.FindProperty("calculateTangents");
}
override public void OnInspectorGUI () {
@ -51,7 +52,8 @@ public class SkeletonComponentInspector : Editor {
for (int i = 0; i < skins.Length - 1; i++) {
String name = component.skeleton.Data.Skins[i].Name;
skins[i] = name;
if (name == initialSkinName.stringValue) skinIndex = i;
if (name == initialSkinName.stringValue)
skinIndex = i;
}
EditorGUILayout.BeginHorizontal();
@ -65,6 +67,8 @@ public class SkeletonComponentInspector : Editor {
}
EditorGUILayout.PropertyField(timeScale);
EditorGUILayout.PropertyField(normals);
EditorGUILayout.PropertyField(tangents);
if (serializedObject.ApplyModifiedProperties() ||
(Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")

View File

@ -35,15 +35,18 @@ public class SkeletonComponent : MonoBehaviour {
public Skeleton skeleton;
public String initialSkinName;
public float timeScale = 1;
public bool calculateNormals;
public bool calculateTangents;
private Mesh mesh;
private float[] vertexPositions = new float[8];
private int lastVertexCount;
private Vector3[] vertices;
private Color32[] colors;
private Vector2[] uvs;
private float[] vertexPositions = new float[8];
private Material[] sharedMaterials = new Material[0];
private List<Material> submeshMaterials = new List<Material>();
private List<int[]> submeshIndexes = new List<int[]>();
private Material[] sharedMaterials = new Material[0];
private Vector4[] tangents = new Vector4[0];
public virtual void Clear () {
GetComponent<MeshFilter>().mesh = null;
@ -59,9 +62,6 @@ public class SkeletonComponent : MonoBehaviour {
mesh.name = "Skeleton Mesh";
mesh.hideFlags = HideFlags.HideAndDontSave;
mesh.MarkDynamic();
// BOZO
//renderer.sharedMaterial = skeletonDataAsset.atlasAsset.material;
vertices = new Vector3[0];
@ -80,6 +80,7 @@ public class SkeletonComponent : MonoBehaviour {
public virtual void Update () {
SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(false);
// Clear fields if missing information to render.
if (skeletonDataAsset == null || skeletonData == null) {
Clear();
@ -180,9 +181,22 @@ public class SkeletonComponent : MonoBehaviour {
mesh.vertices = vertices;
mesh.colors32 = colors;
mesh.uv = uvs;
mesh.subMeshCount = submeshMaterials.Count;
for (int i = 0; i < mesh.subMeshCount; ++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. */