initial commit
This commit is contained in:
parent
6715289efe
commit
788c3389af
37645 changed files with 2526849 additions and 80 deletions
|
|
@ -0,0 +1,210 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
[ExecuteInEditMode]
|
||||
public class BakedVolumetricArea : MonoBehaviour
|
||||
{
|
||||
[SerializeField, Tooltip("Texel density per meter. Controls the resolution of the baked texture.")] public float TexelDensity = 5;
|
||||
// [SerializeField, Tooltip("Texel density ratio scaler. Multiples Texel density per dimension")] Vector3 TexelRatio = new Vector3(1,1,1);
|
||||
[SerializeField] public Vector3 BoxScale = new Vector3(10,5,10);
|
||||
[SerializeField] public Texture3D bakedTexture;
|
||||
[SerializeField] public Vector3Int NormalizedTexelDensity; //exposed to see target resolution
|
||||
[HideInInspector,SerializeField] public Vector3 NormalizedScale;
|
||||
[HideInInspector,SerializeField] public Vector3 Corner;
|
||||
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
VolumetricRegisters.RegisterVolumetricArea(this);
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
VolumetricRegisters.UnregisterVolumetricArea(this);
|
||||
#if UNITY_EDITOR
|
||||
DisableDebugMesh();
|
||||
#endif
|
||||
}
|
||||
private void OnDestroy()
|
||||
{
|
||||
VolumetricRegisters.UnregisterVolumetricArea(this);
|
||||
#if UNITY_EDITOR
|
||||
DisableDebugMesh();
|
||||
#endif
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
|
||||
//Check to see if the same and change if different
|
||||
|
||||
Vector3 tempscale = Vector3.Scale(gameObject.transform.localScale, BoxScale);
|
||||
if (NormalizedScale != tempscale)
|
||||
NormalizedScale = tempscale;
|
||||
|
||||
Vector3 UnclampedResolution = Vector3.Scale(NormalizedScale, new Vector3(TexelDensity, TexelDensity, TexelDensity)) ;
|
||||
|
||||
float Maxres = Mathf.Max(UnclampedResolution.x, UnclampedResolution.y, UnclampedResolution.z);
|
||||
|
||||
float rescaler = Mathf.Min(1, 4096 / Maxres);
|
||||
|
||||
// Debug.Log("Clamped resolution to " + Mathf.RoundToInt(UnclampedResolution.x * rescaler));
|
||||
|
||||
Vector3Int tempTexelDensity = new Vector3Int
|
||||
{
|
||||
x = ((Mathf.RoundToInt(UnclampedResolution.x * rescaler) + 3) / 4 ) * 4,
|
||||
y = ((Mathf.RoundToInt(UnclampedResolution.y * rescaler) + 3) / 4 ) * 4,
|
||||
z = ((Mathf.RoundToInt(UnclampedResolution.z * rescaler) + 3) / 4 ) * 4,
|
||||
};
|
||||
|
||||
//if (tempTexelDensity.x > 8096 || tempTexelDensity.y > 8096 || tempTexelDensity.z > 8096)
|
||||
//{
|
||||
//}
|
||||
|
||||
// if (NormalizedTexelDensity != tempTexelDensity)
|
||||
NormalizedTexelDensity = tempTexelDensity;
|
||||
|
||||
Corner = transform.position - (NormalizedScale * 0.5f);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (UnityEditor.EditorApplication.isPlaying) DisableDebugMesh();
|
||||
}
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
//if (!UnityEditor.Selection.Contains(gameObject)) DisableDebugMesh();
|
||||
Gizmos.color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
|
||||
Gizmos.matrix = Matrix4x4.TRS(gameObject.transform.position, Quaternion.identity, NormalizedScale);
|
||||
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
|
||||
//Gizmos.DrawWireCube(new Vector3(
|
||||
// (0.0f /TexelDensity.x) - (TexelDensity.x * 0.5f),
|
||||
// (0.0f /TexelDensity.y) - (TexelDensity.y * 0.5f),
|
||||
// (0.0f /TexelDensity.z) - (TexelDensity.x * 0.5f)) , new Vector3(1 / TexelDensity.x, 1 / TexelDensity.y, 1 / TexelDensity.z) );
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
/*
|
||||
if (DEBUG)
|
||||
{
|
||||
if (UnityEditor.Selection.Contains(gameObject))
|
||||
{
|
||||
EnableDebugMesh();
|
||||
}
|
||||
else
|
||||
{
|
||||
DisableDebugMesh();
|
||||
}
|
||||
}
|
||||
else if (DebugCube != null)
|
||||
{
|
||||
DisableDebugMesh();
|
||||
}
|
||||
*/
|
||||
OnValidate();
|
||||
// Gizmos.DrawWireSphere(transform.position - (NormalizedScale* 0.5f), .5f);
|
||||
Gizmos.color = new Color(0.5f,0.5f,0.5f,0.25f);
|
||||
Gizmos.matrix = Matrix4x4.TRS(gameObject.transform.position, Quaternion.identity, NormalizedScale);
|
||||
if (!DEBUG) Gizmos.DrawCube(Vector3.zero, Vector3.one);
|
||||
Gizmos.color = Color.white;
|
||||
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
|
||||
// Gizmos.DrawWireSphere(Corner, 0.5f);
|
||||
|
||||
}
|
||||
|
||||
[HideInInspector] public bool DEBUG;
|
||||
[HideInInspector] public static bool VisStateGlobal;
|
||||
[SerializeField,HideInInspector] GameObject DebugCube;
|
||||
[SerializeField,HideInInspector] Material mat;
|
||||
[HideInInspector,SerializeField] public bool MarkDebugCubeForDelete;
|
||||
private bool VisStateLocal;
|
||||
|
||||
public void EnableDebugMesh()
|
||||
{
|
||||
if (bakedTexture == null || DebugCube != null || UnityEditor.EditorApplication.isPlaying) return;
|
||||
|
||||
MarkDebugCubeForDelete = false;
|
||||
DebugCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
DebugCube.transform.parent = gameObject.transform;
|
||||
DebugCube.transform.localPosition = Vector3.zero;
|
||||
DebugCube.transform.localScale = BoxScale;
|
||||
DebugCube.transform.rotation = Quaternion.identity;
|
||||
DestroyImmediate(DebugCube.GetComponent<Collider>());
|
||||
|
||||
Renderer rnd = DebugCube.GetComponent<Renderer>();
|
||||
mat = new Material(Shader.Find("hidden/VolumetricPreview"));
|
||||
|
||||
mat.SetTexture("_Volume", bakedTexture);
|
||||
mat.renderQueue = 3000;
|
||||
|
||||
rnd.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
|
||||
rnd.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
|
||||
rnd.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
||||
|
||||
rnd.material = mat;
|
||||
DebugCube.hideFlags = HideFlags.HideAndDontSave;
|
||||
|
||||
}
|
||||
public void DisableDebugMesh()
|
||||
{
|
||||
if (DebugCube == null) return;
|
||||
MarkDebugCubeForDelete = true;
|
||||
//DestroyImmediate(DebugCube);
|
||||
//DestroyImmediate(mat);
|
||||
}
|
||||
|
||||
public void RefreshDebugMesh()
|
||||
{
|
||||
DisableDebugMesh();
|
||||
EnableDebugMesh();
|
||||
}
|
||||
[ExecuteInEditMode]
|
||||
void LateUpdate()
|
||||
{
|
||||
if (VisStateLocal != VisStateGlobal)
|
||||
{
|
||||
VisStateLocal = VisStateGlobal;
|
||||
if (VisStateGlobal)
|
||||
{
|
||||
EnableDebugMesh();
|
||||
}
|
||||
else
|
||||
{
|
||||
DisableDebugMesh();
|
||||
}
|
||||
}
|
||||
|
||||
if (MarkDebugCubeForDelete && DebugCube != null)
|
||||
{
|
||||
MarkDebugCubeForDelete = false;
|
||||
DestroyImmediate(DebugCube);
|
||||
DestroyImmediate(mat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[MenuItem("GameObject/Light/Baked Volumetric Area", false, 10)]
|
||||
static void CreateBakedVolumetricArea(MenuCommand menuCommand)
|
||||
{
|
||||
// Create a custom game object
|
||||
GameObject go = new GameObject("Baked Volumetric Area");
|
||||
// Ensure it gets reparented if this was a context click (otherwise does nothing)
|
||||
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
|
||||
// Register the creation in the undo system
|
||||
go.AddComponent<BakedVolumetricArea>();
|
||||
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
|
||||
Selection.activeObject = go;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6566dd1b35dd0c242a32526b05cdefc6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: dcd6cec420dd1a94a94af7ac629e9ec0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!114 &11400000
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 12df6f25bd2bedf478f47d04069582bc, type: 3}
|
||||
m_Name: PlaceholderVolumetricSettings
|
||||
m_EditorClassIdentifier:
|
||||
near: 0.01
|
||||
far: 80
|
||||
FroxelWidthResolution: 24
|
||||
FroxelHeightResolution: 24
|
||||
FroxelDepthResolution: 12
|
||||
ClipMapResolution: 64
|
||||
ClipmapScale: 20
|
||||
ClipmapScale2: 200
|
||||
ClipmapResampleThreshold: 3
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9aa770ca8d706d24aa3f993a2394fd83
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,166 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
|
||||
[InitializeOnLoad]
|
||||
#endif
|
||||
public static class SkyManager
|
||||
{
|
||||
public static Texture skytexture;
|
||||
|
||||
static readonly int ID_SkyTexture = Shader.PropertyToID("_SkyTexture");
|
||||
static readonly int ID_SkyMipCount = Shader.PropertyToID("_SkyMipCount");
|
||||
static readonly int ID_MipFogParam = Shader.PropertyToID("_MipFogParameters");
|
||||
|
||||
static SkyManager()
|
||||
{
|
||||
SetSkyMips(new Vector4(0, 1, 1, 0));
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.delayCall += DelayedCheckSky; //Delaying first call when loaded
|
||||
EditorSceneManager.sceneOpened -= SceneOpenedCallback;
|
||||
EditorSceneManager.sceneOpened += SceneOpenedCallback;
|
||||
#endif
|
||||
//Double checking that this doesn't exist. We purposely don't unregister it because we need it constantly called whenever there's a change.
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
}
|
||||
|
||||
private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
CheckSky();
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
static void SceneOpenedCallback(Scene scene, OpenSceneMode mode)
|
||||
{
|
||||
Debug.Log(mode + " : " +scene);
|
||||
if (!EditorApplication.isUpdating && !EditorApplication.isPlayingOrWillChangePlaymode)
|
||||
{
|
||||
CheckSky();
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorApplication.delayCall += DelayedCheckSky;
|
||||
}
|
||||
}
|
||||
|
||||
static void DelayedCheckSky()
|
||||
{
|
||||
CheckSky();
|
||||
EditorApplication.delayCall -= DelayedCheckSky;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static void GenerateSkyTexture()
|
||||
{
|
||||
//Generate Skybox
|
||||
RenderTexture cubetex = new RenderTexture(256, 256, 1, RenderTextureFormat.DefaultHDR);
|
||||
cubetex.enableRandomWrite = true;
|
||||
cubetex.useMipMap = true;
|
||||
cubetex.dimension = UnityEngine.Rendering.TextureDimension.Cube;
|
||||
cubetex.autoGenerateMips = true;
|
||||
cubetex.name = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name + "_MipSky";
|
||||
//cubetex.
|
||||
// cubetex.Create();
|
||||
|
||||
Camera renderCam = new GameObject().AddComponent<Camera>();
|
||||
renderCam.gameObject.hideFlags = HideFlags.DontSave;
|
||||
renderCam.enabled = false;
|
||||
renderCam.cullingMask = 0;
|
||||
renderCam.backgroundColor = Color.black;
|
||||
renderCam.clearFlags = CameraClearFlags.Skybox;
|
||||
renderCam.RenderToCubemap(cubetex);
|
||||
//cubetex.GenerateMips();
|
||||
#if UNITY_EDITOR
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
Object.Destroy(renderCam.gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
Object.DestroyImmediate(renderCam.gameObject);
|
||||
}
|
||||
#else
|
||||
Object.Destroy(renderCam.gameObject);
|
||||
#endif
|
||||
skytexture = cubetex;
|
||||
Debug.Log("Generated sky: " + cubetex.name );
|
||||
|
||||
}
|
||||
static public void CheckSky()
|
||||
{
|
||||
//Debug.Log("Running CheckSky");
|
||||
if (skytexture == null)
|
||||
{
|
||||
GenerateSkyTexture();
|
||||
}
|
||||
|
||||
if (RenderSettings.defaultReflectionMode == DefaultReflectionMode.Custom)
|
||||
{
|
||||
if (RenderSettings.customReflection != null && RenderSettings.customReflection.GetType() == typeof(Cubemap)) SetSkyTexture(RenderSettings.customReflection);
|
||||
else SetSkyTexture(CoreUtils.blackCubeTexture);
|
||||
}
|
||||
else //DefaultReflectionMode.Skybox
|
||||
{
|
||||
if (skytexture != null)
|
||||
{
|
||||
SetSkyTexture(skytexture);
|
||||
}
|
||||
else SetSkyTexture(CoreUtils.blackCubeTexture);
|
||||
}
|
||||
}
|
||||
|
||||
static public void SetSkyMips(Vector4 MipFogParam)
|
||||
{
|
||||
Shader.SetGlobalVector(ID_MipFogParam, MipFogParam);
|
||||
}
|
||||
|
||||
static public void SetSkyTexture(Texture SkyTex)
|
||||
{
|
||||
Shader.SetGlobalTexture(ID_SkyTexture, SkyTex);
|
||||
Shader.SetGlobalInt(ID_SkyMipCount, SkyTex.mipmapCount);
|
||||
}
|
||||
|
||||
// void ClearRenderTexture(RenderTexture rt, Color color)
|
||||
// {
|
||||
// RenderTexture activeRT = RenderTexture.active;
|
||||
// RenderTexture.active = rt;
|
||||
// GL.sRGBWrite = rt.sRGB;
|
||||
// if (rt.dimension == TextureDimension.Tex3D)
|
||||
// {
|
||||
// for (int i = 0; i < rt.depth; i++)
|
||||
// {
|
||||
// Graphics.SetRenderTarget(rt, 0, CubemapFace.Unknown, i);
|
||||
// GL.Clear(false, true, color);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// else if (rt.dimension == TextureDimension.Cube)
|
||||
// {
|
||||
// Graphics.SetRenderTarget(rt, 0, CubemapFace.PositiveX, 0);
|
||||
// GL.Clear(false, true, color);
|
||||
// Graphics.SetRenderTarget(rt, 0, CubemapFace.PositiveY, 0);
|
||||
// GL.Clear(false, true, color);
|
||||
// Graphics.SetRenderTarget(rt, 0, CubemapFace.PositiveZ, 0);
|
||||
// GL.Clear(false, true, color);
|
||||
// Graphics.SetRenderTarget(rt, 0, CubemapFace.NegativeX, 0);
|
||||
// GL.Clear(false, true, color);
|
||||
// Graphics.SetRenderTarget(rt, 0, CubemapFace.NegativeY, 0);
|
||||
// GL.Clear(false, true, color);
|
||||
// Graphics.SetRenderTarget(rt, 0, CubemapFace.NegativeZ, 0);
|
||||
// GL.Clear(false, true, color);
|
||||
// }
|
||||
//
|
||||
// RenderTexture.active = activeRT;
|
||||
// }
|
||||
}
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
public class SkyManager
|
||||
{
|
||||
public static Texture skytexture;
|
||||
|
||||
static readonly int ID_SkyTexture = Shader.PropertyToID("_SkyTexture");
|
||||
static readonly int ID_SkyMipCount = Shader.PropertyToID("_SkyMipCount");
|
||||
static readonly int ID_MipFogParam = Shader.PropertyToID("_MipFogParameters");
|
||||
|
||||
|
||||
static void GenerateSkyTexture()
|
||||
{
|
||||
Debug.Log("Generating Sky");
|
||||
//Generate Skybox
|
||||
RenderTexture cubetex = new RenderTexture(256, 256, 1, RenderTextureFormat.DefaultHDR);
|
||||
cubetex.enableRandomWrite = true;
|
||||
cubetex.useMipMap = true;
|
||||
cubetex.dimension = UnityEngine.Rendering.TextureDimension.Cube;
|
||||
cubetex.autoGenerateMips = true;
|
||||
//cubetex.
|
||||
// cubetex.Create();
|
||||
|
||||
Camera renderCam = new GameObject().AddComponent<Camera>();
|
||||
renderCam.gameObject.hideFlags = HideFlags.DontSave;
|
||||
renderCam.enabled = false;
|
||||
renderCam.cullingMask = 0;
|
||||
renderCam.backgroundColor = Color.black;
|
||||
renderCam.clearFlags = CameraClearFlags.Skybox;
|
||||
renderCam.RenderToCubemap(cubetex);
|
||||
//cubetex.GenerateMips();
|
||||
#if UNITY_EDITOR
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
Object.Destroy(renderCam.gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
Object.DestroyImmediate(renderCam.gameObject);
|
||||
}
|
||||
#else
|
||||
Object.Destroy(renderCam.gameObject);
|
||||
#endif
|
||||
skytexture = cubetex;
|
||||
}
|
||||
static public void CheckSky()
|
||||
{
|
||||
//Debug.Log("Running CheckSky");
|
||||
if (skytexture == null)
|
||||
{
|
||||
GenerateSkyTexture();
|
||||
}
|
||||
|
||||
if (RenderSettings.defaultReflectionMode == DefaultReflectionMode.Custom)
|
||||
{
|
||||
if (RenderSettings.customReflectionTexture != null && RenderSettings.customReflectionTexture.GetType() == typeof(Cubemap)) SetSkyTexture(RenderSettings.customReflectionTexture);
|
||||
else SetSkyTexture(CoreUtils.blackCubeTexture);
|
||||
}
|
||||
else //DefaultReflectionMode.Skybox
|
||||
{
|
||||
if (skytexture != null)
|
||||
{
|
||||
SetSkyTexture(CoreUtils.blackCubeTexture);
|
||||
}
|
||||
else SetSkyTexture(CoreUtils.blackCubeTexture);
|
||||
}
|
||||
}
|
||||
|
||||
static public void SetSkyMips(Vector4 MipFogParam)
|
||||
{
|
||||
Shader.SetGlobalVector(ID_MipFogParam, MipFogParam);
|
||||
}
|
||||
|
||||
static public void SetSkyTexture(Texture SkyTex)
|
||||
{
|
||||
Shader.SetGlobalTexture(ID_SkyTexture, SkyTex);
|
||||
Shader.SetGlobalInt(ID_SkyMipCount, SkyTex.mipmapCount);
|
||||
}
|
||||
|
||||
void ClearRenderTexture(RenderTexture rt, Color color)
|
||||
{
|
||||
RenderTexture activeRT = RenderTexture.active;
|
||||
RenderTexture.active = rt;
|
||||
GL.sRGBWrite = rt.sRGB;
|
||||
if (rt.dimension == TextureDimension.Tex3D)
|
||||
{
|
||||
for (int i = 0; i < rt.depth; i++)
|
||||
{
|
||||
Graphics.SetRenderTarget(rt, 0, CubemapFace.Unknown, i);
|
||||
GL.Clear(false, true, color);
|
||||
}
|
||||
|
||||
}
|
||||
else if (rt.dimension == TextureDimension.Cube)
|
||||
{
|
||||
Graphics.SetRenderTarget(rt, 0, CubemapFace.PositiveX, 0);
|
||||
GL.Clear(false, true, color);
|
||||
Graphics.SetRenderTarget(rt, 0, CubemapFace.PositiveY, 0);
|
||||
GL.Clear(false, true, color);
|
||||
Graphics.SetRenderTarget(rt, 0, CubemapFace.PositiveZ, 0);
|
||||
GL.Clear(false, true, color);
|
||||
Graphics.SetRenderTarget(rt, 0, CubemapFace.NegativeX, 0);
|
||||
GL.Clear(false, true, color);
|
||||
Graphics.SetRenderTarget(rt, 0, CubemapFace.NegativeY, 0);
|
||||
GL.Clear(false, true, color);
|
||||
Graphics.SetRenderTarget(rt, 0, CubemapFace.NegativeZ, 0);
|
||||
GL.Clear(false, true, color);
|
||||
}
|
||||
|
||||
RenderTexture.active = activeRT;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 84a04224ac111cb4c987311c68581b9b
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e7358c46b1d629f4e9039527032109f6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,189 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using UnityEngine.Serialization;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
//[System.Serializable]
|
||||
//public struct int2
|
||||
//{
|
||||
// public int x;
|
||||
// public int y;
|
||||
//}
|
||||
//[System.Serializable]
|
||||
//public struct uint2
|
||||
//{
|
||||
// public uint x;
|
||||
// public uint y;
|
||||
//}
|
||||
|
||||
[System.Serializable]
|
||||
public enum RGBA
|
||||
{
|
||||
Red, Green, Blue, Alpha
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public enum TextureFileExtension
|
||||
{
|
||||
PNG, EXR, JPG, TGA
|
||||
}
|
||||
public static class TextureExtentions
|
||||
{
|
||||
|
||||
public static void SetComputeBuffer(this ComputeShader shader, string name, int kernel, ComputeBuffer buffer)
|
||||
{
|
||||
// Debug.Log("Setting buffer");
|
||||
if (buffer != null)
|
||||
{
|
||||
shader.SetBuffer(kernel, name, buffer);
|
||||
|
||||
// Debug.Log(name + " set");
|
||||
}
|
||||
}
|
||||
|
||||
public static RenderTexture Copy3DSliceToRenderTexture(RenderTexture source, int layer)
|
||||
{
|
||||
|
||||
RenderTexture render = new RenderTexture((int)source.width, (int)source.height, 0, RenderTextureFormat.ARGB32);
|
||||
render.dimension = UnityEngine.Rendering.TextureDimension.Tex2D;
|
||||
render.enableRandomWrite = true;
|
||||
render.wrapMode = TextureWrapMode.Clamp;
|
||||
render.Create();
|
||||
#if UNITY_EDITOR
|
||||
ComputeShader slicer = AssetDatabase.LoadAssetAtPath<ComputeShader>("Packages/com.unity.render-pipelines.universal/Shaders/Volumetrics/Slicer.compute"); //Todo: Fix build error. Find better way to load?
|
||||
|
||||
int kernelIndex = slicer.FindKernel("CSMain");
|
||||
slicer.SetTexture(kernelIndex, "voxels", source);
|
||||
slicer.SetInt("layer", layer);
|
||||
slicer.SetTexture(kernelIndex, "Result", render);
|
||||
slicer.Dispatch(kernelIndex, (int)source.width, (int)source.height, 1);
|
||||
#endif
|
||||
return render;
|
||||
}
|
||||
|
||||
public static Texture2D ConvertFromRenderTexture(RenderTexture rt)
|
||||
{
|
||||
Texture2D output = new Texture2D(rt.width, rt.height);
|
||||
RenderTexture.active = rt;
|
||||
output.ReadPixels(new Rect(0, 0, rt.width, rt.height ), 0, 0);
|
||||
output.Apply();
|
||||
return output;
|
||||
}
|
||||
|
||||
public static Texture2D ConvertToTexture2D(this RenderTexture rt)
|
||||
{
|
||||
Texture2D output = new Texture2D(rt.width, rt.height, TextureFormat.RGBAFloat, false, false);
|
||||
RenderTexture.active = rt;
|
||||
output.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
|
||||
output.Apply();
|
||||
return output;
|
||||
}
|
||||
|
||||
public static Vector2Int GetImageSize(this Texture2D asset)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
|
||||
if (asset != null)
|
||||
{
|
||||
string assetPath = AssetDatabase.GetAssetPath(asset);
|
||||
TextureImporter importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;
|
||||
|
||||
if (importer != null)
|
||||
{
|
||||
object[] args = new object[2] { 0, 0 };
|
||||
MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
mi.Invoke(importer, args);
|
||||
return new Vector2Int((int)args[0], (int)args[1]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return new Vector2Int(0,0);
|
||||
}
|
||||
/// <summary>
|
||||
/// Releases and destroys the rendertexture
|
||||
/// </summary>
|
||||
/// <param name="renderTexture"></param>
|
||||
public static void Clear(this RenderTexture renderTexture)
|
||||
{
|
||||
if (renderTexture == null) return;
|
||||
renderTexture.Release();
|
||||
UnityEngine.Rendering.CoreUtils.Destroy(renderTexture);
|
||||
}
|
||||
|
||||
|
||||
public static void SaveToTexture3D(this RenderTexture Rtex3d, string NameAtPath)
|
||||
{
|
||||
//Texture3D export = new Texture3D((int)Rtex3d.width, (int)Rtex3d.height, (int)Rtex3d.depth, TextureFormat.ARGB32, false);
|
||||
// RenderTexture selectedRenderTexture;
|
||||
|
||||
RenderTexture[] layers = new RenderTexture[(int)Rtex3d.volumeDepth];
|
||||
for (int i = 0; i < (int)Rtex3d.volumeDepth; i++)
|
||||
layers[i] = Copy3DSliceToRenderTexture(Rtex3d, i);
|
||||
|
||||
Texture2D[] finalSlices = new Texture2D[Rtex3d.volumeDepth];
|
||||
for (int i = 0; i < Rtex3d.volumeDepth; i++)
|
||||
finalSlices[i] = ConvertFromRenderTexture(layers[i]);
|
||||
|
||||
|
||||
Texture3D output = new Texture3D((int)Rtex3d.width, (int)Rtex3d.height, (int)Rtex3d.volumeDepth, TextureFormat.RGB24, false);
|
||||
output.filterMode = FilterMode.Trilinear;
|
||||
output.wrapMode = TextureWrapMode.Clamp;
|
||||
Color[] outputPixels = output.GetPixels();
|
||||
|
||||
//Temp slice export for debugging
|
||||
//for (int z = 0; z < Rtex3d.volumeDepth; z++)
|
||||
//{
|
||||
// AssetDatabase.CreateAsset(finalSlices[z], NameAtPath + "_" + z + ".asset");
|
||||
|
||||
//}
|
||||
|
||||
for (int z = 0; z < Rtex3d.volumeDepth; z++)
|
||||
{
|
||||
Color[] layerPixels = finalSlices[z].GetPixels();
|
||||
for (int y = 0; y < Rtex3d.height; y++)
|
||||
|
||||
for (int x = 0; x < Rtex3d.width; x++)
|
||||
{
|
||||
// outputPixels[x + y * Rtex3d.width + z * Rtex3d.width * Rtex3d.height] = layerPixels[x + y * Rtex3d.width * Rtex3d.depth + z + Rtex3d.width];
|
||||
outputPixels[x + y * Rtex3d.width + z * Rtex3d.width * Rtex3d.height] = layerPixels[x + y * Rtex3d.width];
|
||||
// outputPixels[x + y * Rtex3d.width + z * Rtex3d.width * Rtex3d.height] = new Color((float)x / (float)Rtex3d.width, 0,0,0);
|
||||
}
|
||||
}
|
||||
|
||||
output.SetPixels(outputPixels);
|
||||
//output.wrapMode = TextureWrapMode.Clamp; //Clamp texture in sampler too
|
||||
output.Apply();
|
||||
|
||||
// AssetDatabase.CreateAsset(output, "Assets/" + nameOfTheAsset + ".asset");
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.CreateAsset(output, NameAtPath + ".asset"); // Todo: Either disable during build or make a build version
|
||||
Debug.Log("Saved " + NameAtPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static TextureFileExtension GetTextureExtension(this string path)
|
||||
{
|
||||
return (TextureFileExtension)System.Enum.Parse(typeof(TextureFileExtension), Path.GetExtension(path).ToUpper().Replace(".", string.Empty) );
|
||||
}
|
||||
|
||||
|
||||
public static byte[] EncodeTexture(this Texture2D tex, TextureFileExtension textureFileExtension)
|
||||
{
|
||||
switch (textureFileExtension) {
|
||||
case TextureFileExtension.PNG:
|
||||
return tex.EncodeToPNG();
|
||||
case TextureFileExtension.JPG:
|
||||
return tex.EncodeToJPG();
|
||||
case TextureFileExtension.EXR:
|
||||
return tex.EncodeToEXR();
|
||||
case TextureFileExtension.TGA:
|
||||
return tex.EncodeToTGA();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f04a399321e100449a9a5e685e68dbda
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
public class VolumetricMedia : MonoBehaviour
|
||||
{
|
||||
//Simple helper script for baker
|
||||
public enum ShapeType { Sphere, Box };
|
||||
public ShapeType shapeType;
|
||||
[Tooltip("3d texture. RGB is color")]
|
||||
public Texture3D Texture;
|
||||
|
||||
public Vector3 Scale = Vector3.one;
|
||||
|
||||
|
||||
[HideInInspector,SerializeField] public Vector3 NormalizedScale;
|
||||
[HideInInspector,SerializeField] public Vector3 Corner;
|
||||
|
||||
public float LocalExtinction()
|
||||
{
|
||||
return VolumeRenderingUtils.ExtinctionFromMeanFreePath(ViewDistance);
|
||||
}
|
||||
|
||||
|
||||
[Range(0.01f,100)] public float ViewDistance = 1f;
|
||||
[Range(0,1)] public float falloffDistance = .2f;
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
VolumetricRegisters.RegisterParticipatingMedia(this);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
VolumetricRegisters.UnregisterParticipatingMedia(this);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
VolumetricRegisters.UnregisterParticipatingMedia(this);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.color = Color.gray;
|
||||
Gizmos.matrix = Matrix4x4.TRS(gameObject.transform.position, gameObject.transform.rotation, Vector3.Scale( Scale , gameObject.transform.lossyScale) );
|
||||
Gizmos.DrawWireSphere(Vector3.zero, 0.5f);
|
||||
Gizmos.color = new Color(0.4f, 0.4f, 0.4f, .1f);
|
||||
Gizmos.DrawWireSphere(Vector3.zero, 0.5f * (1-falloffDistance) );
|
||||
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
//Check to see if the same and change if different
|
||||
Vector3 tempscale = Vector3.Scale(gameObject.transform.localScale, Scale);
|
||||
if (NormalizedScale != tempscale) NormalizedScale = tempscale; //redundant check to prevent dirtying
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a031249f315218e47bd8ecb2722bebd4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6cd110420304c8d4db47eeffedee7561, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
//using Unity.Mathematics;
|
||||
|
||||
public class VolumetricRegisters
|
||||
{
|
||||
public static List<VolumetricMedia> VolumetricMediaEntities = new List<VolumetricMedia>();
|
||||
public static List<BakedVolumetricArea> volumetricAreas = new List<BakedVolumetricArea>();
|
||||
|
||||
public static List<VolumetricRendering> volumetricRenderers = new List<VolumetricRendering>();
|
||||
|
||||
|
||||
public static bool _meshObjectsNeedRebuilding = true;
|
||||
|
||||
|
||||
public static void RegisterVolumetricArea(BakedVolumetricArea volumetricArea)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (volumetricArea.bakedTexture == null && Application.isPlaying) return; //quick check to make sure that this is valid
|
||||
#else
|
||||
if (volumetricArea.bakedTexture == null) return; //quick check to make sure that this is valid
|
||||
#endif
|
||||
volumetricAreas.Add(volumetricArea);
|
||||
ForceRefreshClipmaps();
|
||||
|
||||
}
|
||||
public static void UnregisterVolumetricArea(BakedVolumetricArea volumetricArea)
|
||||
{
|
||||
volumetricAreas.Remove(volumetricArea);
|
||||
ForceRefreshClipmaps();
|
||||
}
|
||||
|
||||
public static void RegisterParticipatingMedia(VolumetricMedia volumetricMedia)
|
||||
{
|
||||
VolumetricMediaEntities.Add(volumetricMedia);
|
||||
}
|
||||
public static void UnregisterParticipatingMedia(VolumetricMedia volumetricMedia)
|
||||
{
|
||||
VolumetricMediaEntities.Remove(volumetricMedia);
|
||||
}
|
||||
|
||||
|
||||
public static void RegisterVolumetricRenderer(VolumetricRendering volumetricRenderer)
|
||||
{
|
||||
if (!volumetricRenderers.Contains(volumetricRenderer)) volumetricRenderers.Add(volumetricRenderer);
|
||||
}
|
||||
public static void UnregisterVolumetricRenderer(VolumetricRendering volumetricRenderer)
|
||||
{
|
||||
if (volumetricRenderers.Contains(volumetricRenderer)) volumetricRenderers.Remove(volumetricRenderer);
|
||||
}
|
||||
|
||||
public static void ForceRefreshClipmaps()
|
||||
{
|
||||
foreach (VolumetricRendering VolumetricRenderer in volumetricRenderers)
|
||||
{
|
||||
VolumetricRenderer.VolumetricRegisterForceRefresh = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dfa4513648cda3141870f93d47ab3800
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,17 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6588d23683e38f14db1f26f57f266dd6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- cam: {instanceID: 0}
|
||||
- volumetricData: {fileID: 11400000, guid: 9aa770ca8d706d24aa3f993a2394fd83, type: 2}
|
||||
- FroxelFogCompute: {instanceID: 0}
|
||||
- FroxelIntegrationCompute: {instanceID: 0}
|
||||
- ClipmapCompute: {instanceID: 0}
|
||||
- BlurCompute: {instanceID: 0}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Add table
Add a link
Reference in a new issue