You copied the Doc URL to your clipboard.

The WorldSpaceNormalsCreators C# script

This is the code of the WorldSpaceNormalsCreators C# script:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class WorldSpaceNormalsCreator : ScriptableWizard
{
	public GameObject _currentObj;

	private Camera _renderCamera;

	void OnWizardUpdate()
	{
		helpString = "Select object from which generate the world space normals";
		if(_currentObj != null)
		{
			isValid = true;
		}
		else
		{
			isValid = false;
		}
	}
	
	void OnWizardCreate ()
	{
		// Set antialiasing
		QualitySettings.antiAliasing = 4;
		Shader wns = Shader.Find ("Custom/WorldSpaceNormalCreator");
		GameObject go = new GameObject( "WorldSpaceNormalsCam", typeof(Camera) );
		
		//Set the new camera to perform orthographic projection
		_renderCamera = go.GetComponent<Camera> ();
		_renderCamera.orthographic = true;
		_renderCamera.nearClipPlane = 0.0f;
		_renderCamera.farClipPlane = 10f;
		_renderCamera.orthographicSize = 1.0f;

		//Save the current object layer and set it to a unused one
		int prevObjLayer = _currentObj.layer;
		_currentObj.layer = 30; //0x40000000

		//Set the replacement shader for the camera
		_renderCamera.SetReplacementShader (wns,null);
		_renderCamera.useOcclusionCulling = false;

		//Rotate the camera to look at the object to avoid frustum culling
		_renderCamera.transform.rotation = Quaternion.LookRotation 
		  (_currentObj.transform.position - _renderCamera.transform.position);

		MeshRenderer mr = _currentObj.GetComponent<MeshRenderer> ();
		Material[] materials = mr.sharedMaterials;

		foreach (Material m in materials) 
		{
			Texture t = m.GetTexture("_BumpMap");
			if( t == null )
			{
				Debug.LogError("the material has no texture assigned named Bump Map");
				continue;
			}
		
			//Render the world space normal maps to a texture
			Shader.SetGlobalTexture ("_BumpMapGlobal", t);
			RenderTexture rt = new RenderTexture(t.width,t.height,1);
			_renderCamera.targetTexture = rt;
			_renderCamera.pixelRect = new Rect(0,0,t.width,t.height);
			_renderCamera.backgroundColor = new Color( 0.5f, 0.5f, 0.5f);
			_renderCamera.clearFlags = CameraClearFlags.Color;
			_renderCamera.cullingMask = 0x40000000;
			_renderCamera.Render();
			Shader.SetGlobalTexture ("_BumpMapGlobal", null);

			Texture2D outTex = new Texture2D(t.width,t.height);
			RenderTexture.active = rt;
			outTex.ReadPixels(new Rect(0,0,t.width,t.height), 0, 0);
			outTex.Apply();
			RenderTexture.active = null;

			//Save it to PNG
			byte[] _pixels = outTex.EncodeToPNG();
			System.IO.File.WriteAllBytes("Assets/Textures/GeneratedWorldSpaceNormals/"
			+t.name+"_WorldSpace.png",_pixels);
		}
		_currentObj.layer = prevObjLayer;
		DestroyImmediate(go);
	}
	
	[MenuItem("GameObject/World Space Normals Creator")]
	static void CreateWorldSpaceNormals ()
	{
		ScriptableWizard.DisplayWizard("Create World Space Normal", 
		typeof(WorldSpaceNormalsCreator),"Create");
	}
	
}
Was this page helpful? Yes No