skybox subsetter
This commit is contained in:
8
Assets/Skybox Blender/Script/Editor.meta
Normal file
8
Assets/Skybox Blender/Script/Editor.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a9bcdfc4941404a449c412d25c64a15b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,95 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
[CustomEditor(typeof(SkyboxBlender))]
|
||||
public class SkyboxBlenderInspector : Editor
|
||||
{
|
||||
SerializedProperty skyboxMaterials,
|
||||
makeFirstMaterialSkybox,
|
||||
blendSpeed,
|
||||
timeToWait,
|
||||
loop,
|
||||
|
||||
updateLighting,
|
||||
updateReflections,
|
||||
updateEveryFrames,
|
||||
|
||||
keepRotating,
|
||||
rotateToAngle,
|
||||
rotationSpeed,
|
||||
stopRotationOnBlendFinish;
|
||||
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
skyboxMaterials = serializedObject.FindProperty("skyboxMaterials");
|
||||
makeFirstMaterialSkybox = serializedObject.FindProperty("makeFirstMaterialSkybox");
|
||||
|
||||
blendSpeed = serializedObject.FindProperty("blendSpeed");
|
||||
timeToWait = serializedObject.FindProperty("timeToWait");
|
||||
loop = serializedObject.FindProperty("loop");
|
||||
|
||||
updateLighting = serializedObject.FindProperty("updateLighting");
|
||||
updateReflections = serializedObject.FindProperty("updateReflections");
|
||||
updateEveryFrames = serializedObject.FindProperty("updateEveryFrames");
|
||||
|
||||
keepRotating = serializedObject.FindProperty("keepRotating");
|
||||
rotateToAngle = serializedObject.FindProperty("rotateToAngle");
|
||||
rotationSpeed = serializedObject.FindProperty("rotationSpeed");
|
||||
stopRotationOnBlendFinish = serializedObject.FindProperty("stopRotationOnBlendFinish");
|
||||
}
|
||||
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
var button = GUILayout.Button("Click for more tools");
|
||||
if (button) Application.OpenURL("https://bit.ly/3CyjBzT");
|
||||
EditorGUILayout.Space(5);
|
||||
|
||||
SkyboxBlender script = (SkyboxBlender) target;
|
||||
int space = 15;
|
||||
|
||||
EditorGUILayout.Space(space);
|
||||
|
||||
EditorGUILayout.LabelField("Material Options", EditorStyles.boldLabel);
|
||||
EditorGUILayout.PropertyField(skyboxMaterials);
|
||||
EditorGUILayout.PropertyField(makeFirstMaterialSkybox);
|
||||
|
||||
|
||||
EditorGUILayout.Space(space);
|
||||
|
||||
|
||||
EditorGUILayout.LabelField("Blend Options", EditorStyles.boldLabel);
|
||||
EditorGUILayout.PropertyField(blendSpeed);
|
||||
EditorGUILayout.PropertyField(timeToWait);
|
||||
EditorGUILayout.PropertyField(loop);
|
||||
|
||||
|
||||
EditorGUILayout.Space(space);
|
||||
|
||||
|
||||
EditorGUILayout.LabelField("Lighting Options", EditorStyles.boldLabel);
|
||||
EditorGUILayout.PropertyField(updateLighting);
|
||||
EditorGUILayout.PropertyField(updateReflections);
|
||||
if (script.updateLighting || script.updateReflections) {
|
||||
EditorGUILayout.PropertyField(updateEveryFrames);
|
||||
}
|
||||
|
||||
|
||||
EditorGUILayout.Space(space);
|
||||
|
||||
|
||||
EditorGUILayout.LabelField("Rotations Options", EditorStyles.boldLabel);
|
||||
EditorGUILayout.PropertyField(keepRotating);
|
||||
|
||||
if (!script.keepRotating) {
|
||||
EditorGUILayout.PropertyField(rotateToAngle);
|
||||
}
|
||||
|
||||
EditorGUILayout.PropertyField(rotationSpeed);
|
||||
EditorGUILayout.PropertyField(stopRotationOnBlendFinish);
|
||||
|
||||
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ad88a79c72924b74b839ab0394b8e1a5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Skybox Blender/Script/Resources.meta
Normal file
8
Assets/Skybox Blender/Script/Resources.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3db593e2c60ddfa48823ad29be8aee53
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a92b2961bb582e408f6449d93ea9c02
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,101 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!21 &2100000
|
||||
Material:
|
||||
serializedVersion: 8
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: Skybox Blend Material
|
||||
m_Shader: {fileID: 4800000, guid: 06e8da9b2b26f09459e8a93e84b2e0c9, type: 3}
|
||||
m_Parent: {fileID: 0}
|
||||
m_ModifiedSerializedProperties: 0
|
||||
m_ValidKeywords: []
|
||||
m_InvalidKeywords: []
|
||||
m_LightmapFlags: 4
|
||||
m_EnableInstancingVariants: 0
|
||||
m_DoubleSidedGI: 0
|
||||
m_CustomRenderQueue: -1
|
||||
stringTagMap: {}
|
||||
disabledShaderPasses: []
|
||||
m_LockedProperties:
|
||||
m_SavedProperties:
|
||||
serializedVersion: 3
|
||||
m_TexEnvs:
|
||||
- _BumpMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailAlbedoMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailMask:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailNormalMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _EmissionMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _MainTex:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _MetallicGlossMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _OcclusionMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _ParallaxMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _ReflectionTex:
|
||||
m_Texture: {fileID: 8900000, guid: f7cb012c92543074491a665c761d51dc, type: 3}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _Tex:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _Tex2:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
m_Ints: []
|
||||
m_Floats:
|
||||
- _BlendCubemaps: 0
|
||||
- _BumpScale: 1
|
||||
- _Cutoff: 0.5
|
||||
- _DetailNormalMapScale: 1
|
||||
- _DstBlend: 0
|
||||
- _Exposure: 1
|
||||
- _GlossMapScale: 1
|
||||
- _Glossiness: 0.5
|
||||
- _GlossyReflections: 1
|
||||
- _Metallic: 0
|
||||
- _Mode: 0
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.02
|
||||
- _ReflectionAlpha: 0
|
||||
- _Rotation: 0
|
||||
- _SmoothnessTextureChannel: 0
|
||||
- _SpecularHighlights: 1
|
||||
- _SrcBlend: 1
|
||||
- _UVSec: 0
|
||||
- _ZWrite: 1
|
||||
m_Colors:
|
||||
- _Color: {r: 0, g: 0, b: 0, a: 0}
|
||||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
||||
- _Tint: {r: 0.39999998, g: 0.39999998, b: 0.39999998, a: 0.5}
|
||||
- _Tint2: {r: 0.39999998, g: 0.39999998, b: 0.39999998, a: 0.5}
|
||||
m_BuildTextureStacks: []
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c10da0d74d78364bac94506d77290bf
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,99 @@
|
||||
Shader "Skybox/SkyboxBlender" {
|
||||
|
||||
Properties {
|
||||
_Tint ("Tint Color", Color) = (.5, .5, .5, 1)
|
||||
_Tint2 ("Tint Color 2", Color) = (.5, .5, .5, 1)
|
||||
|
||||
[Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
|
||||
|
||||
_Rotation ("Rotation", Range(0, 360)) = 0
|
||||
_BlendCubemaps ("Blend Cubemaps", Range(0, 1)) = 0
|
||||
|
||||
[NoScaleOffset] _Tex ("Cubemap (HDR)", Cube) = "grey" {}
|
||||
[NoScaleOffset] _Tex2 ("Cubemap (HDR) 2", Cube) = "grey" {}
|
||||
}
|
||||
|
||||
SubShader {
|
||||
Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
|
||||
Cull Off ZWrite Off
|
||||
Blend SrcAlpha OneMinusSrcAlpha
|
||||
|
||||
Pass {
|
||||
CGPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
samplerCUBE _Tex;
|
||||
samplerCUBE _Tex2;
|
||||
|
||||
float _BlendCubemaps;
|
||||
half4 _Tex_HDR;
|
||||
|
||||
half4 _Tint;
|
||||
half4 _Tint2;
|
||||
|
||||
half _Exposure;
|
||||
float _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;
|
||||
float3 normal : NORMAL;
|
||||
|
||||
UNITY_VERTEX_INPUT_INSTANCE_ID
|
||||
};
|
||||
|
||||
struct v2f {
|
||||
float4 vertex : SV_POSITION;
|
||||
float3 texcoord : TEXCOORD0;
|
||||
|
||||
UNITY_VERTEX_OUTPUT_STEREO
|
||||
};
|
||||
|
||||
v2f vert (appdata_t v)
|
||||
{
|
||||
v2f o;
|
||||
|
||||
UNITY_SETUP_INSTANCE_ID(v);
|
||||
UNITY_INITIALIZE_OUTPUT(v2f, o);
|
||||
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
|
||||
|
||||
o.vertex = UnityObjectToClipPos(RotateAroundYInDegrees(v.vertex, _Rotation));
|
||||
o.texcoord = v.vertex;
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
fixed4 frag (v2f i) : SV_Target
|
||||
{
|
||||
float4 env1 = texCUBE (_Tex, i.texcoord);
|
||||
float4 env2 = texCUBE (_Tex2, i.texcoord);
|
||||
float4 env = lerp( env1, env2, _BlendCubemaps );
|
||||
|
||||
float4 tint = lerp( _Tint, _Tint2, _BlendCubemaps );
|
||||
half3 c = DecodeHDR (env, _Tex_HDR);
|
||||
|
||||
c = c * tint.rgb * unity_ColorSpaceDouble;
|
||||
c *= _Exposure;
|
||||
|
||||
return half4(c, 1);
|
||||
}
|
||||
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
Fallback Off
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 06e8da9b2b26f09459e8a93e84b2e0c9
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
755
Assets/Skybox Blender/Script/SkyboxBlender.cs
Normal file
755
Assets/Skybox Blender/Script/SkyboxBlender.cs
Normal file
@@ -0,0 +1,755 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
[AddComponentMenu("Skybox Blender/Skybox Blender")]
|
||||
public class SkyboxBlender : MonoBehaviour
|
||||
{
|
||||
[Tooltip("The materials you want to blend to linearly.")]
|
||||
public List<Material> skyboxMaterials;
|
||||
[Tooltip("Checking this will instantly make the first material your current skybox.")]
|
||||
public bool makeFirstMaterialSkybox;
|
||||
[Min(0), Tooltip("The speed of the blending between the skyboxes.")]
|
||||
public float blendSpeed = 0.5f;
|
||||
[Min(0), Tooltip("The time to wait before blending the next skybox material.")]
|
||||
public float timeToWait = 0f;
|
||||
[Tooltip("If enabled, will loop the materials list. When the blender reaches the last skybox in the list, it'll blend back to the first one.")]
|
||||
public bool loop = true;
|
||||
|
||||
[Tooltip("If enabled, the lighting of the world will be updated to that of the skyboxes blending.")]
|
||||
public bool updateLighting;
|
||||
[Tooltip("If enabled, the reflections of the world will be updated to that of the skyboxes blending.")]
|
||||
public bool updateReflections;
|
||||
[Range(1, 30), Tooltip("Set how many frames need to pass during blend before updating the reflections & lighting each time. Updating these take a toll on performance so the higher this number is, the more performant your game will be (during blend) but the less accurate the lighting & reflections update will be. The less this number is, the slower the game will be but the accuracy increases. By average the best performance/accuracy results is setting it between 5-10.")]
|
||||
public int updateEveryFrames = 5;
|
||||
|
||||
|
||||
[Tooltip("Keep rotating the skybox infinetly while blending.")]
|
||||
public bool keepRotating;
|
||||
[Tooltip("if you would prefer a certain degree to rotate the skybox to during blending - 360 is a full turn.")]
|
||||
public float rotateToAngle = 180;
|
||||
[Min(0), Tooltip("The speed of the skybox rotation.")]
|
||||
public float rotationSpeed;
|
||||
[Tooltip("If enabled, the rotation will stop when the blend finishes. If disabled, even after blending the skybox will continue rotating. TAKE NOTE: if loop is enabled in blend options. This will not take effect.")]
|
||||
public bool stopRotationOnBlendFinish;
|
||||
|
||||
|
||||
#region SYSTEM VARIABLES
|
||||
|
||||
Material defaultSkyboxMaterial;
|
||||
Material skyboxBlenderMaterial;
|
||||
Texture currentTexture;
|
||||
|
||||
float totalBlendValue = 1f;
|
||||
public float blendValue { private set; get; }
|
||||
float defaultBlend;
|
||||
float defaultRotation;
|
||||
float rotationSpeedValue;
|
||||
|
||||
int index = 0;
|
||||
public int CurrentIndex {
|
||||
get { return index; }
|
||||
}
|
||||
|
||||
int indexToBlend;
|
||||
int usedBlend;
|
||||
int LightAndReflectionFrames;
|
||||
|
||||
bool linearBlending;
|
||||
bool currentSkyboxNotFirstMaterialBlending;
|
||||
bool comingFromLoop;
|
||||
bool rotateSkybox;
|
||||
bool oneTickBlend = false;
|
||||
bool stillRunning = false;
|
||||
bool singleBlend = false;
|
||||
bool stopped = false;
|
||||
bool blendByIndex;
|
||||
bool stopRotation;
|
||||
bool blendFinished;
|
||||
bool blendingCurrentSkyToListNotSingleBlend;
|
||||
bool isLinearBlend;
|
||||
bool isSetReflectionProbeOnStart;
|
||||
|
||||
ReflectionProbe reflectionProbe;
|
||||
Cubemap cubemap = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region UNITY METHODS
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// load the material
|
||||
skyboxBlenderMaterial = Resources.Load("Material & Shader/Skybox Blend Material", typeof(Material)) as Material;
|
||||
|
||||
if (skyboxBlenderMaterial)
|
||||
{
|
||||
defaultBlend = skyboxBlenderMaterial.GetFloat("_BlendCubemaps");
|
||||
defaultRotation = skyboxBlenderMaterial.GetFloat("_Rotation");
|
||||
defaultSkyboxMaterial = skyboxBlenderMaterial;
|
||||
InspectorAndAwakeChanges();
|
||||
}
|
||||
else {
|
||||
Debug.LogWarning("Can't find Skybox Blend Material in resources. Please re-import!");
|
||||
}
|
||||
|
||||
if (updateLighting || updateReflections)
|
||||
{
|
||||
SetReflectionProbe();
|
||||
UpdateLightingAndReflections(true);
|
||||
}
|
||||
}
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
if (skyboxBlenderMaterial == null) {
|
||||
skyboxBlenderMaterial = Resources.Load("Material & Shader/Skybox Blend Material", typeof(Material)) as Material;
|
||||
}
|
||||
|
||||
InspectorAndAwakeChanges();
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
// return the material to original blend
|
||||
skyboxBlenderMaterial.SetFloat("_BlendCubemaps", defaultBlend);
|
||||
skyboxBlenderMaterial.SetFloat("_Rotation", defaultRotation);
|
||||
|
||||
if (currentTexture != null) {
|
||||
skyboxBlenderMaterial.SetTexture("_Tex", currentTexture);
|
||||
}
|
||||
|
||||
RenderSettings.skybox = defaultSkyboxMaterial;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// when in editor mode, set the skybox material in the skybox linearBlendmaterial
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
if (RenderSettings.skybox == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (RenderSettings.skybox.HasProperty("_Tex"))
|
||||
{
|
||||
skyboxBlenderMaterial.SetTexture("_Tex", RenderSettings.skybox.GetTexture("_Tex"));
|
||||
skyboxBlenderMaterial.SetColor("_Tint", RenderSettings.skybox.GetColor("_Tint"));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (updateReflections && !isSetReflectionProbeOnStart)
|
||||
{
|
||||
if (!SetReflectionProbeTexture()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// skybox blending linearly the list
|
||||
if (linearBlending && !stopped)
|
||||
{
|
||||
// set the type of the used blending
|
||||
usedBlend = 1;
|
||||
blendValue += Time.deltaTime * blendSpeed;
|
||||
skyboxBlenderMaterial.SetFloat("_BlendCubemaps", blendValue);
|
||||
UpdateLightingAndReflections();
|
||||
|
||||
if (skyboxBlenderMaterial.GetFloat("_BlendCubemaps") >= totalBlendValue)
|
||||
{
|
||||
blendFinished = true;
|
||||
linearBlending = false;
|
||||
blendValue = 0f;
|
||||
StopAllCoroutines();
|
||||
|
||||
skyboxBlenderMaterial.SetFloat("_BlendCubemaps", 0f);
|
||||
|
||||
SetSkyBoxes(true, index, false, 0, true);
|
||||
UpdateLightingAndReflections(true);
|
||||
|
||||
if (comingFromLoop)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
// increment index and linearBlend if not reached end
|
||||
if ((index + 1) < skyboxMaterials.Count)
|
||||
{
|
||||
|
||||
if (!comingFromLoop) {
|
||||
index++;
|
||||
}
|
||||
|
||||
comingFromLoop = false;
|
||||
SetSkyBoxes(true, index);
|
||||
|
||||
if (index + 1 < skyboxMaterials.Count) {
|
||||
SetSkyBoxes(false, 0, true, index+1);
|
||||
}
|
||||
|
||||
if (index - (skyboxMaterials.Count - 1) > 0) {
|
||||
if (!oneTickBlend) linearBlending = true;
|
||||
}
|
||||
else {
|
||||
if (!oneTickBlend) StartCoroutine(WaitBeforeBlending());
|
||||
}
|
||||
}
|
||||
|
||||
// if reached end and loopable
|
||||
if (index >= skyboxMaterials.Count-1)
|
||||
{
|
||||
if (loop)
|
||||
{
|
||||
if (oneTickBlend)
|
||||
{
|
||||
stillRunning = false;
|
||||
return;
|
||||
}
|
||||
|
||||
SetSkyBoxes(true, index, true, 0, true);
|
||||
comingFromLoop = true;
|
||||
|
||||
StartCoroutine(WaitBeforeBlending());
|
||||
}
|
||||
else {
|
||||
stillRunning = false;
|
||||
if (stopRotationOnBlendFinish) {
|
||||
StopRotation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
blendFinished = false;
|
||||
}
|
||||
}
|
||||
|
||||
// single blending
|
||||
if (singleBlend && !stopped)
|
||||
{
|
||||
blendValue += Time.deltaTime * blendSpeed;
|
||||
skyboxBlenderMaterial.SetFloat("_BlendCubemaps", blendValue);
|
||||
UpdateLightingAndReflections();
|
||||
|
||||
if (skyboxBlenderMaterial.GetFloat("_BlendCubemaps") >= totalBlendValue)
|
||||
{
|
||||
blendFinished = true;
|
||||
singleBlend = false;
|
||||
blendValue = 0f;
|
||||
StopAllCoroutines();
|
||||
|
||||
if (blendByIndex)
|
||||
{
|
||||
index = indexToBlend;
|
||||
blendByIndex = false;
|
||||
}
|
||||
else {
|
||||
if (index + 1 < skyboxMaterials.Count) index++;
|
||||
else index = 0;
|
||||
}
|
||||
|
||||
skyboxBlenderMaterial.SetFloat("_BlendCubemaps", 0f);
|
||||
|
||||
SetSkyBoxes(true, index, false, 0, true);
|
||||
UpdateLightingAndReflections(true);
|
||||
|
||||
stillRunning = false;
|
||||
|
||||
if (stopRotationOnBlendFinish) {
|
||||
StopRotation();
|
||||
}
|
||||
}
|
||||
else {
|
||||
blendFinished = false;
|
||||
}
|
||||
}
|
||||
|
||||
// blending for if the current skybox is not the same as first material
|
||||
if (currentSkyboxNotFirstMaterialBlending && !stopped)
|
||||
{
|
||||
// set the type of the used blending
|
||||
usedBlend = 2;
|
||||
|
||||
blendValue += Time.deltaTime * blendSpeed;
|
||||
skyboxBlenderMaterial.SetFloat("_BlendCubemaps", blendValue);
|
||||
UpdateLightingAndReflections();
|
||||
|
||||
if (skyboxBlenderMaterial.GetFloat("_BlendCubemaps") >= totalBlendValue)
|
||||
{
|
||||
blendFinished = true;
|
||||
currentSkyboxNotFirstMaterialBlending = false;
|
||||
blendValue = 0f;
|
||||
StopAllCoroutines();
|
||||
|
||||
int indexForFirst = 0;
|
||||
int indexForSecond = 1;
|
||||
|
||||
skyboxBlenderMaterial.SetFloat("_BlendCubemaps", 0f);
|
||||
|
||||
if (skyboxMaterials.Count == 1) {
|
||||
indexForSecond = 0;
|
||||
}
|
||||
|
||||
SetSkyBoxes(true, 0, true, indexForSecond, true);
|
||||
UpdateLightingAndReflections(true);
|
||||
|
||||
if (oneTickBlend) {
|
||||
stillRunning = false;
|
||||
}
|
||||
else {
|
||||
StartCoroutine(WaitBeforeBlending());
|
||||
}
|
||||
|
||||
if (stopRotationOnBlendFinish && !blendingCurrentSkyToListNotSingleBlend) {
|
||||
StopRotation();
|
||||
}
|
||||
|
||||
blendingCurrentSkyToListNotSingleBlend = false;
|
||||
}
|
||||
else {
|
||||
blendFinished = false;
|
||||
}
|
||||
}
|
||||
|
||||
// skybox rotation
|
||||
if (rotateSkybox && !stopRotation)
|
||||
{
|
||||
rotationSpeedValue += Time.deltaTime * rotationSpeed;
|
||||
|
||||
if (keepRotating) {
|
||||
skyboxBlenderMaterial.SetFloat("_Rotation", rotationSpeedValue);
|
||||
}
|
||||
else {
|
||||
if (skyboxBlenderMaterial.GetFloat("_Rotation") < rotateToAngle) {
|
||||
skyboxBlenderMaterial.SetFloat("_Rotation", rotationSpeedValue);
|
||||
}
|
||||
else {
|
||||
rotateSkybox = false;
|
||||
skyboxBlenderMaterial.SetFloat("_Rotation", rotateToAngle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SYSTEM METHODS
|
||||
|
||||
// set the skybox material
|
||||
void SetSkyBoxes(bool firstTex = false, int firstTexIndex = 0, bool secondTex = false, int secondTexIndex = 0, bool apply = false)
|
||||
{
|
||||
if (firstTex)
|
||||
{
|
||||
skyboxBlenderMaterial.SetTexture("_Tex", skyboxMaterials[firstTexIndex].GetTexture("_Tex"));
|
||||
skyboxBlenderMaterial.SetColor("_Tint", skyboxMaterials[firstTexIndex].GetColor("_Tint"));
|
||||
}
|
||||
|
||||
if (secondTex)
|
||||
{
|
||||
skyboxBlenderMaterial.SetTexture("_Tex2", skyboxMaterials[secondTexIndex].GetTexture("_Tex"));
|
||||
skyboxBlenderMaterial.SetColor("_Tint2", skyboxMaterials[secondTexIndex].GetColor("_Tint"));
|
||||
}
|
||||
|
||||
if (apply) {
|
||||
RenderSettings.skybox = skyboxBlenderMaterial;
|
||||
}
|
||||
}
|
||||
|
||||
// setup the skybox material before blending
|
||||
void PrepareMaterialForBlend(int skyboxIndex)
|
||||
{
|
||||
// set texture
|
||||
skyboxBlenderMaterial.SetTexture("_Tex", RenderSettings.skybox.GetTexture("_Tex"));
|
||||
skyboxBlenderMaterial.SetTexture("_Tex2", skyboxMaterials[skyboxIndex].GetTexture("_Tex"));
|
||||
|
||||
// set tint
|
||||
skyboxBlenderMaterial.SetColor("_Tint", RenderSettings.skybox.GetColor("_Tint"));
|
||||
skyboxBlenderMaterial.SetColor("_Tint2", skyboxMaterials[skyboxIndex].GetColor("_Tint"));
|
||||
}
|
||||
|
||||
// wait for time for normal blending
|
||||
IEnumerator WaitBeforeBlending()
|
||||
{
|
||||
isLinearBlend = true;
|
||||
yield return new WaitForSeconds(timeToWait);
|
||||
linearBlending = true;
|
||||
}
|
||||
|
||||
// change skyboxes and material textures on inspector change and script awake
|
||||
public void InspectorAndAwakeChanges()
|
||||
{
|
||||
if (makeFirstMaterialSkybox)
|
||||
{
|
||||
if (skyboxMaterials.Count >= 1)
|
||||
{
|
||||
if (skyboxMaterials[0] != null)
|
||||
{
|
||||
skyboxBlenderMaterial.SetTexture("_Tex", skyboxMaterials[0].GetTexture("_Tex"));
|
||||
skyboxBlenderMaterial.SetColor("_Tint", skyboxMaterials[0].GetColor("_Tint"));
|
||||
RenderSettings.skybox = skyboxBlenderMaterial;
|
||||
Debug.Log("Skybox set to the first material in the list: " + skyboxMaterials[0].name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Debug.LogWarning("You need to set a material first to make it the skybox");
|
||||
}
|
||||
}
|
||||
|
||||
if (skyboxMaterials != null)
|
||||
{
|
||||
if (skyboxMaterials.Count > 1)
|
||||
{
|
||||
if (skyboxMaterials[1] != null) {
|
||||
skyboxBlenderMaterial.SetTexture("_Tex2", skyboxMaterials[1].GetTexture("_Tex"));
|
||||
skyboxBlenderMaterial.SetColor("_Tint2", skyboxMaterials[1].GetColor("_Tint"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// set reflection probe for reflections
|
||||
void SetReflectionProbe()
|
||||
{
|
||||
reflectionProbe = GetComponent<ReflectionProbe>();
|
||||
|
||||
if (reflectionProbe == null) {
|
||||
reflectionProbe = gameObject.AddComponent<ReflectionProbe>() as ReflectionProbe;
|
||||
}
|
||||
|
||||
reflectionProbe.cullingMask = 0;
|
||||
reflectionProbe.refreshMode = ReflectionProbeRefreshMode.ViaScripting;
|
||||
reflectionProbe.mode = ReflectionProbeMode.Realtime;
|
||||
reflectionProbe.timeSlicingMode = ReflectionProbeTimeSlicingMode.NoTimeSlicing;
|
||||
|
||||
if (updateReflections)
|
||||
{
|
||||
RenderSettings.defaultReflectionMode = DefaultReflectionMode.Custom;
|
||||
cubemap = new Cubemap(reflectionProbe.resolution, reflectionProbe.hdr ? TextureFormat.RGBAHalf : TextureFormat.RGBA32, true);
|
||||
}
|
||||
}
|
||||
|
||||
// update the lighting and reflections of the world
|
||||
void UpdateLightingAndReflections(bool forceUpdate=false)
|
||||
{
|
||||
// exit if both options are off
|
||||
if (!updateReflections && !updateLighting)
|
||||
{
|
||||
LightAndReflectionFrames = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!forceUpdate)
|
||||
{
|
||||
// run every set frames for performance
|
||||
if (LightAndReflectionFrames < updateEveryFrames) {
|
||||
LightAndReflectionFrames++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// update the lighting if set
|
||||
if (updateLighting) {
|
||||
DynamicGI.UpdateEnvironment();
|
||||
}
|
||||
|
||||
// if update reflections is off then exit the function
|
||||
if (!updateReflections) {
|
||||
return;
|
||||
}
|
||||
|
||||
LightAndReflectionFrames = 0;
|
||||
reflectionProbe.RenderProbe();
|
||||
|
||||
if (reflectionProbe.texture != null)
|
||||
{
|
||||
Graphics.CopyTexture(reflectionProbe.texture, cubemap as Texture);
|
||||
RenderSettings.customReflection = cubemap;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool SetReflectionProbeTexture()
|
||||
{
|
||||
if (!isSetReflectionProbeOnStart)
|
||||
{
|
||||
SetReflectionProbe();
|
||||
UpdateLightingAndReflections(true);
|
||||
|
||||
if (reflectionProbe.texture != null) {
|
||||
isSetReflectionProbeOnStart = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isSetReflectionProbeOnStart;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region PUBLIC APIs
|
||||
|
||||
// trigger the skybox blend
|
||||
public void Blend(bool singlePassBlend = false, bool rotate = true)
|
||||
{
|
||||
if (currentSkyboxNotFirstMaterialBlending && !stopped) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (isLinearBlend && !stopped) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (rotate) {
|
||||
rotateSkybox = true;
|
||||
stopRotation = false;
|
||||
}
|
||||
|
||||
|
||||
if ((stopped || stillRunning) && !singlePassBlend) {
|
||||
stopped = false;
|
||||
|
||||
if (blendFinished) {
|
||||
if ((usedBlend == 1 || usedBlend == 2) && !stillRunning) {
|
||||
StartCoroutine(WaitBeforeBlending());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stopped = false;
|
||||
blendByIndex = false;
|
||||
|
||||
StopAllCoroutines();
|
||||
currentTexture = RenderSettings.skybox.GetTexture("_Tex");
|
||||
|
||||
|
||||
if (blendValue > 0) {
|
||||
oneTickBlend = singlePassBlend;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (singlePassBlend) {
|
||||
if (index == 0 && currentTexture != skyboxMaterials[0].GetTexture("_Tex")) {
|
||||
PrepareMaterialForBlend(0);
|
||||
currentSkyboxNotFirstMaterialBlending = true;
|
||||
}
|
||||
else {
|
||||
int indexToTransition = index;
|
||||
|
||||
if (!stopped) {
|
||||
if (index >= skyboxMaterials.Count - 1) {
|
||||
indexToTransition = 0;
|
||||
}
|
||||
else {
|
||||
indexToTransition++;
|
||||
}
|
||||
}
|
||||
|
||||
PrepareMaterialForBlend(indexToTransition);
|
||||
singleBlend = true;
|
||||
}
|
||||
|
||||
RenderSettings.skybox = skyboxBlenderMaterial;
|
||||
stillRunning = true;
|
||||
}
|
||||
else {
|
||||
// if only one element then linear blend from current scene skybox to the first material
|
||||
if (skyboxMaterials.Count == 1) {
|
||||
if (currentTexture != skyboxMaterials[0].GetTexture("_Tex")) {
|
||||
PrepareMaterialForBlend(0);
|
||||
RenderSettings.skybox = skyboxBlenderMaterial;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (index == 0 && skyboxMaterials[0] != null) {
|
||||
if (currentTexture == skyboxMaterials[0].GetTexture("_Tex")) {
|
||||
SetSkyBoxes(true, 0, false, 0, true);
|
||||
}
|
||||
else {
|
||||
SetSkyBoxes(false, 0, true, 0, true);
|
||||
|
||||
currentSkyboxNotFirstMaterialBlending = true;
|
||||
blendingCurrentSkyToListNotSingleBlend = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// is this the last material
|
||||
if (index >= skyboxMaterials.Count - 1) {
|
||||
comingFromLoop = true;
|
||||
}
|
||||
|
||||
|
||||
// if the rotate parameter passed
|
||||
if (rotate) {
|
||||
rotateSkybox = true;
|
||||
stopRotation = false;
|
||||
}
|
||||
|
||||
|
||||
// flag some vars to start the blending in Update
|
||||
if (!currentSkyboxNotFirstMaterialBlending) {
|
||||
linearBlending = true;
|
||||
stillRunning = true;
|
||||
|
||||
if (rotate) rotateSkybox = true;
|
||||
}
|
||||
|
||||
|
||||
isLinearBlend = true;
|
||||
}
|
||||
|
||||
|
||||
oneTickBlend = singlePassBlend;
|
||||
}
|
||||
|
||||
// call using material index
|
||||
public void Blend(int skyboxIndex, bool rotate = true)
|
||||
{
|
||||
stopped = false;
|
||||
|
||||
if (stillRunning) return;
|
||||
|
||||
|
||||
if (index == skyboxIndex) {
|
||||
Debug.Log("Skybox material already set on the one you're trying to call.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (skyboxIndex > skyboxMaterials.Count - 1) {
|
||||
Debug.Log("The passed index is bigger than the Count of the skybox materials list.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (skyboxIndex < 0) {
|
||||
skyboxIndex = skyboxMaterials.Count - 1;
|
||||
}
|
||||
|
||||
|
||||
if (skyboxMaterials[skyboxIndex] == null) {
|
||||
Debug.Log("There is no material in the list with the passed index.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
StopAllCoroutines();
|
||||
currentTexture = RenderSettings.skybox.GetTexture("_Tex");
|
||||
|
||||
|
||||
blendByIndex = true;
|
||||
indexToBlend = skyboxIndex;
|
||||
|
||||
|
||||
PrepareMaterialForBlend(skyboxIndex);
|
||||
singleBlend = true;
|
||||
|
||||
|
||||
RenderSettings.skybox = skyboxBlenderMaterial;
|
||||
|
||||
|
||||
if (rotate) {
|
||||
rotateSkybox = true;
|
||||
stopRotation = false;
|
||||
}
|
||||
|
||||
|
||||
stillRunning = true;
|
||||
oneTickBlend = true;
|
||||
}
|
||||
|
||||
// cancel the current blend and reset the skybox to what it was before the blend
|
||||
public void Cancel()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
|
||||
|
||||
linearBlending = false;
|
||||
singleBlend = false;
|
||||
currentSkyboxNotFirstMaterialBlending = false;
|
||||
blendingCurrentSkyToListNotSingleBlend = false;
|
||||
oneTickBlend = false;
|
||||
stopped = false;
|
||||
blendValue = 0;
|
||||
stillRunning = false;
|
||||
isLinearBlend = false;
|
||||
comingFromLoop = false;
|
||||
|
||||
|
||||
skyboxBlenderMaterial.SetFloat("_BlendCubemaps", 0f);
|
||||
|
||||
|
||||
SetSkyBoxes(true, index, false, 0, true);
|
||||
UpdateLightingAndReflections(true);
|
||||
}
|
||||
|
||||
// stop the blending
|
||||
public void Stop(bool stopRot=true)
|
||||
{
|
||||
stopped = true;
|
||||
StopAllCoroutines();
|
||||
|
||||
if (stopRot && rotateSkybox) {
|
||||
stopRotation = true;
|
||||
}
|
||||
}
|
||||
|
||||
// resume the blending
|
||||
public void Resume(bool resumeRot=true)
|
||||
{
|
||||
stopped = false;
|
||||
|
||||
if (resumeRot) {
|
||||
stopRotation = false;
|
||||
}
|
||||
|
||||
if (usedBlend == 1 || usedBlend == 2) {
|
||||
if (blendFinished) {
|
||||
StartCoroutine(WaitBeforeBlending());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check if blending is in process
|
||||
public bool IsBlending()
|
||||
{
|
||||
if (stopped) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (linearBlending|| singleBlend || currentSkyboxNotFirstMaterialBlending) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// rotate the skybox only
|
||||
public void Rotate()
|
||||
{
|
||||
skyboxBlenderMaterial.SetTexture("_Tex", RenderSettings.skybox.GetTexture("_Tex"));
|
||||
skyboxBlenderMaterial.SetColor("_Tint", RenderSettings.skybox.GetColor("_Tint"));
|
||||
RenderSettings.skybox = skyboxBlenderMaterial;
|
||||
|
||||
rotateSkybox = true;
|
||||
stopRotation = false;
|
||||
}
|
||||
|
||||
// stop the rotation only
|
||||
public void StopRotation()
|
||||
{
|
||||
rotateSkybox = false;
|
||||
stopRotation = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
11
Assets/Skybox Blender/Script/SkyboxBlender.cs.meta
Normal file
11
Assets/Skybox Blender/Script/SkyboxBlender.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70633c70e529b824d9168cd2abfc1a5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user