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,33 @@
#include "/Math.cginc"
struct Crater {
float3 centre;
float radius;
float floor;
float smoothness;
};
// Crater data and settings
StructuredBuffer<Crater> craters;
int numCraters;
float floorHeight;
float rimSteepness;
float rimWidth;
float smoothFactor;
float calculateCraterDepth(float3 vertexPos) {
float craterHeight = 0;
for (int i = 0; i < numCraters; i ++) {
float x = length(vertexPos - craters[i].centre) / max(craters[i].radius, 0.0001);
float cavity = x * x - 1;
float rimX = min(x - 1 - rimWidth, 0);
float rim = rimSteepness * rimX * rimX;
float craterShape = smoothMax(cavity, craters[i].floor, craters[i].smoothness);
craterShape = smoothMin(craterShape, rim, craters[i].smoothness);
craterHeight += craterShape * craters[i].radius;
}
return craterHeight;
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: de61f86c990f74bca9aff6a6c95ce22d
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,258 @@
#include "/SimplexNoise.cginc"
float simpleNoise(float3 pos, int numLayers, float scale, float persistence, float lacunarity, float multiplier) {
float noiseSum = 0;
float amplitude = 1;
float frequency = scale;
for (int i = 0; i < numLayers; i ++) {
noiseSum += snoise(pos * frequency) * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return noiseSum * multiplier;
}
float simpleNoise(float3 pos, float scale, float multiplier) {
const int numLayers = 4;
const float persistence = .5;
const float lacunarity = 2;
return simpleNoise(pos, numLayers, scale, persistence, lacunarity, multiplier);
}
float simpleNoise(float3 pos) {
const float scale = 1;
const float multiplier = 1;
return simpleNoise(pos, scale, multiplier);
}
float simpleNoise(float3 pos, float4 params[3]) {
// Extract parameters for readability
float3 offset = params[0].xyz;
int numLayers = int(params[0].w);
float persistence = params[1].x;
float lacunarity = params[1].y;
float scale = params[1].z;
float multiplier = params[1].w;
float verticalShift = params[2].x;
// Sum up noise layers
float noiseSum = 0;
float amplitude = 1;
float frequency = scale;
for (int i = 0; i < numLayers; i ++) {
noiseSum += snoise(pos * frequency + offset) * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return noiseSum * multiplier + verticalShift;
}
// Simple noise, but with smaller values dampened
float dampenedSimpleNoise(float3 pos, float4 params[3]) {
// Parameters
float3 offset = params[0].xyz;
int numLayers = int(params[0].w);
float persistence = params[1].x;
float lacunarity = params[1].y;
float scale = params[1].z;
float multiplier = params[1].w;
float gain = 1.2;
// Sum up noise layers
float noiseSum = 0;
float amplitude = 1;
float frequency = scale;
float weight = 1;
for (int i = 0; i < numLayers; i ++) {
float noiseVal = snoise(pos * frequency + offset) * .5 + .5;
noiseVal *= weight;
weight = saturate(noiseVal * gain);
noiseSum += noiseVal * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return noiseSum * multiplier;
}
// Noise very roughly in range 0-1
float continentNoise(float3 pos, float4 params[3]) {
// Parameters
float3 offset = params[0].xyz;
int numLayers = int(params[0].w);
float persistence = params[1].x;
float lacunarity = params[1].y;
float scale = params[1].z;
float multiplier = params[1].w;
float gain = 1;
// Sum up noise layers
float noiseSum = 0;
float amplitude = 1;
float frequency = scale;
float weight = 1;
for (int i = 0; i < numLayers; i ++) {
float noiseVal = snoise(pos * frequency + offset) * .5 + .5;
noiseVal *= weight;
weight = saturate(noiseVal * gain);
noiseSum += noiseVal * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return noiseSum;
}
float simpleNoise2(float3 pos, float4 params[3]) {
// Parameters
float3 offset = params[0].xyz;
int numLayers = int(params[0].w);
float persistence = params[1].x;
float lacunarity = params[1].y;
float scale = params[1].z;
float multiplier = params[1].w;
// Sum up noise layers
float noiseSum = 0;
float amplitude = 1;
float frequency = scale;
for (int i = 0; i < numLayers; i ++) {
float noiseVal = snoise(pos * frequency + offset);
noiseSum += (noiseVal+1)*.5 * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return noiseSum;
}
float simpleNoiseMusgrave(float3 pos, float4 params[3], float o) {
// Parameters
float3 offset = params[0].xyz;
int numLayers = int(params[0].w);
float persistence = params[1].x;
float lacunarity = params[1].y;
float scale = params[1].z;
float multiplier = params[1].w;
// first octave
float result = snoise(pos) + o;
pos *= lacunarity;
// Sum up noise layers
float amplitude = 1;
float frequency = scale * lacunarity;
float weight = result;
for (int i = 1; i < numLayers; i ++) {
weight = min(1, weight);
float signal = (snoise(pos * frequency) + o) * pow (abs(frequency), -persistence);
result += weight * signal;
weight *= signal;
//amplitude *= persistence;
frequency *= lacunarity;
pos *= lacunarity;
}
return result * multiplier;
}
float ridgidNoise(float3 pos, float4 params[3]) {
// Extract parameters for readability
float3 offset = params[0].xyz;
int numLayers = int(params[0].w);
float persistence = params[1].x;
float lacunarity = params[1].y;
float scale = params[1].z;
float multiplier = params[1].w;
float power = params[2].x;
float gain = params[2].y;
float verticalShift = params[2].z;
// Sum up noise layers
float noiseSum = 0;
float amplitude = 1;
float frequency = scale;
float ridgeWeight = 1;
for (int i = 0; i < numLayers; i ++) {
float noiseVal = 1 - abs(snoise(pos * frequency + offset));
noiseVal = pow(abs(noiseVal), power);
noiseVal *= ridgeWeight;
ridgeWeight = saturate(noiseVal * gain);
noiseSum += noiseVal * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return noiseSum * multiplier + verticalShift;
}
// Sample the noise several times at small offsets from the centre and average the result
// This reduces some of the harsh jaggedness that can occur
float smoothedRidgidNoise(float3 pos, float4 params[3]) {
float3 sphereNormal = normalize(pos);
float3 axisA = cross(sphereNormal, float3(0,1,0));
float3 axisB = cross(sphereNormal, axisA);
float offsetDst = params[2].w * 0.01;
float sample0 = ridgidNoise(pos, params);
float sample1 = ridgidNoise(pos - axisA * offsetDst, params);
float sample2 = ridgidNoise(pos + axisA * offsetDst, params);
float sample3 = ridgidNoise(pos - axisB * offsetDst, params);
float sample4 = ridgidNoise(pos + axisB * offsetDst, params);
return (sample0 + sample1 + sample2 + sample3 + sample4) / 5;
}
float ridgidNoise2(float3 pos, float4 params[3]) {
// Parameters
float3 offset = params[0].xyz;
int numLayers = int(params[0].w);
float persistence = params[1].x;
float lacunarity = params[1].y;
float scale = params[1].z;
float multiplier = params[1].w;
float power = params[2].x;
// Sum up noise layers
float noiseSum = 0;
float amplitude = 1;
float frequency = scale;
float ridgeWeight = 1;
for (int i = 0; i < numLayers; i ++) {
float noiseVal = 1 - abs(snoise(pos * frequency + offset));
noiseVal = (noiseVal * .5 + .5);
noiseVal *= ridgeWeight;
ridgeWeight = saturate(noiseVal);
noiseSum += noiseVal * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
noiseSum = 1 - abs(noiseSum*2-1);
noiseSum = pow(abs(noiseSum), power);
return noiseSum * multiplier;
}
float4 fractalNoiseGrad(float3 pos, int numLayers, float scale, float persistence, float lacunarity) {
float4 noise = 0;
float amplitude = 1;
float frequency = scale;
for (int i = 0; i < numLayers; i ++) {
noise += snoise_grad(pos * frequency) * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return noise;
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 7a4d381262f094ec1ba9b87c715a7ae9
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,73 @@
static const float PI = 3.14159265359;
static const float TAU = PI * 2;
static const float maxFloat = 3.402823466e+38;
// Remap a value from one range to another
float remap(float v, float minOld, float maxOld, float minNew, float maxNew) {
return saturate(minNew + (v-minOld) * (maxNew - minNew) / (maxOld-minOld));
}
// Remap the components of a vector from one range to another
float4 remap(float4 v, float minOld, float maxOld, float minNew, float maxNew) {
return saturate(minNew + (v-minOld) * (maxNew - minNew) / (maxOld-minOld));//
}
// Remap a float value (with a known mininum and maximum) to a value between 0 and 1
float remap01(float v, float minOld, float maxOld) {
return saturate((v-minOld) / (maxOld-minOld));
}
// Remap a float2 value (with a known mininum and maximum) to a value between 0 and 1
float2 remap01(float2 v, float2 minOld, float2 maxOld) {
return saturate((v-minOld) / (maxOld-minOld));
}
// Smooth minimum of two values, controlled by smoothing factor k
// When k = 0, this behaves identically to min(a, b)
float smoothMin(float a, float b, float k) {
k = max(0, k);
// https://www.iquilezles.org/www/articles/smin/smin.htm
float h = max(0, min(1, (b - a + k) / (2 * k)));
return a * h + b * (1 - h) - k * h * (1 - h);
}
// Smooth maximum of two values, controlled by smoothing factor k
// When k = 0, this behaves identically to max(a, b)
float smoothMax(float a, float b, float k) {
k = min(0, -k);
float h = max(0, min(1, (b - a + k) / (2 * k)));
return a * h + b * (1 - h) - k * h * (1 - h);
}
float Blend(float startHeight, float blendDst, float height) {
return smoothstep(startHeight - blendDst / 2, startHeight + blendDst / 2, height);
}
// Returns vector (dstToSphere, dstThroughSphere)
// If ray origin is inside sphere, dstToSphere = 0
// If ray misses sphere, dstToSphere = maxValue; dstThroughSphere = 0
float2 raySphere(float3 sphereCentre, float sphereRadius, float3 rayOrigin, float3 rayDir) {
float3 offset = rayOrigin - sphereCentre;
float a = 1; // Set to dot(rayDir, rayDir) if rayDir might not be normalized
float b = 2 * dot(offset, rayDir);
float c = dot (offset, offset) - sphereRadius * sphereRadius;
float d = b * b - 4 * a * c; // Discriminant from quadratic formula
// Number of intersections: 0 when d < 0; 1 when d = 0; 2 when d > 0
if (d > 0) {
float s = sqrt(d);
float dstToSphereNear = max(0, (-b - s) / (2 * a));
float dstToSphereFar = (-b + s) / (2 * a);
// Ignore intersections that occur behind the ray
if (dstToSphereFar >= 0) {
return float2(dstToSphereNear, dstToSphereFar - dstToSphereNear);
}
}
// Ray did not intersect sphere
return float2(maxFloat, 0);
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 8dc954e584876428396a17a9271bc3f1
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,191 @@
// Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader
//
// Original work (webgl-noise) Copyright (C) 2011 Ashima Arts.
// Translation and modification was made by Keijiro Takahashi.
//
// This shader is based on the webgl-noise GLSL shader. For further details
// of the original shader, please see the following description from the
// original source code.
//
//
// Description : Array and textureless GLSL 2D/3D/4D simplex
// noise functions.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
float3 mod289(float3 x)
{
return x - floor(x / 289.0) * 289.0;
}
float4 mod289(float4 x)
{
return x - floor(x / 289.0) * 289.0;
}
float4 permute(float4 x)
{
return mod289((x * 34.0 + 1.0) * x);
}
float4 taylorInvSqrt(float4 r)
{
return 1.79284291400159 - r * 0.85373472095314;
}
float snoise(float3 v)
{
const float2 C = float2(1.0 / 6.0, 1.0 / 3.0);
// First corner
float3 i = floor(v + dot(v, C.yyy));
float3 x0 = v - i + dot(i, C.xxx);
// Other corners
float3 g = step(x0.yzx, x0.xyz);
float3 l = 1.0 - g;
float3 i1 = min(g.xyz, l.zxy);
float3 i2 = max(g.xyz, l.zxy);
// x1 = x0 - i1 + 1.0 * C.xxx;
// x2 = x0 - i2 + 2.0 * C.xxx;
// x3 = x0 - 1.0 + 3.0 * C.xxx;
float3 x1 = x0 - i1 + C.xxx;
float3 x2 = x0 - i2 + C.yyy;
float3 x3 = x0 - 0.5;
// Permutations
i = mod289(i); // Avoid truncation effects in permutation
float4 p =
permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0))
+ i.y + float4(0.0, i1.y, i2.y, 1.0))
+ i.x + float4(0.0, i1.x, i2.x, 1.0));
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float4 j = p - 49.0 * floor(p / 49.0); // mod(p,7*7)
float4 x_ = floor(j / 7.0);
float4 y_ = floor(j - 7.0 * x_); // mod(j,N)
float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0;
float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0;
float4 h = 1.0 - abs(x) - abs(y);
float4 b0 = float4(x.xy, y.xy);
float4 b1 = float4(x.zw, y.zw);
//float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0;
//float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0;
float4 s0 = floor(b0) * 2.0 + 1.0;
float4 s1 = floor(b1) * 2.0 + 1.0;
float4 sh = -step(h, 0.0);
float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
float3 g0 = float3(a0.xy, h.x);
float3 g1 = float3(a0.zw, h.y);
float3 g2 = float3(a1.xy, h.z);
float3 g3 = float3(a1.zw, h.w);
// Normalise gradients
float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3)));
g0 *= norm.x;
g1 *= norm.y;
g2 *= norm.z;
g3 *= norm.w;
// Mix final noise value
float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
m = m * m;
m = m * m;
float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3));
return 42.0 * dot(m, px);
}
float4 snoise_grad(float3 v)
{
const float2 C = float2(1.0 / 6.0, 1.0 / 3.0);
// First corner
float3 i = floor(v + dot(v, C.yyy));
float3 x0 = v - i + dot(i, C.xxx);
// Other corners
float3 g = step(x0.yzx, x0.xyz);
float3 l = 1.0 - g;
float3 i1 = min(g.xyz, l.zxy);
float3 i2 = max(g.xyz, l.zxy);
// x1 = x0 - i1 + 1.0 * C.xxx;
// x2 = x0 - i2 + 2.0 * C.xxx;
// x3 = x0 - 1.0 + 3.0 * C.xxx;
float3 x1 = x0 - i1 + C.xxx;
float3 x2 = x0 - i2 + C.yyy;
float3 x3 = x0 - 0.5;
// Permutations
i = mod289(i); // Avoid truncation effects in permutation
float4 p =
permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0))
+ i.y + float4(0.0, i1.y, i2.y, 1.0))
+ i.x + float4(0.0, i1.x, i2.x, 1.0));
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float4 j = p - 49.0 * floor(p / 49.0); // mod(p,7*7)
float4 x_ = floor(j / 7.0);
float4 y_ = floor(j - 7.0 * x_); // mod(j,N)
float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0;
float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0;
float4 h = 1.0 - abs(x) - abs(y);
float4 b0 = float4(x.xy, y.xy);
float4 b1 = float4(x.zw, y.zw);
//float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0;
//float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0;
float4 s0 = floor(b0) * 2.0 + 1.0;
float4 s1 = floor(b1) * 2.0 + 1.0;
float4 sh = -step(h, 0.0);
float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
float3 g0 = float3(a0.xy, h.x);
float3 g1 = float3(a0.zw, h.y);
float3 g2 = float3(a1.xy, h.z);
float3 g3 = float3(a1.zw, h.w);
// Normalise gradients
float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3)));
g0 *= norm.x;
g1 *= norm.y;
g2 *= norm.z;
g3 *= norm.w;
// Compute noise and gradient at P
float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
float4 m2 = m * m;
float4 m3 = m2 * m;
float4 m4 = m2 * m2;
float3 grad =
-6.0 * m3.x * x0 * dot(x0, g0) + m4.x * g0 +
-6.0 * m3.y * x1 * dot(x1, g1) + m4.y * g1 +
-6.0 * m3.z * x2 * dot(x2, g2) + m4.z * g2 +
-6.0 * m3.w * x3 * dot(x3, g3) + m4.w * g3;
float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3));
return 42.0 * float4(grad, dot(m4, px));
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 7ab50d4af95614b3c9e1f4b079a25ec7
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,440 @@
#ifndef NOISE_SIMPLEX_FUNC
#define NOISE_SIMPLEX_FUNC
/*
Description:
Array- and textureless CgFx/HLSL 2D, 3D and 4D simplex noise functions.
a.k.a. simplified and optimized Perlin noise.
The functions have very good performance
and no dependencies on external data.
2D - Very fast, very compact code.
3D - Fast, compact code.
4D - Reasonably fast, reasonably compact code.
------------------------------------------------------------------
Ported by:
Lex-DRL
I've ported the code from GLSL to CgFx/HLSL for Unity,
added a couple more optimisations (to speed it up even further)
and slightly reformatted the code to make it more readable.
Original GLSL functions:
https://github.com/ashima/webgl-noise
Credits from original glsl file are at the end of this cginc.
------------------------------------------------------------------
Usage:
float ns = snoise(v);
// v is any of: float2, float3, float4
Return type is float.
To generate 2 or more components of noise (colorful noise),
call these functions several times with different
constant offsets for the arguments.
E.g.:
float3 colorNs = float3(
snoise(v),
snoise(v + 17.0),
snoise(v - 43.0),
);
Remark about those offsets from the original author:
People have different opinions on whether these offsets should be integers
for the classic noise functions to match the spacing of the zeroes,
so we have left that for you to decide for yourself.
For most applications, the exact offsets don't really matter as long
as they are not too small or too close to the noise lattice period
(289 in this implementation).
*/
// 1 / 289
#define NOISE_SIMPLEX_1_DIV_289 0.00346020761245674740484429065744f
float mod289(float x) {
return x - floor(x * NOISE_SIMPLEX_1_DIV_289) * 289.0;
}
float2 mod289(float2 x) {
return x - floor(x * NOISE_SIMPLEX_1_DIV_289) * 289.0;
}
float3 mod289(float3 x) {
return x - floor(x * NOISE_SIMPLEX_1_DIV_289) * 289.0;
}
float4 mod289(float4 x) {
return x - floor(x * NOISE_SIMPLEX_1_DIV_289) * 289.0;
}
// ( x*34.0 + 1.0 )*x =
// x*x*34.0 + x
float permute(float x) {
return mod289(
x*x*34.0 + x
);
}
float3 permute(float3 x) {
return mod289(
x*x*34.0 + x
);
}
float4 permute(float4 x) {
return mod289(
x*x*34.0 + x
);
}
float4 grad4(float j, float4 ip)
{
const float4 ones = float4(1.0, 1.0, 1.0, -1.0);
float4 p, s;
p.xyz = floor( frac(j * ip.xyz) * 7.0) * ip.z - 1.0;
p.w = 1.5 - dot( abs(p.xyz), ones.xyz );
// GLSL: lessThan(x, y) = x < y
// HLSL: 1 - step(y, x) = x < y
p.xyz -= sign(p.xyz) * (p.w < 0);
return p;
}
// ----------------------------------- 2D -------------------------------------
float snoise(float2 v)
{
const float4 C = float4(
0.211324865405187, // (3.0-sqrt(3.0))/6.0
0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
-0.577350269189626, // -1.0 + 2.0 * C.x
0.024390243902439 // 1.0 / 41.0
);
// First corner
float2 i = floor( v + dot(v, C.yy) );
float2 x0 = v - i + dot(i, C.xx);
// Other corners
// float2 i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0);
// Lex-DRL: afaik, step() in GPU is faster than if(), so:
// step(x, y) = x <= y
// Actually, a simple conditional without branching is faster than that madness :)
int2 i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0);
float4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
// Permutations
i = mod289(i); // Avoid truncation effects in permutation
float3 p = permute(
permute(
i.y + float3(0.0, i1.y, 1.0 )
) + i.x + float3(0.0, i1.x, 1.0 )
);
float3 m = max(
0.5 - float3(
dot(x0, x0),
dot(x12.xy, x12.xy),
dot(x12.zw, x12.zw)
),
0.0
);
m = m*m ;
m = m*m ;
// Gradients: 41 points uniformly over a line, mapped onto a diamond.
// The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
float3 x = 2.0 * frac(p * C.www) - 1.0;
float3 h = abs(x) - 0.5;
float3 ox = floor(x + 0.5);
float3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt( a0*a0 + h*h );
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
// Compute final noise value at P
float3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
// ----------------------------------- 3D -------------------------------------
float snoise(float3 v)
{
const float2 C = float2(
0.166666666666666667, // 1/6
0.333333333333333333 // 1/3
);
const float4 D = float4(0.0, 0.5, 1.0, 2.0);
// First corner
float3 i = floor( v + dot(v, C.yyy) );
float3 x0 = v - i + dot(i, C.xxx);
// Other corners
float3 g = step(x0.yzx, x0.xyz);
float3 l = 1 - g;
float3 i1 = min(g.xyz, l.zxy);
float3 i2 = max(g.xyz, l.zxy);
float3 x1 = x0 - i1 + C.xxx;
float3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
float3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
// Permutations
i = mod289(i);
float4 p = permute(
permute(
permute(
i.z + float4(0.0, i1.z, i2.z, 1.0 )
) + i.y + float4(0.0, i1.y, i2.y, 1.0 )
) + i.x + float4(0.0, i1.x, i2.x, 1.0 )
);
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float n_ = 0.142857142857; // 1/7
float3 ns = n_ * D.wyz - D.xzx;
float4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
float4 x_ = floor(j * ns.z);
float4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
float4 x = x_ *ns.x + ns.yyyy;
float4 y = y_ *ns.x + ns.yyyy;
float4 h = 1.0 - abs(x) - abs(y);
float4 b0 = float4( x.xy, y.xy );
float4 b1 = float4( x.zw, y.zw );
//float4 s0 = float4(lessThan(b0,0.0))*2.0 - 1.0;
//float4 s1 = float4(lessThan(b1,0.0))*2.0 - 1.0;
float4 s0 = floor(b0)*2.0 + 1.0;
float4 s1 = floor(b1)*2.0 + 1.0;
float4 sh = -step(h, 0.0);
float4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
float4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
float3 p0 = float3(a0.xy,h.x);
float3 p1 = float3(a0.zw,h.y);
float3 p2 = float3(a1.xy,h.z);
float3 p3 = float3(a1.zw,h.w);
//Normalise gradients
float4 norm = rsqrt(float4(
dot(p0, p0),
dot(p1, p1),
dot(p2, p2),
dot(p3, p3)
));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
// Mix final noise value
float4 m = max(
0.6 - float4(
dot(x0, x0),
dot(x1, x1),
dot(x2, x2),
dot(x3, x3)
),
0.0
);
m = m * m;
return 42.0 * dot(
m*m,
float4(
dot(p0, x0),
dot(p1, x1),
dot(p2, x2),
dot(p3, x3)
)
);
}
// ----------------------------------- 4D -------------------------------------
float snoise(float4 v)
{
const float4 C = float4(
0.138196601125011, // (5 - sqrt(5))/20 G4
0.276393202250021, // 2 * G4
0.414589803375032, // 3 * G4
-0.447213595499958 // -1 + 4 * G4
);
// First corner
float4 i = floor(
v +
dot(
v,
0.309016994374947451 // (sqrt(5) - 1) / 4
)
);
float4 x0 = v - i + dot(i, C.xxxx);
// Other corners
// Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
float4 i0;
float3 isX = step( x0.yzw, x0.xxx );
float3 isYZ = step( x0.zww, x0.yyz );
i0.x = isX.x + isX.y + isX.z;
i0.yzw = 1.0 - isX;
i0.y += isYZ.x + isYZ.y;
i0.zw += 1.0 - isYZ.xy;
i0.z += isYZ.z;
i0.w += 1.0 - isYZ.z;
// i0 now contains the unique values 0,1,2,3 in each channel
float4 i3 = saturate(i0);
float4 i2 = saturate(i0-1.0);
float4 i1 = saturate(i0-2.0);
// x0 = x0 - 0.0 + 0.0 * C.xxxx
// x1 = x0 - i1 + 1.0 * C.xxxx
// x2 = x0 - i2 + 2.0 * C.xxxx
// x3 = x0 - i3 + 3.0 * C.xxxx
// x4 = x0 - 1.0 + 4.0 * C.xxxx
float4 x1 = x0 - i1 + C.xxxx;
float4 x2 = x0 - i2 + C.yyyy;
float4 x3 = x0 - i3 + C.zzzz;
float4 x4 = x0 + C.wwww;
// Permutations
i = mod289(i);
float j0 = permute(
permute(
permute(
permute(i.w) + i.z
) + i.y
) + i.x
);
float4 j1 = permute(
permute(
permute(
permute (
i.w + float4(i1.w, i2.w, i3.w, 1.0 )
) + i.z + float4(i1.z, i2.z, i3.z, 1.0 )
) + i.y + float4(i1.y, i2.y, i3.y, 1.0 )
) + i.x + float4(i1.x, i2.x, i3.x, 1.0 )
);
// Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
// 7*7*6 = 294, which is close to the ring size 17*17 = 289.
const float4 ip = float4(
0.003401360544217687075, // 1/294
0.020408163265306122449, // 1/49
0.142857142857142857143, // 1/7
0.0
);
float4 p0 = grad4(j0, ip);
float4 p1 = grad4(j1.x, ip);
float4 p2 = grad4(j1.y, ip);
float4 p3 = grad4(j1.z, ip);
float4 p4 = grad4(j1.w, ip);
// Normalise gradients
float4 norm = rsqrt(float4(
dot(p0, p0),
dot(p1, p1),
dot(p2, p2),
dot(p3, p3)
));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
p4 *= rsqrt( dot(p4, p4) );
// Mix contributions from the five corners
float3 m0 = max(
0.6 - float3(
dot(x0, x0),
dot(x1, x1),
dot(x2, x2)
),
0.0
);
float2 m1 = max(
0.6 - float2(
dot(x3, x3),
dot(x4, x4)
),
0.0
);
m0 = m0 * m0;
m1 = m1 * m1;
return 49.0 * (
dot(
m0*m0,
float3(
dot(p0, x0),
dot(p1, x1),
dot(p2, x2)
)
) + dot(
m1*m1,
float2(
dot(p3, x3),
dot(p4, x4)
)
)
);
}
// Credits from source glsl file:
//
// Description : Array and textureless GLSL 2D/3D/4D simplex
// noise functions.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
//
// The text from LICENSE file:
//
//
// Copyright (C) 2011 by Ashima Arts (Simplex noise)
// Copyright (C) 2011 by Stefan Gustavson (Classic noise)
//
// 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.
#endif

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 61fdef2195a524f9fab29698a024c2f7
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,130 @@
// Description : Array and textureless GLSL 2D/3D/4D simplex
// noise functions.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : stegu
// Lastmod : 20110822 (ijm)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
// https://github.com/stegu/webgl-noise
//
// Translated to HLSL
float4 mod289(float4 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0; }
float mod289(float x) {
return x - floor(x * (1.0 / 289.0)) * 289.0; }
float4 permute(float4 x) {
return mod289(((x*34.0)+1.0)*x);
}
float permute(float x) {
return mod289(((x*34.0)+1.0)*x);
}
float4 taylorInvSqrt(float4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
float taylorInvSqrt(float r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
float4 grad4(float j, float4 ip)
{
const float4 ones = float4(1.0, 1.0, 1.0, -1.0);
float4 p,s;
p.xyz = floor( frac (j* ip.xyz) * 7.0) * ip.z - 1.0;
p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
s = float4(p < 0);
p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
return p;
}
// (sqrt(5) - 1)/4 = F4, used once below
#define F4 0.309016994374947451
float snoise(float4 v)
{
const float4 C = float4( 0.138196601125011, // (5 - sqrt(5))/20 G4
0.276393202250021, // 2 * G4
0.414589803375032, // 3 * G4
-0.447213595499958); // -1 + 4 * G4
// First corner
float4 i = floor(v + dot(v, F4) );
float4 x0 = v - i + dot(i, C.xxxx);
// Other corners
// Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
float4 i0;
float3 isX = step( x0.yzw, x0.xxx );
float3 isYZ = step( x0.zww, x0.yyz );
// i0.x = dot( isX, float3( 1.0 ) );
i0.x = isX.x + isX.y + isX.z;
i0.yzw = 1.0 - isX;
// i0.y += dot( isYZ.xy, float2( 1.0 ) );
i0.y += isYZ.x + isYZ.y;
i0.zw += 1.0 - isYZ.xy;
i0.z += isYZ.z;
i0.w += 1.0 - isYZ.z;
// i0 now contains the unique values 0,1,2,3 in each channel
float4 i3 = saturate(i0);
float4 i2 = saturate(i0-1.0);
float4 i1 = saturate(i0-2.0);
// x0 = x0 - 0.0 + 0.0 * C.xxxx
// x1 = x0 - i1 + 1.0 * C.xxxx
// x2 = x0 - i2 + 2.0 * C.xxxx
// x3 = x0 - i3 + 3.0 * C.xxxx
// x4 = x0 - 1.0 + 4.0 * C.xxxx
float4 x1 = x0 - i1 + C.xxxx;
float4 x2 = x0 - i2 + C.yyyy;
float4 x3 = x0 - i3 + C.zzzz;
float4 x4 = x0 + C.wwww;
// Permutations
i = mod289(i);
float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
float4 j1 = permute( permute( permute( permute (
i.w + float4(i1.w, i2.w, i3.w, 1.0 ))
+ i.z + float4(i1.z, i2.z, i3.z, 1.0 ))
+ i.y + float4(i1.y, i2.y, i3.y, 1.0 ))
+ i.x + float4(i1.x, i2.x, i3.x, 1.0 ));
// Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
// 7*7*6 = 294, which is close to the ring size 17*17 = 289.
float4 ip = float4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;
float4 p0 = grad4(j0, ip);
float4 p1 = grad4(j1.x, ip);
float4 p2 = grad4(j1.y, ip);
float4 p3 = grad4(j1.z, ip);
float4 p4 = grad4(j1.w, ip);
// Normalise gradients
float4 norm = taylorInvSqrt(float4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
p4 *= taylorInvSqrt(dot(p4,p4));
// Mix contributions from the five corners
float3 m0 = max(0.6 - float3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0);
float2 m1 = max(0.6 - float2(dot(x3,x3), dot(x4,x4) ), 0.0);
m0 = m0 * m0;
m1 = m1 * m1;
return 49.0 * ( dot(m0*m0, float3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
+ dot(m1*m1, float2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: cd3204f7427fd4bd09c91ba886f63cec
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,100 @@
float4 triplanar(float3 vertPos, float3 normal, float scale, sampler2D tex) {
// Calculate triplanar coordinates
float2 uvX = vertPos.zy * scale;
float2 uvY = vertPos.xz * scale;
float2 uvZ = vertPos.xy * scale;
float4 colX = tex2D (tex, uvX);
float4 colY = tex2D (tex, uvY);
float4 colZ = tex2D (tex, uvZ);
// Square normal to make all values positive + increase blend sharpness
float3 blendWeight = normal * normal;
// Divide blend weight by the sum of its components. This will make x + y + z = 1
blendWeight /= dot(blendWeight, 1);
return colX * blendWeight.x + colY * blendWeight.y + colZ * blendWeight.z;
}
float4 triplanarOffset(float3 vertPos, float3 normal, float3 scale, sampler2D tex, float2 offset) {
float3 scaledPos = vertPos / scale;
float4 colX = tex2D (tex, scaledPos.zy + offset);
float4 colY = tex2D(tex, scaledPos.xz + offset);
float4 colZ = tex2D (tex,scaledPos.xy + offset);
// Square normal to make all values positive + increase blend sharpness
float3 blendWeight = normal * normal;
// Divide blend weight by the sum of its components. This will make x + y + z = 1
blendWeight /= dot(blendWeight, 1);
return colX * blendWeight.x + colY * blendWeight.y + colZ * blendWeight.z;
}
float3 ObjectToTangentVector(float4 tangent, float3 normal, float3 objectSpaceVector) {
float3 normalizedTangent = normalize(tangent.xyz);
float3 binormal = cross(normal, normalizedTangent) * tangent.w;
float3x3 rot = float3x3 (normalizedTangent, binormal, normal);
return mul(rot, objectSpaceVector);
}
// Reoriented Normal Mapping
// http://blog.selfshadow.com/publications/blending-in-detail/
// Altered to take normals (-1 to 1 ranges) rather than unsigned normal maps (0 to 1 ranges)
float3 blend_rnm(float3 n1, float3 n2)
{
n1.z += 1;
n2.xy = -n2.xy;
return n1 * dot(n1, n2) / n1.z - n2;
}
// Sample normal map with triplanar coordinates
// Returned normal will be in obj/world space (depending whether pos/normal are given in obj or world space)
// Based on: medium.com/@bgolus/normal-mapping-for-a-triplanar-shader-10bf39dca05a
float3 triplanarNormal(float3 vertPos, float3 normal, float3 scale, float2 offset, sampler2D normalMap) {
float3 absNormal = abs(normal);
// Calculate triplanar blend
float3 blendWeight = saturate(pow(normal, 4));
// Divide blend weight by the sum of its components. This will make x + y + z = 1
blendWeight /= dot(blendWeight, 1);
// Calculate triplanar coordinates
float2 uvX = vertPos.zy * scale + offset;
float2 uvY = vertPos.xz * scale + offset;
float2 uvZ = vertPos.xy * scale + offset;
// Sample tangent space normal maps
// UnpackNormal puts values in range [-1, 1] (and accounts for DXT5nm compression)
float3 tangentNormalX = UnpackNormal(tex2D(normalMap, uvX));
float3 tangentNormalY = UnpackNormal(tex2D(normalMap, uvY));
float3 tangentNormalZ = UnpackNormal(tex2D(normalMap, uvZ));
// Swizzle normals to match tangent space and apply reoriented normal mapping blend
tangentNormalX = blend_rnm(half3(normal.zy, absNormal.x), tangentNormalX);
tangentNormalY = blend_rnm(half3(normal.xz, absNormal.y), tangentNormalY);
tangentNormalZ = blend_rnm(half3(normal.xy, absNormal.z), tangentNormalZ);
// Apply input normal sign to tangent space Z
float3 axisSign = sign(normal);
tangentNormalX.z *= axisSign.x;
tangentNormalY.z *= axisSign.y;
tangentNormalZ.z *= axisSign.z;
// Swizzle tangent normals to match input normal and blend together
float3 outputNormal = normalize(
tangentNormalX.zyx * blendWeight.x +
tangentNormalY.xzy * blendWeight.y +
tangentNormalZ.xyz * blendWeight.z
);
return outputNormal;
}
float3 triplanarNormalTangentSpace(float3 vertPos, float3 normal, float3 scale, float4 tangent, sampler2D normalMap) {
float3 textureNormal = triplanarNormal(vertPos, normal, scale, 0, normalMap);
return ObjectToTangentVector(tangent, normal, textureNormal);
}
float3 triplanarNormalTangentSpace(float3 vertPos, float3 normal, float3 scale, float2 offset, float4 tangent, sampler2D normalMap) {
float3 textureNormal = triplanarNormal(vertPos, normal, scale, offset, normalMap);
return ObjectToTangentVector(tangent, normal, textureNormal);
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 31f13bf4eeb1a49068672a85ab42adad
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant: