<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ghost Mantis Games &#187; Unity</title>
	<atom:link href="https://ghostmantis.com/?cat=63&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>https://ghostmantis.com</link>
	<description>Development Blog</description>
	<lastBuildDate>Wed, 21 May 2014 03:58:42 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.7.41</generator>
	<item>
		<title>[TUTORIAL] &#8211; Automatically Create Light Probes</title>
		<link>https://ghostmantis.com/?p=332</link>
		<comments>https://ghostmantis.com/?p=332#comments</comments>
		<pubDate>Sat, 28 Sep 2013 15:44:16 +0000</pubDate>
		<dc:creator><![CDATA[Fishypants]]></dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity]]></category>
		<category><![CDATA[chick]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[female]]></category>
		<category><![CDATA[forest]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[light probes]]></category>
		<category><![CDATA[mari]]></category>
		<category><![CDATA[maya]]></category>
		<category><![CDATA[mmo]]></category>
		<category><![CDATA[mmorpg]]></category>
		<category><![CDATA[multiplayer]]></category>
		<category><![CDATA[online]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[sexy]]></category>
		<category><![CDATA[step by step]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unity]]></category>
		<category><![CDATA[unity engine]]></category>
		<category><![CDATA[unity3d]]></category>
		<category><![CDATA[video game]]></category>
		<category><![CDATA[videogame]]></category>

		<guid isPermaLink="false">http://ghostmantis.com/?p=332</guid>
		<description><![CDATA[Ok, so I ended up tweaking and bug fixing this script a bit more, but I feel it is ready to post. Please keep in mind this isn&#8217;t thoroughly tested so there might still be bugs in it. How to use: First and foremost this script assumes that you have an empty gameobject in your scene named &#8220;_LightProbes&#8221;, and that&#160;<a href="https://ghostmantis.com/?p=332" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>Ok, so I ended up tweaking and bug fixing this script a bit more, but I feel it is ready to post. Please keep in mind this isn&#8217;t thoroughly tested so there might still be bugs in it.</p>
<p>How to use:</p>
<ol>
<li>First and foremost this script assumes that you have an empty gameobject in your scene named &#8220;_LightProbes&#8221;, and that it has a light probe group component attached to it. If you do not, then either make an empty game object called &#8220;_LightProbes&#8221; and attach a light probe component, or edit the script to point to your game object where your lightprobes are attached.</li>
<li>Save this code in a file called &#8220;CreateLightProbes.cs&#8221;. Place this file in your projects Editor folder. If you do not have one, create a folder called &#8220;Editor&#8221;. A &#8220;Custom&#8221; menu tab will appear at the top of Unity once you place this script in the Editor folder.</li>
<li>Select meshes that you would like light probes to appear on. In our setup we have flat ground planes, as this works the best. This will not work for caves or enclosed areas!</li>
<li>Make sure that the meshes have a collider attached to them. Mesh colliders work the best!</li>
<li>Set the spacing and secondary height values in the UI. The spacing is how far apart on the X and Z axis the probes will be. After it places a 2d grid of probes it will duplicate those and move them upwards to form a volume. The secondary spacing is how high up to move them.</li>
<li>Select the objects you want to generate light probes for and click <strong>Create Light Probes on Selected Objects</strong> and you&#8217;re done! Rebuild the probe lighting from the Lightmapping tab to see the new probes.</li>
</ol>
<p>Code:</p>
<pre class="brush: csharp; gutter: true">// --------------------------------------------------
// Copyright (C) 2013 Ghost Mantis Games LLC
// This software is provided &quot;as is&quot;, withouth warranty of any kind.
// This software is provided exclusively to you free of charge.
// Ghost Mantis Games LLC does not make and hereby disclaims any warranties
// of any kind. By using this software you agree to these terms.
// --------------------------------------------------

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class CreateLightProbes : EditorWindow {
	float Spacing = 2.0f;
	float SecondHeight = 4.0f;

	[MenuItem(&quot;Custom/Create Light Probes&quot;)]

	static void Init(){
		// Get existing open window or if none, make a new one:
		CreateLightProbes window = (CreateLightProbes)EditorWindow.GetWindow(typeof(CreateLightProbes), true, &quot;Create Light Probes&quot;);
		window.position = new Rect((Screen.width / 2) - 125, Screen.height / 2 + 85, 300, 80);
		window.Show();
	}

	void OnGUI(){
		GUILayout.Label (&quot;Creation:&quot;, EditorStyles.boldLabel);
		Spacing = EditorGUILayout.FloatField(&quot;Spacing:&quot;, Spacing);
		SecondHeight = EditorGUILayout.FloatField(&quot;Secondary Height:&quot;, SecondHeight);

		// Clamp values
		if(Spacing &lt; 0.1f) Spacing = 0.1f;
		if(SecondHeight &lt; 0.1f) SecondHeight = 0.1f;

		if(GUILayout.Button(&quot;Create Light Probes On Selected Objects&quot;)){
			Bounds();
		}
	}

	void Bounds(){
		// Get selection
		GameObject[] Select = Selection.gameObjects;
		if(Select.Length &lt; 1) return;

		// Get total bounds for selected objects
		float minX = 0.0f;
		float minY = 0.0f;
		float minZ = 0.0f;
		float maxX = 0.0f;
		float maxY = 0.0f;
		float maxZ = 0.0f;
		for(int i=0; i&lt;Select.Length; i++){
			// First check that mesh has a collider attached to it
			// if it doesn&#039;t then we can&#039;t raycast so we should ignore this object
			Collider col = Select[i].GetComponent();
			if(col == null) continue;

			// Get renderer component attached to object
			// If there is no renderer attached then we ignore this object
			Renderer renderer = Select[i].GetComponent();
			if(renderer == null) continue;

			// Get the renderer bounds
			Bounds bbox = renderer.bounds;

			// Update total bounds
			if(bbox.min.x &lt; minX) minX = bbox.min.x;
			if(bbox.min.y &lt; minY) minY = bbox.min.y;
			if(bbox.min.z &lt; minZ) minZ = bbox.min.z;
			if(bbox.max.x &gt; maxX) maxX = bbox.max.x;
			if(bbox.max.y &gt; maxY) maxY = bbox.max.y;
			if(bbox.max.z &gt; maxZ) maxZ = bbox.max.z;
		}

		// Now go through in a grid and attempt to place a light probe using raycasting
		float xCount = minX;
		float zCount = minZ;
		List VertPositions = new List();
		for(int z=0; z&lt;maxZ; z++){
			for(int x=0; x&lt;maxX; x++){
				// Raycast downwards through each selected object and
				// attempt to find one that we are over
				for(int j=0; j&lt;Select.Length; j++){
					Collider col = Select[j].GetComponent();

					RaycastHit hit;
					Ray ray = new Ray();
					ray.origin = new Vector3(xCount, maxY, zCount);
					ray.direction = -Vector3.up;
					if(col.Raycast(ray, out hit, (maxY-minY)*2)){
						VertPositions.Add(hit.point + new Vector3(0, 0.07f, 0));
						VertPositions.Add(hit.point + new Vector3(0, SecondHeight, 0));
					}
				}

				xCount += Spacing;
			}

			// Reset X Counter
			xCount = minX;

			zCount += Spacing;
		}

		// Check if we have any hits
		if(VertPositions.Count &lt; 1) return;

		// Get _LightProbes game object
		GameObject LightProbeGameObj = GameObject.Find(&quot;_LightProbes&quot;);
		if(LightProbeGameObj == null) return;

		// Get light probe group component
		LightProbeGroup LPGroup = LightProbeGameObj.GetComponent(&quot;LightProbeGroup&quot;) as LightProbeGroup;
		if(LPGroup == null) return;

		// Create lightprobe positions
		Vector3[] ProbePos = new Vector3[VertPositions.Count];
		for(int i=0; i&lt;VertPositions.Count; i++){
			ProbePos[i] = VertPositions[i];
		}

		// Set new light probes
		LPGroup.probePositions = ProbePos;

		Debug.Log(&quot;Finished Probe Calculations with: &quot; + ProbePos.Length + &quot;Probes.&quot;);
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>https://ghostmantis.com/?feed=rss2&#038;p=332</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
