Unified Volumetric Flow
Fixed towing system Tweaked Nebula Flow (a lot) Extracted environment appearance settings into ZoneEnvironment data structure Stardust particles now follow global flow field
This commit is contained in:
parent
f3df2ba4cf
commit
00f752edf7
39 changed files with 812 additions and 341 deletions
|
|
@ -1,4 +1,6 @@
|
|||
//Tell unity that we have defined a kernel
|
||||
#include "Assets/Shaders/Volumetric.cginc"
|
||||
|
||||
//Tell unity that we have defined a kernel
|
||||
#pragma kernel UpdateParticles
|
||||
|
||||
//Store Particle data in a struct
|
||||
|
|
@ -22,7 +24,6 @@ SamplerState samplerHueTexture;
|
|||
//uniform float4x4 noiseOffsetMatrix;
|
||||
|
||||
float time;
|
||||
float3 transform;
|
||||
float spacing;
|
||||
float ceilingHeight;
|
||||
float floorHeight;
|
||||
|
|
@ -115,19 +116,26 @@ void UpdateParticles(uint3 id : SV_DispatchThreadID, int3 groupID : SV_GroupThre
|
|||
int x = id % span - span / 2;
|
||||
int y = id / span - span / 2;
|
||||
|
||||
int xt = (int)(transform.x / spacing);
|
||||
int yt = (int)(transform.y / spacing);
|
||||
int xt = (int)(_GridTransform.x / spacing);
|
||||
int yt = (int)(_GridTransform.y / spacing);
|
||||
uint xp = x + xt + offset;
|
||||
uint yp = y + yt + offset;
|
||||
|
||||
particles[id.x].color = HueTexture.SampleLevel(samplerHueTexture, float2(RandomFirst(xp, yp), 0.0f), 0.0f).rgb;
|
||||
|
||||
const float l = frac(time+RandomFirst(xp, yp));
|
||||
|
||||
particles[id.x].position = float3(
|
||||
x * spacing + xt * spacing + perlinNoise1D(RandomValue()*1024+time)*spacing, 0,
|
||||
y * spacing + yt * spacing + perlinNoise1D(RandomValue()*1024+time)*spacing);
|
||||
float2 puv = float2(.5,.5) - (particles[id.x].position.xz - transform.xy) / (transform.z);
|
||||
float gravDepth = Heightmap.SampleLevel(samplerHeightmap, puv, 0.0f).r;
|
||||
float headroom = (ceilingHeight+gravDepth);
|
||||
particles[id.x].position.y = -gravDepth + floorHeight + -floorHeight * 4 * pow(RandomValue(), max(1, heightExponent/(headroom*headroom))) + perlinNoise1D(RandomValue()*1024+time)*spacing/2;
|
||||
particles[id.x].size = lerp(minimumSize, maximumSize, RandomValue());
|
||||
x * spacing + xt * spacing + perlinNoise1D(RandomValue()*1024)*spacing, 0,
|
||||
y * spacing + yt * spacing + perlinNoise1D(RandomValue()*1024)*spacing);
|
||||
float n;
|
||||
const float2 puv = float2(.5,.5) - (particles[id.x].position.xz - _GridTransform.xy) / _GridTransform.z;
|
||||
const float gravDepth = Heightmap.SampleLevel(samplerHeightmap, puv, 0.0f).r;
|
||||
const float headroom = ceilingHeight + gravDepth;
|
||||
particles[id.x].position.y = -gravDepth + floorHeight + -floorHeight * 4 *
|
||||
pow(RandomValue(), max(1, heightExponent/(headroom*headroom))) +
|
||||
perlinNoise1D(RandomValue()*1024)*spacing/2;
|
||||
const float3 f = flow(particles[id.x].position, n);
|
||||
particles[id.x].position += f * (l * n);
|
||||
const float density = d(particles[id.x].position);
|
||||
particles[id.x].size = lerp(minimumSize, maximumSize, RandomValue() * n * density) * parabola(l, 2);
|
||||
particles[id.x].color = HueTexture.SampleLevel(samplerHueTexture, float2(RandomValue(), 0.0f), 0.0f).rgb;
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ public class Stardust : MonoBehaviour
|
|||
#region variables
|
||||
//public bool debugLog = false;
|
||||
//public float proxyPersistTime = 2;
|
||||
public GameSettings Settings;
|
||||
public Camera TargetCamera;
|
||||
public ComputeShader ParticleCalculation;
|
||||
public Material ParticleMaterial;
|
||||
|
|
@ -24,11 +25,16 @@ public class Stardust : MonoBehaviour
|
|||
public float Floor = -25.0f;
|
||||
public float HeightExponent = 4;
|
||||
|
||||
public RenderTexture NebulaSurfaceHeight;
|
||||
public RenderTexture NebulaPatchHeight;
|
||||
public RenderTexture NebulaPatch;
|
||||
public RenderTexture NebulaTint;
|
||||
public Texture Heightmap;
|
||||
public Texture2D ParticleColors;
|
||||
//public Transform TargetTransform;
|
||||
public Camera GravityCamera;
|
||||
|
||||
private float _flowScroll;
|
||||
private const int GROUP_SIZE = 128;
|
||||
private int _updateParticlesKernel;
|
||||
#endregion
|
||||
|
|
@ -67,7 +73,7 @@ public class Stardust : MonoBehaviour
|
|||
for (int i = 0; i < Span * Span; ++i)
|
||||
{
|
||||
particles[i].Position = Random.insideUnitSphere * 100;
|
||||
particles[i].Color = Vector3.one;//white
|
||||
particles[i].Color = Vector3.one; //white
|
||||
particles[i].Size = Random.value;
|
||||
}
|
||||
|
||||
|
|
@ -98,7 +104,7 @@ public class Stardust : MonoBehaviour
|
|||
ParticleCalculation.SetFloat("time", Time.time * Speed);
|
||||
|
||||
var pos = GravityCamera.transform.position;
|
||||
ParticleCalculation.SetVector("transform", new Vector4(pos.x,pos.z,GravityCamera.orthographicSize*2));
|
||||
ParticleCalculation.SetVector("_GridTransform", new Vector4(pos.x,pos.z,GravityCamera.orthographicSize*2));
|
||||
ParticleCalculation.SetFloat("spacing", Spacing);
|
||||
ParticleCalculation.SetFloat("ceilingHeight", Ceiling);
|
||||
ParticleCalculation.SetFloat("floorHeight", Floor);
|
||||
|
|
@ -109,6 +115,29 @@ public class Stardust : MonoBehaviour
|
|||
|
||||
ParticleCalculation.SetTexture(_updateParticlesKernel, "Heightmap", Heightmap);
|
||||
ParticleCalculation.SetTexture(_updateParticlesKernel, "HueTexture", ParticleColors);
|
||||
|
||||
ParticleCalculation.SetTexture(_updateParticlesKernel, "_NebulaSurfaceHeight", NebulaSurfaceHeight);
|
||||
ParticleCalculation.SetTexture(_updateParticlesKernel, "_NebulaPatchHeight", NebulaPatchHeight);
|
||||
ParticleCalculation.SetTexture(_updateParticlesKernel, "_NebulaPatch", NebulaPatch);
|
||||
ParticleCalculation.SetTexture(_updateParticlesKernel, "_NebulaTint", NebulaTint);
|
||||
|
||||
ParticleCalculation.SetFloat("_NebulaFillDensity", Settings.DefaultEnvironment.Nebula.FillDensity);
|
||||
ParticleCalculation.SetFloat("_SafetyDistance", Settings.DefaultEnvironment.Nebula.FillDistance);
|
||||
ParticleCalculation.SetFloat("_NebulaFloorDensity", Settings.DefaultEnvironment.Nebula.FloorDensity);
|
||||
ParticleCalculation.SetFloat("_NebulaPatchDensity", Settings.DefaultEnvironment.Nebula.PatchDensity);
|
||||
ParticleCalculation.SetFloat("_NebulaFloorOffset", Settings.DefaultEnvironment.Nebula.FloorOffset);
|
||||
ParticleCalculation.SetFloat("_NebulaFloorBlend", Settings.DefaultEnvironment.Nebula.FloorBlend);
|
||||
ParticleCalculation.SetFloat("_NebulaPatchBlend", Settings.DefaultEnvironment.Nebula.PatchBlend);
|
||||
// Shader.SetGlobalFloat("_TintExponent", Settings.DefaultEnvironment.);
|
||||
ParticleCalculation.SetFloat("_NoiseScale", Settings.DefaultEnvironment.Noise.Scale);
|
||||
ParticleCalculation.SetFloat("_NoiseExponent", Settings.DefaultEnvironment.Noise.Exponent);
|
||||
ParticleCalculation.SetFloat("_NoiseAmplitude", Settings.DefaultEnvironment.Noise.Amplitude);
|
||||
ParticleCalculation.SetFloat("_NoiseSpeed", Settings.DefaultEnvironment.Noise.Speed);
|
||||
ParticleCalculation.SetFloat("_FlowScale", Settings.DefaultEnvironment.Flow.Scale);
|
||||
ParticleCalculation.SetFloat("_FlowAmplitude", Settings.DefaultEnvironment.Flow.Amplitude);
|
||||
_flowScroll += Settings.DefaultEnvironment.Flow.ScrollSpeed * Time.deltaTime;
|
||||
ParticleCalculation.SetFloat("_FlowScroll", _flowScroll);
|
||||
ParticleCalculation.SetFloat("_FlowSpeed", Settings.DefaultEnvironment.Flow.Speed);
|
||||
|
||||
//Dispatch, launch threads on GPU
|
||||
int numberOfGroups = Mathf.CeilToInt((float)Span * Span / GROUP_SIZE);
|
||||
|
|
@ -116,7 +145,7 @@ public class Stardust : MonoBehaviour
|
|||
|
||||
ParticleMaterial.SetBuffer("particles", _particlesBuffer);
|
||||
ParticleMaterial.SetBuffer("quadPoints", _quadPoints);
|
||||
Graphics.DrawProcedural(ParticleMaterial, new Bounds(pos, Spacing * Span * Vector3.one), MeshTopology.Triangles, 6, Span * Span, TargetCamera, null, ShadowCastingMode.Off, false, 0);
|
||||
Graphics.DrawProcedural(ParticleMaterial, new Bounds(pos, new Vector3(Spacing * Span, 2048, Spacing * Span)), MeshTopology.Triangles, 6, Span * Span, TargetCamera, null, ShadowCastingMode.Off, false, 0);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ Material:
|
|||
- _Cutoff: 0.5
|
||||
- _DetailNormalMapScale: 1
|
||||
- _DstBlend: 0
|
||||
- _EmissionGain: 0.716
|
||||
- _EmissionGain: 0.103
|
||||
- _GlossMapScale: 1
|
||||
- _Glossiness: 0.5
|
||||
- _GlossyReflections: 1
|
||||
|
|
@ -69,7 +69,7 @@ Material:
|
|||
- _Mode: 0
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.02
|
||||
- _Power: 2
|
||||
- _Power: 0.5
|
||||
- _SmoothnessTextureChannel: 0
|
||||
- _SpecularHighlights: 1
|
||||
- _SrcBlend: 1
|
||||
|
|
@ -78,5 +78,5 @@ Material:
|
|||
m_Colors:
|
||||
- _Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
||||
- _TintColor: {r: 1.4980392, g: 1.4980392, b: 1.4980392, a: 1}
|
||||
- _TintColor: {r: 5.992157, g: 5.992157, b: 5.992157, a: 1}
|
||||
m_BuildTextureStacks: []
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@
|
|||
o.uv = quadPoints[id] + 0.5f;
|
||||
|
||||
//transfer color of particle and global tint to vertex
|
||||
o.color = float4(particles[inst].color, 1) * _TintColor * 10000 / (dist * dist);
|
||||
o.color = float4(particles[inst].color * _TintColor.rgb, 10000 / (dist * dist));
|
||||
o.screenPos = ComputeScreenPos(o.pos);
|
||||
|
||||
return o;
|
||||
|
|
|
|||
|
|
@ -1,31 +1,13 @@
|
|||
Shader "VolSample/Volume Render" {
|
||||
Shader "VolSample/Volume Render Auto" {
|
||||
Properties {
|
||||
_MainTex( "", 2D ) = "white" {}
|
||||
_StepExponent("Step Exponent", float) = 1
|
||||
_Surface("Surface", 2D) = "black" {}
|
||||
_Patch("Patch", 2D) = "black" {}
|
||||
_Displacement("Displacement", 2D) = "black" {}
|
||||
_TintTexture("Tint Texture", 2D ) = "white" {}
|
||||
[HDR]_Tint("Tint Color", Color) = (1,1,1,1)
|
||||
//_GridTransform("Grid Transform", Vector) = (0,0,0,0)
|
||||
_GridFillDensity("Fill Density", float) = 1
|
||||
_GridFloorDensity("Floor Density", float) = 1
|
||||
_GridPatchDensity("Patch Density", float) = 1
|
||||
_GridFloorOffset("Floor Offset", float) = 1
|
||||
_GridFloorBlend("Floor Blend", float) = 1
|
||||
_GridPatchBlend("Patch Blend", float) = 1
|
||||
_Exposure("Exposure", float) = 1
|
||||
_Gamma("Gamma", float) = .5
|
||||
_TintExponent("Tint Exponent", float) = .5
|
||||
_DepthCeiling("Depth Ceiling", float) = 1000
|
||||
_DepthBlend("Depth Blend", float) = 100
|
||||
_NoiseStrength("Noise Strength", float) = 1
|
||||
_NoiseFrequency("Noise Frequency", float) = 1
|
||||
_NoiseExponent("Noise Exponent", float) = 2
|
||||
_NoiseSpeed("Noise Speed", float) = 1
|
||||
_TurbulenceScale("Turbulence Scale", float) = 1
|
||||
_TurbulenceAmplitude("Turbulence Amplitude", float) = 1
|
||||
_FlowSpeed("Flow Speed", float) = 1
|
||||
_SafetyDistance("Safety Distance", float) = 20
|
||||
_Scattering("Scattering", float) = 1
|
||||
_ScatteringMinDist("ScatteringMinDist", float) = 1
|
||||
|
|
@ -80,7 +62,7 @@
|
|||
col.a *= stepSize * weight;
|
||||
sum.rgb += col.rgb * col.a * (1.0 - sum.a);
|
||||
sum.a += col.a;
|
||||
scatterSum += pow(col.a, _ScatteringDensityExponent) * scatter;
|
||||
scatterSum += pow(sum.a, _ScatteringDensityExponent) * scatter;
|
||||
}
|
||||
|
||||
float4 RayMarch( in float3 origin, in float3 direction, in float zbuf, in float2 screenUV, out float scatterSum )
|
||||
|
|
@ -111,7 +93,7 @@
|
|||
RaymarchStep( origin + rayDist * direction, step, 1-rayDist/_DepthCeiling, sum, _Scattering/pow(max(rayDist,_ScatteringMinDist),_ScatteringDistExponent), scatterSum);
|
||||
}
|
||||
|
||||
sum.rgb = pow( sum.rgb, 1 / _Gamma ) * _Exposure;
|
||||
sum.rgb = pow( sum.rgb / pow(100,_TintExponent), 1 / _Gamma ) * _Exposure;
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
|
|
|||
236
Assets/Shaders/Raymarching/VolumeRenderCustom.shader
Normal file
236
Assets/Shaders/Raymarching/VolumeRenderCustom.shader
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
Shader "VolSample/Volume Render" {
|
||||
Properties {
|
||||
_MainTex( "", 2D ) = "white" {}
|
||||
_StepExponent("Step Exponent", float) = 1
|
||||
_NebulaSurfaceHeight("Surface Displacement", 2D) = "black" {}
|
||||
_NebulaPatch("Patch", 2D) = "black" {}
|
||||
_NebulaPatchHeight("Patch Displacement", 2D) = "black" {}
|
||||
_NebulaTint("Tint Texture", 2D ) = "white" {}
|
||||
[HDR]_Tint("Tint Color", Color) = (1,1,1,1)
|
||||
//_GridTransform("Grid Transform", Vector) = (0,0,0,0)
|
||||
_NebulaFillDensity("Fill Density", float) = 1
|
||||
_NebulaFloorDensity("Floor Density", float) = 1
|
||||
_NebulaPatchDensity("Patch Density", float) = 1
|
||||
_NebulaFloorOffset("Floor Offset", float) = 1
|
||||
_NebulaFloorBlend("Floor Blend", float) = 1
|
||||
_NebulaPatchBlend("Patch Blend", float) = 1
|
||||
_Exposure("Exposure", float) = 1
|
||||
_Gamma("Gamma", float) = .5
|
||||
_TintExponent("Tint Exponent", float) = .5
|
||||
_DepthCeiling("Depth Ceiling", float) = 1000
|
||||
_DepthBlend("Depth Blend", float) = 100
|
||||
_NoiseAmplitude("Noise Amplitude", float) = 1
|
||||
_NoiseScale("Noise Scale", float) = 100
|
||||
_NoiseExponent("Noise Exponent", float) = 2
|
||||
_NoiseSpeed("Noise Speed", float) = 1
|
||||
_FlowScroll("Flow Scroll", float) = 1
|
||||
_FlowScale("Flow Scale", float) = 1
|
||||
_FlowAmplitude("Flow Amplitude", float) = 1
|
||||
_FlowSpeed("Flow Speed", float) = 1
|
||||
_SafetyDistance("Safety Distance", float) = 20
|
||||
_Scattering("Scattering", float) = 1
|
||||
_ScatteringMinDist("ScatteringMinDist", float) = 1
|
||||
_ScatteringDistExponent("ScatteringDistExponent", float) = 1
|
||||
_ScatteringDensityExponent("ScatteringDensityExponent", float) = 1
|
||||
}
|
||||
|
||||
CGINCLUDE;
|
||||
|
||||
uniform sampler2D _MainTex;
|
||||
uniform sampler2D _CameraDepthTexture;
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
#include "Assets/Shaders/Volumetric.cginc"
|
||||
|
||||
// the number of volume samples to take
|
||||
#define SAMPLE_COUNT 96
|
||||
|
||||
// Shared shader code for pixel view rays, given screen pos and camera frame vectors.
|
||||
// Camera vectors are passed in as this shader is run from a post proc camera, so the unity built-in values are not useful.
|
||||
uniform float4x4 _CamProj;
|
||||
uniform float4x4 _CamInvProj;
|
||||
uniform float3 _CamPos;
|
||||
uniform float3 _CamForward;
|
||||
uniform float3 _CamRight;
|
||||
uniform float _HalfFov;
|
||||
uniform int _FrameNumber;
|
||||
|
||||
uniform float _StepExponent;
|
||||
|
||||
uniform half _Gamma,
|
||||
_Exposure,
|
||||
_DepthCeiling,
|
||||
_DepthBlend,
|
||||
_Scattering,
|
||||
_ScatteringDensityExponent,
|
||||
_ScatteringDistExponent,
|
||||
_ScatteringMinDist;
|
||||
|
||||
// Dithering
|
||||
sampler2D _DitheringTex;
|
||||
float4 _DitheringCoords;
|
||||
|
||||
inline float nrand(float2 ScreenUVs)
|
||||
{
|
||||
return frac(sin(ScreenUVs.x * 12.9898 + ScreenUVs.y * 78.233) * 43758.5453);
|
||||
}
|
||||
|
||||
void RaymarchStep( in float3 pos, in float stepSize, in float weight, inout float4 sum, in float scatter, inout float scatterSum)
|
||||
{
|
||||
float4 col = VolumeSampleColor( pos );
|
||||
col.a *= stepSize * weight;
|
||||
sum.rgb += col.rgb * col.a * (1.0 - sum.a);
|
||||
sum.a += col.a;
|
||||
scatterSum += pow(col.a, _ScatteringDensityExponent) * scatter;
|
||||
}
|
||||
|
||||
float4 RayMarch( in float3 origin, in float3 direction, in float zbuf, in float2 screenUV, out float scatterSum )
|
||||
{
|
||||
scatterSum = 0;
|
||||
float blue = tex2D(_DitheringTex, screenUV * _DitheringCoords.xy + _DitheringCoords.zw).r;
|
||||
//float rand = frac(blue + _FrameNumber * 1.61803398875);
|
||||
//return float4(rand, rand, rand, 1);
|
||||
//half rand = frac(nrand(screenUV) + _FrameNumber * 1.61803398875);
|
||||
//half rand = nrand(screenUV + frac(_Time.x));
|
||||
half rand = blue;
|
||||
float4 sum = (float4)0.;
|
||||
scatterSum = 0.;
|
||||
|
||||
// setup sampling
|
||||
|
||||
float rayDist = 0;
|
||||
for( int i = 0; i < SAMPLE_COUNT; i++ )
|
||||
{
|
||||
float prevRayDist = rayDist;
|
||||
rayDist = pow((i+rand)/SAMPLE_COUNT,_StepExponent) * _DepthCeiling + 1;
|
||||
float distToSurf = zbuf - rayDist;
|
||||
if( distToSurf <= 0.001 || sum.a > .99) break;
|
||||
|
||||
float step = rayDist - prevRayDist;
|
||||
//float wt = (distToSurf >= step) ? 1. : distToSurf / step;
|
||||
|
||||
RaymarchStep( origin + rayDist * direction, step, 1-rayDist/_DepthCeiling, sum, _Scattering/pow(max(rayDist,_ScatteringMinDist),_ScatteringDistExponent), scatterSum);
|
||||
}
|
||||
|
||||
sum.rgb = pow( sum.rgb, 1 / _Gamma ) * _Exposure;
|
||||
return sum;
|
||||
}
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float4 pos : SV_POSITION;
|
||||
float4 screenPos : TEXCOORD1;
|
||||
};
|
||||
|
||||
v2f vert( appdata_base v )
|
||||
{
|
||||
v2f o;
|
||||
o.pos = UnityObjectToClipPos( v.vertex );
|
||||
o.screenPos = ComputeScreenPos( o.pos );
|
||||
return o;
|
||||
}
|
||||
|
||||
void computeCamera( in float2 q, out float3 rayOrigin, out float3 rd )
|
||||
{
|
||||
rayOrigin = _WorldSpaceCameraPos;
|
||||
|
||||
float2 p = 2.0*(q - 0.5);
|
||||
float4 d = mul(_CamProj, float4(p, 0, 1));
|
||||
d.xyz /= d.w;
|
||||
//rd = (d - rayOrigin);
|
||||
rd = normalize(d - rayOrigin);
|
||||
rayOrigin = d;
|
||||
//rayOrigin += _ProjectionParams.y * rd;
|
||||
}
|
||||
|
||||
inline float LinearEyeDepthToOutDepth(float z)
|
||||
{
|
||||
return (1 - _ZBufferParams.w * z) / (_ZBufferParams.z * z);
|
||||
}
|
||||
|
||||
float3 DepthToWorld(float2 uv, float depth) {
|
||||
float z = (1-depth) * 2.0 - 1.0;
|
||||
|
||||
float4 clipSpacePosition = float4(uv * 2.0 - 1.0, z, 1.0);
|
||||
|
||||
float4 viewSpacePosition = mul(_CamInvProj,clipSpacePosition);
|
||||
viewSpacePosition /= viewSpacePosition.w;
|
||||
|
||||
float4 worldSpacePosition = mul(unity_ObjectToWorld,viewSpacePosition);
|
||||
|
||||
return worldSpacePosition.xyz;
|
||||
}
|
||||
|
||||
// MRT shader
|
||||
struct FragmentOutput
|
||||
{
|
||||
half4 dest0 : COLOR0;
|
||||
half4 dest1 : COLOR1;
|
||||
};
|
||||
|
||||
FragmentOutput frag( v2f i ) : SV_Target //out float outDepth : SV_Depth
|
||||
{
|
||||
FragmentOutput o;
|
||||
|
||||
float2 q = i.screenPos.xy / i.screenPos.w;
|
||||
|
||||
// camera
|
||||
float3 rayOrigin, rayDirection;
|
||||
computeCamera( q, rayOrigin, rayDirection );
|
||||
|
||||
// z buffer / scene depth for this pixel
|
||||
float4 screenPos = UNITY_PROJ_COORD( i.screenPos );
|
||||
float2 screenUV = screenPos.xy / screenPos.w;
|
||||
|
||||
float depthSample = tex2Dproj( _CameraDepthTexture, screenPos ).r;
|
||||
//float depthValue = LinearEyeDepth(depthSample);
|
||||
|
||||
float3 worldDepth = DepthToWorld(screenPos, depthSample);
|
||||
float depthValue = length(worldDepth-rayOrigin);
|
||||
|
||||
// march through volume
|
||||
float scatter;
|
||||
float4 clouds = RayMarch( rayOrigin, rayDirection, depthValue, screenUV, scatter );
|
||||
o.dest1 = .5 + scatter*.5;//smoothstep(1,.99,clouds.a)*pow(clouds.a,1)*.5;
|
||||
|
||||
float3 bgcol = tex2Dlod( _MainTex, float4(q, 0., 0.) );
|
||||
// add in camera render colours, if not zfar (so we exclude skybox)
|
||||
if(clouds.a < .99 && depthValue < _DepthCeiling)
|
||||
{
|
||||
// Blend out camera render when outside marching range
|
||||
if( depthValue >= _DepthCeiling - _DepthBlend )
|
||||
clouds.a = lerp(clouds.a, 1, (depthValue - (_DepthCeiling - _DepthBlend)) / _DepthBlend);
|
||||
|
||||
//clouds.xyz = lerp(clouds.xyz, bgcol, 1-clouds.a);
|
||||
clouds.xyz += (1. - clouds.a) * bgcol;
|
||||
// assume zbuffer represents opaque surface
|
||||
|
||||
}
|
||||
clouds.a = 1.;
|
||||
|
||||
//outDepth = LinearEyeDepthToOutDepth(min(depthValue, rayDist));
|
||||
|
||||
o.dest0 = clouds;
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
ENDCG
|
||||
|
||||
Subshader
|
||||
{
|
||||
Pass
|
||||
{
|
||||
ZTest Always Cull Off ZWrite Off
|
||||
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
|
||||
Fallback off
|
||||
|
||||
} // shader
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7940bc6e15d348ff930f4ebb3c475cf2
|
||||
timeCreated: 1623409494
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
#include "UnityCG.cginc"
|
||||
|
||||
//
|
||||
// Value Noise 3D Deriv
|
||||
|
|
@ -35,30 +36,29 @@ float4 Value3D_Deriv( float3 P )
|
|||
return float4(1,1,1,1);
|
||||
}
|
||||
|
||||
uniform sampler2D _Surface;
|
||||
float4 _Surface_TexelSize;
|
||||
uniform sampler2D _Patch;
|
||||
uniform sampler2D _Displacement;
|
||||
uniform sampler2D _TintTexture;
|
||||
float4 _TintTexture_TexelSize;
|
||||
uniform sampler2D _NebulaSurfaceHeight;
|
||||
float4 _NebulaSurfaceHeight_TexelSize;
|
||||
uniform sampler2D _NebulaPatch;
|
||||
uniform sampler2D _NebulaPatchHeight;
|
||||
uniform sampler2D _NebulaTint;
|
||||
float4 _NebulaTint_TexelSize;
|
||||
|
||||
uniform float3 _GridTransform;
|
||||
|
||||
uniform half4 _Tint;
|
||||
|
||||
uniform half _GridFillDensity,
|
||||
_GridFloorDensity,
|
||||
_GridPatchDensity,
|
||||
_GridFloorOffset,
|
||||
_GridFloorBlend,
|
||||
_GridPatchBlend,
|
||||
uniform half _NebulaFillDensity,
|
||||
_NebulaFloorDensity,
|
||||
_NebulaPatchDensity,
|
||||
_NebulaFloorOffset,
|
||||
_NebulaFloorBlend,
|
||||
_NebulaPatchBlend,
|
||||
_TintExponent,
|
||||
_NoiseFrequency,
|
||||
_NoiseScale,
|
||||
_NoiseExponent,
|
||||
_NoiseStrength,
|
||||
_NoiseAmplitude,
|
||||
_NoiseSpeed,
|
||||
_TurbulenceScale,
|
||||
_TurbulenceAmplitude,
|
||||
_FlowScale,
|
||||
_FlowAmplitude,
|
||||
_FlowScroll,
|
||||
_FlowSpeed,
|
||||
_SafetyDistance;
|
||||
|
||||
|
|
@ -93,19 +93,19 @@ float parabola( float x, float k )
|
|||
float f(float3 pos)
|
||||
{
|
||||
float2 uv = -(pos.xz-_GridTransform.xy)/_GridTransform.z + float2(.5,.5);
|
||||
const float surface = tex2Dlod(_Surface, half4(uv, 0, 0)).r;
|
||||
const float dist = pos.y + surface - _GridFloorOffset;
|
||||
const float surfaceDisp = tex2Dlod(_NebulaSurfaceHeight, half4(uv, 0, 0)).r;
|
||||
const float dist = pos.y + surfaceDisp - _NebulaFloorOffset;
|
||||
|
||||
const float fillDensity = smoothstep(_SafetyDistance,0,dist) * _GridFillDensity;
|
||||
const float fillDensity = smoothstep(_SafetyDistance,0,dist) * _NebulaFillDensity;
|
||||
|
||||
if(dist < _SafetyDistance)
|
||||
{
|
||||
const float patch = tex2Dlod(_Patch, half4(uv, 0, 0)).r;
|
||||
const float displacement = tex2Dlod(_Displacement, half4(uv, 0, 0)).r;
|
||||
const float patch = tex2Dlod(_NebulaPatch, half4(uv, 0, 0)).r;
|
||||
const float patchDisp = tex2Dlod(_NebulaPatchHeight, half4(uv, 0, 0)).r;
|
||||
|
||||
const float patchDensity = saturate((-abs(pos.y+displacement)+patch)/_GridPatchBlend)*_GridPatchDensity;
|
||||
const float patchDensity = saturate((-abs(pos.y+patchDisp - _NebulaFloorOffset)+patch)/_NebulaPatchBlend)*_NebulaPatchDensity;
|
||||
const float floorDist = -dist;
|
||||
const float floorDensity = floorDist/_GridFloorBlend*_GridFloorDensity;
|
||||
const float floorDensity = floorDist/_NebulaFloorBlend*_NebulaFloorDensity;
|
||||
const float fogDensity = patchDensity + max(0,floorDensity);
|
||||
return fogDensity + fillDensity;
|
||||
}
|
||||
|
|
@ -117,13 +117,13 @@ float f(float3 pos)
|
|||
float3 calcNormal (float3 pos, float2 uv)
|
||||
{
|
||||
float3 n = float3(pos.x,
|
||||
tex2D(_Surface, float2(uv.x, uv.y + _Surface_TexelSize.y)).x,
|
||||
pos.z + _Surface_TexelSize.y * _GridTransform.z);
|
||||
tex2D(_NebulaSurfaceHeight, float2(uv.x, uv.y + _NebulaSurfaceHeight_TexelSize.y)).x,
|
||||
pos.z + _NebulaSurfaceHeight_TexelSize.y * _GridTransform.z);
|
||||
float3 me = float3(pos.x,
|
||||
tex2D(_Surface, float2(uv.x, uv.y)).x,
|
||||
tex2D(_NebulaSurfaceHeight, float2(uv.x, uv.y)).x,
|
||||
pos.z);
|
||||
float3 e = float3(pos.x + _Surface_TexelSize.x * _GridTransform.z,
|
||||
tex2D(_Surface, float2(uv.x + _Surface_TexelSize.x, uv.y)).x,
|
||||
float3 e = float3(pos.x + _NebulaSurfaceHeight_TexelSize.x * _GridTransform.z,
|
||||
tex2D(_NebulaSurfaceHeight, float2(uv.x + _NebulaSurfaceHeight_TexelSize.x, uv.y)).x,
|
||||
pos.z);
|
||||
float3 norm = cross(normalize(n-me),normalize(e-me));
|
||||
return norm;
|
||||
|
|
@ -131,11 +131,11 @@ float3 calcNormal (float3 pos, float2 uv)
|
|||
|
||||
float2 tintGradient (float3 pos, float2 uv, float mip, out float3 tint)
|
||||
{
|
||||
tint = tex2Dlod(_TintTexture, float4(uv.x, uv.y, 0, mip));
|
||||
tint = tex2Dlod(_NebulaTint, float4(uv.x, uv.y, 0, mip));
|
||||
float me = length(tint);
|
||||
return float2(
|
||||
length(tex2Dlod(_TintTexture, float4(uv.x + 0.001953125, uv.y, 0, mip))) - me,
|
||||
length(tex2Dlod(_TintTexture, float4(uv.x, uv.y + 0.001953125, 0, mip))) - me
|
||||
length(tex2Dlod(_NebulaTint, float4(uv.x + 0.001953125, uv.y, 0, mip))) - me,
|
||||
length(tex2Dlod(_NebulaTint, float4(uv.x, uv.y + 0.001953125, 0, mip))) - me
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -149,22 +149,26 @@ float3 calcNormal( float3 p ) // for function f(p)
|
|||
k.xxx*f( p + k.xxx*h ) );
|
||||
}
|
||||
|
||||
float3 flow(float3 pos, out float3 normal)
|
||||
float3 flow(float3 pos, out float noise)
|
||||
{
|
||||
//float2 uv = -(pos.xz-_GridTransform.xy)/_GridTransform.z + float2(.5,.5);
|
||||
normal = calcNormal(pos);
|
||||
//normal = calcNormal(pos);
|
||||
//return normalize(float3(-normal.x,0,-normal.z)) * _TurbulenceAmplitude;
|
||||
const float4 simplex = Value3D_Deriv( pos / _TurbulenceScale - float3(0,_Time.x,0) );
|
||||
return cross(normalize(simplex.yzw), normal) * _TurbulenceAmplitude * (simplex.x-.5);
|
||||
const float4 noiseSample1 = Value3D_Deriv( pos / _FlowScale - float3(0,_FlowScroll,0) );
|
||||
const float4 noiseSample2 = Value3D_Deriv( (pos / _FlowScale - float3(0,_FlowScroll,0)) / 1.61803398875 ); // make it golden
|
||||
noise = noiseSample1.x;
|
||||
return cross(normalize(noiseSample2.yzw), normalize(noiseSample1.yzw)) * _FlowAmplitude;
|
||||
//return BitangentNoise3D(pos / _FlowScale - float3(0,_FlowScroll,0)) * _FlowAmplitude;
|
||||
}
|
||||
|
||||
float d(float3 pos, out float3 normal)
|
||||
float d(float3 pos)
|
||||
{
|
||||
const float3 turbulence = flow(pos, normal);
|
||||
const float lerp1 = frac(_Time.x * _FlowSpeed);
|
||||
const float lerp2 = frac(_Time.x * _FlowSpeed + .5);
|
||||
const float noise1 = pow(triNoise3d((pos+turbulence * lerp1)*_NoiseFrequency),_NoiseExponent) * _NoiseStrength * parabola(lerp1, 2);
|
||||
const float noise2 = pow(triNoise3d((pos+turbulence * lerp2)*_NoiseFrequency),_NoiseExponent) * _NoiseStrength * parabola(lerp2, 2);
|
||||
float noise;
|
||||
const float3 fl = flow(pos, noise);
|
||||
const float lerp1 = frac(_Time.x * _FlowSpeed + noise);
|
||||
const float lerp2 = frac(_Time.x * _FlowSpeed + noise + .5);
|
||||
const float noise1 = pow(triNoise3d((pos+fl * lerp1) / _NoiseScale),_NoiseExponent) * _NoiseAmplitude * parabola(lerp1, 2);
|
||||
const float noise2 = pow(triNoise3d((pos+fl * lerp2) / _NoiseScale),_NoiseExponent) * _NoiseAmplitude * parabola(lerp2, 2);
|
||||
pos.y += (noise1 + noise2);
|
||||
return f(pos);
|
||||
}
|
||||
|
|
@ -172,12 +176,12 @@ float d(float3 pos, out float3 normal)
|
|||
float4 VolumeSampleColor(float3 pos)
|
||||
{
|
||||
float2 uv = -(pos.xz-_GridTransform.xy)/_GridTransform.z + float2(.5,.5);
|
||||
float3 normal;
|
||||
float density = d(pos, normal);
|
||||
float3 tint = tex2Dlod(_TintTexture, float4(uv.x, uv.y, 0, 1 / pow(density, .25)));
|
||||
float density = d(pos);
|
||||
float3 tint = tex2Dlod(_NebulaTint, float4(uv.x, uv.y, 0, 1 / pow(density, .25)));
|
||||
//float2 tintGrad = tintGradient(pos, uv, 1 / pow(density, .25), tint);
|
||||
//float3 tintNormal = normalize(float3(tintGrad.x, 0, tintGrad.y));
|
||||
//tint *= abs(dot(normal, float3(0,-1,0)));
|
||||
const float albedo = pow(1/max(.01,density), _TintExponent) * smoothstep(0,-250,pos.y);
|
||||
return float4(albedo*_Tint.rgb*tint, density);
|
||||
// pow(abs(1-density), _TintExponent)
|
||||
const float albedo = smoothstep(0,-250,pos.y) * pow(1/max(.01,density), _TintExponent);
|
||||
return float4(albedo*tint, density);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue