Rotating Cubemap in Unity

Hello Everyone,

In this post we will discuss about applying custom matrix for rotation in Unity Shaders to rotate the cubemap. This can be done to any other shader.
Specifically, we will be looking at how to rotate a cubemap by n Degrees in any axes. I will be using the Reflection Glossiness shader that Unity provides. You can download the shader from : http://unity3d.com/unity/download/archive
The step by step approach:
(For the scripts please follow git: https://github.com/theMaxscriptGuy/Unity_Dev/tree/master/CubeMap_Rotation)

What we will need:

1) Cube map from http://www.topsolidblog.com/

http://www.flickr.com/photos/42629471@N05/3992503362/

2) Reflection Glossiness shader: Its placed in the whateverDriveYouDownload:\builtin_shaders-4.5.4\DefaultResourcesExtra folder. Named : Reflect-Glossy.shader

3) A custom script in C#(I prefer cs. js can also be used). This will contain all the attributes for changing the rotation and will set the cubemap rotation.

Step 1: Setup Unity Editor

Copy and paste the Reflect-Glossy.shader in the Unity Editor.

Create two planes, one ground and other vertical on which we will apply the rotating texture.

Create a point light so that we can see the objects in the gameplay.

Create a material and apply it to the vertical plane.

Select the image and in the inspector, change the Texture type to Reflection. This will make it a cubemap.

Create a custom script named: SetRotation. Your Editor should look like the Image_01.

Step 2: Editing the shader

Open the Reflect-Glossy.shader in MonoDevelop.

Change the first line from : Shader “Reflective/Specular” {

To : Shader “Reflective/Specular2” {

By this we change the shader name to Specular2 in the category Reflective. This keeps the Specular shader intact.

Add a line: uniform float4x4 _Rotation; after the half _Shininess property. By this we are defining a custom matrix. We will multiply the texturemap by this matrix

Then simply replace the line:

fixed4 reflcol = texCUBE (_Cube, IN.worldRefl);

with

fixed4 reflcol = texCUBE (_Cube, mul(_Rotation, float4(IN.worldRefl,0)));

Here the multiplication takes place.

Select the material of the plane and change the shader to Reflective/Specular2.

Add the cubemap to the reflection cubemap property of the material.

Step 3: Writing custom script

Make a public int property : angle

Make another public int property : speed

In the update function write:

Quaternion rot = Quaternion.Euler (0, angle, 0); 

//if you want to make a rotating animation, delete the above line and write : Quaternion rot = Quaternion.Euler (0, (Time.time* speed), 0);
Matrix4x4 m = new Matrix4x4 ();
m.SetTRS(Vector3.zero, rot,new Vector3(1,1,1) );
renderer.material.SetMatrix (“_Rotation”, m);

apply this script to the plane.

By this we change the shader name to Specular2 in the category Reflective. This keeps the Specular shader intact.

For the scripts please follow git: https://github.com/theMaxscriptGuy/Unity_Dev/tree/master/CubeMap_Rotation

Hope you find this useful.

Thanks,

theMaxscriptGuy

Image_01 Image_02

3 thoughts on “Rotating Cubemap in Unity

  1. This is very informative! Thanks for sharing!
    Is there a way to rotate the texture with a slider as a property?
    Instead of adding a Script?

    • Thank you for the reply. As we are adding special/custom attributes, we will need to add a script. Though, what you can do is change the textfield into a slider as a UI option.
      Please let me know if you have any queries.

Leave a comment