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,8 @@
fileFormatVersion: 2
guid: 1e03537b7c5244b0882d8e7efc2c0abe
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

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

View file

@ -0,0 +1,87 @@
#pragma kernel CSMain
#include "../../Includes/Math.cginc"
RWTexture2D<float4> Result;
int textureSize;
int numOutScatteringSteps;
float atmosphereRadius;
float avgDensityHeight01;
float densityFalloff;
float4 params;
float densityAtPoint(float2 densitySamplePoint) {
float planetRadius = 1;
float2 planetCentre = 0;
float heightAboveSurface = length(densitySamplePoint - planetCentre) - planetRadius;
float height01 = heightAboveSurface / (atmosphereRadius - planetRadius);
float localDensity = exp(-height01 * densityFalloff) * (1 - height01);
return localDensity;
}
float opticalDepth(float2 rayOrigin, float2 rayDir, float rayLength) {
int numOpticalDepthPoints = numOutScatteringSteps;
float2 densitySamplePoint = rayOrigin;
float stepSize = rayLength / (numOpticalDepthPoints - 1);
float opticalDepth = 0;
for (int i = 0; i < numOpticalDepthPoints; i ++) {
float localDensity = densityAtPoint(densitySamplePoint);
opticalDepth += localDensity * stepSize;
densitySamplePoint += rayDir * stepSize;
}
return opticalDepth;
}
float calculateOutScattering(float2 inPoint, float2 outPoint) {
float planetRadius = 1;
float skinWidth = planetRadius / 1000.0;
float lightTravelDst = length(outPoint - inPoint);
float2 outScatterPoint = inPoint;
float2 rayDir = (outPoint - inPoint) / lightTravelDst;
float stepSize = (lightTravelDst - skinWidth) / (numOutScatteringSteps);
float outScatterAmount = 0;
for (int i = 0; i < numOutScatteringSteps; i ++) {
outScatterPoint += rayDir * stepSize;
// height at planet surface = 0, at furthest extent of atmosphere = 1
float height = length(outScatterPoint - 0) - planetRadius;//
float height01 = saturate(height / (atmosphereRadius - planetRadius));
outScatterAmount += exp(-height01 * densityFalloff) * stepSize;
}
return outScatterAmount;
}
[numthreads(8,8,1)]
void CSMain (uint2 id : SV_DispatchThreadID)
{
const float planetRadius = 1;
float2 uv = id / (float)textureSize;
float height01 = uv.y;
float angle = uv.x * PI;
//angle = (1-cos(angle))/2;
float2 dir = float2(sin(angle), cos(angle));
float y = -2 * uv.x + 1;
float x = sin(acos(y));
dir = float2(x,y);
float2 inPoint = float2(0, lerp(planetRadius, atmosphereRadius, height01));
float dstThroughAtmosphere = raySphere(0, atmosphereRadius, float3(inPoint,0), float3(dir,0)).y;
float2 outPoint = inPoint + dir * raySphere(0, atmosphereRadius, float3(inPoint,0), float3(dir,0)).y;
//float outScattering = calculateOutScattering(inPoint, outPoint);
float outScattering = opticalDepth(inPoint + dir * 0.0001, dir, dstThroughAtmosphere-0.0002);
Result[id.xy] = outScattering;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 463adb4526f5a48bdb8c71df4c6dd88d
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

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

View file

@ -0,0 +1,57 @@
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
#include "../../Includes/Craters.cginc"
//
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float> heights;
uint numVertices;
// Continent settings:
float oceanDepthMultiplier;
float oceanFloorDepth;
float oceanFloorSmoothing;
float mountainBlend;
// Old:
float4 noiseParams_continents[3];
float4 noiseParams_mask[3];
float4 noiseParams_mountains[3];
float4 noiseParams_warp[3];
float4 params;
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numVertices) {
return;
}
float3 pos = vertices[id];
float3 warpNoise = float3(simpleNoise(pos, noiseParams_warp),simpleNoise(pos-100, noiseParams_warp),simpleNoise(pos+100, noiseParams_warp)) * 0.01;
float3 warpedPos = pos + warpNoise;
float craterDepth = calculateCraterDepth(pos);
float continentShape = simpleNoise(pos + warpNoise * params.x, noiseParams_continents);
continentShape = smoothMax(continentShape, -oceanFloorDepth, oceanFloorSmoothing);
if (continentShape < 0) {
continentShape *= 1 + oceanDepthMultiplier;
}
float ridgeNoise = smoothedRidgidNoise(pos + warpNoise * params.y, noiseParams_mountains);
float mask = Blend(0, mountainBlend, simpleNoise(pos + warpNoise * params.z, noiseParams_mask));
// Calculate final height
float finalHeight = 1 + continentShape * 0.01 + ridgeNoise * 0.01 * mask + craterDepth;
//finalHeight = 1 + Blend(0, params.x, simpleNoise(pos, noiseParams_mask)) * 0.05;
// Set terrain data
heights[id] = finalHeight;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b33d87d0482194cf2a21c78c2cc775f8
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,51 @@
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
#include "../../Includes/Math.cginc"
//
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float> heights;
uint numVertices;
// Continent settings:
float oceanDepthMultiplier;
float oceanFloorDepth;
float oceanFloorSmoothing;
float mountainBlend;
float4 noiseParams_continents[3];
float4 noiseParams_mask[3];
float4 noiseParams_mountains[3];
float4 params;
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numVertices) {
return;
}
float3 pos = vertices[id];
float continentShape = simpleNoise(pos, noiseParams_continents);
continentShape = smoothMax(continentShape, -oceanFloorDepth, oceanFloorSmoothing);
if (continentShape < 0) {
continentShape *= 1 + oceanDepthMultiplier;
}
float ridgeNoise = smoothedRidgidNoise(pos, noiseParams_mountains);
float mask = Blend(0, mountainBlend, simpleNoise(pos, noiseParams_mask));
// Calculate final height
float finalHeight = 1 + continentShape * 0.01 + ridgeNoise * 0.01 * mask;
//finalHeight = 1 + Blend(0, params.x, simpleNoise(pos, noiseParams_mask)) * 0.05;
// Set terrain data
heights[id] = finalHeight;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d6df9e6cc324e48a892dd6a9cf741d65
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,69 @@
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
#include "../../Includes/Craters.cginc"
//
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float> heights;
uint numVertices;
// Continent settings:
float shoreSteepness;
float continentFlatness;
float continentLevel;
float2 maskMinMax;
float mountainBlend;
float4 noiseParams_continents[3];//
float4 noiseParams_mask[3];
// Ridge noise
float4 noiseParams_mountains[3];
float4 params;
float mountainSmoothing;
float calculateMountainNoise(float3 pos) {
float3 sphereNormal = pos;
float3 axisA = cross(sphereNormal, float3(0,1,0));
float3 axisB = cross(sphereNormal, axisA);
// 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 offsetDst = mountainSmoothing * 0.01;
float sample0 = ridgidNoise(pos, noiseParams_mountains);
float sample1 = ridgidNoise(pos - axisA * offsetDst, noiseParams_mountains);
float sample2 = ridgidNoise(pos + axisA * offsetDst, noiseParams_mountains);
float sample3 = ridgidNoise(pos - axisB * offsetDst, noiseParams_mountains);
float sample4 = ridgidNoise(pos + axisB * offsetDst, noiseParams_mountains);
return (sample0 + sample1 + sample2 + sample3 + sample4) / 5;
}
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numVertices) {
return;
}
float3 pos = vertices[id];
float continentShape = simpleNoise(pos, noiseParams_continents);
continentShape = continentShape * (1 + continentShape / continentLevel * shoreSteepness);
if (continentShape < 0) {
continentShape *= 1+mountainSmoothing * abs(continentShape);
}
float craterDepth = calculateCraterDepth(pos);
//float heightAboveShore = max(0, continentShape);
//float k = (1 - saturate(heightAboveShore / continentLevel)) * shoreSteepness;
// continentShape = smoothMin(continentShape, 0, shoreSteepness);
// Calculate final height
float finalHeight = 1 + continentShape * 0.01 + craterDepth;
// Set terrain data
heights[id] = finalHeight;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e32322f519a614092915aa9cbc4cb7c2
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,41 @@
#pragma kernel CSMain
#include "../../Includes/Craters.cginc"
#include "../../Includes/FractalNoise.cginc"
//
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float> heights;
uint numVertices;
float4 testParams;
// Noise settings
float4 noiseParams_shape[3];
float4 noiseParams_ridge[3];
float4 noiseParams_ridge2[3];
[numthreads(512, 1, 1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numVertices) { return; }
static const float elevationMultiplier = 0.01;
float3 pos = vertices[id];
// Craters
float craterDepth = calculateCraterDepth(pos);
// Low-frequency noise to add some slight deformation to the base sphere shape of the moon
float shapeNoise = simpleNoise(pos, noiseParams_shape);
// Ridge noise
float ridgeNoise = smoothedRidgidNoise(pos, noiseParams_ridge);
float ridge2 = smoothedRidgidNoise(pos, noiseParams_ridge2);
float noiseSum = (shapeNoise + ridgeNoise + ridge2) * elevationMultiplier;
float finalHeight = 1 + craterDepth + noiseSum;
heights[id] = finalHeight;
// Debug ridge mask
//terrainHeights[id] = 1 + ridgeMask * .1;
//terrainHeights[id] = 1 + ridgeNoise * elevationMultiplier;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1c8bff6c5dfd7461f8403bd2cef35625
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,44 @@
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
#include "../../Includes/Craters.cginc"
//
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float> heights;
uint numVertices;
float plateauHeight;
float plateauSmoothing;
float4 noiseParams_continents[3];
float4 noiseParams_mountains[3];
float4 noiseParams_mountains2[3];
float4 noiseParams_warp[3];
float4 params;
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numVertices) {
return;
}
float3 pos = vertices[id];
float3 warpNoise = float3(simpleNoise(pos, noiseParams_warp),simpleNoise(pos-100, noiseParams_warp),simpleNoise(pos+100, noiseParams_warp)) * 0.01;
float craterDepth = calculateCraterDepth(pos);
float continentShape = simpleNoise(pos + warpNoise, noiseParams_continents);
continentShape = smoothMin(continentShape, plateauHeight, plateauSmoothing);
float ridgeNoise = smoothedRidgidNoise(pos, noiseParams_mountains);
float ridgeNoise2 = smoothedRidgidNoise(pos, noiseParams_mountains2); //
// Calculate final height
float finalHeight = 1 + (continentShape + ridgeNoise + ridgeNoise2) * 0.01 + craterDepth;
//finalHeight = 1 + Blend(0, params.x, simpleNoise(pos, noiseParams_mask)) * 0.05;
// Set terrain data
heights[id] = finalHeight;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 13008f929df404f2c9c4d64443449aed
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

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

View file

@ -0,0 +1,69 @@
Shader "Hidden/OceanMask"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "../../Includes/Math.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 viewVector : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
// Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
// (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv.xy * 2 - 1, 0, -1));
o.viewVector = mul(unity_CameraToWorld, float4(viewVector,0));
return o;
}
sampler2D _MainTex;
static const int maxNumSpheres = 10;
float4 spheres[maxNumSpheres];
int numSpheres;
fixed4 frag (v2f i) : SV_Target
{
float3 rayOrigin = _WorldSpaceCameraPos;
float3 rayDir = normalize(i.viewVector);
//return float4(rayDir,1);
float nearest = 0;
for (int sphereIndex = 0; sphereIndex < numSpheres; sphereIndex ++) {
float2 hitInfo = raySphere(spheres[sphereIndex].xyz, spheres[sphereIndex].w, rayOrigin, rayDir);
if (hitInfo.y > 0) {
return 1;
}
}
return 0;
}
ENDCG
}
}
}

View file

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

View file

@ -0,0 +1,33 @@
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
RWStructuredBuffer<float3> points;
uint numPoints;
float maxStrength;
float3 perturb(float3 pos) {
float scale = 50;
float fx = simpleNoise(pos * 1, 2, scale, .5, 2, 1);
float fy = simpleNoise(pos * 2, 2, scale, .5, 2, 1);
float fz = simpleNoise(pos * 3, 2, scale, .5, 2, 1);
float3 offset = float3(fx, fy, fz);
offset = smoothstep(-1,1,offset) * 2 -1;
return offset;
}
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numPoints) {
return;
}
float3 pos = points[id];
float height = length(pos);
float3 offset = perturb(pos);
float3 newPos = pos + offset * maxStrength;
newPos = normalize(newPos) * height;
points[id] = newPos;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c95c667630d66481daf208e2a7a4d95d
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,29 @@
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
RWStructuredBuffer<float3> points;
uint numPoints;
float maxStrength;
float3 perturb(float3 pos) {
float3 noise = fractalNoiseGrad(pos, 4, 25, .5, 2).xyz;
noise = smoothstep(-1,1,noise) * 2 -1;
return noise;
}
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numPoints) {
return;
}
float3 pos = points[id];
float height = length(pos);
float3 offset = perturb(pos);
float3 newPos = pos + offset * maxStrength;
newPos = normalize(newPos) * height;
points[id] = newPos;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0cc4782d709194aa5a22ed5e9ab8bf09
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,25 @@
#pragma kernel CSMain
StructuredBuffer<float3> vertices;
StructuredBuffer<int> triangles;
RWStructuredBuffer<float> steepness;
int numTriangles;
[numthreads(256,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if ((int)id >= numTriangles) {
return;
}
int triangleIndex = id * 3;
float3 a = vertices[triangles[triangleIndex]];
float3 b = vertices[triangles[triangleIndex+1]];
float3 c = vertices[triangles[triangleIndex+2]];
float3 faceNormal = normalize(cross(a-b, a-c));
float3 sphereNormal = normalize((a+b+c)/3);
steepness[id] = 1 - dot(faceNormal, sphereNormal);
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b65c4a31f112141149dfdfb7d00a93f2
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

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

View file

@ -0,0 +1,46 @@
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
#include "../../Includes/Math.cginc"
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float4> shadingData;
uint numVertices;
// Noise settings
float4 noiseParams_detailWarp[3];
float4 noiseParams_detail[3];
float4 noiseParams_large[3];
float4 noiseParams_small[3];
// Second warp
float4 noiseParams_warp2[3];
float4 noiseParams_noise2[3];
[numthreads(512, 1, 1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numVertices) { return; }
float3 pos = vertices[id];
// Large, low frequency noise
float largeNoise = simpleNoise(pos, noiseParams_large);
float smallNoise = simpleNoise(pos, noiseParams_small);
// Warped detail noise
float detailWarp = simpleNoise(pos, noiseParams_detailWarp);
float detailNoise = simpleNoise(pos + detailWarp * 0.1, noiseParams_detail);
// Second warp noise
float3 warpOffset2;
warpOffset2.x = simpleNoise(pos, noiseParams_warp2);
warpOffset2.y = simpleNoise(pos + 99999, noiseParams_warp2);
warpOffset2.z = simpleNoise(pos - 99999, noiseParams_warp2);
float warpedNoise2 = simpleNoise(pos + warpOffset2 * 0.1, noiseParams_noise2);
shadingData[id] = float4(largeNoise, detailNoise, smallNoise, warpedNoise2);
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f3a2904f78271471481822700022a157
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,87 @@
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
#include "../../Includes/Math.cginc"
//
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float4> shadingData;
uint numVertices;
StructuredBuffer<float4> points;
int numRandomPoints;
float4 noiseParams_biomeWarp[3];
float4 noiseParams_detailWarp[3];
float4 noiseParams_detail[3];
float4 params;
StructuredBuffer<float4> ejectaCraters;
int numEjectaCraters;
float calcSphereNoise(float3 pos) {
float sphereNoise = 1.#INF;
for (int i = 0; i < numRandomPoints; i ++) {
float3 sphereCentre = points[i].xyz;
float sphereRadius = points[i].w;
float dst = length(pos - sphereCentre);
float t = dst / sphereRadius;
sphereNoise = min(sphereNoise, t);
}
return sphereNoise;
}
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numVertices) { return; }
float3 pos = vertices[id];
float3 domainWarp = 0;
domainWarp.x = simpleNoise(pos, noiseParams_biomeWarp);
domainWarp.y = simpleNoise(pos + 100, noiseParams_biomeWarp);
domainWarp.z = simpleNoise(pos - 100, noiseParams_biomeWarp);
float3 detailWarp = 0;
detailWarp.x = simpleNoise(pos, noiseParams_detailWarp);
detailWarp.y = simpleNoise(pos + 100, noiseParams_detailWarp);
detailWarp.z = simpleNoise(pos - 100, noiseParams_detailWarp);
float domainWarp2 = simpleNoise(pos, noiseParams_detailWarp);
// Sphere noise
float3 warpedPos = pos + (domainWarp) * 0.1;
float sphereNoise = calcSphereNoise(warpedPos);
float detailNoise = simpleNoise(pos + domainWarp2 * 0.1, noiseParams_detail);
// Calculate uv coords for crater 'ejecta ray' texture to be projected onto the surface around select craters
float2 ejectaUV = 1;
float minDst = 999;
for (int craterIndex = 0; craterIndex < numEjectaCraters; craterIndex ++) {
float3 craterCentre = ejectaCraters[craterIndex].xyz;
float dst = length(craterCentre - pos);
float scaledDst = dst / ejectaCraters[craterIndex].w;
if (scaledDst < minDst) {
minDst = scaledDst;
float3 craterUp = craterCentre;
float3 craterForward = normalize(cross(craterUp, float3(0,1,0)));
float3 forward = normalize(cross(pos, craterUp));
float cross_x = forward.y * craterForward.z - forward.z * craterForward.y;
float cross_y = forward.z * craterForward.x - forward.x * craterForward.z;
float cross_z = forward.x * craterForward.y - forward.y * craterForward.x;
float angleSign = sign(craterUp.x * cross_x + craterUp.y * cross_y + craterUp.z * cross_z);
float angle = acos(dot(craterForward, forward)) * angleSign;
//float estBiomeVal = Blend(7 * 0.8, 0.8, calcSphereNoise(craterCentre));
//float biomeSign = (estBiomeVal < 0.5)?-1:1;
ejectaUV = float2 (angle, scaledDst);
}
}
shadingData[id] = float4(ejectaUV.xy, detailNoise, sphereNoise);
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8f6c3d86008df43ca88b9d64cf8e0514
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

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

View file

@ -0,0 +1,37 @@
#pragma kernel CSMain
Texture2D<float4> SourceTex;
RWTexture2D<float4> Result;
uint textureSize;
int blurSize;
uint2 Wrap(int x, int y) {
x = (x < 0) ? textureSize + x : x;
x = (x >= (int)textureSize) ? x - textureSize : x;
y = (y < 0) ? textureSize + y : y;
y = (y >= (int)textureSize) ? y - textureSize : y;
return uint2(x, y);
}
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
if (id.x >= textureSize || id.y >= textureSize) {
return;
}
float4 sum = 0;
for (int offsetX = -blurSize; offsetX <= blurSize; offsetX ++) {
for (int offsetY = -blurSize; offsetY <= blurSize; offsetY ++) {
uint2 sampleCoord = Wrap(id.x + offsetX, id.y + offsetY);
float4 sample = SourceTex[sampleCoord];
sum += sample;
}
}
int numSamples = (blurSize * 2 + 1) * (blurSize * 2 + 1);
float4 result = sum / numSamples;
Result[id.xy] = result;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3bbb70231abe74a25b57d54568b663d9
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,49 @@
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
#include "../../Includes/Math.cginc"
//
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;
StructuredBuffer<float4> spots;
uint resolution;
int numSpots;
float background;
float4 params;
float smoothing;
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
if (id.x >= resolution || id.y >= resolution) {
return;
}
float2 pos = id.xy / (float)resolution;
float alpha = 0;
float minDst = 999;
for (int i = 0; i < numSpots; i ++) {
float4 spot = spots[i];
float r = spot.z;
float dst = length(spot.xy - pos);
float minDstOld = minDst;
// minDst = smoothMin(dst, minDst, smoothing * 0.001);
if ( dst <= r) {
//alpha = max(alpha, spot.w);
//float v = 1-dst/r;
//alpha += v*v*spot.w;
alpha = max(alpha,spot.w);
}
}
alpha = 1-saturate(background + alpha);
Result[id.xy] = float4(alpha, alpha, alpha, 1);//
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a06a7dddd79684a96a2169e2128e5f88
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,101 @@
#pragma kernel CSMain
#pragma kernel Normalize
#include "../../Includes/SimplexNoise3.cginc"
static const int minMaxAccuracy = 1000000;
RWTexture2D<float4> Result;
uint resolution;
RWStructuredBuffer<int> minMax;
float4 noiseParams_simple[3];
float4 noiseParams_warp[3];
float valueFloor;
float4 params;
// Remap a 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));
}
//
float tilingNoise(float2 pos, float4 noiseParams[3], float warp) {
static const int x1 =-1, y1=-1, x2=1, y2=1;
static const float dx = x2 - x1;
static const float dy = y2 - y1;
static const float tau = 3.14159265359 * 2;
// Params
float3 offset = warp;
float3 offset3d = noiseParams[0].xyz;//
int numLayers = int(noiseParams[0].w);
float persistence = noiseParams[1].x;
float lacunarity = noiseParams[1].y;
float scale = noiseParams[1].z;
float multiplier = noiseParams[1].w;
// web.archive.org/web/20160304043608/https://www.gamedev.net/blog/33/entry-2138456-seamless-noise/
float nx = x1 + cos(pos.x * tau) * dx;
float ny = y1 + cos(pos.y * tau) * dy;
float nz = x1 + sin(pos.x * tau) * dx;
float nw = y1 + sin (pos.y * tau) * dy;
float4 pos4D = float4(nx, ny, nz, nw) / tau;
float4 dir = normalize(pos4D);
pos4D += dir * 0.1 * params.x;
//pos4D += snoise(pos4D * params.w * 0.001);
float noise = 0;
float amplitude = 1;
float frequency = scale;
for (int i = 0; i < numLayers; i ++) {
float layerVal = snoise((pos4D + warp) * frequency);//
//layerVal = 1-abs(layerVal);
//layerVal *= layerVal;
noise += layerVal * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return noise * multiplier;
}
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
if (id.x >= resolution || id.y >= resolution) {
return;
}
float2 pos = id.xy/(float)resolution;
float warpNoise = tilingNoise(pos, noiseParams_warp, 0) * 0.1;
float noise = tilingNoise(pos, noiseParams_simple, warpNoise);
// keep inside range [0,1] as will be clamped in texture
float approxMax = 2;
noise = saturate((noise + approxMax * 0.5) / approxMax);
Result[id.xy] = noise;
// keep track of min max (using int to support atomic operation)
int val = (int)(noise * minMaxAccuracy);
InterlockedMin(minMax[0],val);
InterlockedMax(minMax[1],val);
}
[numthreads(8,8,1)]
void Normalize (uint3 id : SV_DispatchThreadID)
{
if (id.x >= resolution || id.y >= resolution) {
return;
}
float minVal = (float)minMax[0]/minMaxAccuracy;
float maxVal = (float)minMax[1]/minMaxAccuracy;
float4 normalizedValue = (Result[id.xy]-minVal)/(maxVal-minVal);
Result[id.xy] = normalizedValue;
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2821ce0c209074cf9bb487300949b8f6
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 65536
userData:
assetBundleName:
assetBundleVariant:

View file

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

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:

View file

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

View file

@ -0,0 +1,191 @@
Shader "Hidden/Atmosphere"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "../Includes/Math.cginc"
//
struct appdata {
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 viewVector : TEXCOORD1;
};
v2f vert (appdata v) {
v2f output;
output.pos = UnityObjectToClipPos(v.vertex);
output.uv = v.uv;
// Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
// (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv.xy * 2 - 1, 0, -1));
output.viewVector = mul(unity_CameraToWorld, float4(viewVector,0));
return output;
}
float2 squareUV(float2 uv) {
float width = _ScreenParams.x;
float height =_ScreenParams.y;
//float minDim = min(width, height);
float scale = 1000;
float x = uv.x * width;
float y = uv.y * height;
return float2 (x/scale, y/scale);
}
sampler2D _BlueNoise;
sampler2D _MainTex;
sampler2D _BakedOpticalDepth;
sampler2D _CameraDepthTexture;
float4 params;
float3 dirToSun;
float3 planetCentre;
float atmosphereRadius;
float oceanRadius;
float planetRadius;
// Paramaters
int numInScatteringPoints;
int numOpticalDepthPoints;
float intensity;
float4 scatteringCoefficients;
float ditherStrength;
float ditherScale;
float densityFalloff;
float densityAtPoint(float3 densitySamplePoint) {
float heightAboveSurface = length(densitySamplePoint - planetCentre) - planetRadius;
float height01 = heightAboveSurface / (atmosphereRadius - planetRadius);
float localDensity = exp(-height01 * densityFalloff) * (1 - height01);
return localDensity;
}
float opticalDepth(float3 rayOrigin, float3 rayDir, float rayLength) {
float3 densitySamplePoint = rayOrigin;
float stepSize = rayLength / (numOpticalDepthPoints - 1);
float opticalDepth = 0;
for (int i = 0; i < numOpticalDepthPoints; i ++) {
float localDensity = densityAtPoint(densitySamplePoint);
opticalDepth += localDensity * stepSize;
densitySamplePoint += rayDir * stepSize;
}
return opticalDepth;
}
float opticalDepthBaked(float3 rayOrigin, float3 rayDir) {
float height = length(rayOrigin - planetCentre) - planetRadius;
float height01 = saturate(height / (atmosphereRadius - planetRadius));
float uvX = 1 - (dot(normalize(rayOrigin - planetCentre), rayDir) * .5 + .5);
return tex2Dlod(_BakedOpticalDepth, float4(uvX, height01,0,0));
}
float opticalDepthBaked2(float3 rayOrigin, float3 rayDir, float rayLength) {
float3 endPoint = rayOrigin + rayDir * rayLength;
float d = dot(rayDir, normalize(rayOrigin-planetCentre));
float opticalDepth = 0;
const float blendStrength = 1.5;
float w = saturate(d * blendStrength + .5);
float d1 = opticalDepthBaked(rayOrigin, rayDir) - opticalDepthBaked(endPoint, rayDir);
float d2 = opticalDepthBaked(endPoint, -rayDir) - opticalDepthBaked(rayOrigin, -rayDir);
opticalDepth = lerp(d2, d1, w);
return opticalDepth;
}
float3 calculateLight(float3 rayOrigin, float3 rayDir, float rayLength, float3 originalCol, float2 uv) {
float blueNoise = tex2Dlod(_BlueNoise, float4(squareUV(uv) * ditherScale,0,0));
blueNoise = (blueNoise - 0.5) * ditherStrength;
float3 inScatterPoint = rayOrigin;
float stepSize = rayLength / (numInScatteringPoints - 1);
float3 inScatteredLight = 0;
float viewRayOpticalDepth = 0;
for (int i = 0; i < numInScatteringPoints; i ++) {
float sunRayLength = raySphere(planetCentre, atmosphereRadius, inScatterPoint, dirToSun).y;
float sunRayOpticalDepth = opticalDepthBaked(inScatterPoint + dirToSun * ditherStrength, dirToSun);
float localDensity = densityAtPoint(inScatterPoint);
viewRayOpticalDepth = opticalDepthBaked2(rayOrigin, rayDir, stepSize * i);
float3 transmittance = exp(-(sunRayOpticalDepth + viewRayOpticalDepth) * scatteringCoefficients);
inScatteredLight += localDensity * transmittance;
inScatterPoint += rayDir * stepSize;
}
inScatteredLight *= scatteringCoefficients * intensity * stepSize / planetRadius;
inScatteredLight += blueNoise * 0.01;
// Attenuate brightness of original col (i.e light reflected from planet surfaces)
// This is a hacky mess, TODO: figure out a proper way to do this
const float brightnessAdaptionStrength = 0.15;
const float reflectedLightOutScatterStrength = 3;
float brightnessAdaption = dot (inScatteredLight,1) * brightnessAdaptionStrength;
float brightnessSum = viewRayOpticalDepth * intensity * reflectedLightOutScatterStrength + brightnessAdaption;
float reflectedLightStrength = exp(-brightnessSum);
float hdrStrength = saturate(dot(originalCol,1)/3-1);
reflectedLightStrength = lerp(reflectedLightStrength, 1, hdrStrength);
float3 reflectedLight = originalCol * reflectedLightStrength;
float3 finalCol = reflectedLight + inScatteredLight;
return finalCol;
}
float4 frag (v2f i) : SV_Target
{
float4 originalCol = tex2D(_MainTex, i.uv);
float sceneDepthNonLinear = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
float sceneDepth = LinearEyeDepth(sceneDepthNonLinear) * length(i.viewVector);
float3 rayOrigin = _WorldSpaceCameraPos;
float3 rayDir = normalize(i.viewVector);
float dstToOcean = raySphere(planetCentre, oceanRadius, rayOrigin, rayDir);
float dstToSurface = min(sceneDepth, dstToOcean);
float2 hitInfo = raySphere(planetCentre, atmosphereRadius, rayOrigin, rayDir);
float dstToAtmosphere = hitInfo.x;
float dstThroughAtmosphere = min(hitInfo.y, dstToSurface - dstToAtmosphere);
if (dstThroughAtmosphere > 0) {
const float epsilon = 0.0001;
float3 pointInAtmosphere = rayOrigin + rayDir * (dstToAtmosphere + epsilon);
float3 light = calculateLight(pointInAtmosphere, rayDir, dstThroughAtmosphere - epsilon * 2, originalCol, i.uv);
return float4(light, 1);
}
return originalCol;
}
ENDCG
}
}
}

View file

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

Binary file not shown.

View file

@ -0,0 +1,104 @@
fileFormatVersion: 2
guid: 8886a14b14a1b44089f789c0b8c05643
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -100
wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 0
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,124 @@
Shader "Hidden/Ocean"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "../Includes/Math.cginc"
#include "../Includes/Triplanar.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 viewVector : TEXCOORD1;
};
v2f vert (appdata v) {
v2f output;
output.pos = UnityObjectToClipPos(v.vertex);
output.uv = v.uv;
// Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
// (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv * 2 - 1, 0, -1));
output.viewVector = mul(unity_CameraToWorld, float4(viewVector,0));
return output;
}
float4 colA;
float4 colB;
float4 specularCol;
float depthMultiplier;
float alphaMultiplier;
float smoothness;
sampler2D waveNormalA;
sampler2D waveNormalB;
float waveStrength;
float waveNormalScale;
float waveSpeed;
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
float4 params;
float planetScale;
float3 oceanCentre;
float oceanRadius;
float3 dirToSun;
fixed4 frag (v2f i) : SV_Target
{
fixed4 originalCol = tex2D(_MainTex, i.uv);
float3 rayPos = _WorldSpaceCameraPos;
float viewLength = length(i.viewVector);
float3 rayDir = i.viewVector / viewLength;
float nonlin_depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
float sceneDepth = LinearEyeDepth(nonlin_depth) * viewLength;
float2 hitInfo = raySphere(oceanCentre, oceanRadius, rayPos, rayDir);
float dstToOcean = hitInfo.x;
float dstThroughOcean = hitInfo.y;
float3 rayOceanIntersectPos = rayPos + rayDir * dstToOcean - oceanCentre;
// dst that view ray travels through ocean (before hitting terrain / exiting ocean)
float oceanViewDepth = min(dstThroughOcean, sceneDepth - dstToOcean);
if (oceanViewDepth > 0) {
float3 clipPlanePos = rayPos + i.viewVector * _ProjectionParams.y;
float dstAboveWater = length(clipPlanePos - oceanCentre) - oceanRadius;
float t = 1 - exp(-oceanViewDepth / planetScale * depthMultiplier);
float alpha = 1-exp(-oceanViewDepth / planetScale * alphaMultiplier);
float4 oceanCol = lerp(colA, colB, t);
float3 oceanSphereNormal = normalize(rayOceanIntersectPos);
float2 waveOffsetA = float2(_Time.x * waveSpeed, _Time.x * waveSpeed * 0.8);
float2 waveOffsetB = float2(_Time.x * waveSpeed * - 0.8, _Time.x * waveSpeed * -0.3);
float3 waveNormal = triplanarNormal(rayOceanIntersectPos, oceanSphereNormal, waveNormalScale / planetScale, waveOffsetA, waveNormalA);
waveNormal = triplanarNormal(rayOceanIntersectPos, waveNormal, waveNormalScale / planetScale, waveOffsetB, waveNormalB);
waveNormal = normalize(lerp(oceanSphereNormal, waveNormal, waveStrength));
//return float4(oceanNormal * .5 + .5,1);
float diffuseLighting = saturate(dot(oceanSphereNormal, dirToSun));
float specularAngle = acos(dot(normalize(dirToSun - rayDir), waveNormal));
float specularExponent = specularAngle / (1 - smoothness);
float specularHighlight = exp(-specularExponent * specularExponent);
oceanCol *= diffuseLighting;
oceanCol += specularHighlight * (dstAboveWater > 0) * specularCol;
//return float4(oceanSphereNormal,1);
float4 finalCol = originalCol * (1-alpha) + oceanCol * alpha;
return float4(finalCol.xyz, params.x);
}
return originalCol;
}
ENDCG
}
}
}

View file

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

View file

@ -0,0 +1,53 @@
Shader "Hidden/Test"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
float myMultiplier;
float myValue;
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
float mySum = 0;
for (int i = 0; i < 10; i ++) {
mySum += myValue * myMultiplier;
}
return mySum;
}
ENDCG
}
}
}

View file

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

View file

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

View file

@ -0,0 +1,197 @@
Shader "Celestial/Alien"
{
Properties
{
[Header(Flat Terrain)]
_ShoreCol("Shore Colour", Color) = (0,0,0,1)
_FlatColA("Flat Colour A", Color) = (0,0,0,1)
_FlatColB("Flat Colour B", Color) = (0,0,0,1)
_FlatColBlend("Colour Blend", Range(0,3)) = 1.5
_FlatColBlendNoise("Blend Noise", Range(0,1)) = 0.3
_ShoreHeight("Shore Height", Range(0,0.2)) = 0.05
_ShoreBlend("Shore Blend", Range(0,0.2)) = 0.03
_MaxFlatHeight("Max Flat Height", Range(0,1)) = 0.5
[Header(Steep Terrain)]
_SteepColA("Steep Colour A", Color) = (0,0,0,1)
_SteepColB("Steep Colour B", Color) = (0,0,0,1)
_SteepBands("Steep Bands", Range(1, 20)) = 8
_SteepBandStrength("Band Strength", Range(-1,1)) = 0.5
[Header(Flat to Steep Transition)]
_SteepnessThreshold("Steep Threshold", Range(0,1)) = 0.5
_FlatToSteepBlend("Flat to Steep Blend", Range(0,1)) = 0.1
_FlatToSteepNoise("Flat to Steep Noise", Range(0,1)) = 0.1
[Header(Noise)]
[NoScaleOffset] _NoiseTex ("Noise Texture", 2D) = "white" {}
_NoiseScale("Noise Scale", Float) = 1
[Header(Other)]
_FresnelCol("Fresnel Colour", Color) = (1,1,1,1)
_FresnelStrengthNear("Fresnel Strength Min", float) = 2
_FresnelStrengthFar("Fresnel Strength Max", float) = 5
_FresnelPow("Fresnel Power", float) = 2
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_GlossinessFlat ("Smoothness Flat", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_TestParams ("Test Params", Vector) = (0,0,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:vert
#pragma target 3.5
#include "../Includes/Triplanar.cginc"
#include "../Includes/Math.cginc"
struct Input
{
float2 uv_MainTex;
float4 terrainData;
float3 vertPos;
float3 normal;
float4 tangent;
float fresnel;
};
float4 _FresnelCol;
float _FresnelStrengthNear;
float _FresnelStrengthFar;
float _FresnelPow;
float bodyScale;
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.vertPos = v.vertex;
o.normal = v.normal;
o.terrainData = v.texcoord;
o.tangent = v.tangent;
// Fresnel (fade out when close to body)
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
float3 bodyWorldCentre = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
float camRadiiFromSurface = (length(bodyWorldCentre - _WorldSpaceCameraPos.xyz) - bodyScale) / bodyScale;
float fresnelT = smoothstep(0,1,camRadiiFromSurface);
float3 viewDir = normalize(worldPos - _WorldSpaceCameraPos.xyz);
float3 normWorld = normalize(mul(unity_ObjectToWorld, float4(v.normal,0)));
float fresStrength = lerp(_FresnelStrengthNear, _FresnelStrengthFar, fresnelT);
o.fresnel = saturate(fresStrength * pow(1 + dot(viewDir, normWorld), _FresnelPow));
}
// Flat terrain:
float4 _ShoreCol;
float4 _FlatColA;
float4 _FlatColA2;
float4 _FlatColB;
float4 _FlatColB2;
float _FlatColBlend;
float _FlatColBlendNoise;
float _ShoreHeight;
float _ShoreBlend;
float _MaxFlatHeight;
// Steep terrain
float4 _SteepColA;
float4 _SteepColB;
float _SteepBands;
float _SteepBandStrength;
// Flat to steep transition
float _SteepnessThreshold;
float _FlatToSteepBlend;
float _FlatToSteepNoise;
// Other:
float _Glossiness;
float _GlossinessFlat;
float _Metallic;
sampler2D _NoiseTex;
float _NoiseScale;
float4 _TestParams;
// Height data:
float2 heightMinMax;
float oceanLevel;
float Blend2(float startHeight, float blendDst, float height) {
float t = remap01(height, startHeight, startHeight + blendDst / 2);
return t;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Calculate steepness: 0 where totally flat, 1 at max steepness
float3 sphereNormal = normalize(IN.vertPos);
float steepness = 1 - dot (sphereNormal, IN.normal);
steepness = remap01(steepness, 0, 0.65);
// Calculate heights
float terrainHeight = length(IN.vertPos);
float shoreHeight = lerp(heightMinMax.x, 1, oceanLevel);
float aboveShoreHeight01 = remap01(terrainHeight, shoreHeight, heightMinMax.y);
float flatHeight01 = remap01(aboveShoreHeight01, 0, _MaxFlatHeight);
// Sample noise texture at two different scales
float4 texNoise = triplanar(IN.vertPos, IN.normal, _NoiseScale, _NoiseTex);
// Flat terrain colour
float biomeWeight = saturate(Blend(0, 0.5, IN.terrainData.x + IN.terrainData.y * -0.15));
float channelBlend = Blend(0, 1, IN.terrainData.w);
float flatBlendA = lerp(texNoise.r, texNoise.b, channelBlend);
//flatBlendA = (int)(flatBlendA * (_TestParams.w + 1)) / _TestParams.w;
float spotBlend = lerp(-0.7, 0.7, Blend(0, 2, IN.terrainData.w));
float3 flatColA = lerp(_FlatColA, _FlatColA2, saturate(flatHeight01 + (flatBlendA-0.5) * spotBlend));
float3 flatColB = lerp(_FlatColB, _FlatColB2, saturate(flatHeight01 + (flatBlendA-0.5) * spotBlend));
float3 flatTerrainCol = lerp(flatColA, flatColB, biomeWeight);
//flatTerrainCol = flatColB;
float shoreBlendWeight = 1-Blend(_ShoreHeight, _ShoreBlend, flatHeight01);
flatTerrainCol = lerp(flatTerrainCol, _ShoreCol, shoreBlendWeight);
// Steep terrain colour
float3 sphereTangent = normalize(float3(-sphereNormal.z, 0, sphereNormal.x));
float3 normalTangent = normalize(IN.normal - sphereNormal * dot(IN.normal, sphereNormal));
float banding = dot(sphereTangent, normalTangent) * .5 + .5;
banding += (texNoise.a-0.5) * 0.1;
banding = (int)(banding * (_SteepBands + 1)) / _SteepBands;
banding = (abs(banding - 0.5) * 2 - 0.5) * _SteepBandStrength;
float3 steepTerrainCol = lerp(_SteepColA, _SteepColB, aboveShoreHeight01 + banding);
// Flat to steep colour transition
float flatBlendNoise = (texNoise.b - 0.5) * -_FlatToSteepNoise;
//float b = lerp(0, _TestParams.x, Blend(0, 3, 5)) * 0.1;
float flatStrength = 1 - Blend2(_SteepnessThreshold + flatBlendNoise, saturate(_FlatToSteepBlend), steepness);
// Set surface colour
float3 compositeCol = lerp(steepTerrainCol, flatTerrainCol, flatStrength);
compositeCol = lerp(compositeCol, _FresnelCol, IN.fresnel);
o.Albedo = compositeCol;
// Glossiness
//float glossiness = dot(o.Albedo, 1) / 3 * _Glossiness;
//glossiness = max(glossiness, snowWeight * _SnowSpecular);
o.Smoothness = max(steepness * _Glossiness, _GlossinessFlat);
o.Metallic = _Metallic;
//o.Albedo = camRadiiFromSurface;
}
ENDCG
}
FallBack "Diffuse"
}

View file

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

View file

@ -0,0 +1,245 @@

Shader "Celestial/Earth"
{
Properties
{
[Header(Flat Terrain)]
_ShoreLow("Shore Low", Color) = (0,0,0,1)
_ShoreHigh("Shore High", Color) = (0,0,0,1)
_FlatLowA("Flat Low A", Color) = (0,0,0,1)
_FlatHighA("Flat High A", Color) = (0,0,0,1)
_FlatLowB("Flat Low B", Color) = (0,0,0,1)
_FlatHighB("Flat High B", Color) = (0,0,0,1)
_FlatColBlend("Colour Blend", Range(0,3)) = 1.5
_FlatColBlendNoise("Blend Noise", Range(0,1)) = 0.3
_ShoreHeight("Shore Height", Range(0,0.2)) = 0.05
_ShoreBlend("Shore Blend", Range(0,0.2)) = 0.03
_MaxFlatHeight("Max Flat Height", Range(0,1)) = 0.5
[Header(Steep Terrain)]
_SteepLow("Steep Colour Low", Color) = (0,0,0,1)
_SteepHigh("Steep Colour High", Color) = (0,0,0,1)
_SteepBands("Steep Bands", Range(1, 20)) = 8
_SteepBandStrength("Band Strength", Range(-1,1)) = 0.5
[Header(Flat to Steep Transition)]
_SteepnessThreshold("Steep Threshold", Range(0,1)) = 0.5
_FlatToSteepBlend("Flat to Steep Blend", Range(0,0.3)) = 0.1
_FlatToSteepNoise("Flat to Steep Noise", Range(0,0.2)) = 0.1
[Header(Snowy Poles)]
[Toggle()]
_UseSnowyPoles("Use Poles", float) = 0
_SnowCol("Snow Colour", Color) = (1,1,1,1)
_SnowLongitude("Snow Longitude", Range(0,1)) = 0.8
_SnowBlend("Snow Blend", Range(0, 0.2)) = 0.1
_SnowSpecular("Snow Specular", Range(0,1)) = 1
_SnowHighlight("Snow Highlight", Range(1,2)) = 1.2
_SnowNoiseA("Snow Noise A", Range(0,10)) = 5
_SnowNoiseB("Snow Noise B", Range(0,10)) = 4
[Header(Noise)]
[NoScaleOffset] _NoiseTex ("Noise Texture", 2D) = "white" {}
_NoiseScale("Noise Scale", Float) = 1
_NoiseScale2("Noise Scale2", Float) = 1
[Header(Other)]
_FresnelCol("Fresnel Colour", Color) = (1,1,1,1)
_FresnelStrengthNear("Fresnel Strength Min", float) = 2
_FresnelStrengthFar("Fresnel Strength Max", float) = 5
_FresnelPow("Fresnel Power", float) = 2
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_TestParams ("Test Params", Vector) = (0,0,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:vert
#pragma target 3.5
#include "../Includes/Triplanar.cginc"
#include "../Includes/Math.cginc"
float4 _TestParams;
float4 _FresnelCol;
float _FresnelStrengthNear;
float _FresnelStrengthFar;
float _FresnelPow;
float bodyScale;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
float4 terrainData;
float3 vertPos;
float3 normal;
float4 tangent;
float fresnel;
};
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.vertPos = v.vertex;
o.normal = v.normal;
o.terrainData = v.texcoord;
o.tangent = v.tangent;
// Fresnel (fade out when close to body)
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
float3 bodyWorldCentre = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
float camRadiiFromSurface = (length(bodyWorldCentre - _WorldSpaceCameraPos.xyz) - bodyScale) / bodyScale;
float fresnelT = smoothstep(0,1,camRadiiFromSurface);
float3 viewDir = normalize(worldPos - _WorldSpaceCameraPos.xyz);
float3 normWorld = normalize(mul(unity_ObjectToWorld, float4(v.normal,0)));
float fresStrength = lerp(_FresnelStrengthNear, _FresnelStrengthFar, fresnelT);
o.fresnel = saturate(fresStrength * pow(1 + dot(viewDir, normWorld), _FresnelPow));
}
// Flat terrain:
float4 _ShoreLow;
float4 _ShoreHigh;
float4 _FlatLowA;
float4 _FlatHighA;
float4 _FlatLowB;
float4 _FlatHighB;
float _FlatColBlend;
float _FlatColBlendNoise;
float _ShoreHeight;
float _ShoreBlend;
float _MaxFlatHeight;
// Steep terrain
float4 _SteepLow;
float4 _SteepHigh;
float _SteepBands;
float _SteepBandStrength;
// Flat to steep transition
float _SteepnessThreshold;
float _FlatToSteepBlend;
float _FlatToSteepNoise;
// Snowy poles
float _UseSnowyPoles;
float3 _SnowCol;
float _SnowLongitude;
float _SnowBlend;
float _SnowSpecular;
float _SnowHighlight;
float _SnowNoiseA;
float _SnowNoiseB;
// Other:
float _Glossiness;
float _Metallic;
sampler2D _NoiseTex;
sampler2D _SnowNormal;
float _NoiseScale;
float _NoiseScale2;
// Height data:
float2 heightMinMax;
float oceanLevel;
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
{
fixed4 c;
c.rgb = s.Albedo * 0.8;
c.a = s.Alpha;
return c;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Calculate steepness: 0 where totally flat, 1 at max steepness
float3 sphereNormal = normalize(IN.vertPos);
float steepness = 1 - dot (sphereNormal, IN.normal);
steepness = remap01(steepness, 0, 0.65);
// Calculate heights
float terrainHeight = length(IN.vertPos);
float shoreHeight = lerp(heightMinMax.x, 1, oceanLevel);
float aboveShoreHeight01 = remap01(terrainHeight, shoreHeight, heightMinMax.y);
float flatHeight01 = remap01(aboveShoreHeight01, 0, _MaxFlatHeight);
// Sample noise texture at two different scales
float4 texNoise = triplanar(IN.vertPos, IN.normal, _NoiseScale, _NoiseTex);
float4 texNoise2 = triplanar(IN.vertPos, IN.normal, _NoiseScale2, _NoiseTex);
// Flat terrain colour A and B
float flatColBlendWeight = Blend(0, _FlatColBlend, (flatHeight01-.5) + (texNoise.b - 0.5) * _FlatColBlendNoise);
float3 flatTerrainColA = lerp(_FlatLowA, _FlatHighA, flatColBlendWeight);
flatTerrainColA = lerp(flatTerrainColA, (_FlatLowA + _FlatHighA) / 2, texNoise.a);
float3 flatTerrainColB = lerp(_FlatLowB, _FlatHighB, flatColBlendWeight);
flatTerrainColB = lerp(flatTerrainColB, (_FlatLowB + _FlatHighB) / 2, texNoise.a);
// Biome
float biomeWeight = Blend(_TestParams.x, _TestParams.y,IN.terrainData.x);
biomeWeight = Blend(0, _TestParams.z, IN.vertPos.x + IN.terrainData.x * _TestParams.x + IN.terrainData.y * _TestParams.y);
float3 flatTerrainCol = lerp(flatTerrainColA, flatTerrainColB, biomeWeight);
// Shore
float shoreBlendWeight = 1-Blend(_ShoreHeight, _ShoreBlend, flatHeight01);
float4 shoreCol = lerp(_ShoreLow, _ShoreHigh, remap01(aboveShoreHeight01, 0, _ShoreHeight));
shoreCol = lerp(shoreCol, (_ShoreLow + _ShoreHigh) / 2, texNoise.g);
flatTerrainCol = lerp(flatTerrainCol, shoreCol, shoreBlendWeight);
// Steep terrain colour
float3 sphereTangent = normalize(float3(-sphereNormal.z, 0, sphereNormal.x));
float3 normalTangent = normalize(IN.normal - sphereNormal * dot(IN.normal, sphereNormal));
float banding = dot(sphereTangent, normalTangent) * .5 + .5;
banding = (int)(banding * (_SteepBands + 1)) / _SteepBands;
banding = (abs(banding - 0.5) * 2 - 0.5) * _SteepBandStrength;
float3 steepTerrainCol = lerp(_SteepLow, _SteepHigh, aboveShoreHeight01 + banding);
// Flat to steep colour transition
float flatBlendNoise = (texNoise2.r - 0.5) * _FlatToSteepNoise;
float flatStrength = 1 - Blend(_SteepnessThreshold + flatBlendNoise, _FlatToSteepBlend, steepness);
float flatHeightFalloff = 1 - Blend(_MaxFlatHeight + flatBlendNoise, _FlatToSteepBlend, aboveShoreHeight01);
flatStrength *= flatHeightFalloff;
// Snowy poles
float3 snowCol = 0;
float snowWeight = 0;
float snowLineNoise = IN.terrainData.y * _SnowNoiseA * 0.01 + (texNoise.b-0.5) * _SnowNoiseB * 0.01;
snowWeight = Blend(_SnowLongitude, _SnowBlend, abs(IN.vertPos.y + snowLineNoise)) * _UseSnowyPoles;
float snowSpeckle = 1 - texNoise2.g * 0.5 * 0.1;
snowCol = _SnowCol * lerp (1, _SnowHighlight, aboveShoreHeight01 + banding) * snowSpeckle;
// Set surface colour
float3 compositeCol = lerp(steepTerrainCol, flatTerrainCol, flatStrength);
compositeCol = lerp(compositeCol, snowCol, snowWeight);
compositeCol = lerp(compositeCol, _FresnelCol, IN.fresnel);
o.Albedo = compositeCol;
// Glossiness
float glossiness = dot(o.Albedo, 1) / 3 * _Glossiness;
glossiness = max(glossiness, snowWeight * _SnowSpecular);
o.Smoothness = glossiness;
o.Metallic = _Metallic;
}
ENDCG
}
FallBack "Diffuse"
}

View file

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

View file

@ -0,0 +1,200 @@
Shader "Celestial/MoonA"
{
Properties
{
[Header(Colours)]
_PrimaryColA ("Primary A", Color) = (1,1,1,1)
_SecondaryColA ("Secondary A", Color) = (1,1,1,1)
_PrimaryColB ("Primary B", Color) = (1,1,1,1)
_SecondaryColB ("Secondary B", Color) = (1,1,1,1)
_EjectaCol ("Ejecta Col", Color) = (1,1,1,1)
_BiomeBlendStrength ("Biome Blend", Float) = 2
_BiomeWarpStrength("Biome Warp", Float) = 0
[Toggle()]
_UseEjecta("Use Ejecta", float) = 1
_EjectaBrightness("Ejecta Brightness", Float) = 0.1
[Header(Normals)]
[NoScaleOffset] _CraterRay ("Crater Ray", 2D) = "white" {}
[NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {}
[NoScaleOffset] _NormalMapFlat ("Normal Map Flat", 2D) = "bump" {}
[NoScaleOffset] _NormalMapSteep ("Normal Map Steep", 2D) = "bump" {}
_NormalMapFlatScale ("Normal Map Flat Scale", Float) = 10
_NormalMapSteepScale ("Normal Map Steep Scale", Float) = 10
_MainTexScale ("Main Tex Scale", Float) = 10
_RandomBiomeValues ("Random Biome Values", Vector) = (0,0,0,0)
_NormalMapStrength ("Normal Map Strength", Range(0,1)) = 0.3
[Header(Other)]
_FresnelCol("Fresnel Colour", Color) = (1,1,1,1)
_FresnelStrengthNear("Fresnel Strength Min", float) = 2
_FresnelStrengthFar("Fresnel Strength Max", float) = 5
_FresnelPow("Fresnel Power", float) = 12
_SmoothnessA ("Smoothness A", Range(0,1)) = 0.5
_SmoothnessB ("Smoothness B", Range(0,1)) = 0.5
_SmoothnessEjecta ("Smoothness Ejecta", Range(0,1)) = 0.5
_MetallicA ("Metallic A", Range(0,1)) = 0.0
_MetallicB ("Metallic B", Range(0,1)) = 0.0
_TestParams ("Test Params", Vector) = (0,0,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert
#pragma target 3.5
#include "../Includes/Triplanar.cginc"
#include "../Includes/Math.cginc"
sampler2D _MainTex;
sampler2D _CraterRay;
sampler2D _NormalMapFlat;
sampler2D _NormalMapSteep;
float _NormalMapStrength;
float _NormalMapFlatScale;
float _NormalMapSteepScale;
float _MainTexScale;
float _SmoothnessA;
float _SmoothnessB;
float _SmoothnessEjecta;
float _MetallicA;
float _MetallicB;
float2 heightMinMax;
float _EjectaBrightness;
float _UseEjecta;
float _BiomeBlendStrength;
float _BiomeWarpStrength;
float4 _RandomBiomeValues;
float4 _TestParams;
float _AvgBiomeNoiseDst;
// Colors:
float4 _PrimaryColA;
float4 _SecondaryColA;
float4 _PrimaryColB;
float4 _SecondaryColB;
float4 _EjectaCol;
float4 _FresnelCol;
float _FresnelStrengthNear;
float _FresnelStrengthFar;
float _FresnelPow;
float bodyScale;
struct Input
{
float3 vertPos;
float3 normal;
float4 terrainData;
float3 worldNormal;
float4 tangent;
float4 craterUV;
float fresnel;
INTERNAL_DATA
};
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT (Input,o);
o.vertPos = v.vertex;
o.terrainData = v.texcoord;
o.normal = v.normal;
o.tangent = v.tangent;
//
float craterDst = v.texcoord.y;
float2 craterUV = 0.5 + float2(cos(v.texcoord.x), sin(v.texcoord.x)) * craterDst;
o.craterUV = float4(craterUV.xy, craterDst, 0);
// Fresnel (fade out when close to body)
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
float3 bodyWorldCentre = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
float camRadiiFromSurface = (length(bodyWorldCentre - _WorldSpaceCameraPos.xyz) - bodyScale) / bodyScale;
float fresnelT = smoothstep(0,1,camRadiiFromSurface);
float3 viewDir = normalize(worldPos - _WorldSpaceCameraPos.xyz);
float3 normWorld = normalize(mul(unity_ObjectToWorld, float4(v.normal,0)));
float fresStrength = lerp(_FresnelStrengthNear, _FresnelStrengthFar, fresnelT);
o.fresnel = saturate(fresStrength * pow(1 + dot(viewDir, normWorld), _FresnelPow));
}
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
{
fixed4 c;
c.rgb = s.Albedo * 0.8;
c.a = s.Alpha;
return c;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
float4 terrainData = IN.terrainData;
float height01 = remap01(length(IN.vertPos), heightMinMax.x, heightMinMax.y);
// Calculate steepness: 0 where totally flat, 1 at max steepness
const float maxSteepnessApprox = 0.3;
float steepness = 1 - dot(IN.normal, normalize(IN.vertPos));
steepness = remap01(steepness, 0, maxSteepnessApprox);
float warpNoise = Blend(0, 3, IN.terrainData.z);
// Sample noise texture
float4 texNoise = triplanar(IN.vertPos, IN.normal, _MainTexScale, _MainTex);
// Sample normal maps:
// There are two maps, one for steep slopes like mountains and crater walls, and one for flat regions
// Slopes always use the steep map, but flat regions blend between the flat and steep maps to add variety
float3 normalMapFlat = triplanarNormalTangentSpace(IN.vertPos, IN.normal, _NormalMapFlatScale, IN.tangent, _NormalMapFlat);
float3 normalMapSteep = triplanarNormalTangentSpace(IN.vertPos, IN.normal, _NormalMapSteepScale, IN.tangent, _NormalMapSteep);
//float normalBlend = Blend(_AvgBiomeNoiseDst * 0.8, 1.2, terrainData.w);
//float normalBlend = warpNoise;
float normalBlend = lerp(texNoise.r, texNoise.g, Blend(0, 2, terrainData.z));
float3 flatAndSteepNormal = lerp(normalMapFlat, normalMapSteep, normalBlend);
float3 normal = lerp(flatAndSteepNormal, normalMapSteep, steepness);
normal = lerp(float3(0,0,1), normal, _NormalMapStrength);
o.Normal = normal;
// Blend between primary and secondary colours by height (plus some noise)
float heightNoiseA = -texNoise.g * steepness - (texNoise.b - 0.5) * 0.7 + (texNoise.a - 0.5) * _RandomBiomeValues.x;
float heightNoiseB = (texNoise.g-0.5) * _RandomBiomeValues.y + (texNoise.r-0.5) * _RandomBiomeValues.z;
float heightBlendWeightA = Blend(0.5, 0.6, height01 + heightNoiseA) * warpNoise;
float heightBlendWeightB = Blend(0.5, 0.6, height01 + heightNoiseB) * warpNoise;
float3 colBiomeA = lerp(_PrimaryColA, _SecondaryColA, heightBlendWeightA);
float3 colBiomeB = lerp(_PrimaryColB, _SecondaryColB, heightBlendWeightB);
// Blend between colour A and B based on terrain data noise
float biomeNoise = dot(texNoise.ga - 0.5, _RandomBiomeValues.zw) * 4;
float biomeWeight = Blend(_AvgBiomeNoiseDst * 0.8 + terrainData.z * _BiomeWarpStrength, _BiomeBlendStrength + warpNoise * 15, terrainData.w + biomeNoise);
float3 biomeCol = lerp(colBiomeA, colBiomeB, biomeWeight);
// Crater ejecta
float ejecta = tex2D(_CraterRay, IN.craterUV.xy).r;
float craterAlpha = IN.craterUV.z < 1;
craterAlpha = abs(IN.terrainData.y) < 0.5;
float3 compositeCol = lerp(biomeCol, _EjectaCol, saturate(ejecta * 1.5) * craterAlpha * _UseEjecta);
compositeCol = lerp(compositeCol, _FresnelCol, IN.fresnel);
o.Albedo = compositeCol;
// Set metallic and smoothness
o.Metallic = lerp(_MetallicA, _MetallicB, biomeWeight);;
float smoothness = lerp(_SmoothnessA, _SmoothnessB, biomeWeight);
smoothness = lerp(smoothness, _SmoothnessEjecta, saturate(ejecta * 1.5) * craterAlpha);
o.Smoothness = smoothness;
}
ENDCG
}
FallBack "Diffuse"
}

View file

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

View file

@ -0,0 +1,190 @@
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Celestial/MoonB"
{
Properties
{
[Header(Colours)]
_PrimaryColA ("Primary A", Color) = (1,1,1,1)
_SecondaryColA ("Secondary A", Color) = (1,1,1,1)
_PrimaryColB ("Primary B", Color) = (1,1,1,1)
_SecondaryColB ("Secondary B", Color) = (1,1,1,1)
_BiomeBlendStrength ("Biome Blend", Float) = 2
_BiomeWeight ("Biome Weight", Float) = 1.6
_BiomeWarpStrength("Biome Warp", Float) = 0
_EjectaBrightness("Ejecta Brightness", Float) = 0.1
[Header(Normals)]
[NoScaleOffset] _CraterRay ("Crater Ray", 2D) = "white" {}
[NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {}
[NoScaleOffset] _NormalMapFlat ("Normal Map Flat", 2D) = "bump" {}
[NoScaleOffset] _NormalMapSteep ("Normal Map Steep", 2D) = "bump" {}
_NormalMapFlatScale ("Normal Map Flat Scale", Float) = 10
_NormalMapSteepScale ("Normal Map Steep Scale", Float) = 10
_MainTexScale ("Main Tex Scale", Float) = 10
_RandomBiomeValues ("Random Biome Values", Vector) = (0,0,0,0)
_NormalMapStrength ("Normal Map Strength", Range(0,1)) = 0.3
[Header(Other)]
_FresnelCol("Fresnel Colour", Color) = (1,1,1,1)
_FresnelStrength("Fresnel Strength", float) = 0.5
_FresnelPow("Fresnel Power", float) = 2
_SmoothnessA ("Smoothness A", Range(0,1)) = 0.5
_SmoothnessB ("Smoothness B", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_TestParams ("Test Params", Vector) = (0,0,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert
#pragma target 3.5
#include "../Includes/Triplanar.cginc"
#include "../Includes/Math.cginc"
sampler2D _MainTex;
sampler2D _CraterRay;
sampler2D _NormalMapFlat;
sampler2D _NormalMapSteep;
float _NormalMapStrength;
float _NormalMapFlatScale;
float _NormalMapSteepScale;
float _MainTexScale;
float _SmoothnessA;
float _SmoothnessB;
float _Metallic;
float2 heightMinMax;
float _EjectaBrightness;
float _BiomeBlendStrength;
float _BiomeWeight;
float _BiomeWarpStrength;
float4 _RandomBiomeValues;
float4 _TestParams;
float _AvgBiomeNoiseDst;
// Colors:
float4 _PrimaryColA;
float4 _SecondaryColA;
float4 _PrimaryColB;
float4 _SecondaryColB;
float4 _SteepCol;
float4 _FresnelCol;
float _FresnelStrength;
float _FresnelPow;
float bodyScale;
struct Input
{
float3 vertPos;
float3 normal;
float4 terrainData;
float3 worldNormal;
float4 tangent;
float4 craterUV;
float3 viewDir;
float fresnel;
INTERNAL_DATA
};
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT (Input,o);
o.vertPos = v.vertex;
o.terrainData = v.texcoord;
o.normal = v.normal;
o.tangent = v.tangent;
//
float craterDst = abs(v.texcoord.y);
float craterColWeight = (v.texcoord.y < 0)? 0 : 1;
float2 craterUV = 0.5 + float2(cos(v.texcoord.x), sin(v.texcoord.x)) * craterDst;
o.craterUV = float4(craterUV.xy, craterDst, craterColWeight);
// Fresnel (fade out when close to body)
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
float3 bodyWorldCentre = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
float camRadiiFromSurface = (length(bodyWorldCentre - _WorldSpaceCameraPos.xyz) - bodyScale) / bodyScale;
float fresnelT = smoothstep(0,1,camRadiiFromSurface);
float3 viewDir = normalize(worldPos - _WorldSpaceCameraPos.xyz);
float3 normWorld = normalize(mul(unity_ObjectToWorld, float4(v.normal,0)));
o.fresnel = saturate(_FresnelStrength * fresnelT * pow(1 + dot(viewDir, normWorld), _FresnelPow));
}
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
{
fixed4 c;
c.rgb = s.Albedo * 0.8;
c.a = s.Alpha;
return c;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
float4 texNoise = triplanar(IN.vertPos, IN.normal, _MainTexScale, _MainTex);
float steepness = 1 - dot(IN.normal, normalize(IN.vertPos));
float height01 = remap01(length(IN.vertPos), heightMinMax.x, heightMinMax.y);
float t = 1-Blend(_TestParams.y,_TestParams.z, height01);
float highAndFlat = (1 - remap01(steepness, 0.08, 0.2)) * Blend(0.6, 0.4, height01);
float biomeWeight = Blend(_AvgBiomeNoiseDst * _BiomeWeight, _BiomeBlendStrength, IN.terrainData.w + IN.terrainData.z * _BiomeWarpStrength);
//o.Albedo = lerp(_PrimaryColA, _PrimaryColB, biomeWeight);
//o.Albedo = steepness;
float heightBanded01 = saturate(int((height01+(texNoise.b-.5)*_TestParams.w) * _TestParams.z) / (_TestParams.z));
float3 colA = lerp(_PrimaryColA, _SecondaryColA, height01);
float3 colB = lerp(_PrimaryColB, _SecondaryColB, height01);
float3 compositeCol = lerp(colA, colB, biomeWeight);// * lerp(0.7, 1, texNoise.b);
compositeCol = lerp(compositeCol, _SteepCol, Blend(0.5, 0.18, height01));
//compositeCol = lerp(compositeCol, 1, highAndFlat);
// Ejecta
// Crater ejecta
//float ejectaColorWeight = IN.craterUV.w;
//float3 ejectaCol = _SecondaryColA;
float ejecta = tex2D(_CraterRay, IN.craterUV.xy).r;
float craterAlpha = IN.craterUV.z < 1;
//o.Albedo = lerp(biomeCol, ejectaCol, saturate(ejecta * 1.5) * craterAlpha);
//compositeCol = lerp(compositeCol, _SteepCol, steepness);
// compositeCol = lerp(compositeCol, float3(1,0,0), ejecta * craterAlpha);
compositeCol = lerp(compositeCol, _FresnelCol, IN.fresnel);
//o.Albedo = craterAlpha;
o.Albedo =compositeCol;
// Normals
float3 normalMapFlat = triplanarNormalTangentSpace(IN.vertPos, IN.normal, _NormalMapFlatScale, IN.tangent, _NormalMapFlat);
//float3 normalMapSteep = triplanarNormalTangentSpace(IN.vertPos, IN.normal, _NormalMapSteepScale, IN.tangent, _NormalMapSteep);
//float normalBlend = lerp(texNoise.r, texNoise.g, Blend(0, 2, terrainData.z));
//float3 flatAndSteepNormal = lerp(normalMapFlat, normalMapSteep, normalBlend);
//float3 normal = lerp(flatAndSteepNormal, normalMapSteep, steepness);
float3 normal = lerp(float3(0,0,1), normalMapFlat, _NormalMapStrength);
o.Normal = normal;
//o.Albedo = highAndFlat;
//o.Albedo = );
// Set metallic and smoothness
o.Metallic = 0;
o.Smoothness = 0;
}
ENDCG
}
FallBack "Diffuse"
}

View file

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