Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realy black hole #29

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Content.Client/Singularity/SingularityOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public sealed class SingularityOverlay : Overlay, IEntityEventSubscriber
/// Maximum number of distortions that can be shown on screen at a time.
/// If this value is changed, the shader itself also needs to be updated.
/// </summary>
public const int MaxCount = 5;
public const int MaxCount = 32;

private const float MaxDistance = 20f;

private const float MaxDeformation = 8.0f;

public override OverlaySpace Space => OverlaySpace.WorldSpace;
public override bool RequestScreenTexture => true;

Expand All @@ -38,6 +40,7 @@ public SingularityOverlay()
private readonly Vector2[] _positions = new Vector2[MaxCount];
private readonly float[] _intensities = new float[MaxCount];
private readonly float[] _falloffPowers = new float[MaxCount];
private readonly bool[] _haveEventHorizons = new bool[MaxCount];
private int _count = 0;

protected override bool BeforeDraw(in OverlayDrawArgs args)
Expand Down Expand Up @@ -68,6 +71,7 @@ protected override bool BeforeDraw(in OverlayDrawArgs args)
_positions[_count] = tempCoords;
_intensities[_count] = distortion.Intensity;
_falloffPowers[_count] = distortion.FalloffPower;
_haveEventHorizons[_count] = distortion.HasEventHorizon;
_count++;

if (_count == MaxCount)
Expand All @@ -87,6 +91,8 @@ protected override void Draw(in OverlayDrawArgs args)
_shader?.SetParameter("position", _positions);
_shader?.SetParameter("intensity", _intensities);
_shader?.SetParameter("falloffPower", _falloffPowers);
_shader?.SetParameter("hasEventHorizon", _haveEventHorizons);
_shader?.SetParameter("maxDeformation", MaxDeformation);
_shader?.SetParameter("SCREEN_TEXTURE", ScreenTexture);

var worldHandle = args.WorldHandle;
Expand Down Expand Up @@ -128,10 +134,16 @@ private void OnProjectFromScreenToMap(ref PixelToMapEvent args)
if (distance >= maxDistance)
deformation = 0.0f;
else
{
deformation *= 1.0f - MathF.Pow(distance / maxDistance, 4.0f);

if (deformation > 0.8)
deformation = MathF.Pow(deformation, 0.3f);
if (_haveEventHorizons[i] && _intensities[i] > 0.0f)
{
deformation += MathF.Pow(deformation / MaxDeformation, 32.0f);
if (deformation > 2.0f * MaxDeformation)
deformation = 0.0f;
}
}

finalCoords -= delta * deformation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ public sealed partial class SingularityDistortionComponent : Component

[DataField, AutoNetworkedField, ViewVariables(VVAccess.ReadWrite)]
public float FalloffPower = MathF.Sqrt(2f);

/// <summary>
/// Whether this Distortion should have a visible pitch-black region in the center. True for bodies with
/// extreme gravity, like the singularity.
/// </summary>
[DataField, AutoNetworkedField, ViewVariables(VVAccess.ReadWrite)]
public bool HasEventHorizon;
}
}
5 changes: 2 additions & 3 deletions Resources/Prototypes/Entities/Objects/Fun/toys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1217,12 +1217,11 @@
- type: Sprite
sprite: Objects/Fun/toys.rsi
state: singularitytoy
- type: Icon
sprite: Objects/Fun/toys.rsi
state: singularitytoy
color: transparent
- type: SingularityDistortion
intensity: 2000
falloffPower: 2.6
hasEventHorizon: true
- type: Item
size: Normal
sprite: Objects/Fun/toys.rsi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
description: A mesmerizing swirl of darkness that sucks in everything. If it's moving towards you, run.
components:
- type: Clickable
- type: AmbientSound
volume: -4
range: 14
sound:
path: /Audio/Effects/singularity.ogg
- type: Physics
bodyType: Dynamic
bodyStatus: InAir
Expand Down Expand Up @@ -54,49 +49,15 @@
- type: SingularityDistortion
falloffPower: 2.529822
intensity: 3645
hasEventHorizon: true
- type: RadiationSource
slope: 0.2 # its emit really far away
intensity: 2
- type: PointLight
enabled: true
radius: 10
- type: Appearance
- type: GuideHelp
guides: [ Singularity, Power ] # uhhh.. I would hoped they'd have read the manual before ever getting in viewing distance...
- type: WarpPoint
follow: true
location: "! сингулярность"
- type: Sprite
sprite: Structures/Power/Generation/Singularity/singularity_1.rsi
shader: unshaded
layers:
- map: [ "VisualLevel" ]
state: singularity_1
- type: GenericVisualizer
visuals:
enum.SingularityAppearanceKeys.Singularity:
VisualLevel:
1:
sprite: Structures/Power/Generation/Singularity/singularity_1.rsi
state: singularity_1
scale: 1.0,1.0
2:
sprite: Structures/Power/Generation/Singularity/singularity_2.rsi
state: singularity_2
scale: 1.0,1.0
3:
sprite: Structures/Power/Generation/Singularity/singularity_3.rsi
state: singularity_3
scale: 1.0,1.0
4:
sprite: Structures/Power/Generation/Singularity/singularity_4.rsi
state: singularity_4
scale: 1.0,1.0
5:
sprite: Structures/Power/Generation/Singularity/singularity_5.rsi
state: singularity_5
scale: 1.5,1.5
6:
sprite: Structures/Power/Generation/Singularity/singularity_6.rsi
state: singularity_6
scale: .9,.9
sprite: null
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@
- type: CanMoveInAir
- type: RandomWalk
- type: SingularityDistortion
intensity: 1000
falloffPower: 2.7
intensity: 200
falloffPower: 1.7

- type: entity
id: AnomalyElectricity
Expand Down
42 changes: 26 additions & 16 deletions Resources/Textures/Shaders/singularity.swsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ uniform sampler2D SCREEN_TEXTURE;
uniform highp vec2 renderScale;
uniform highp float maxDistance;
uniform lowp int count;
uniform highp float maxDeformation;

uniform highp float[5] falloffPower;
uniform highp float[5] intensity;
uniform highp vec2[5] position;
// the `5`s in the array lengths correspond to the upper limit on the simultaneous distortion sources that can be present on screen at a time.
uniform highp float[32] falloffPower;
uniform highp float[32] intensity;
uniform highp vec2[32] position;
uniform bool[32] hasEventHorizon;
// The array lengths correspond to the upper limit on the simultaneous distortion sources that can be present on screen at a time.
// If you want to change this, make sure to change all of them here, in the for loop, and, in whatever overlay assigns the uniforms
// (apparently #define is an unknown preprocessor directive)

Expand All @@ -18,28 +20,36 @@ void fragment() {
highp vec2 delta;
highp float distance;
highp float deformation;

for (int i = 0; i < 5 && i < count; i++) {

lowp float brightnessFactor = 1.0;
lowp float cumulativeBrightnessFactor = 1.0;

for (int i = 0; i < 32 && i < count; i++) {

delta = FRAGCOORD.xy - position[i];
distance = length(delta / renderScale);
distance = length(delta / renderScale);

deformation = intensity[i] / pow(distance, falloffPower[i]);

// ensure deformation goes to zero at max distance
// avoids long-range single-pixel shifts that are noticeable when leaving PVS.

if (distance >= maxDistance) {
deformation = 0.0;
} else {
deformation *= (1.0 - pow(distance/maxDistance, 4.0));
deformation *= (1.0 - pow(distance / maxDistance, 4.0));

if (hasEventHorizon[i] && intensity[i] > 0.0) {
deformation += pow(deformation / maxDeformation, 32.0);

brightnessFactor = clamp(-0.75 * deformation + 10.0, 0.0, 1.0);
cumulativeBrightnessFactor *= brightnessFactor;
}
}

if(deformation > 0.8)
deformation = pow(deformation, 0.3);

finalCoords -= deformation * delta;
}

COLOR = zTextureSpec(SCREEN_TEXTURE, finalCoords*SCREEN_PIXEL_SIZE );

lowp vec4 color = zTextureSpec(SCREEN_TEXTURE, finalCoords * SCREEN_PIXEL_SIZE);
color.rgb *= cumulativeBrightnessFactor;

COLOR = color;
}
Loading