最近写独立游戏,需要旋转天空盒。有两种办法:
方法A,通过shader传参数,旋转天空盒。
方法B,多相机,分层渲染。专门一个相机去渲染天空盒,然后旋转相机。
这里先讲方法1。方法2在URP下,最好是用相机堆栈(Camera Stack)处理,具体就不讲了。
步骤1:? 更改原始的skybox?shader
更改默认的天空盒shader,去官网下载对应的内置shader,然后更改如下(我做了标记,如果实在不清楚改哪行,可以用对比工具对比一下):
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
Shader "CCG/SkyBox_6_Sided" {
Properties {
_Tint ("Tint Color", Color) = (.5, .5, .5, .5)
[Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
_Rotation ("Rotation", Range(0, 360)) = 0
_RotationAxis("Rotation axis", Vector) = (0, 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;
float _Rotation;
float3 _RotationAxis;
float3 RotateAroundYInDegrees (float3 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 float3(mul(m, vertex.xz), vertex.y).xzy;
}
// 计算函数
float4x4 rotationMatrix(float3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return float4x4(oc * axis.x * axis.x + c,
oc * axis.x * axis.y - axis.z * s,
oc * axis.z * axis.x + axis.y * s,
0.0,
oc * axis.x * axis.y + axis.z * s,
oc * axis.y * axis.y + c,
oc * axis.y * axis.z - axis.x * s,
0.0,
oc * axis.z * axis.x - axis.y * s,
oc * axis.y * axis.z + axis.x * s,
oc * axis.z * axis.z + c,
0.0,
0.0, 0.0, 0.0, 1.0);
}
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert (appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
//float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
// 重新计算rotated
float3 rotated = mul( rotationMatrix( normalize(_RotationAxis.xyz), _Rotation * UNITY_PI / 180.0), v.vertex).xyz;
o.vertex = UnityObjectToClipPos(rotated);
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
#pragma target 2.0
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
#pragma target 2.0
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
#pragma target 2.0
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
#pragma target 2.0
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
#pragma target 2.0
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
#pragma target 2.0
sampler2D _DownTex;
half4 _DownTex_HDR;
half4 frag (v2f i) : SV_Target { return skybox_frag(i,_DownTex, _DownTex_HDR); }
ENDCG
}
}
}
步骤2: 新建一个材质球,用刚写的shader。
并且把天空盒的6张图对应放好。
?步骤3:在灯光设置里,更改天空盒
步骤4: 写脚本,功能是旋转天空盒。
如下,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateSkyBox : MonoBehaviour
{
// 设置旋转速度
public float RotateSpeed = 10.2f;
// 设置绕哪个轴旋转
public Vector3 skyAxis = new Vector3(1, 0, 0);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// 注意对应是新写的这个shader, 传入轴向
RenderSettings.skybox.SetVector("_RotationAxis", skyAxis);
// 设置旋转的数值
RenderSettings.skybox.SetFloat("_Rotation", Time.time*RotateSpeed);
}
}
步骤5:脚本挂载到摄像机上,运行
附注:shader代码参考:https://gist.github.com/sergiobd/c786b594d7f5d8826d68f36f3a80f65e
?
|