Last active
March 22, 2018 22:24
-
-
Save Arakade/1567b982dfdfb27f345d0dd651e89f13 to your computer and use it in GitHub Desktop.
Unity3D script to rotate the skybox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' | |
Shader "Skybox/6 Sided-Rotated" { | |
Properties { | |
_Tint ("Tint Color", Color) = (.5, .5, .5, .5) | |
[Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0 | |
[NoScaleOffset] _FrontTex ("Front [+Z] (HDR)", 2D) = "grey" {} | |
[NoScaleOffset] _BackTex ("Back [-Z] (HDR)", 2D) = "grey" {} | |
[NoScaleOffset] _LeftTex ("Left [+X] (HDR)", 2D) = "grey" {} | |
[NoScaleOffset] _RightTex ("Right [-X] (HDR)", 2D) = "grey" {} | |
[NoScaleOffset] _UpTex ("Up [+Y] (HDR)", 2D) = "grey" {} | |
[NoScaleOffset] _DownTex ("Down [-Y] (HDR)", 2D) = "grey" {} | |
} | |
SubShader { | |
Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" } | |
Cull Off ZWrite Off | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
half4 _Tint; | |
half _Exposure; | |
uniform float4x4 _Rotation; | |
float4 RotateAroundYInDegrees (float4 vertex, float degrees) | |
{ | |
float alpha = degrees * UNITY_PI / 180.0; | |
float sina, cosa; | |
sincos(alpha, sina, cosa); | |
float2x2 m = float2x2(cosa, -sina, sina, cosa); | |
return float4(mul(m, vertex.xz), vertex.yw).xzyw; | |
} | |
struct appdata_t { | |
float4 vertex : POSITION; | |
float2 texcoord : TEXCOORD0; | |
}; | |
struct v2f { | |
float4 vertex : SV_POSITION; | |
float2 texcoord : TEXCOORD0; | |
}; | |
v2f vert (appdata_t v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(mul(v.vertex, _Rotation)); | |
o.texcoord = v.texcoord; | |
return o; | |
} | |
half4 skybox_frag (v2f i, sampler2D smp, half4 smpDecode) | |
{ | |
half4 tex = tex2D (smp, i.texcoord); | |
half3 c = DecodeHDR (tex, smpDecode); | |
c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb; | |
c *= _Exposure; | |
return half4(c, 1); | |
} | |
ENDCG | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
sampler2D _FrontTex; | |
half4 _FrontTex_HDR; | |
half4 frag (v2f i) : SV_Target { return skybox_frag(i,_FrontTex, _FrontTex_HDR); } | |
ENDCG | |
} | |
Pass{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
sampler2D _BackTex; | |
half4 _BackTex_HDR; | |
half4 frag (v2f i) : SV_Target { return skybox_frag(i,_BackTex, _BackTex_HDR); } | |
ENDCG | |
} | |
Pass{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
sampler2D _LeftTex; | |
half4 _LeftTex_HDR; | |
half4 frag (v2f i) : SV_Target { return skybox_frag(i,_LeftTex, _LeftTex_HDR); } | |
ENDCG | |
} | |
Pass{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
sampler2D _RightTex; | |
half4 _RightTex_HDR; | |
half4 frag (v2f i) : SV_Target { return skybox_frag(i,_RightTex, _RightTex_HDR); } | |
ENDCG | |
} | |
Pass{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
sampler2D _UpTex; | |
half4 _UpTex_HDR; | |
half4 frag (v2f i) : SV_Target { return skybox_frag(i,_UpTex, _UpTex_HDR); } | |
ENDCG | |
} | |
Pass{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
sampler2D _DownTex; | |
half4 _DownTex_HDR; | |
half4 frag (v2f i) : SV_Target { return skybox_frag(i,_DownTex, _DownTex_HDR); } | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' | |
Shader "Skybox/Cubemap-Rotated" { | |
Properties { | |
_Tint ("Tint Color", Color) = (.5, .5, .5, .5) | |
[Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0 | |
[NoScaleOffset] _Tex ("Cubemap (HDR)", Cube) = "grey" {} | |
} | |
SubShader { | |
Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" } | |
Cull Off ZWrite Off | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
samplerCUBE _Tex; | |
half4 _Tex_HDR; | |
half4 _Tint; | |
half _Exposure; | |
uniform float4x4 _Rotation; | |
float4 RotateAroundYInDegrees (float4 vertex, float degrees) | |
{ | |
float alpha = degrees * UNITY_PI / 180.0; | |
float sina, cosa; | |
sincos(alpha, sina, cosa); | |
float2x2 m = float2x2(cosa, -sina, sina, cosa); | |
return float4(mul(m, vertex.xz), vertex.yw).xzyw; | |
} | |
struct appdata_t { | |
float4 vertex : POSITION; | |
}; | |
struct v2f { | |
float4 vertex : SV_POSITION; | |
float3 texcoord : TEXCOORD0; | |
}; | |
v2f vert (appdata_t v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(mul(v.vertex, _Rotation)); | |
o.texcoord = v.vertex.xyz; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
half4 tex = texCUBE (_Tex, i.texcoord); | |
half3 c = DecodeHDR (tex, _Tex_HDR); | |
c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb; | |
c *= _Exposure; | |
return half4(c, 1); | |
} | |
ENDCG | |
} | |
} | |
Fallback Off | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
namespace UGS.environ { | |
/// <summary> | |
/// Rotates the skybox of the scene or camera. | |
/// Requires a special shader. | |
/// </summary> | |
internal sealed class SkyBoxRotation : MonoBehaviour { | |
[SerializeField] | |
private string propertyName = "_Rotation"; | |
[SerializeField] | |
private Vector3 eulerAngles = Vector3.zero; | |
[SerializeField] | |
private bool affectCameraOnGameObject = false; | |
[SerializeField] | |
private bool continuousUpdate = false; | |
private Material skyBoxMaterial; | |
public void Awake() { | |
setup(); | |
} | |
[ContextMenu("Re-setup")] | |
private void setup() { | |
skyBoxMaterial = getSkyboxMaterial(); | |
} | |
public void Start() { | |
applyRotation(); | |
} | |
public void OnEnable() { | |
if (continuousUpdate) { | |
StartCoroutine(continuousUpdateCoroutine()); | |
} | |
} | |
public void OnDisable() { | |
if (continuousUpdate) { | |
StopAllCoroutines(); | |
} | |
} | |
private IEnumerator continuousUpdateCoroutine() { | |
while (true) { | |
applyRotation(); | |
yield return null; | |
} | |
} | |
[ContextMenu("Apply skybox rotation.")] | |
public void applyRotation(bool reacquireMaterial = false) { | |
if (reacquireMaterial) { | |
setup(); | |
} | |
var rot = Quaternion.Euler(eulerAngles); | |
var matrix = Matrix4x4.TRS(Vector3.zero, rot, new Vector3(1, 1, 1)); | |
skyBoxMaterial.SetMatrix(propertyName, matrix); | |
} | |
private Material getSkyboxMaterial() { | |
if (affectCameraOnGameObject) { | |
var skybox = GetComponent<Skybox>(); | |
Assert.IsNotNull(skybox); | |
return skybox.material; | |
} else { | |
return RenderSettings.skybox; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment