[Unity Shaders] Reflecting Your World create Cubemaps -- in Unity3D

Writing in the front



In order to create the reflection in the Shaders effect, we will learn how to create our own Cubemaps first. Of course, you can find many ready-made online at Cubemaps, but you will soon want to, how to make their own Cubemaps, because those online is not your own reflection in the game scene.
Make your own Cubemaps is critical for creating reflection effects more realistic. Methods we will learn a few can be used directly in the Unity in editor. In addition, we will also have a look at the single game in effect. This knowledge will help you understand the next chapter (illumination model) content in.


Well, we have the following formal learning how to create our Shaders Cubemaps!




Begin to work



Unity provides a JavaScript code for us to generate Cubemap from the us to create scenes. So, let's look at how it works. This link. It is the foundation of our script. Next, we will translate it into C#. In the last section of this chapter (the dynamic creation of Cubemaps), we will learn how to create a simple system to create a Cubemaps from a different location, and then converted at the reflection maps in the use of these data, simulation game character moving within the scene in effect, it will eventually be reflected in real time system 1.5.


Here, we only learn how to create a separate Cubemap.
  1. We need a light source to create some elements as Cubemaps. Therefore, we need to put some geometric plane in the scene. You can use a modeling software, such as Maya or Max, but you can also use Unity with plane. Anything which way, it does not matter. Your scene should be like this:

  2. The objects in the scene are shown as follows:

    Among them, Position is an empty object, it will only as the observation position will be environmental information rendering from this point observed to our Cubemap.





Realization



  1. First of all, we need to create a new script, but because we want a popup editor window, so we must put the script into a folder called Editor. Create a folder called Editor in your Project panel, and then create a C# script, called GenerateStaticCubemap.

  2. Open the script, in order to use a specific function, we need to use the new using directive:
    using UnityEngine;
    using UnityEditor;
    using System.Collections

  3. In order for Unity to realize, this script is a popup editor window, we need to let the GenerateStaticCubemap class inherits the ScriptableWizard class. This allows us to use some of the underlying function is very good.
    public class GenerateStaticCubemap : ScriptableWizard {

  4. Then, we need to add some new variables to store the new CubeMap and its position (i.e., the above mentioned position null object).
    	public Transform renderPosition;
    	public Cubemap cubemap;
  5. The first function is the built-in function OnWizardUpdate Unity(). The wizard (wizard) first pop-up or when GUI is the user changes (such as drag some object, input some characters) to be called. Therefore, here we can check the user has to wizard fills in all the resources we need. Here, if the Cubemap or its position (a transform) has not been filled, then set the built-in variable isValid to false, until you get all the resources.
    	void OnWizardUpdate() {
    		helpString = "Select transform to render" +
    			" from and cubemap to render into";
    		if (renderPosition != null && cubemap != null) {
    			isValid = true;
    		}
    		else {
    			isValid = false;
    		}
    	}
  6. When the isValid variable is true, the wizard will call OnWizardCreate () function. In this function, we will create a new camera, then put it on the set before the transform position, and then call the RenderToCubemap function to get the final Cubemap.
    	void OnWizardCreate() {
    		GameObject go = new GameObject("CubeCam", typeof(Camera));
    
    		go.transform.position = renderPosition.position;
    		go.transform.rotation = Quaternion.identity;
    
    		go.camera.RenderToCubemap(cubemap);
    
    		DestroyImmediate(go);
    	}
  7. Finally, we need to open this wizard from the Unit editor menu bar. This requires the MenuItem keyword.
    	[MenuItem("CookBook/Render Cubemap")]
    	static void RenderCubemap() {
    		ScriptableWizard.DisplayWizard("Render CubeMap", typeof(GenerateStaticCubemap), "Render!");
    	}


Finally, the code is as follows:
using UnityEngine;
using UnityEditor;
using System.Collections;

public class GenerateStaticCubemap : ScriptableWizard {

	public Transform renderPosition;
	public Cubemap cubemap;

	void OnWizardUpdate() {
		helpString = "Select transform to render" +
			" from and cubemap to render into";
		if (renderPosition != null && cubemap != null) {
			isValid = true;
		}
		else {
			isValid = false;
		}
	}

	void OnWizardCreate() {
		GameObject go = new GameObject("CubeCam", typeof(Camera));

		go.transform.position = renderPosition.position;
		go.transform.rotation = Quaternion.identity;

		go.camera.RenderToCubemap(cubemap);

		DestroyImmediate(go);
	}

	[MenuItem("CookBook/Render Cubemap")]
	static void RenderCubemap() {
		ScriptableWizard.DisplayWizard("Render CubeMap", typeof(GenerateStaticCubemap), "Render!");
	}
}

At this time, back to the Unity editor page, click the Unity menu bar (sometimes need to click to refresh) will see a new option: CookBook/Render Cubemap, as shown below:




Click it and you will open a wizard interface, as shown below. It requires two resources, One is the Render Position, This will determine the viewing position (you can understand you will put a mirror on the position, Note here is because the camera set up the code of the rotation as the initial value, This means that the camera will look to the blue arrow in the direction), One is the Cubemap, Can understand the achievement is the reflection in the mirror image. Following my example.






Click on the Render! Button, check your Cubemap, you will see such a scenario similar to the following.




Can be found, we have put the image rendering to a cube. Congratulations, you have completed one of the Cubemap's own! You can try to test in different scenes.






Interpretation



Remember the beginning we inherited from the ScriptableWizard class, this is to tell Unity3D that we want to make a new pop-up window type of user editor, which is why we need the scripts in the Editor folder. If we do not do this, the Unity will not make it identified as a user script editor types.


We next statement parameter is used for storing the Cubemap rendering position, as well as to where the new rendering the Cubemap into the Project, such as the above is my name in advance to create project file for the First Cubemap a Cubemap. With these we can create their own Cubemap.


Then we use the OnWizardUpdate () function, which is provided by the ScriptableWizard class to us. Similarly, the isValid variable is a built-in variables. It allows us to easily open or close the wizard below the Create button (here refers to the button Render!). This can prevent users from below using empty transform or Cubemap operation.


Once we determine the user provides the correct data, we can get into the OnWizardCreate () function. This is Cubemap really is created where. It first creates a new GameObject constructor, and its type is Camera. Then put it in the transform provides the user position.


Here, we only need to do is to provide the user of the Cubemap passed to the RenderToCubemap function, generating six pictures.


Finally, we create a menu of options for the wizard, so users can open it from the Unity menu bar at the top of the. In addition to the [MenuItem (" CookBook/Render Cubemap")] outside the keywords, we also need to declare function is static function.


So far, we have completed a simple tool, can be used directly to generate the Cubemaps in the Unity editor!



Posted by Phoebe at July 25, 2014 - 7:02 AM