How can I rotate the entire skybox Constantly in-game? I don’t want any animated clouds or stuff like that, just rotate the whole skybox. An example would be the Unreal Tournament Map, Facing worlds. I want to accomplish the same thing with my space skybox.
Link to a video with Facing Worlds game-play = - YouTube
Thanks for your help!
The only thing you need to do is add a 6 sided skybox material to you scene, create an empty game object with a script containing the following line:
void Update ()
{
RenderSettings.skybox.SetFloat("_Rotation", Time.time);
//To set the speed, just multiply Time.time with whatever amount you want.
}
This will rotate your skybox.
Create a separate camera that only renders the skybox; put it on the furthest back layer, set your other camera(s) to clear “depth only,” and put a script on the skybox camera to rotate it.
This is a shader which can rotate a skybox in any direction without using a second camera. It will rotate with Light0’s rotation. Un-comment the indicated lines if you are using an HDRI map. Cubemaps still work with these lines on, just less efficiently.
Shader "Custom/MovingSkyboxUpload" {
Properties {
_CubeMap ("Skybox Cube Map", cube) = "grey" {}
}
SubShader {
Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
Cull Off ZWrite Off
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// -------------------- [ un-comment For HDRI Cubemaps ] --------------------
//#include "UnityCG.cginc"
//half4 _CubeMap_HDR;
// -------------------- [ un-comment For HDRI Cubemaps ] --------------------
uniform samplerCUBE _CubeMap;
struct vertexInput{
float4 vertex: POSITION;
};
struct vertexOutput{
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD1;
fixed3x3 R : TEXCOORD4;
};
vertexOutput vert(vertexInput v){
vertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
fixed3 new_up = normalize(_WorldSpaceLightPos0.xyz);
// folowing code from http://stackoverflow.com/questions/32257338/vertex-position-relative-to-normal
float3 up = float3(0,1,0);
float3 w = cross(up, new_up);
float s = length(w); // Sine of the angle
float c = dot(up, new_up); // Cosine of the angle
float3x3 VX = float3x3 (
0, -1 * w.z, w.y,
w.z, 0, -1 * w.x,
-1 * w.y, w.x, 0
); // This is the skew-symmetric cross-product matrix of v
float3x3 I = float3x3 (
1, 0, 0,
0, 1, 0,
0, 0, 1
); // The identity matrix
o.R = I + VX + mul(VX , VX) * (1 - c)/(s*s); // The rotation matrix! YAY!
// previous code from http://stackoverflow.com/questions/32257338/vertex-position-relative-to-normal
return o;
}
float4 frag(vertexOutput i) : COLOR{
float3 viewDirection = normalize( _WorldSpaceCameraPos.xyz - i.posWorld.xyz );
viewDirection = mul (viewDirection , i.R);
// -------------------- [ un-comment For HDRI Cubemaps ] --------------------
//return fixed4(DecodeHDR (texCUBE(_CubeMap, viewDirection), _CubeMap_HDR),1);
// -------------------- [ un-comment For HDRI Cubemaps ] --------------------
return texCUBE(_CubeMap, viewDirection);// if upside down, add - in front of viewDirection
}
ENDCG
}
}
}
Built in Unity 5.4.1
Plus 1 to @ronald677br 's answer. If you want to modify a different skybox, you can also retrieve the skybox component from the camera.
For instance:
public float SkyboxSpeed = 1f;
GetComponent<Skybox>().material.SetFloat("_Rotation", Time.time * SkyboxSpeed);
I found the best way that wasn’t too complex is to add a SkyBox to the “Main Camera” and add a script to it that contains something along the lines of this.
using UnityEngine;
public class HomeCameraRotation : MonoBehaviour {
public Skybox box;
public float speed = 5f;
void Start() {
}
void LateUpdate() {
box.material.SetFloat("_Rotation", Time.time * speed);
}
}