Wave shader WIP (#288)

* Created wave shader

* more to _CP14 folders

* add to tree

* fix

* pipiipi

* Provided shader uniform to component fields

* Fixed wave shader shit

* goodies

---------

Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: Ed <edwardxperia2000@gmail.com>
This commit is contained in:
Tornado Tech
2024-07-23 18:27:09 +10:00
committed by GitHub
parent 9f1b4fc51c
commit e806655c9d
7 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
namespace Content.Client._CP14.Wave;
[RegisterComponent]
[Access(typeof(CP14WaveShaderSystem))]
public sealed partial class CP14WaveShaderComponent : Component
{
[DataField]
public float Speed = 10f;
[DataField]
public float Dis = 10f;
[DataField]
public float Offset = 0f;
}

View File

@@ -0,0 +1,53 @@
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Client._CP14.Wave;
public sealed class CP14WaveShaderSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly IRobustRandom _random = default!;
private ShaderInstance _shader = default!;
public override void Initialize()
{
base.Initialize();
_shader = _protoMan.Index<ShaderPrototype>("Wave").InstanceUnique();
SubscribeLocalEvent<CP14WaveShaderComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<CP14WaveShaderComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<CP14WaveShaderComponent, BeforePostShaderRenderEvent>(OnBeforeShaderPost);
}
private void OnStartup(Entity<CP14WaveShaderComponent> entity, ref ComponentStartup args)
{
entity.Comp.Offset = _random.NextFloat(0, 1000);
SetShader(entity.Owner, _shader);
}
private void OnShutdown(Entity<CP14WaveShaderComponent> entity, ref ComponentShutdown args)
{
SetShader(entity.Owner, null);
}
private void SetShader(Entity<SpriteComponent?> entity, ShaderInstance? instance)
{
if (!Resolve(entity, ref entity.Comp, false))
return;
entity.Comp.PostShader = instance;
entity.Comp.GetScreenTexture = instance is not null;
entity.Comp.RaiseShaderEvent = instance is not null;
}
private void OnBeforeShaderPost(Entity<CP14WaveShaderComponent> entity, ref BeforePostShaderRenderEvent args)
{
_shader.SetParameter("Speed", entity.Comp.Speed);
_shader.SetParameter("Dis", entity.Comp.Dis);
_shader.SetParameter("Offset", entity.Comp.Offset);
}
}

View File

@@ -4,6 +4,7 @@ namespace Content.Server.Entry
public static class IgnoredComponents
{
public static string[] List => new[] {
"CP14WaveShader", //CP14 Wave shader
"ConstructionGhost",
"IconSmooth",
"InteractionOutline",

View File

@@ -3,6 +3,9 @@
name: high bush
description: Very tall and dense thickets. Perhaps someone is watching you from them
components:
- type: CP14WaveShader
speed: 2.5
dis: 4
- type: SpriteFade
- type: Clickable
- type: Sprite

View File

@@ -4,6 +4,9 @@
name: tree
description: Decades of life and growth, saturating the surrounding nature with fresh air.
components:
- type: CP14WaveShader
speed: 1
dis: 5
- type: SpriteFade
- type: Clickable
- type: Sprite

View File

@@ -0,0 +1,4 @@
- type: shader
id: Wave
kind: source
path: "/Textures/_CP14/Shaders/wave.swsl"

View File

@@ -0,0 +1,24 @@
preset raw;
varying highp vec2 Pos;
varying highp vec4 VtxModulate;
uniform highp float Speed = 3.0;
uniform highp float Dis = 10.0;
uniform highp float Offset = 0.0;
void fragment() {
// Re-create base mvp
COLOR = zTexture(UV) * VtxModulate;
}
void vertex() {
vec2 pos = aPos;
pos.x += sin(TIME * Speed + Offset) * Dis * (UV2.y - 1.0);
VERTEX = apply_mvp(pos);
Pos = (VERTEX + 1.0) / 2.0;
UV = mix(modifyUV.xy, modifyUV.zw, tCoord);
UV2 = tCoord2;
VtxModulate = zFromSrgb(modulate);
}