14 January 2012

Unity3d - Removing Render Texture alpha

 If you want to apply the Render Texture to a GUITexture object so it will be sized to the screen with other GUITexture objects. But GUITextures don't have indpendent materials or shaders so you won't be able to remove the alpha which is added to the render texture. Just setting the color of the GUITexture and the Render Texture camera to white and 100% still leaves the Render Texture transparent. So you can remove the alpha values from that render texture using the following script.




var alpha = 1.0;
private var mat : Material;

function Start ()
{
    mat = new Material(
        "Shader \"Hidden/Clear Alpha\" {" +
        "Properties { _Alpha(\"Alpha\", Float)=1.0 } " +
        "SubShader {" +
        "    Pass {" +
        "        ZTest Always Cull Off ZWrite Off" +
        "        ColorMask A" +
        "        SetTexture [_Dummy] {" +
        "            constantColor(0,0,0,[_Alpha]) combine constant }" +
        "    }" +
        "}" +
        "}"
    );
}

function OnPostRender()
{
    GL.PushMatrix();
    GL.LoadOrtho();
    mat.SetFloat( "_Alpha", alpha );
    mat.SetPass(0);
    GL.Begin( GL.QUADS );
    GL.Vertex3( 0, 0, 0.1 );
    GL.Vertex3( 1, 0, 0.1 );
    GL.Vertex3( 1, 1, 0.1 );
    GL.Vertex3( 0, 1, 0.1 );
    GL.End();
    GL.PopMatrix();

Apply this script to camera of whose render texture is to be taken.

Installing Kinect on Windows(OpenNI) with Unity Wrapper

Real application of Kinect starts when you integrate it with any gaming engine. For a head start, OpenNI provides a wrapper in C# to be used in Unity3D gaming engine. They have also given a sample code for Ogre. You can start working on them after few simple steps of installation.

The fastest way to get started with this is to install the Zigfu bundle and Wrapper for OpenNI in Unity.
1. Download and install the latest zigfu development bundle from http://www.zigfu.com/devtools.html. This bundle install OpenNI, NITE and sensor drivers for kinect.
2. Now download the Zigfu Unity3D wrapper and extract it in your project. It contains sample scenes for help.
Now you can start developing your own motion sensing applications and games.

If you want to go with the latest OpenNI drivers and wrappers, you'll have to install them individually.
1. Download OpenNI module from http://75.98.78.94/Downloads/OpenNIModules.aspx. Select "OpenNI Binaries" in the first drop down box, then select the stable/unstable release and then download the specific module according to your system. If you want to work with PCL also, download this module from http://pointclouds.org/downloads/windows.html in the OpenNI column.
2. Now select the "OpenNI Compliant Middleware Binaries"  and download the middleware binary for your system.
3. Select the "OpenNI Compliant Hardware Binaries" and download stable/unstable release according to your system. If you want to work with PCL, you'll have to download it separately from http://pointclouds.org/downloads/windows.html under the sensor column.
4. Install all these downloaded binaries in the order in which they were downloaded.
5. Now you'll have to download the Unity3d wrapper from https://github.com/OpenNI/UnityWrapper according to your version of binaries.
6. Extract this and include in your project to get started with using kinect for application development.
7. Or you can download the Sinbad sample in Ogre from https://github.com/OpenNI/SampleAppSinbad to get started in Ogre gaming engine.

12 January 2012

Unity3d - Displaying output of one camera as background of other camera

Unity is an integrated authoring tool for creating 3D video games or other interactive content such as architectural visualizations or real-time 3D animations. Unity's development environment runs on Microsoft Windows and Mac OS X, and the games it produces can be run on Windows, Mac, Xbox 360, PlayStation 3, Wii, iPad, iPhone, as well as the Android platform. It can also produce browser games that use the Unity web player plugin, supported on mac and Windows but not Linux. The web player is also used for deployment as Mac widgets.

I was working with a problem where I needed to display the output of one camera as the background image of the other camera. The concepts used in this method are applicable at lot of places during the game development like showing the mirror effect in car races or the score board. 

I would suggest to go through the following links before using this method. The following links will give a rough idea about the Camera, GUI Texture, Render Texture and Layers in Unity.

Setup your scene with the Main Camera showing your game objects. After this follow these steps to create the background using a different Camera  -

1. Create a new camera (GameObject->Create Other-> Camera), and name it "Background Camera".

2. Create a new GUI Texture (GameObject->Create Other->GUI Texture), and name it "Background Image".

3. Click the "Layer" dropdown menu in the Background Image's inspector pane, and select "Add Layer".

4. In the next free "User Layer" slot, create a new layer name called "Background Image". This will be directly under the layer named "Terrain" if you haven't yet added any others.

5. Select your Background Image in the hierarchy, and give it the desired texture, and set the x, y, width and height under "Pixel Inset" so that it fills the screen appropriately.

6. Near the top of the inspector window, Use the layer dropdown menu to assign your "Background Image" layer that you defined earlier to this game object.

7. Now select your Background Camera in the hierarchy, and adjust these settings in the inspector:
   * Un-check Flare Layer and Audio Listener (but leave GUILayer enabled)
   * Set Clear Flags to Solid Color
   * Set Depth to -1
   * Set Culling Mask, first to "Nothing", and then to "Background Image"

8. Now Select your other (Main) camera and in the inspector:
   * Set its Clear Flags to "Depth Only"
   * Click its culling mask setting, and un-check "Background Image". This should result in the culling mask displaying as "Mixed..."

9. Now your system should display the Background GUI Texture as the background of the main camera. Since, this GUI Texture is the background for the present camera, you can change it dynamically from the script to display any other video texture or Render Texture from other camera. Render Texture is  used to render the camera view to a texture which can be displayed somewhere else.

10. Create a new Render Texture by Assets->Create->Render Texture. Give a proper name to this render texture and edit the properties according to your needs.

11. Now setup the scene and a camera whose output you want to display as the background in the Main Camera.

12. Go to the 'Target Texture'  option of this new camera and assign the render texture you created to it.

13. Now make a script which assigns this render texture as GUI Texture to the Background Image you created earlier.