More Nebula Fuckery

Performance optimization for nebula raymarching
Fixed zone generator crashing due to lack of station and turret hull data (where'd they go?!)
This commit is contained in:
Reinier Vens 2022-07-12 14:22:23 +01:00
commit 7210d298af
17 changed files with 90 additions and 108 deletions

View file

@ -58,7 +58,7 @@ Material:
m_Floats:
- _BumpScale: 1
- _Cutoff: 1
- _Depth: 0.5
- _Depth: 0.25
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _Frequency: 6.2832

View file

@ -58,7 +58,7 @@ Material:
m_Floats:
- _BumpScale: 1
- _Cutoff: 1
- _Depth: 2
- _Depth: 15
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _Frequency: 6.2832
@ -70,7 +70,7 @@ Material:
- _OcclusionStrength: 1
- _Parallax: 0.02
- _Phase: 0
- _Power: 2
- _Power: 3
- _SinePower: 1.25
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1

View file

@ -57,7 +57,7 @@ Material:
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Constant: -51.6
- _Constant: -30
- _Cutoff: 0.5
- _Depth: 10
- _DetailNormalMapScale: 1

View file

@ -57,7 +57,7 @@ Material:
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.153
- _Cutoff: 0.1
- _Depth: 2000
- _DetailNormalMapScale: 1
- _DstBlend: 0

View file

@ -58,7 +58,7 @@ Material:
m_Floats:
- _BumpScale: 1
- _Cutoff: 1
- _Depth: 8
- _Depth: 15
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _Frequency: 6.2832
@ -70,7 +70,7 @@ Material:
- _OcclusionStrength: 1
- _Parallax: 0.02
- _Phase: 0
- _Power: 124.97
- _Power: 128
- _SinePower: 1.25
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1

View file

@ -14,7 +14,7 @@ float GetDensity(float3 startPos, float3 dir, float maxSampleDistance, float ray
if(raymarchDistance > maxSampleDistance) break;
float step = raymarchDistance - prevRayDist;
float3 rayPos = startPos + dir * raymarchDistance;
float fade = smoothstep(_ProjectionParams.z*.75,_ProjectionParams.z*.95, raymarchDistance);
float fade = smoothstep(_ProjectionParams.z*.95,_ProjectionParams.z, raymarchDistance);
IntegrateRaymarch(startPos, rayPos, fade, step, result);
if (result.intTransmittance < 0.01f) {
result.intTransmittance = 0;

View file

@ -117,7 +117,7 @@ float ApplyCoverageToDensity(float sampleResult, float coverage){
float SampleDensity(float3 worldPos,int lod, bool cheap, out float wetness) {
wetness = 0;
return d(worldPos)*_CloudOverallDensity*10;
return density(worldPos)*_CloudOverallDensity*10;
// //Store the pos without wind applied.
// float3 unwindWorldPos = worldPos;
//
@ -292,7 +292,7 @@ void InitRaymarchStatus(inout RaymarchStatus result){
void IntegrateRaymarch(float3 startPos, float3 rayPos, float fade, float stepsize, inout RaymarchStatus result){
float4 c = VolumeSampleColor(rayPos);
float density = c.a;//SampleDensity(rayPos, 0, false, wetness);
float density = c.a;
if (density <= 0.0f)
return;
float extinction = _ExtinctionCoefficient * density;
@ -300,7 +300,7 @@ void IntegrateRaymarch(float3 startPos, float3 rayPos, float fade, float stepsiz
float clampedExtinction = max(extinction, 1e-7);
float transmittance = exp(-extinction * stepsize);
float3 luminance = c.rgb * (1-fade);//SampleEnergy(rayPos, viewdir);
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?

View file

@ -28,7 +28,7 @@ RenderTexture:
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_FilterMode: 2
m_Aniso: 0
m_MipBias: 0
m_WrapU: 1

BIN
Assets/Resources/Settings.asset (Stored with Git LFS)

Binary file not shown.

View file

@ -16184,11 +16184,11 @@ MonoBehaviour:
Span: 128
Speed: -0.15
MinimumSize: 0
MaximumSize: 0.5
MaximumSize: 0.75
Spacing: 6
Ceiling: -200
Floor: -40
HeightExponent: 1000000
Ceiling: -300
Floor: -20
HeightExponent: 100000
NebulaSurfaceHeight: {fileID: 8400000, guid: 2fc21a9d3e9647948b56e384cd9378b3, type: 2}
NebulaPatchHeight: {fileID: 8400000, guid: c618a6b20b3c7394590c59a9fa467e86, type: 2}
NebulaPatch: {fileID: 8400000, guid: 06ec5ef8a5830fc44b1ac1a6e123add5, type: 2}

Binary file not shown.

View file

@ -400,7 +400,7 @@ MonoBehaviour:
m_HorizontalDamping: 0.5
m_VerticalDamping: 0.5
m_ScreenX: 0.45
m_ScreenY: 0.45
m_ScreenY: 0.39
m_DeadZoneWidth: 0
m_DeadZoneHeight: 0
m_SoftZoneWidth: 0.8
@ -440,8 +440,8 @@ MonoBehaviour:
m_YDamping: 0
m_ZDamping: 0
m_TargetMovementOnly: 0
m_ScreenX: 0.75
m_ScreenY: 0.75
m_ScreenX: 0.74
m_ScreenY: 0.6
m_CameraDistance: 381.18
m_DeadZoneWidth: 0
m_DeadZoneHeight: 0
@ -802,8 +802,8 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 741153267}
m_LocalRotation: {x: 0.24134962, y: 0.7351235, z: -0.5426147, w: 0.32697564}
m_LocalPosition: {x: -144.21849, y: 325.46454, z: 116.09439}
m_LocalRotation: {x: 0.17419034, y: 0.82403576, z: -0.4098015, w: 0.3502649}
m_LocalPosition: {x: -255.6498, y: 261.96085, z: 220.16467}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 412917866}
@ -865,8 +865,8 @@ MonoBehaviour:
MaximumSize: 0.5
Spacing: 6
Ceiling: 200
Floor: -50
HeightExponent: 1000000
Floor: -10
HeightExponent: 300000
NebulaSurfaceHeight: {fileID: 8400000, guid: 2fc21a9d3e9647948b56e384cd9378b3, type: 2}
NebulaPatchHeight: {fileID: 8400000, guid: c618a6b20b3c7394590c59a9fa467e86, type: 2}
NebulaPatch: {fileID: 8400000, guid: 06ec5ef8a5830fc44b1ac1a6e123add5, type: 2}
@ -1252,8 +1252,8 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 965376321}
m_LocalRotation: {x: 0.24134962, y: 0.7351235, z: -0.5426147, w: 0.32697564}
m_LocalPosition: {x: -144.21849, y: 325.46454, z: 116.09439}
m_LocalRotation: {x: 0.17419034, y: 0.82403576, z: -0.4098015, w: 0.3502649}
m_LocalPosition: {x: -255.6498, y: 261.96085, z: 220.16467}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
@ -1671,7 +1671,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1362573162}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: -214, z: 0}
m_LocalPosition: {x: 0, y: -125, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}

View file

@ -46,6 +46,11 @@ public class LoadoutGenerator
public EntityPack GenerateShipLoadout(Predicate<HullData> hullFilter = null)
{
var hullData = RandomHull(HullType.Ship, hullFilter);
if(hullData==null)
{
ItemManager.Log("Unable to generate ship loadout: no compatible hull found!");
return null;
}
var hull = ItemManager.CreateInstance(hullData) as EquippableItem;
if(hull==null)
ItemManager.Log("WHAT???");
@ -57,7 +62,13 @@ public class LoadoutGenerator
public OrbitalEntityPack GenerateTurretLoadout()
{
var hull = ItemManager.CreateInstance(RandomHull(HullType.Turret)) as EquippableItem;
var hullData = RandomHull(HullType.Turret);
if(hullData==null)
{
ItemManager.Log("Unable to generate turret loadout: no compatible hull found!");
return null;
}
var hull = ItemManager.CreateInstance(hullData) as EquippableItem;
var entity = new OrbitalEntity(ItemManager, null, hull, Guid.Empty, ItemManager.GameplaySettings.DefaultEntitySettings);
entity.Faction = Faction;
OutfitEntity(entity);
@ -67,6 +78,11 @@ public class LoadoutGenerator
public OrbitalEntityPack GenerateStationLoadout()
{
var hullData = RandomHull(HullType.Station);
if(hullData==null)
{
ItemManager.Log("Unable to generate station loadout: no compatible hull found!");
return null;
}
var hull = ItemManager.CreateInstance(hullData) as EquippableItem;
var entity = new OrbitalEntity(ItemManager, null, hull, Guid.Empty, ItemManager.GameplaySettings.DefaultEntitySettings);
entity.Faction = Faction;

View file

@ -277,6 +277,7 @@ public static class ZoneGenerator
};
pack.Orbits.Add(turretOrbit);
var turret = loadoutGenerator.GenerateTurretLoadout();
if (turret == null) return;
turret.Orbit = turretOrbit.ID;
pack.Entities.Add(turret);
}
@ -313,6 +314,7 @@ public static class ZoneGenerator
var lagrangeOrbit = CreateLagrangeOrbit(orbit);
var station = GetLoadoutGenerator(nearestFaction).GenerateStationLoadout();
if (station == null) continue;
station.Orbit = lagrangeOrbit.ID;
station.SecurityLevel = security;
station.SecurityRadius = pack.Radius;
@ -324,7 +326,9 @@ public static class ZoneGenerator
var enemyCount = (int)(random.NextFloat() * factionPresence * 2) + stationCount;
for (int i = 0; i < enemyCount; i++)
{
pack.Entities.Add(GetLoadoutGenerator(nearestFaction).GenerateShipLoadout());
var ship = GetLoadoutGenerator(nearestFaction).GenerateShipLoadout();
if (ship == null) continue;
pack.Entities.Add(ship);
}
return pack;

View file

@ -135,7 +135,7 @@ void UpdateParticles(uint3 id : SV_DispatchThreadID, int3 groupID : SV_GroupThre
float noise;
const float3 flowDir = flow(particles[id.x].position, noise);
particles[id.x].position += flowDir * (lifetime * noise);
const float density = pow(f(particles[id.x].position) + .01, 0.15);
particles[id.x].size = lerp(minimumSize, maximumSize, saturate(RandomValue() * density)) * parabola(lifetime, 2);
particles[id.x].color = HueTexture.SampleLevel(samplerHueTexture, float2(RandomValue(), 0.0f), 0.0f).rgb * density;
const float d = pow(density(particles[id.x].position) + .01, 0.15);
particles[id.x].size = lerp(minimumSize, maximumSize, saturate(RandomValue() * d)) * parabola(lifetime, 2);
particles[id.x].color = HueTexture.SampleLevel(samplerHueTexture, float2(RandomValue(), 0.0f), 0.0f).rgb * d;
}

View file

@ -97,63 +97,17 @@ float2 getUV(float3 pos)
return -(pos.xz-_GridTransform.xy)/_GridTransform.z + float2(.5,.5);
}
float f(float3 pos)
float cloudDensity(float3 pos, float surfaceDisp)
{
float2 uv = getUV(pos);
const float surfaceDisp = tex2Dlod(_NebulaSurfaceHeight, half4(uv, 0, 0)).r;
const float dist = pos.y + surfaceDisp - _NebulaFloorOffset;
const float patch = tex2Dlod(_NebulaPatch, half4(uv, 0, 0)).r;
const float patchDisp = tex2Dlod(_NebulaPatchHeight, half4(uv, 0, 0)).r;
const float fillDensity = smoothstep(min(_SafetyDistance,surfaceDisp),0,dist) * _NebulaFillDensity;
if(dist < _SafetyDistance)
{
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+patchDisp - _NebulaFloorOffset)+patch)/_NebulaPatchBlend)*_NebulaPatchDensity;
const float floorDist = -dist;
const float floorDensity = floorDist/_NebulaFloorBlend*_NebulaFloorDensity;
const float fogDensity = patchDensity + max(0,floorDensity);
return fogDensity + fillDensity;
}
return fillDensity;
}
float3 calcNormal (float3 pos, float2 uv)
{
float3 n = float3(pos.x,
tex2D(_NebulaSurfaceHeight, float2(uv.x, uv.y + _NebulaSurfaceHeight_TexelSize.y)).x,
pos.z + _NebulaSurfaceHeight_TexelSize.y * _GridTransform.z);
float3 me = float3(pos.x,
tex2D(_NebulaSurfaceHeight, float2(uv.x, uv.y)).x,
pos.z);
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;
}
float2 tintGradient (float3 pos, float2 uv, float mip, out float3 tint)
{
tint = tex2Dlod(_NebulaTint, float4(uv.x, uv.y, 0, mip));
float me = length(tint);
return float2(
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
);
}
float3 calcNormal( float3 p ) // for function f(p)
{
const float h = 0.0001; // replace by an appropriate value
const float2 k = float2(1,-1);
return normalize( k.xyy*f( p + k.xyy*h ) +
k.yyx*f( p + k.yyx*h ) +
k.yxy*f( p + k.yxy*h ) +
k.xxx*f( p + k.xxx*h ) );
const float patchDensity = saturate((-abs(pos.y+patchDisp - _NebulaFloorOffset)+patch)/_NebulaPatchBlend)*_NebulaPatchDensity;
const float floorDist = -dist;
const float floorDensity = floorDist/_NebulaFloorBlend*_NebulaFloorDensity;
return patchDensity + max(0,floorDensity);
}
float3 flow(float3 pos, out float noise)
@ -166,33 +120,41 @@ float3 flow(float3 pos, out float noise)
float tri2(in float x){return 1-2*abs(frac(x)-.5);}
float d(float3 pos)
float density(float3 pos)
{
float noise;
const float3 fl = flow(pos, noise);
const float lerp1 = frac(_Time.x * _FlowSpeed + noise);
const float lerp2 = frac(_Time.x * _FlowSpeed + .5 + noise);
const float noise1 = pow(triNoise3d((pos+fl * lerp1) / _NoiseScale),_NoiseExponent) * _NoiseAmplitude * tri2(lerp1);
const float noise2 = pow(triNoise3d((pos+fl * lerp2) / _NoiseScale),_NoiseExponent) * _NoiseAmplitude * tri2(lerp2);
pos.y += (noise1 + noise2);
const float lerp3 = frac(_Time.x * _FlowSpeed * 2);
const float lerp4 = frac(_Time.x * _FlowSpeed * 2 + .5);
const float noise3 = pow(triNoise3d((pos+fl * lerp3) / _NoiseScale * 2), _NoiseExponent) * _NoiseAmplitude * tri2(lerp3) / 2;
const float noise4 = pow(triNoise3d((pos+fl * lerp4) / _NoiseScale * 2), _NoiseExponent) * _NoiseAmplitude * tri2(lerp4) / 2;
pos.y -= (noise3 + noise4);
return f(pos);
float2 uv = getUV(pos);
const float surfaceDisp = tex2Dlod(_NebulaSurfaceHeight, half4(uv, 0, 0)).r;
const float dist = pos.y + surfaceDisp;
float d = pow((dist+20)/300,-2) * _NebulaFillDensity;
if(dist < _SafetyDistance)
{
const float heightFade = smoothstep(_SafetyDistance,_SafetyDistance*.75,dist);
float noise;
const float3 fl = flow(pos, noise);
const float lerp1 = frac(_Time.x * _FlowSpeed + noise);
const float lerp2 = frac(_Time.x * _FlowSpeed + .5 + noise);
const float noise1 = pow(triNoise3d((pos+fl * lerp1) / _NoiseScale),_NoiseExponent) * _NoiseAmplitude * tri2(lerp1);
const float noise2 = pow(triNoise3d((pos+fl * lerp2) / _NoiseScale),_NoiseExponent) * _NoiseAmplitude * tri2(lerp2);
pos.y += (noise1 + noise2) * heightFade;
const float lerp3 = frac(_Time.x * _FlowSpeed * 2);
const float lerp4 = frac(_Time.x * _FlowSpeed * 2 + .5);
const float noise3 = pow(triNoise3d((pos+fl * lerp3) / _NoiseScale * 4), _NoiseExponent) * _NoiseAmplitude * tri2(lerp3) / 2;
const float noise4 = pow(triNoise3d((pos+fl * lerp4) / _NoiseScale * 4), _NoiseExponent) * _NoiseAmplitude * tri2(lerp4) / 2;
pos.y -= (noise3 + noise4) * heightFade;
d += cloudDensity(pos, surfaceDisp);
}
return d;
}
float4 VolumeSampleColor(float3 pos)
{
float density = d(pos);
float clampedDensity = max(.01,density);
float d = density(pos);
float2 uv = getUV(pos);
float3 tint = tex2Dlod(_NebulaTint, float4(uv.x, uv.y, 0, pow(clampedDensity, _TintLodExponent)));
float3 tint = tex2Dlod(_NebulaTint, float4(uv.x, uv.y, 0, pow(max(.01,d), _TintLodExponent)));
//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)));
// pow(abs(1-density), _TintExponent)
const float albedo = smoothstep(0,-250,pos.y) * pow(clampedDensity, _TintExponent) * _NebulaLuminance;
return float4(albedo*tint, density);
const float albedo = smoothstep(0,-250,pos.y) * pow(max(.1,d), _TintExponent) * _NebulaLuminance;
return float4(albedo*tint, d);
}

BIN
GameData/PlayerSettings.msgpack (Stored with Git LFS)

Binary file not shown.