Raymarching Cleanup
Removed redundant code from raymarching implementation Moved last remaining raymarching variable (Extinction) to environment settings
This commit is contained in:
parent
e585a8c3b1
commit
d61d2f19ed
150 changed files with 187 additions and 8921 deletions
31
Assets/Shaders/Raymarching/CloudNormalRaymarch.cginc
Normal file
31
Assets/Shaders/Raymarching/CloudNormalRaymarch.cginc
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
#include "./CloudShaderHelper.cginc"
|
||||
|
||||
float GetDensity(float3 startPos, float3 dir, float maxSampleDistance, float raymarchOffset, out float3 intensity,out float depth) {
|
||||
float raymarchDistance = 0;
|
||||
|
||||
RaymarchStatus result;
|
||||
InitRaymarchStatus(result);
|
||||
|
||||
[loop]
|
||||
for (int j = 0; j < SAMPLE_COUNT; j++) {
|
||||
float prevRayDist = raymarchDistance;
|
||||
raymarchDistance = pow((j+raymarchOffset)/SAMPLE_COUNT,2) * _ProjectionParams.z;
|
||||
if(raymarchDistance > maxSampleDistance) break;
|
||||
float step = raymarchDistance - prevRayDist;
|
||||
float3 rayPos = startPos + dir * raymarchDistance;
|
||||
float fade = smoothstep(_ProjectionParams.z*.95,_ProjectionParams.z, raymarchDistance);
|
||||
IntegrateRaymarch(startPos, rayPos, fade, step, result);
|
||||
if (result.intTransmittance < 0.01f) {
|
||||
result.intTransmittance = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
depth = result.depth / result.depthweightsum / _ProjectionParams.z;
|
||||
if (depth == 0.0f) {
|
||||
depth = maxSampleDistance;
|
||||
}
|
||||
intensity = result.intensity;
|
||||
return (1.0f - result.intTransmittance);
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 31ddb89f7354b2e4384d433c9dc67aac
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
291
Assets/Shaders/Raymarching/CloudShader.shader
Normal file
291
Assets/Shaders/Raymarching/CloudShader.shader
Normal file
|
|
@ -0,0 +1,291 @@
|
|||
// Based on github.com/yangrc1234/VolumeCloud
|
||||
|
||||
Shader "Aetheria/CloudShader"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_MainTex("MainTex",2D) = "white"{}
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
Cull Off ZWrite Off ZTest Always
|
||||
//Pass1, Render a undersampled buffer. The buffer is dithered using bayer matrix(every 3x3 pixel) and halton sequence.
|
||||
//Why does it need a bayer matrix as offset? See technical overview on github page.
|
||||
Pass
|
||||
{
|
||||
CGPROGRAM
|
||||
#pragma target 5.0
|
||||
#pragma multi_compile LOW_QUALITY MEDIUM_QUALITY HIGH_QUALITY ULTRA_QUALITY //High quality uses more samples.
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
|
||||
|
||||
#if defined(ULTRA_QUALITY)
|
||||
#define SAMPLE_COUNT 256
|
||||
#endif
|
||||
#if defined(HIGH_QUALITY)
|
||||
#define SAMPLE_COUNT 128
|
||||
#endif
|
||||
#if defined(MEDIUM_QUALITY)
|
||||
#define SAMPLE_COUNT 64
|
||||
#endif
|
||||
#if defined(LOW_QUALITY)
|
||||
#define SAMPLE_COUNT 32
|
||||
#endif
|
||||
|
||||
#include "./CloudNormalRaymarch.cginc"
|
||||
#include "UnityCG.cginc"
|
||||
#include "Assets/Shaders/PackFloat.cginc"
|
||||
sampler2D _CameraDepthTexture;
|
||||
float _RaymarchOffset; //raymarch offset by halton sequence, [0,1]
|
||||
float4 _ProjectionExtents;
|
||||
sampler2D _DitheringTex;
|
||||
float4 _DitheringCoords;
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct Interpolator {
|
||||
float4 vertex : SV_POSITION;
|
||||
float4 screenPos : TEXCOORD0;
|
||||
float2 vsray : TEXCOORD1;
|
||||
};
|
||||
|
||||
Interpolator vert (appdata v)
|
||||
{
|
||||
Interpolator o;
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
v.vertex.z = 0.5;
|
||||
o.screenPos = ComputeScreenPos(o.vertex);
|
||||
o.vsray = (2.0 * v.uv - 1.0) * _ProjectionExtents.xy + _ProjectionExtents.zw;
|
||||
return o;
|
||||
}
|
||||
|
||||
float GetRaymarchEndFromSceneDepth(float sceneDepth, out float raymarchEnd) {
|
||||
raymarchEnd = sceneDepth * _ProjectionParams.z; //raymarch to scene depth.
|
||||
return sceneDepth<.99;
|
||||
}
|
||||
|
||||
float4 frag (Interpolator i) : SV_Target
|
||||
{
|
||||
float3 vspos = float3(i.vsray, 1.0);
|
||||
float4 worldPos = mul(unity_CameraToWorld,float4(vspos,1.0));
|
||||
worldPos /= worldPos.w;
|
||||
|
||||
float sceneDepth = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos)).r);
|
||||
float raymarchEnd;
|
||||
bool occluded = GetRaymarchEndFromSceneDepth(sceneDepth, raymarchEnd);
|
||||
float3 viewDir = normalize(worldPos.xyz - _WorldSpaceCameraPos);
|
||||
|
||||
float2 screenPos = i.screenPos.xy / i.screenPos.w;
|
||||
|
||||
//float blue = tex2D(_DitheringTex, screenPos * _DitheringCoords.xy + _DitheringCoords.zw).r;
|
||||
float dither = tex2D(_DitheringTex, screenPos * _DitheringCoords.xy).r;
|
||||
float offset = -fmod(_RaymarchOffset + dither, 1.0f); //final offset combined. The value will be multiplied by sample step in GetDensity.
|
||||
|
||||
float3 intensity;
|
||||
float distance;
|
||||
//TODO: sceneDepth here is distance in camera z-axis, but the parameter should be radial distance.
|
||||
#if USE_HI_HEIGHT
|
||||
int iteration_count;
|
||||
float density = HierarchicalRaymarch(worldPos, viewDir, raymarchEnd, offset, /*out*/intensity, /*out*/distance, /*out*/iteration_count);
|
||||
#else
|
||||
float density = GetDensity(_WorldSpaceCameraPos, viewDir, raymarchEnd, offset, /*out*/intensity, /*out*/distance);
|
||||
#endif
|
||||
if(!occluded) density = 1;
|
||||
return float4(intensity, pack(distance, density));
|
||||
}
|
||||
|
||||
ENDCG
|
||||
}
|
||||
|
||||
//Pass 2, blend undersampled image with history buffer to new buffer.
|
||||
Pass{
|
||||
CGPROGRAM
|
||||
#pragma target 5.0
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#pragma multi_compile LOW_QUALITY MEDIUM_QUALITY HIGH_QUALITY
|
||||
|
||||
#include "./CloudShaderHelper.cginc"
|
||||
#include "UnityCG.cginc"
|
||||
#include "Assets/Shaders/PackFloat.cginc"
|
||||
|
||||
sampler2D _MainTex; //history buffer.
|
||||
float4 _MainTex_TexelSize;
|
||||
sampler2D _UndersampleCloudTex; //current undersampled tex.
|
||||
float4 _UndersampleCloudTex_TexelSize;
|
||||
|
||||
float4x4 _PrevVP; //View projection matrix of last frame. Used to temporal reprojection.
|
||||
|
||||
//These values are needed for doing extra raymarch when out of bound.
|
||||
sampler2D _CameraDepthTexture;
|
||||
float4 _ProjectionExtents;
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float4 vertex : SV_POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
float2 vsray : TEXCOORD1;
|
||||
float4 screenPos : TEXCOORD2;
|
||||
};
|
||||
|
||||
v2f vert(appdata v)
|
||||
{
|
||||
v2f o;
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
o.uv = v.uv;
|
||||
o.vsray = (2.0 * v.uv - 1.0) * _ProjectionExtents.xy + _ProjectionExtents.zw;
|
||||
o.screenPos = ComputeScreenPos(o.vertex);
|
||||
return o;
|
||||
}
|
||||
|
||||
//Get uv of wspos in history buffer.
|
||||
float2 PrevUV(float4 wspos, out half outOfBound) {
|
||||
float4 prevUV = mul(_PrevVP, wspos);
|
||||
prevUV.xy = 0.5 * (prevUV.xy / prevUV.w) + 0.5;
|
||||
half oobmax = max(0.0 - prevUV.x, 0.0 - prevUV.y);
|
||||
half oobmin = max(prevUV.x - 1.0, prevUV.y - 1.0);
|
||||
outOfBound = step(0, max(oobmin, oobmax));
|
||||
return prevUV;
|
||||
}
|
||||
|
||||
//Code from https://zhuanlan.zhihu.com/p/64993622. Do AABB clip in TAA(clip to center).
|
||||
float4 ClipAABB(float4 aabbMin, float4 aabbMax, float4 prevSample)
|
||||
{
|
||||
// note: only clips towards aabb center (but fast!)
|
||||
float4 p_clip = 0.5 * (aabbMax + aabbMin);
|
||||
float4 e_clip = 0.5 * (aabbMax - aabbMin);
|
||||
|
||||
float4 v_clip = prevSample - p_clip;
|
||||
float4 v_unit = v_clip / e_clip;
|
||||
float4 a_unit = abs(v_unit);
|
||||
float ma_unit = max(max(a_unit.x, max(a_unit.y, a_unit.z)), a_unit.w);
|
||||
|
||||
if (ma_unit > 1.0)
|
||||
return p_clip + v_clip / ma_unit;
|
||||
else
|
||||
return prevSample;// point inside aabb
|
||||
}
|
||||
|
||||
float4 frag(v2f i) : SV_Target
|
||||
{
|
||||
float3 vspos = float3(i.vsray, 1.0);
|
||||
// float4 worldPos = mul(unity_CameraToWorld, float4(vspos, 1.0f));
|
||||
// worldPos /= worldPos.w;
|
||||
float4 raymarchResult = tex2D(_UndersampleCloudTex, i.uv);
|
||||
float distance;
|
||||
float density;
|
||||
float density2;
|
||||
unpack(raymarchResult.a, distance, density);
|
||||
distance *= _ProjectionParams.z;
|
||||
//float intensity = raymarchResult.x;
|
||||
|
||||
{ //Do temporal reprojection and clip things.
|
||||
half outOfBound;
|
||||
float2 prevUV = PrevUV(mul(unity_CameraToWorld, float4(normalize(vspos) * distance, 1.0)), outOfBound); //find uv in history buffer.
|
||||
|
||||
float4 prevSample = tex2D(_MainTex, prevUV);
|
||||
float2 xoffset = float2(_UndersampleCloudTex_TexelSize.x, 0.0f);
|
||||
float2 yoffset = float2(0.0f, _UndersampleCloudTex_TexelSize.y);
|
||||
|
||||
float4 m1 = 0.0f, m2 = 0.0f;
|
||||
//The loop below calculates mean and variance used to calculate AABB.
|
||||
[unroll]
|
||||
for (int x = -1; x <= 1; x ++) {
|
||||
[unroll]
|
||||
for (int y = -1; y <= 1; y ++ ) {
|
||||
float4 val;
|
||||
if (x == 0 && y == 0) {
|
||||
val = float4(raymarchResult.rgb, distance);
|
||||
}
|
||||
else {
|
||||
val = tex2Dlod(_UndersampleCloudTex, float4(i.uv + xoffset * x + yoffset * y, 0.0, 0.0));
|
||||
float distance2;
|
||||
unpack(val.a, distance2, density2);
|
||||
val = float4(val.rgb, distance2);
|
||||
}
|
||||
m1 += val;
|
||||
m2 += val * val;
|
||||
}
|
||||
}
|
||||
//Code from https://zhuanlan.zhihu.com/p/64993622.
|
||||
float gamma = 0.5f;
|
||||
float4 mu = m1 / 9;
|
||||
float4 sigma = sqrt(abs(m2 / 9 - mu * mu));
|
||||
float4 minc = mu - gamma * sigma;
|
||||
float4 maxc = mu + gamma * sigma;
|
||||
prevSample = ClipAABB(minc, maxc, prevSample);
|
||||
|
||||
//Blend
|
||||
raymarchResult = lerp(float4(prevSample.rgb, density), float4(raymarchResult.rgb, density2), max(0.05f, outOfBound));
|
||||
}
|
||||
return raymarchResult;
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
|
||||
//Pass3, Blend final cloud image with final image.
|
||||
Pass{
|
||||
Cull Off ZWrite Off ZTest Always
|
||||
CGPROGRAM
|
||||
#pragma target 5.0
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
sampler2D _MainTex; //Final image without cloud.
|
||||
sampler2D _CloudTex; //The full resolution cloud tex we generated.
|
||||
sampler2D _CameraDepthTexture;
|
||||
float4 _ProjectionExtents;
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float2 uv : TEXCOORD0;
|
||||
float4 screenPos : TEXCOORD1;
|
||||
float4 vertex : SV_POSITION;
|
||||
float2 vsray : TEXCOORD2;
|
||||
};
|
||||
|
||||
v2f vert(appdata v)
|
||||
{
|
||||
v2f o;
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
o.uv = v.uv;
|
||||
o.screenPos = ComputeScreenPos(o.vertex);
|
||||
o.vsray = (2.0 * v.uv - 1.0) * _ProjectionExtents.xy + _ProjectionExtents.zw;
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
half4 frag(v2f i) : SV_Target
|
||||
{
|
||||
float3 vspos = float3(i.vsray, 1.0);
|
||||
//float4 worldPos = mul(unity_CameraToWorld,float4(vspos,1.0));
|
||||
|
||||
half4 mcol = tex2D(_MainTex,i.uv);
|
||||
float4 currSample = tex2D(_CloudTex, i.uv);
|
||||
|
||||
return half4(mcol.rgb * (1 - currSample.a) + currSample.rgb, 1);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Assets/Shaders/Raymarching/CloudShader.shader.meta
Normal file
10
Assets/Shaders/Raymarching/CloudShader.shader.meta
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 84e97169589c5e94e8521fe4deb2e3f8
|
||||
timeCreated: 1521964058
|
||||
licenseType: Free
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
257
Assets/Shaders/Raymarching/CloudShaderHelper.cginc
Normal file
257
Assets/Shaders/Raymarching/CloudShaderHelper.cginc
Normal file
|
|
@ -0,0 +1,257 @@
|
|||
#include "UnityCG.cginc"
|
||||
|
||||
#define EARTH_RADIUS 6371000.0
|
||||
#define EARTH_CENTER float3(0, -EARTH_RADIUS, 0)
|
||||
|
||||
#ifndef SAMPLE_COUNT
|
||||
#define SAMPLE_COUNT 64
|
||||
#endif
|
||||
|
||||
#define TRANSMITTANCE_SAMPLE_STEP 512.0f
|
||||
|
||||
static const float bayerOffsets[3][3] = {
|
||||
{0, 7, 3},
|
||||
{6, 5, 2},
|
||||
{4, 1, 8}
|
||||
};
|
||||
|
||||
//Base shape
|
||||
float _CloudStartHeight;
|
||||
float _CloudEndHeight;
|
||||
sampler3D _BaseTex;
|
||||
float _BaseTile;
|
||||
sampler2D _HeightDensity;
|
||||
//Detal shape
|
||||
sampler3D _DetailTex;
|
||||
float _DetailTile;
|
||||
float _DetailStrength;
|
||||
//Curl distortion
|
||||
sampler2D _CurlNoise;
|
||||
float _CurlTile;
|
||||
float _CurlStrength;
|
||||
//Top offset
|
||||
float _CloudTopOffset;
|
||||
|
||||
//Overall cloud size.
|
||||
float _CloudSize;
|
||||
//Overall Density
|
||||
float _CloudOverallDensity;
|
||||
float _CloudCoverageModifier;
|
||||
float _CloudTypeModifier;
|
||||
|
||||
half4 _WindDirection;
|
||||
sampler2D _WeatherTex;
|
||||
float _WeatherTexSize;
|
||||
|
||||
//Lighting
|
||||
float _ScatteringCoefficient;
|
||||
float _ExtinctionCoefficient;
|
||||
float _SilverIntensity;
|
||||
float _SilverSpread;
|
||||
|
||||
float SampleDensity(float3 worldPos, int lod, bool cheap, out float wetness);
|
||||
|
||||
float Remap(float original_value, float original_min, float original_max, float new_min, float new_max)
|
||||
{
|
||||
return new_min + (((original_value - original_min) / (original_max - original_min)) * (new_max - new_min));
|
||||
}
|
||||
|
||||
float RemapClamped(float original_value, float original_min, float original_max, float new_min, float new_max)
|
||||
{
|
||||
return new_min + (saturate((original_value - original_min) / (original_max - original_min)) * (new_max - new_min));
|
||||
}
|
||||
|
||||
float HeightPercent(float3 worldPos) {
|
||||
float sqrMag = worldPos.x * worldPos.x + worldPos.z * worldPos.z;
|
||||
|
||||
float heightOffset = EARTH_RADIUS - sqrt(max(0.0, EARTH_RADIUS * EARTH_RADIUS - sqrMag));
|
||||
|
||||
return saturate((worldPos.y + heightOffset - _CloudStartHeight) / (_CloudEndHeight-_CloudStartHeight));
|
||||
}
|
||||
|
||||
static float4 cloudGradients[3] = {
|
||||
float4(0, 0.07, 0.08, 0.15),
|
||||
float4(0, 0.2, 0.42, 0.6),
|
||||
float4(0, 0.08, 0.75, 1)
|
||||
};
|
||||
|
||||
float SampleHeight(float heightPercent,float cloudType) {
|
||||
float4 gradient;
|
||||
float cloudTypeVal;
|
||||
if (cloudType < 0.5) {
|
||||
gradient = lerp(cloudGradients[0], cloudGradients[1], cloudType*2.0);
|
||||
}
|
||||
else {
|
||||
gradient = lerp(cloudGradients[1], cloudGradients[2], (cloudType - 0.5)*2.0);
|
||||
}
|
||||
|
||||
return RemapClamped(heightPercent, gradient.x, gradient.y, 0.0, 1.0)
|
||||
* RemapClamped(heightPercent, gradient.z, gradient.w, 1.0, 0.0);
|
||||
}
|
||||
|
||||
float3 ApplyWind(float3 worldPos) {
|
||||
float heightPercent = HeightPercent(worldPos);
|
||||
|
||||
// skew in wind direction
|
||||
worldPos.xz -= (heightPercent) * _WindDirection.xy * _CloudTopOffset;
|
||||
|
||||
//animate clouds in wind direction and add a small upward bias to the wind direction
|
||||
worldPos.xz -= (_WindDirection.xy + float3(0.0, 0.1, 0.0)) * _Time.y * _WindDirection.z;
|
||||
worldPos.y -= _WindDirection.z * 0.4 * _Time.y;
|
||||
return worldPos;
|
||||
}
|
||||
|
||||
float HenryGreenstein(float g, float cosTheta) {
|
||||
|
||||
float k = 3.0 / (8.0 * 3.1415926f) * (1.0 - g * g) / (2.0 + g * g);
|
||||
return k * (1.0 + cosTheta * cosTheta) / pow(abs(1.0 + g * g - 2.0 * g * cosTheta), 1.5);
|
||||
}
|
||||
|
||||
float ApplyCoverageToDensity(float sampleResult, float coverage){
|
||||
sampleResult = RemapClamped(sampleResult, 1.0 - coverage, 1.0, 0.0, 1.0);
|
||||
sampleResult *= coverage;
|
||||
return sampleResult;
|
||||
}
|
||||
|
||||
#include "Assets/Shaders/Volumetric.cginc"
|
||||
|
||||
float _MultiScatteringA;
|
||||
float _MultiScatteringB;
|
||||
float _MultiScatteringC;
|
||||
|
||||
//We raymarch to sun using length of pattern 1,2,4,8, corresponding to step value.
|
||||
//First sample(length 1) should sample at length 0.5, meaning an average inside length 1.
|
||||
//Second sample should sample at 1.5, meaning an average inside [1, 2],
|
||||
//Third should sample at 3.0, which is [2, 4]
|
||||
//Forth at 6.0, meaning [4, 8]
|
||||
static const float shadowSampleDistance[5] = {
|
||||
0.5, 1.5, 3.0, 6.0, 12.0
|
||||
};
|
||||
|
||||
static const float shadowSampleContribution[5] = {
|
||||
1.0f, 1.0f, 2.0f, 4.0f, 8.0f
|
||||
};
|
||||
|
||||
float SampleOpticsDistanceToSun(float3 worldPos) {
|
||||
int mipmapOffset = 0.5;
|
||||
float opticsDistance = 0.0f;
|
||||
[unroll]
|
||||
for (int i = 0; i < 5; i++) {
|
||||
half3 direction = _WorldSpaceLightPos0;
|
||||
float3 samplePoint = worldPos + direction * shadowSampleDistance[i] * TRANSMITTANCE_SAMPLE_STEP;
|
||||
float wetness;
|
||||
float sampleResult = SampleDensity(samplePoint, mipmapOffset, true, wetness);
|
||||
opticsDistance += shadowSampleContribution[i] * TRANSMITTANCE_SAMPLE_STEP * sampleResult;
|
||||
mipmapOffset += 0.5;
|
||||
}
|
||||
return opticsDistance;
|
||||
}
|
||||
|
||||
float SampleEnergy(float3 worldPos, float3 viewDir) {
|
||||
float opticsDistance = SampleOpticsDistanceToSun(worldPos);
|
||||
float result = 0.0f;
|
||||
float cosTheta = dot(viewDir, _WorldSpaceLightPos0);
|
||||
[unroll]
|
||||
for (int octaveIndex = 0; octaveIndex < 2; octaveIndex++) { //Multi scattering approximation from Frostbite.
|
||||
float transmittance = exp(-_ExtinctionCoefficient * pow(_MultiScatteringB, octaveIndex) * opticsDistance);
|
||||
float ecMult = pow(_MultiScatteringC, octaveIndex);
|
||||
float phase = lerp(HenryGreenstein(.1f * ecMult, cosTheta), HenryGreenstein((0.99 - _SilverSpread) * ecMult, cosTheta), 0.5f);
|
||||
result += phase * transmittance * _ScatteringCoefficient * pow(_MultiScatteringA, octaveIndex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//Code from https://area.autodesk.com/blogs/game-dev-blog/volumetric-clouds/.
|
||||
bool ray_trace_sphere(float3 center, float3 rd, float3 offset, float radius, out float t1, out float t2) {
|
||||
float3 p = center - offset;
|
||||
float b = dot(p, rd);
|
||||
float c = dot(p, p) - (radius * radius);
|
||||
|
||||
float f = b * b - c;
|
||||
if (f >= 0.0) {
|
||||
float dem = sqrt(f);
|
||||
t1 = -b - dem;
|
||||
t2 = -b + dem;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool resolve_ray_start_end(float3 ws_origin, float3 ws_ray, out float start, out float end) {
|
||||
//case includes on ground, inside atm, above atm.
|
||||
float ot1, ot2, it1, it2;
|
||||
bool outIntersected = ray_trace_sphere(ws_origin, ws_ray, EARTH_CENTER, EARTH_RADIUS + _CloudEndHeight, ot1, ot2);
|
||||
if (!outIntersected || ot2 < 0.0f)
|
||||
return false; //you see nothing.
|
||||
|
||||
bool inIntersected = ray_trace_sphere(ws_origin, ws_ray, EARTH_CENTER, EARTH_RADIUS + _CloudStartHeight, it1, it2);
|
||||
|
||||
if (inIntersected) {
|
||||
if (it1 * it2 < 0) {
|
||||
//we're on ground.
|
||||
start = max(it2, 0);
|
||||
end = ot2;
|
||||
}
|
||||
else {
|
||||
//we're inside atm, or above atm.
|
||||
if (ot1 * ot2 < 0) { //Inside atm.
|
||||
if (it1 > 0.0) {
|
||||
//Look down.
|
||||
end = it1;
|
||||
}
|
||||
else {
|
||||
//Look up.
|
||||
end = ot2;
|
||||
}
|
||||
start = 0.0f;
|
||||
} else { //Outside atm
|
||||
if (ot1 < 0.0) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
start = ot1;
|
||||
end = it1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
end = ot2;
|
||||
start = max(ot1, 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct RaymarchStatus {
|
||||
float3 intensity;
|
||||
float depth;
|
||||
float depthweightsum;
|
||||
float intTransmittance;
|
||||
};
|
||||
|
||||
void InitRaymarchStatus(inout RaymarchStatus result){
|
||||
result.intTransmittance = 1.0f;
|
||||
result.intensity = 0.0f;
|
||||
result.depthweightsum = 0.00001f;
|
||||
result.depth = 0.0f;
|
||||
}
|
||||
|
||||
void IntegrateRaymarch(float3 startPos, float3 rayPos, float fade, float stepsize, inout RaymarchStatus result){
|
||||
float4 c = VolumeSampleColor(rayPos);
|
||||
float density = c.a;
|
||||
if (density <= 0.0f)
|
||||
return;
|
||||
float extinction = _ExtinctionCoefficient * density;
|
||||
|
||||
float clampedExtinction = max(extinction, 1e-7);
|
||||
float transmittance = exp(-extinction * stepsize);
|
||||
|
||||
float3 luminance = c.rgb * (1-fade);
|
||||
float3 integScatt = (luminance - luminance * transmittance) / clampedExtinction;
|
||||
float depthWeight = result.intTransmittance * (1-transmittance); //Is it a better idead to use (1-transmittance) * intTransmittance as depth weight?
|
||||
|
||||
result.intensity += result.intTransmittance * integScatt;
|
||||
result.depth += depthWeight * length(rayPos - startPos);
|
||||
result.depthweightsum += depthWeight;
|
||||
result.intTransmittance *= transmittance;
|
||||
}
|
||||
10
Assets/Shaders/Raymarching/CloudShaderHelper.cginc.meta
Normal file
10
Assets/Shaders/Raymarching/CloudShaderHelper.cginc.meta
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c7e80b274d611c144b9c2cebb238550b
|
||||
timeCreated: 1521965851
|
||||
licenseType: Free
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue