NEW CLOUDS NEW CLOUDS NEW CLOUDS WHOOO

Added new clouds shader with improved raymarching loop
Currently only being used in main menu (rest is coming right up!)
Removed WWise (it's nice, but all it's doing is throwing exceptions rn since we ain't about to get a license)
This commit is contained in:
Reinier Vens 2022-07-10 02:53:09 +01:00
commit b08423b936
1066 changed files with 9806 additions and 58766 deletions

View file

@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud.Sample {
public class MouseMovement: MonoBehaviour {
private void Update() {
float yaw = Input.GetAxis("Mouse X");
float pitch = -Input.GetAxis("Mouse Y");
var currAngle = transform.rotation.eulerAngles;
currAngle.x += pitch;
currAngle.y += yaw;
transform.rotation = Quaternion.Euler(currAngle);
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 0a9c4d2e823dfc247b2f6850f76ca2b6
timeCreated: 1530937970
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 44271309eab254245a7f8e6479ee5fad
folderAsset: yes
timeCreated: 1523161001
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud {
public abstract class BaseNoiseGenerator : INoiseGenerator {
public int period = 16;
public abstract float Noise(Vector3 pos);
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 374ff9f8fc4c1114b897fc5a6213d9ff
timeCreated: 1522028198
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,35 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud {
[System.Serializable]
public class CurlNoiseGenerator : ITextureGenerator {
public Texture2D perlinNoise;
public float brightness = 10.0f;
public float scale = 10.0f;
public PerlinNoiseGenerator perlin;
private float noise(float x, float y) {
return perlin.Noise(new Vector3(x, y));
}
public Color Sample(float x, float y) {
float eps = 1.0f / 128.0f;
float n1, n2, a, b;
n1 = noise(x, y + eps);
n2 = noise(x, y - eps);
a = (n1 - n2) / (2);
n1 = noise(x + eps, y);
n2 = noise(x - eps, y);
b = (n1 - n2) / (2);
Vector2 curl = new Vector2(a, -b);
return new Color(curl.x, curl.y, 0);
}
public Color Sample(Vector3 pos) {
return Sample(pos.x, pos.y);
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: f5c2ff57fc70e4346aa2667d13cce209
timeCreated: 1522740335
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,20 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud {
[System.Serializable]
public class CustomTexGenerator : INoiseGenerator {
public float channel1WorleyPercent = 0.5f;
public int channel1WorleyPeriod = 4;
public int channel1PerlinPeriod = 4;
public int worleyOcataves = 4;
public int perlinOctaves = 4;
public float Noise(Vector3 pos) {
return channel1WorleyPercent * WorleyNoiseGenerator.OctaveNoise(pos, channel1WorleyPeriod, worleyOcataves)
+ (1 - channel1WorleyPercent) * PerlinNoiseGenerator.OctaveNoise(pos, channel1PerlinPeriod, perlinOctaves);
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ca47c3192075ced48b6f40850f6268ea
timeCreated: 1522037646
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,45 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud {
[System.Serializable]
public class First3DTexGenerator : ITextureGenerator {
public int texResolution = 32;
public int perlinOctaves = 4;
public int channel1PerlinPeriod = 16;
public int channel2WorleyPeriod = 16;
public int channel3WorleyPeriod = 32;
public int channel4WorleyPeriod = 64;
public Color Sample(Vector3 pos) {
Color res = new Color();
res.r = PerlinNoiseGenerator.OctaveNoise(pos, channel1PerlinPeriod, perlinOctaves);
res.g = WorleyNoiseGenerator.OctaveNoise(pos, channel2WorleyPeriod, 3);
res.b = WorleyNoiseGenerator.OctaveNoise(pos, channel3WorleyPeriod, 3);
res.a = WorleyNoiseGenerator.OctaveNoise(pos, channel4WorleyPeriod, 3);
return res;
}
}
[System.Serializable]
public class Second3DTexGenerator : ITextureGenerator {
public int texResolution = 32;
public int channel1WorleyFreq = 16;
public int channel2WorleyFreq = 32;
public int channel3WorleyFreq = 64;
public Color Noise(Vector3 pos) {
Color res = new Color();
res.r = WorleyNoiseGenerator.OctaveNoise(pos, channel1WorleyFreq, 3);
res.g = WorleyNoiseGenerator.OctaveNoise(pos, channel2WorleyFreq, 3);
res.b = WorleyNoiseGenerator.OctaveNoise(pos, channel3WorleyFreq, 3);
res.a = 1.0f;
return res;
}
public Color Sample(Vector3 pos) {
return Noise(pos);
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ddef2f45efe7ed54c98ca379a97cc280
timeCreated: 1522028698
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,23 @@
using System;
using UnityEngine;
namespace Yangrc.VolumeCloud {
public class NoiseTextureAdapter : ITextureGenerator {
private INoiseGenerator noiseGen;
public NoiseTextureAdapter(INoiseGenerator noiseGen) {
this.noiseGen = noiseGen;
}
public Color Sample(Vector3 pos) {
return noiseGen.Noise(pos) * Color.white;
}
}
public interface ITextureGenerator {
Color Sample(Vector3 pos);
}
public interface INoiseGenerator {
float Noise(Vector3 pos);
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 36639cbbe3e718444b2f731daafe2ec5
timeCreated: 1522029916
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,97 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* code from https://github.com/keijiro/PerlinNoise
* changed to make the noise tilable.
*/
namespace Yangrc.VolumeCloud {
[System.Serializable]
public class PerlinNoiseGenerator : BaseNoiseGenerator {
public int octaves = 4;
public override float Noise(Vector3 pos) {
return OctaveNoise(pos, period, octaves);
}
static float Fade(float t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
static float Lerp(float t, float a, float b) {
return a + t * (b - a);
}
static float Grad(int hash, float x, float y, float z) {
var h = hash & 15;
var u = h < 8 ? x : y;
var v = h < 4 ? y : (h == 12 || h == 14 ? x : z);
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
}
static int[] perm = {
151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
151
};
private static int wrap(int n, int period) {
n++;
return period > 0 ? n % period : n;
}
public static float Noise(Vector3 pos, int period) {
pos *= period;
var x = pos.x;
var y = pos.y;
var z = pos.z;
var X = Mathf.FloorToInt(x) & 0xff;
var Y = Mathf.FloorToInt(y) & 0xff;
var Z = Mathf.FloorToInt(z) & 0xff;
x -= Mathf.Floor(x);
y -= Mathf.Floor(y);
z -= Mathf.Floor(z);
var u = Fade(x);
var v = Fade(y);
var w = Fade(z);
var A = (perm[wrap(X, period)] + Y) & 0xff;
var B = (perm[wrap(X + 1, period)] + Y) & 0xff;
var AA = (perm[wrap(A, period)] + Z) & 0xff;
var BA = (perm[wrap(B, period)] + Z) & 0xff;
var AB = (perm[wrap(A + 1, period)] + Z) & 0xff;
var BB = (perm[wrap(B + 1, period)] + Z) & 0xff;
var result = Lerp(w, Lerp(v, Lerp(u, Grad(perm[AA], x, y, z), Grad(perm[BA], x - 1, y, z)),
Lerp(u, Grad(perm[AB], x, y - 1, z), Grad(perm[BB], x - 1, y - 1, z))),
Lerp(v, Lerp(u, Grad(perm[AA + 1], x, y, z - 1), Grad(perm[BA + 1], x - 1, y, z - 1)),
Lerp(u, Grad(perm[AB + 1], x, y - 1, z - 1), Grad(perm[BB + 1], x - 1, y - 1, z - 1))));
return (result + 1.0f) / 2.0f;
}
public static float OctaveNoise(Vector3 pos, int period, int octaves, float persistence = 0.5f) {
float total = 0.0f;
float result = 0.0f;
float amp = .5f;
float freq = 1.0f;
for (int i = 0; i < octaves; i++) {
total += amp;
result += (Noise(pos, Mathf.RoundToInt(freq * period)) * 2.0f - 1.0f) * amp;
amp *= persistence;
freq *= 2.0f;
}
if (octaves == 0)
return 0.0f;
return (result / total + 1.0f) / 2.0f;
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 73833a3529798be4ba6baa1db73dc2aa
timeCreated: 1521990941
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud {
public class Utils {
public static Texture3D GetTex(ITextureGenerator texGen, int textureResolution, TextureFormat format = TextureFormat.RGBA32) {
Texture3D result = new Texture3D(textureResolution, textureResolution, textureResolution, format, true);
Color[] colors = new Color[textureResolution * textureResolution * textureResolution];
for (int x = 0; x < textureResolution; x++) {
for (int y = 0; y < textureResolution; y++) {
for (int z = 0; z < textureResolution; z++) {
colors[x * textureResolution * textureResolution + y * textureResolution + z]
= texGen.Sample(new Vector3(x, y, z) / textureResolution);
}
}
}
result.SetPixels(colors);
result.wrapMode = TextureWrapMode.Repeat;
result.Apply();
return result;
}
public static Texture2D GetPreviewTex(ITextureGenerator texGen, int textureResolution, TextureFormat format = TextureFormat.RGBA32) {
Texture2D result = new Texture2D(textureResolution, textureResolution, format, true);
Color[] colors = new Color[textureResolution * textureResolution];
for (int x = 0; x < textureResolution; x++) {
for (int y = 0; y < textureResolution; y++) {
colors[x * textureResolution + y]
= texGen.Sample(new Vector3(x, y, 0) / textureResolution);
}
}
result.SetPixels(colors);
result.wrapMode = TextureWrapMode.Repeat;
result.Apply();
return result;
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 7830b1199edb76145b042d63acddae71
timeCreated: 1522029782
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,59 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud {
[System.Serializable]
public class WorleyNoiseGenerator : BaseNoiseGenerator {
public float brightness = 1.0f;
public float contrast = 1.0f;
public int octaves = 4;
public override float Noise(Vector3 pos) {
return Mathf.Clamp01(((OctaveNoise(pos, period, octaves)) + (brightness - 1.0f)) * contrast);
}
private static int wrap(int n, int period) {
return n >= 0 ? n % period : period + n;
}
public static float Noise(Vector3 pos, int period) {
pos *= period;
var x = Mathf.FloorToInt(pos.x);
var y = Mathf.FloorToInt(pos.y);
var z = Mathf.FloorToInt(pos.z);
Vector3Int boxPos = new Vector3Int(x, y, z);
float minDistance = float.MaxValue;
for (int xoffset = -1; xoffset <= 1; xoffset++) {
for (int yoffset = -1; yoffset <= 1; yoffset++) {
for (int zoffset = -1; zoffset <= 1; zoffset++) {
var newboxPos = boxPos + new Vector3Int(xoffset, yoffset, zoffset);
var hashValue = (wrap(newboxPos.x, period) + wrap(newboxPos.y, period) * 131 + wrap(newboxPos.z, period) * 17161) % int.MaxValue;
UnityEngine.Random.InitState(hashValue);
var featurePoint = new Vector3(UnityEngine.Random.value + newboxPos.x, UnityEngine.Random.value + newboxPos.y, UnityEngine.Random.value + newboxPos.z);
minDistance = Mathf.Min(minDistance, Vector3.Distance(pos, featurePoint));
}
}
}
return 1.0f - minDistance;
}
public static float OctaveNoise(Vector3 pos, int period, int octaves, float persistence = 0.5f) {
float result = 0.0f;
float amp = .5f;
float freq = 1.0f;
float totalAmp = 0.0f;
for (int i = 0; i < octaves; i++) {
totalAmp += amp;
result += Noise(pos, Mathf.RoundToInt(freq * period)) * amp;
amp *= persistence;
freq /= persistence;
}
if (octaves == 0)
return 0.0f;
return result / totalAmp;
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 185d43ae86d668448bb0a6d33b1dce7e
timeCreated: 1521970890
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7f65bd86e2f467d479541420d7d522c6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,115 @@
// Copyright (c) <2015> <Playdead>
// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE.TXT)
// AUTHOR: Lasse Jon Fuglsang Pedersen <lasse@playdead.com>
using UnityEngine;
public abstract class EffectBase : MonoBehaviour
{
public void EnsureArray<T>(ref T[] array, int size, T initialValue = default(T))
{
if (array == null || array.Length != size)
{
array = new T[size];
for (int i = 0; i != size; i++)
array[i] = initialValue;
}
}
public void EnsureArray<T>(ref T[,] array, int size0, int size1, T defaultValue = default(T))
{
if (array == null || array.Length != size0 * size1)
{
array = new T[size0, size1];
for (int i = 0; i != size0; i++)
{
for (int j = 0; j != size1; j++)
array[i, j] = defaultValue;
}
}
}
public void EnsureMaterial(ref Material material, Shader shader)
{
if (shader != null)
{
if (material == null || material.shader != shader)
material = new Material(shader);
if (material != null)
material.hideFlags = HideFlags.DontSave;
}
else
{
Debug.LogWarning("missing shader", this);
}
}
public void EnsureDepthTexture(Camera camera)
{
if ((camera.depthTextureMode & DepthTextureMode.Depth) == 0)
camera.depthTextureMode |= DepthTextureMode.Depth;
}
public void EnsureKeyword(Material material, string name, bool enabled)
{
if (enabled != material.IsKeywordEnabled(name))
{
if (enabled)
material.EnableKeyword(name);
else
material.DisableKeyword(name);
}
}
public bool EnsureRenderTarget(ref RenderTexture rt, int width, int height, RenderTextureFormat format, FilterMode filterMode, TextureWrapMode wrapMode = TextureWrapMode.Clamp, bool randomWrite = false, bool useMipmap = false, int depthBits = 0, int antiAliasing = 1)
{
if (rt != null && (rt.width != width || rt.height != height || rt.format != format || rt.filterMode != filterMode || rt.enableRandomWrite != randomWrite || rt.wrapMode != wrapMode || rt.antiAliasing != antiAliasing || rt.useMipMap != useMipmap))
{
rt.Release();
rt = null;
}
if (rt == null)
{
rt = new RenderTexture(width, height, depthBits, format, RenderTextureReadWrite.Default);
rt.antiAliasing = antiAliasing;
rt.useMipMap = useMipmap;
rt.filterMode = filterMode;
rt.enableRandomWrite = randomWrite;
rt.wrapMode = wrapMode;
rt.Create();
return true;// new target
}
return false;// same target
}
public void ReleaseRenderTarget(ref RenderTexture rt)
{
if (rt != null)
{
RenderTexture.ReleaseTemporary(rt);
rt = null;
}
}
public void DrawFullscreenQuad()
{
GL.PushMatrix();
GL.LoadOrtho();
GL.Begin(GL.QUADS);
{
GL.MultiTexCoord2(0, 0.0f, 0.0f);
GL.Vertex3(0.0f, 0.0f, 0.0f); // BL
GL.MultiTexCoord2(0, 1.0f, 0.0f);
GL.Vertex3(1.0f, 0.0f, 0.0f); // BR
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 0.0f); // TR
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, 0.0f); // TL
}
GL.End();
GL.PopMatrix();
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 25ff30f599e672448a5fd63cd72b740c
timeCreated: 1522586438
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,172 @@
// Copyright (c) <2015> <Playdead>
// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE.TXT)
// AUTHOR: Lasse Jon Fuglsang Pedersen <lasse@playdead.com>
#if UNITY_5_5_OR_NEWER
#define SUPPORT_STEREO
#endif
using UnityEngine;
public static class Vector2Extension
{
// positive if v2 is on the left side of v1
public static float SignedAngle(this Vector2 v1, Vector2 v2)
{
Vector2 n1 = v1.normalized;
Vector2 n2 = v2.normalized;
float dot = Vector2.Dot(n1, n2);
if (dot > 1.0f)
dot = 1.0f;
if (dot < -1.0f)
dot = -1.0f;
float theta = Mathf.Acos(dot);
float sgn = Vector2.Dot(new Vector2(-n1.y, n1.x), n2);
if (sgn >= 0.0f)
return theta;
else
return -theta;
}
public static Vector2 Rotate(this Vector2 v, float theta)
{
float cs = Mathf.Cos(theta);
float sn = Mathf.Sin(theta);
float x1 = v.x * cs - v.y * sn;
float y1 = v.x * sn + v.y * cs;
return new Vector2(x1, y1);
}
}
public static class Vector3Extension
{
public static Vector3 WithX(this Vector3 v, float x)
{
return new Vector3(x, v.y, v.z);
}
public static Vector3 WithY(this Vector3 v, float y)
{
return new Vector3(v.x, y, v.z);
}
public static Vector3 WithZ(this Vector3 v, float z)
{
return new Vector3(v.x, v.y, z);
}
}
public static class Matrix4x4Extension
{
public static Matrix4x4 GetPerspectiveProjection(float left, float right, float bottom, float top, float near, float far)
{
float x = (2.0f * near) / (right - left);
float y = (2.0f * near) / (top - bottom);
float a = (right + left) / (right - left);
float b = (top + bottom) / (top - bottom);
float c = -(far + near) / (far - near);
float d = -(2.0f * far * near) / (far - near);
float e = -1.0f;
Matrix4x4 m = new Matrix4x4();
m[0, 0] = x; m[0, 1] = 0; m[0, 2] = a; m[0, 3] = 0;
m[1, 0] = 0; m[1, 1] = y; m[1, 2] = b; m[1, 3] = 0;
m[2, 0] = 0; m[2, 1] = 0; m[2, 2] = c; m[2, 3] = d;
m[3, 0] = 0; m[3, 1] = 0; m[3, 2] = e; m[3, 3] = 0;
return m;
}
public static Matrix4x4 GetOrthographicProjection(float left, float right, float bottom, float top, float near, float far)
{
float x = 2.0f / (right - left);
float y = 2.0f / (top - bottom);
float z = -2.0f / (far - near);
float a = -(right + left) / (right - left);
float b = -(top + bottom) / (top - bottom);
float c = -(far + near) / (far - near);
float d = 1.0f;
Matrix4x4 m = new Matrix4x4();
m[0, 0] = x; m[0, 1] = 0; m[0, 2] = 0; m[0, 3] = a;
m[1, 0] = 0; m[1, 1] = y; m[1, 2] = 0; m[1, 3] = b;
m[2, 0] = 0; m[2, 1] = 0; m[2, 2] = z; m[2, 3] = c;
m[3, 0] = 0; m[3, 1] = 0; m[3, 2] = 0; m[3, 3] = d;
return m;
}
}
public static class CameraExtension
{
public static Vector4 GetProjectionExtents(this Camera camera)
{
return GetProjectionExtents(camera, 0.0f, 0.0f);
}
public static Vector4 GetProjectionExtents(this Camera camera, float texelOffsetX, float texelOffsetY)
{
if (camera == null)
return Vector4.zero;
float oneExtentY = camera.orthographic ? camera.orthographicSize : Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView);
float oneExtentX = oneExtentY * camera.aspect;
float texelSizeX = oneExtentX / (0.5f * camera.pixelWidth);
float texelSizeY = oneExtentY / (0.5f * camera.pixelHeight);
float oneJitterX = texelSizeX * texelOffsetX;
float oneJitterY = texelSizeY * texelOffsetY;
return new Vector4(oneExtentX, oneExtentY, oneJitterX, oneJitterY);// xy = frustum extents at distance 1, zw = jitter at distance 1
}
#if SUPPORT_STEREO
public static Vector4 GetProjectionExtents(this Camera camera, Camera.StereoscopicEye eye)
{
return GetProjectionExtents(camera, eye, 0.0f, 0.0f);
}
public static Vector4 GetProjectionExtents(this Camera camera, Camera.StereoscopicEye eye, float texelOffsetX, float texelOffsetY)
{
Matrix4x4 inv = Matrix4x4.Inverse(camera.GetStereoProjectionMatrix(eye));
Vector3 ray00 = inv.MultiplyPoint3x4(new Vector3(-1.0f, -1.0f, 0.95f));
Vector3 ray11 = inv.MultiplyPoint3x4(new Vector3(1.0f, 1.0f, 0.95f));
ray00 /= -ray00.z;
ray11 /= -ray11.z;
float oneExtentX = 0.5f * (ray11.x - ray00.x);
float oneExtentY = 0.5f * (ray11.y - ray00.y);
float texelSizeX = oneExtentX / (0.5f * camera.pixelWidth);
float texelSizeY = oneExtentY / (0.5f * camera.pixelHeight);
float oneJitterX = 0.5f * (ray11.x + ray00.x) + texelSizeX * texelOffsetX;
float oneJitterY = 0.5f * (ray11.y + ray00.y) + texelSizeY * texelOffsetY;
return new Vector4(oneExtentX, oneExtentY, oneJitterX, oneJitterY);// xy = frustum extents at distance 1, zw = jitter at distance 1
}
#endif
public static Matrix4x4 GetProjectionMatrix(this Camera camera)
{
return GetProjectionMatrix(camera, 0.0f, 0.0f);
}
public static Matrix4x4 GetProjectionMatrix(this Camera camera, float texelOffsetX, float texelOffsetY)
{
if (camera == null)
return Matrix4x4.identity;
Vector4 extents = GetProjectionExtents(camera, texelOffsetX, texelOffsetY);
float cf = camera.farClipPlane;
float cn = camera.nearClipPlane;
float xm = extents.z - extents.x;
float xp = extents.z + extents.x;
float ym = extents.w - extents.y;
float yp = extents.w + extents.y;
if (camera.orthographic)
return Matrix4x4Extension.GetOrthographicProjection(xm, xp, ym, yp, cn, cf);
else
return Matrix4x4Extension.GetPerspectiveProjection(xm * cn, xp * cn, ym * cn, yp * cn, cn, cf);
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: a04505329bedf944d9a0203d67fd6cb4
timeCreated: 1522586797
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) [2015] [Playdead]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 45afbde13518d91449c0aae2364f37a9
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,146 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud {
[CreateAssetMenu]
public class VolumeCloudConfiguration : ScriptableObject {
[Header("Weather map")]
[Tooltip("R for coverage, G for density, B for cloud type")]
public Texture2D weatherTex;
public float weatherTexSize = 40000;
[Header("Shape")]
public Vector2 cloudHeightRange = new Vector2(1500.0f, 9000.0f);
public Texture3D baseTexture;
[Range(0, 5)]
public float baseTile = 2.0f;
public Texture2D heightDensityMap;
public float overallSize = 50000;
public float topOffset = 0.0f;
[Header("Shape - Detail")]
public Texture3D detailTexture;
[Range(0, 80)]
public float detailTile = 36.0f;
[Range(.01f, .5f)]
public float detailStrength = 0.2f;
[Header("Shape - Curl")]
public Texture2D curlNoise;
[Range(0.001f, 1.0f)]
public float curlTile = 0.01f;
public float curlStrength = 5.0f;
[Header("Shape - Modifiers")]
[Range(0, 1)]
public float overallDensity = 1.0f;
[Range(0, 1)]
public float cloudTypeModifier = 1.0f;
[Range(0, 1)]
public float cloudCoverageModifier = 1.0f;
[Header("Shape - Wind")]
public Vector2 windDirection;
public float windSpeed;
[Header("Lighting")]
public Color ambientColor = new Color(214, 37, 154);
public const float COEFFICIENT_SCALE = 1e-2f;
[Range(0.1f, 2.0f)]
public float scatteringCoefficient = .5f;
[Range(0.1f, 2.0f)]
public float extinctionCoefficient = .52f;
[Header("Lighting - Multi Scattering Approximation")]
[Range(0.01f, 1)]
[Tooltip("Value used in multi-scattering approximation, higher causes more light be scattered. Must be lower than multiScatteringExtinction.")]
public float multiScatteringScattering = .5f;
[Range(0.01f, 1)]
[Tooltip("Value used in multi-scattering approximation, higher causes more light be extincted. Must be higher than multiScatteringScattering.")]
public float multiScatteringExtinction = .5f;
[Range(0.01f, 1)]
[Tooltip("Value used in multi-scattering approximation, phase function is p(g * pow(multiScatteringEC, octave), theta)")]
public float multiScatteringEC = .5f;
[Header("Lighting - Silver")]
public float silverSpread = 0.1f;
[Header("Lighting - Atmosphere")]
public float atmosphereSaturateDistance = 100000.0f;
private class PropertyHash {
public static int cloudStartHeight = Shader.PropertyToID("_CloudStartHeight");
public static int cloudEndHeight = Shader.PropertyToID("_CloudEndHeight");
public static int baseTex = Shader.PropertyToID("_BaseTex");
public static int baseTile = Shader.PropertyToID("_BaseTile");
public static int detailTex = Shader.PropertyToID("_DetailTex");
public static int detailTile = Shader.PropertyToID("_DetailTile");
public static int detailStrength = Shader.PropertyToID("_DetailStrength");
public static int curlNoise = Shader.PropertyToID("_CurlNoise");
public static int curlTile = Shader.PropertyToID("_CurlTile");
public static int curlStrength = Shader.PropertyToID("_CurlStrength");
public static int heightDensity = Shader.PropertyToID("_HeightDensity");
public static int topOffset = Shader.PropertyToID("_CloudTopOffset");
public static int cloudSize = Shader.PropertyToID("_CloudSize");
public static int cloudOverallDensity = Shader.PropertyToID("_CloudOverallDensity");
public static int cloudTypeModifier = Shader.PropertyToID("_CloudTypeModifier");
public static int cloudCoverageModifier = Shader.PropertyToID("_CloudCoverageModifier");
public static int windDirection = Shader.PropertyToID("_WindDirection");
public static int weatherTex = Shader.PropertyToID("_WeatherTex");
public static int weatherTexSize = Shader.PropertyToID("_WeatherTexSize");
public static int scatteringCoefficient = Shader.PropertyToID("_ScatteringCoefficient");
public static int extinctionCoefficient = Shader.PropertyToID("_ExtinctionCoefficient");
public static int multiScatteringA = Shader.PropertyToID("_MultiScatteringA");
public static int multiScatteringB = Shader.PropertyToID("_MultiScatteringB");
public static int multiScatteringC = Shader.PropertyToID("_MultiScatteringC");
public static int silverSpread = Shader.PropertyToID("_SilverSpread");
public static int atmosphereColorSaturateDistance = Shader.PropertyToID("_AtmosphereColorSaturateDistance");
public static int ambientColor = Shader.PropertyToID("_AmbientColor");
}
public void ApplyToMaterial(Material mat) {
mat.SetFloat(PropertyHash.cloudStartHeight, cloudHeightRange.x);
mat.SetFloat(PropertyHash.cloudEndHeight, cloudHeightRange.y);
mat.SetTexture(PropertyHash.baseTex, baseTexture);
mat.SetTexture(PropertyHash.detailTex, detailTexture);
mat.SetTexture(PropertyHash.curlNoise, curlNoise);
mat.SetFloat(PropertyHash.baseTile, baseTile);
mat.SetTexture(PropertyHash.heightDensity, heightDensityMap);
mat.SetFloat(PropertyHash.detailTile, detailTile);
mat.SetFloat(PropertyHash.detailStrength, detailStrength);
mat.SetFloat(PropertyHash.curlTile, curlTile);
mat.SetFloat(PropertyHash.curlStrength, curlStrength);
mat.SetFloat(PropertyHash.topOffset, topOffset);
mat.SetFloat(PropertyHash.cloudSize, overallSize);
mat.SetFloat(PropertyHash.cloudOverallDensity, overallDensity);
mat.SetFloat(PropertyHash.cloudTypeModifier, cloudTypeModifier);
mat.SetFloat(PropertyHash.cloudCoverageModifier, cloudCoverageModifier);
mat.SetVector(PropertyHash.windDirection, new Vector4(windDirection.x, windDirection.y, windSpeed, -windSpeed));
mat.SetTexture(PropertyHash.weatherTex, weatherTex);
mat.SetFloat(PropertyHash.weatherTexSize, weatherTexSize);
mat.SetFloat(PropertyHash.scatteringCoefficient, scatteringCoefficient * COEFFICIENT_SCALE);
mat.SetFloat(PropertyHash.extinctionCoefficient, extinctionCoefficient * COEFFICIENT_SCALE);
mat.SetFloat(PropertyHash.multiScatteringA, multiScatteringScattering );
mat.SetFloat(PropertyHash.multiScatteringB, multiScatteringExtinction );
mat.SetFloat(PropertyHash.multiScatteringC, multiScatteringEC );
mat.SetFloat(PropertyHash.silverSpread, silverSpread);
mat.SetFloat(PropertyHash.atmosphereColorSaturateDistance, atmosphereSaturateDistance);
mat.SetColor(PropertyHash.ambientColor, ambientColor);
}
}
}

View file

@ -0,0 +1,16 @@
fileFormatVersion: 2
guid: edad34612c85d2e4da1745df6c135220
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences:
- baseTexture: {fileID: 11700000, guid: 3ed67598438ec1e4dba28385e35a557b, type: 2}
- heightDensityMap: {fileID: 2800000, guid: a27c79a96e56b794a9bcb99b3b2df92b, type: 3}
- detailTexture: {fileID: 11700000, guid: c978a1d1fdb99d54eb7910eb562fcbd7, type: 2}
- curlNoise: {fileID: 2800000, guid: 864afbdb4b1b36c42be5cac8f2556237, type: 2}
- weatherTex: {fileID: 2800000, guid: c73be893af46a1e4db152d42aacae768, type: 3}
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,241 @@
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace Yangrc.VolumeCloud {
/// <summary>
/// Generate halton sequence.
/// code from unity post-processing stack.
/// </summary>
public class HaltonSequence {
public int radix = 3;
private int storedIndex = 0;
public float Get() {
float result = 0f;
float fraction = 1f / (float)radix;
int index = storedIndex;
while (index > 0) {
result += (float)(index % radix) * fraction;
index /= radix;
fraction /= (float)radix;
}
storedIndex++;
return result;
}
}
public enum Quality {
Low,
Normal, //Low sample count
High, //High sample count and high shadow sample count.
}
/// <summary>
/// Cloud renderer post processing.
/// </summary>
[ImageEffectAllowedInSceneView]
[ExecuteInEditMode,RequireComponent(typeof(Camera))]
[ImageEffectOpaque]
public class VolumeCloudRenderer : EffectBase {
[Header("Config")]
public VolumeCloudConfiguration configuration;
[Header("Render Settings")]
[Range(0, 2)]
public int downSample = 1;
public Quality quality;
public bool allowCloudFrontObject;
[SerializeField]
[Tooltip("Enable ap calculation using ap system. Do not enable if ap system is not present")]
private bool useApSystem;
private Material mat;
private Material heightDownsampleMat;
private RenderTexture[] fullBuffer;
private int fullBufferIndex;
private RenderTexture undersampleBuffer;
private Matrix4x4 prevV;
private Camera mcam;
private HaltonSequence sequence = new HaltonSequence() { radix = 3 };
// The index of 4x4 pixels.
private int frameIndex = 0;
private float bayerOffsetIndex = 0;
private bool firstFrame = true;
[Header("Hi-Height")]
[SerializeField]
private bool useHierarchicalHeightMap;
private Vector2Int hiHeightLevelRange = new Vector2Int(0, 9);
private Vector2Int heightLutTextureSize = new Vector2Int(512, 512);
private RenderTexture heightLutTexture;
private RenderTexture hiHeightTexture;
private RenderTexture[] hiHeightTempTextures;
[Header("Shader references(DONT EDIT)")]
[SerializeField]
private Shader cloudShader;
[SerializeField]
private ComputeShader heightPreprocessShader;
[SerializeField]
private Shader cloudHeightProcessShader;
void EnsureMaterial(bool force = false) {
if (mat == null || force) {
mat = new Material(cloudShader);
}
if (heightDownsampleMat == null || force){
heightDownsampleMat = new Material(cloudHeightProcessShader);
}
}
private void OnDestroy() {
if (this.fullBuffer != null) {
for (int i = 0; i < fullBuffer.Length; i++) {
fullBuffer[i].Release();
fullBuffer[i] = null;
}
}
if (this.undersampleBuffer != null) {
this.undersampleBuffer.Release();
this.undersampleBuffer = null;
}
if (hiHeightTexture != null) {
hiHeightTexture.Release();
hiHeightTexture = null;
}
if (heightLutTexture != null) {
heightLutTexture.Release();
heightLutTexture = null;
}
}
private void Start() {
this.EnsureMaterial(true);
}
private void GenerateHierarchicalHeightMap() {
RenderTexture defaultTarget = RenderTexture.active;
if (this.configuration.weatherTex.width != 512 || this.configuration.weatherTex.height != 512) {
throw new UnityException("Hierarchical height map mode only supports weather tex of size 512*512!");
}
if (EnsureRenderTarget(ref heightLutTexture, heightLutTextureSize.x, heightLutTextureSize.y, RenderTextureFormat.RFloat, FilterMode.Point, randomWrite: true)) {
var kernal = heightPreprocessShader.FindKernel("CSMain");
heightPreprocessShader.SetTexture(kernal, "heightDensityMap", configuration.heightDensityMap);
heightPreprocessShader.SetTexture(kernal, "heightLutResult", this.heightLutTexture);
heightPreprocessShader.Dispatch(kernal, heightLutTextureSize.x / 32, heightLutTextureSize.y / 32, 1);
}
EnsureRenderTarget(ref hiHeightTexture, 512, 512, RenderTextureFormat.RFloat, configuration.weatherTex.filterMode, wrapMode: configuration.weatherTex.wrapMode, randomWrite: true, useMipmap:true);
EnsureArray(ref hiHeightTempTextures, 10);
for (int i = 0; i <= 9; i++) {
EnsureRenderTarget(ref hiHeightTempTextures[i], 512 >> i, 512 >> i, RenderTextureFormat.RFloat, FilterMode.Point);
}
//RenderTexture previousLevel = null;//Previous level hi-height map.
//EnsureRenderTarget(ref previousLevel, 512, 512, RenderTextureFormat.RFloat, FilterMode.Point, randomWrite: true); //The first level is same size as weather tex.
this.heightDownsampleMat.SetTexture("_WeatherTex", this.configuration.weatherTex);
this.heightDownsampleMat.SetTexture("_HeightLut", this.heightLutTexture);
Graphics.Blit(null, hiHeightTempTextures[0], this.heightDownsampleMat, 0); //The first pass convert weather tex into height map.
Graphics.CopyTexture(hiHeightTempTextures[0], 0, 0, hiHeightTexture, 0, 0); //Copy first level into target texture.
for (int i = 1; i <= Mathf.Min(9, hiHeightLevelRange.y); i++) {
Graphics.Blit(hiHeightTempTextures[i - 1], hiHeightTempTextures[i], this.heightDownsampleMat, 1);
Graphics.CopyTexture(hiHeightTempTextures[i], 0, 0, hiHeightTexture, 0, i);
}
RenderTexture.active = defaultTarget;
}
[ImageEffectOpaque]
private void OnRenderImage(RenderTexture source, RenderTexture destination) {
if (this.configuration == null || cloudShader == null) {
Graphics.Blit(source, destination);
return;
}
mcam = GetComponent<Camera>();
var width = mcam.pixelWidth >> downSample;
var height = mcam.pixelHeight >> downSample;
this.EnsureMaterial();
this.configuration.ApplyToMaterial(this.mat);
EnsureArray(ref fullBuffer, 2);
firstFrame |= EnsureRenderTarget(ref fullBuffer[0], width, height, RenderTextureFormat.ARGBFloat, FilterMode.Bilinear);
firstFrame |= EnsureRenderTarget(ref fullBuffer[1], width, height, RenderTextureFormat.ARGBFloat, FilterMode.Bilinear);
firstFrame |= EnsureRenderTarget(ref undersampleBuffer, width , height, RenderTextureFormat.ARGBFloat, FilterMode.Bilinear);
frameIndex = (frameIndex + 1)% 16;
fullBufferIndex = (fullBufferIndex + 1) % 2;
if (useHierarchicalHeightMap) {
GenerateHierarchicalHeightMap();
mat.EnableKeyword("USE_HI_HEIGHT");
mat.SetTexture("_HiHeightMap", this.hiHeightTexture);
mat.SetInt("_HeightMapSize", this.hiHeightTexture.width);
mat.SetInt("_HiHeightMinLevel", this.hiHeightLevelRange.x);
mat.SetInt("_HiHeightMaxLevel", this.hiHeightLevelRange.y);
} else {
mat.DisableKeyword("USE_HI_HEIGHT");
}
/* Some code is from playdead TAA. */
//1. Pass1, Render a undersampled buffer. The buffer is dithered using bayer matrix(every 3x3 pixel) and halton sequence.
//If it's first frame, force a high quality sample to make the initial history buffer good enough.
if (firstFrame || quality == Quality.High) {
mat.EnableKeyword("HIGH_QUALITY");
mat.DisableKeyword("MEDIUM_QUALITY");
mat.DisableKeyword("LOW_QUALITY");
} else if (quality == Quality.Normal) {
mat.DisableKeyword("HIGH_QUALITY");
mat.EnableKeyword("MEDIUM_QUALITY");
mat.DisableKeyword("LOW_QUALITY");
} else if (quality == Quality.Low) {
mat.DisableKeyword("HIGH_QUALITY");
mat.DisableKeyword("MEDIUM_QUALITY");
mat.EnableKeyword("LOW_QUALITY");
}
if (useApSystem) {
mat.EnableKeyword("USE_YANGRC_AP");
} else {
mat.DisableKeyword("USE_YANGRC_AP");
}
if (allowCloudFrontObject) {
mat.EnableKeyword("ALLOW_CLOUD_FRONT_OBJECT");
} else {
mat.DisableKeyword("ALLOW_CLOUD_FRONT_OBJECT");
}
mat.SetVector("_ProjectionExtents", mcam.GetProjectionExtents());
mat.SetFloat("_RaymarchOffset", sequence.Get());
mat.SetVector("_TexelSize", undersampleBuffer.texelSize);
Graphics.Blit(null, undersampleBuffer, mat, 0);
//2. Pass 2, blend undersampled image with history buffer to new buffer.
mat.SetTexture("_UndersampleCloudTex", undersampleBuffer);
mat.SetMatrix("_PrevVP", GL.GetGPUProjectionMatrix(mcam.projectionMatrix,false) * prevV);
mat.SetVector("_ProjectionExtents", mcam.GetProjectionExtents());
if (firstFrame) { //Wait, is this the first frame? If it is, the history buffer is empty yet. It will cause glitch if we use it directly. Fill it using the undersample buffer.
Graphics.Blit(undersampleBuffer, fullBuffer[fullBufferIndex]);
}
Graphics.Blit(fullBuffer[fullBufferIndex], fullBuffer[fullBufferIndex ^ 1], mat, 1);
//3. Pass3, Calculate lighting, blend final cloud image with final image.
mat.SetTexture("_CloudTex", fullBuffer[fullBufferIndex ^ 1]);
Graphics.Blit(source, destination, mat, 2);
//4. Cleanup
prevV = mcam.worldToCameraMatrix;
firstFrame = false;
}
}
}

View file

@ -0,0 +1,19 @@
fileFormatVersion: 2
guid: 9f1db61281524fb40bc9a1f6acce6baf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences:
- cloudShader: {fileID: 4800000, guid: 84e97169589c5e94e8521fe4deb2e3f8, type: 3}
- configuration: {fileID: 11400000, guid: a15886e93ac3e7847861da392782f13d, type: 2}
- heightPreprocessShader: {fileID: 7200000, guid: 40276bd53f35a694e8d4e025ca310775,
type: 3}
- cloudHeightProcessShader: {fileID: 4800000, guid: b80bf04ca56d3d642a6b925cff3f5ddf,
type: 3}
- heightLutTexture: {instanceID: 0}
- hiHeightTexture: {instanceID: 0}
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yangrc.VolumeCloud {
public class VolumeCloudWindowConfigurations : ScriptableObject {
public CurlNoiseGenerator testGenerator;
public First3DTexGenerator first3DTexGenerator;
public string first3DTexSaveName = "First3DTex";
public Second3DTexGenerator second3DTexGenerator;
public string second3DTexSaveName = "Second3DTex";
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 165f864f325ab6a48beaeff3cfed3c43
timeCreated: 1521969835
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: