ReverseNormals

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<csharp>" to "<syntaxhighlight lang="csharp">")
Line 10: Line 10:
  
 
==CSharp- ReverseNormals.cs==
 
==CSharp- ReverseNormals.cs==
<csharp>
+
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;
 
using System.Collections;
 
using System.Collections;

Revision as of 20:44, 10 January 2012

Author: Joachim Ante

Description

This script lets you reverse normals. The normals will only be reversed in playmode so it will still look inverted when viewing in edit mode.

Usage

Attach the script to the mesh you want normals to be reversed

CSharp- ReverseNormals.cs

using UnityEngine;
using System.Collections;
 
[RequireComponent(typeof(MeshFilter))]
public class ReverseNormals : MonoBehaviour {
 
	void Start () {
		MeshFilter filter = GetComponent(typeof (MeshFilter)) as MeshFilter;
		if (filter != null)
		{
			Mesh mesh = filter.mesh;
 
			Vector3[] normals = mesh.normals;
			for (int i=0;i<normals.Length;i++)
				normals[i] = -normals[i];
			mesh.normals = normals;
 
			for (int m=0;m<mesh.subMeshCount;m++)
			{
				int[] triangles = mesh.GetTriangles(m);
				for (int i=0;i<triangles.Length;i+=3)
				{
					int temp = triangles[i + 0];
					triangles[i + 0] = triangles[i + 1];
					triangles[i + 1] = temp;
				}
				mesh.SetTriangles(triangles, m);
			}
		}		
	}
}
</csharp>
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox