Procedural Planets

Integrated Sebastian Lague's Planet Generator
Created Tour function in SectorRenderer which makes the camera follow each terrestrial planet in turn
This commit is contained in:
Reinier Vens 2020-12-05 23:43:02 +00:00
commit 53a91351b7
332 changed files with 11904 additions and 78 deletions

View file

@ -0,0 +1,45 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
[CreateAssetMenu (menuName = "Celestial Body/Textures/Noise")]
public class NoiseGenerator : TextureGenerator {
public SimpleNoiseSettings noiseSettings;
public SimpleNoiseSettings warpNoiseSettings;
[Range (0, 1)]
public float valueFloor;
public bool normalize;
ComputeBuffer minMaxBuffer;
protected override void Run () {
var prng = new PRNG (seed);
var offset = new Vector4 (prng.Value (), prng.Value (), prng.Value (), prng.Value ()) * 10;
ComputeHelper.CreateStructuredBuffer<int> (ref minMaxBuffer, 2);
minMaxBuffer.SetData (new int[] { int.MaxValue, 0 });
compute.SetBuffer (0, "minMax", minMaxBuffer);
compute.SetBuffer (1, "minMax", minMaxBuffer);
int threadGroupSize = ComputeHelper.GetThreadGroupSizes (compute, 0).x;
int numThreadGroups = Mathf.CeilToInt ((float) textureSize / threadGroupSize);
compute.SetVector ("offset", offset);
compute.SetTexture (0, "Result", renderTexture);
noiseSettings.SetComputeValues (compute, prng, "_simple");
warpNoiseSettings.SetComputeValues (compute, prng, "_warp");
compute.SetInt ("resolution", (int) textureSize);
compute.SetFloat ("valueFloor", valueFloor);
compute.Dispatch (0, numThreadGroups, numThreadGroups, 1);
// Normalize
if (normalize) {
compute.SetTexture (1, "Result", renderTexture);
compute.Dispatch (1, numThreadGroups, numThreadGroups, 1);
}
ComputeHelper.Release (minMaxBuffer);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bdb453923c7684a56a87ac2658c30fe0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,108 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu (menuName = "Celestial Body/Textures/Spots")]
public class SpotsTexture : TextureGenerator {
[Header ("Spot Settings")]
public float fadeDst = 1;
public float smoothing;
[Range (0, 1)]
public float background = 0;
public Layer[] layers;
public bool blur;
public ComputeShader blurCompute;
[Range (1, 20)]
public int blurSize = 1;
public bool invertAlpha;
public float initialRadius;
public float radiusMultiplier;
protected override void Run () {
const float dstScale = 0.001f;
var spots = new List<Vector4> ();
Random.InitState (seed);
int layerIndex = 0;
float currentRadius = initialRadius * dstScale;
foreach (Layer layer in layers) {
float cellSize = 1f / layer.numCellsPerAxis;
//float currentRadius = layer.radius * dstScale;
float alpha = Mathf.Lerp (0, 1 - background, layer.alpha);
layerIndex++;
alpha = layerIndex / (float) layers.Length;
if (invertAlpha) {
alpha = 1 - alpha;
}
Debug.Log (alpha + " " + currentRadius);
for (int x = 0; x < layer.numCellsPerAxis; x++) {
for (int y = 0; y < layer.numCellsPerAxis; y++) {
Vector2 randomOffset = new Vector2 (Random.value, Random.value) * cellSize;
Vector2 cellCorner = new Vector2 (x, y) * cellSize;
Vector2 pos = cellCorner + randomOffset;
var spot = new Vector4 (pos.x, pos.y, currentRadius, alpha);
spots.Add (spot);
// mirror
float mirrorX = (pos.x < .5f) ? pos.x + 1 : pos.x - 1;
float mirrorY = (pos.y < .5f) ? pos.y + 1 : pos.y - 1;
float mirrorDstX = Mathf.Min (Mathf.Abs (mirrorX - 1), Mathf.Abs (mirrorX));
float mirrorDstY = Mathf.Min (Mathf.Abs (mirrorY - 1), Mathf.Abs (mirrorY));
if (mirrorDstX < currentRadius) {
spots.Add (new Vector4 (mirrorX, pos.y, currentRadius, alpha));
}
if (mirrorDstY < currentRadius) {
spots.Add (new Vector4 (pos.x, mirrorY, currentRadius, alpha));
}
if (mirrorDstX < currentRadius && mirrorDstY < currentRadius) {
spots.Add (new Vector4 (mirrorX, mirrorY, currentRadius, alpha));
}
}
}
currentRadius *= radiusMultiplier;
}
if (spots.Count > 0) {
var spotBuffer = ComputeHelper.CreateAndSetBuffer<Vector4> (spots.ToArray (), compute, "spots");
compute.SetTexture (0, "Result", renderTexture);
compute.SetInt ("numSpots", spots.Count);
compute.SetInt ("resolution", renderTexture.width);
compute.SetFloat ("fadeDst", fadeDst * dstScale);
compute.SetFloat ("smoothing", smoothing);
compute.SetFloat ("background", background);
ComputeHelper.Run (compute, renderTexture.width, renderTexture.height);
ComputeHelper.Release (spotBuffer);
if (blur && blurCompute) {
var unblurredTexture = new RenderTexture (renderTexture);
Graphics.CopyTexture (renderTexture, unblurredTexture);
blurCompute.SetTexture (0, "SourceTex", unblurredTexture);
blurCompute.SetTexture (0, "Result", renderTexture);
blurCompute.SetInt ("blurSize", blurSize);
blurCompute.SetInt ("textureSize", (int) textureSize);
ComputeHelper.Run (blurCompute, renderTexture.width, renderTexture.height);
unblurredTexture.Release ();
}
} else {
Debug.Log ("No points set in layers");
}
}
[System.Serializable]
public class Layer {
public int numCellsPerAxis = 10;
public float radius = 3;
[Range (0, 1)]
public float alpha = 1;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 06a813b0ccba3410b9a549474923d94b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextureCombiner : MonoBehaviour {
public Texture2D red;
public Texture2D green;
public Texture2D blue;
public Texture2D alpha;
void Update () {
}
public void SaveTexture (string path) {
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6262023da795b40d0b5b2401e31682a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,66 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
public abstract class TextureGenerator : ScriptableObject {
public enum TextureSize { u128 = 128, u256 = 256, u512 = 512, u1024 = 1024, u2048 = 2048, u4096 = 4096 }
[Header ("Texture Settings")]
public string textureName = "Unnamed";
public TextureSize textureSize = TextureSize.u512;
public FilterMode filterMode = FilterMode.Bilinear;
public GraphicsFormat format = GraphicsFormat.R8G8B8A8_UNorm;
public bool useMips = true;
[Header ("Compute Settings")]
public ComputeShader compute;
public int seed;
public Vector4 testParams;
[SerializeField, HideInInspector]
protected RenderTexture renderTexture;
public RenderTexture GenerateTexture () {
int resolution = (int) textureSize;
CreateTexture (ref renderTexture, resolution, textureName);
if (compute) {
compute.SetVector ("params", testParams);
Run ();
if (useMips) {
renderTexture.GenerateMips ();
}
}
return renderTexture;
}
protected abstract void Run ();
void CreateTexture (ref RenderTexture texture, int resolution, string name) {
if (texture == null || !texture.IsCreated () || texture.width != resolution || texture.height != resolution || texture.useMipMap != useMips || texture.graphicsFormat != format) {
if (texture != null) {
texture.Release ();
}
texture = new RenderTexture (resolution, resolution, 0);
texture.graphicsFormat = format;
texture.enableRandomWrite = true;
texture.autoGenerateMips = false;
texture.useMipMap = useMips;
texture.Create ();
}
texture.wrapMode = TextureWrapMode.Repeat;
texture.filterMode = filterMode;
texture.name = name;
}
void OnDestroy () {
if (renderTexture != null) {
renderTexture.Release ();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dd616f6062740499daa6966dfc839377
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,58 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class TextureViewer : MonoBehaviour {
public TextureGenerator generator;
[Header ("Preview Settings")]
[Range (1, 10)]
public int tiling = 1;
public Material previewMaterial;
public MeshRenderer[] previewObjects;
void Update () {
UpdateTexture ();
}
public void UpdateTexture () {
if (generator) {
var renderTexture = generator.GenerateTexture ();
DisplayPreview (renderTexture);
}
}
void DisplayPreview (RenderTexture renderTexture) {
if (previewMaterial == null) {
previewMaterial = new Material (Shader.Find ("Unlit/Texture"));
}
previewMaterial.SetTexture ("_MainTex", renderTexture);
previewMaterial.mainTextureScale = new Vector2 (tiling, tiling);
if (previewObjects != null) {
foreach (var previewObject in previewObjects) {
if (previewObject) {
previewObject.sharedMaterial = previewMaterial;
}
}
}
}
public void SaveTexture (string path) {
if (generator) {
var renderTexture = generator.GenerateTexture ();
var oldRT = RenderTexture.active;
var tex2D = new Texture2D (renderTexture.width, renderTexture.height);
RenderTexture.active = renderTexture;
tex2D.ReadPixels (new Rect (0, 0, renderTexture.width, renderTexture.height), 0, 0);
tex2D.Apply ();
System.IO.File.WriteAllBytes (System.IO.Path.Combine (path, renderTexture.name + ".png"), tex2D.EncodeToPNG ());
RenderTexture.active = oldRT;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5f6d47056bddf4361b0f9b6453e34fa2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: