- Home /
Rotate Skybox Constantly
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 = https://www.youtube.com/watch?v=LSt8FztPJb4
Thanks for your help!
Answer by ronald677br · Dec 16, 2016 at 04:31 PM
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.
Thanks! So simple, easy, and works perfectly. This should be the accepted answer.
For anyone who saw this and got a confused (like me), remember to throw it into the update function:
// Speed multiplier
public float speed$$anonymous$$ultiplier;
// Update is called once per frame
void Update ()
{
//Sets the float value of "_Rotation", adjust it by Time.time and a multiplier.
RenderSettings.skybox.SetFloat("_Rotation", Time.time * speed$$anonymous$$ultiplier);
}
Answer by Kiloblargh · Mar 02, 2014 at 10:02 PM
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 working very Well, Thanks! I do have one problem though. Whenever i turn my character using the mouse the skybox turns with it. It looks very weird. Am I doing something wrong?
That is happening because your skybox camera is not taking your main cameras' rotation into account. I was able to successfully create an independently rotating skybox by adding the following code to the rotation script on the skybox cam. Hope it helps
private Vector3 rotationValue; private float turnValue = 0.0f; private float turnVal { get { return turnValue; } set { turnValue = value; if (turnValue >= 360f) turnValue = 0.0f; } }
void LateUpdate ()
{
turnVal += Time.deltaTime;
rotationValue = new Vector3 (Camera.main.transform.rotation.eulerAngles.x, Camera.main.transform.rotation.eulerAngles.y + turnVal, Camera.main.transform.rotation.eulerAngles.z);
transform.rotation = Quaternion.Euler (rotationValue);
}
Another solution to match the rotations to movement, is to simply make the Skybox camera object a child object of the main camera. As long as your rotations are relational (transform.Rotate for instance) it should work well.
Answer by NicRule · Jan 28, 2016 at 03:22 AM
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
@NicRule In Unity 5.4 it compiles but shows the HDRI wrong.
Can you kindly have a look and repost the shader code?
Thank you very much.
I have little experience with HDRI, but when I tried using one, it just came in upside down. I fixed it by adding a negative sign to viewDirection. Was that your problem?
return texCUBE(_Cube$$anonymous$$ap, -viewDirection);
Greetings Nick and thank you so much for the reply.
Here you can see the problem -->https://www.dropbox.com/s/3mstpj98s9p3jjx/shader_color_issue.jpg?dl=0
Its in the colors , it looks like inverted the colors or similar effect.
Thank you , i am waiting a long for a solution on this one.
Answer by Stephane-Imbert · Jan 13, 2017 at 02:47 AM
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);
Answer by hiaiden2 · Nov 18, 2018 at 12:05 AM
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);
}
}
Your answer
Follow this Question
Related Questions
Problem with creating skyboxes 3 Answers
skybox alternative iOS --- performance better with textured dome? 0 Answers
offset for skybox 1 Answer
how do you change a skybox material by name 1 Answer
Skybox Movie Texture 1 Answer