mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:35:34 +00:00
Player shader
This commit is contained in:
parent
357ced3e94
commit
6162d11165
19 changed files with 480 additions and 24 deletions
84
Scripts/Components/Actors/3D/AnimatedShaderSprite3D.cs
Normal file
84
Scripts/Components/Actors/3D/AnimatedShaderSprite3D.cs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.Actors._3D;
|
||||
|
||||
[Tool]
|
||||
public partial class AnimatedShaderSprite3D : AnimatedSprite3D
|
||||
{
|
||||
private bool _isConnected = false;
|
||||
private ShaderMaterial _shaderMaterial;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
ConnectSignals();
|
||||
_shaderMaterial = (ShaderMaterial)MaterialOverride;
|
||||
|
||||
}
|
||||
|
||||
private void ConnectSignals()
|
||||
{
|
||||
if (_isConnected)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isConnected = true;
|
||||
FrameChanged += HandleFrameChanged;
|
||||
}
|
||||
|
||||
private void HandleFrameChanged()
|
||||
{
|
||||
_shaderMaterial.SetShaderParameter("tex", SpriteFrames.GetFrameTexture(Animation, Frame));
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
DisconnectSignals();
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void DisconnectSignals()
|
||||
{
|
||||
if (!_isConnected)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FrameChanged -= HandleFrameChanged;
|
||||
}
|
||||
|
||||
// public override void _Process(double delta)
|
||||
// {
|
||||
// ((ShaderMaterial)MaterialOverride).SetShaderParameter("albedo_texture", SpriteFrames.GetFrameTexture(Animation, Frame));
|
||||
//
|
||||
// ((ShaderMaterial)MaterialOverride).SetShaderParameter("billboard_mode", (int)this.GetBillboardMode());
|
||||
// }
|
||||
|
||||
// [Export(PropertyHint.File)] public string ShaderPath { get; private set; }
|
||||
//
|
||||
// public override void _Ready()
|
||||
// {
|
||||
// if (MaterialOverride is null)
|
||||
// {
|
||||
// ApplyMaterialOverride();
|
||||
// }
|
||||
//
|
||||
// ApplyTexture();
|
||||
// //texture_changed.connect(_apply_texture)
|
||||
// }
|
||||
//
|
||||
// private void ApplyMaterialOverride()
|
||||
// {
|
||||
// var shaderMaterial = new ShaderMaterial();
|
||||
// shaderMaterial.Shader = GD.Load<Shader>(ShaderPath);
|
||||
// MaterialOverride = shaderMaterial;
|
||||
// }
|
||||
//
|
||||
// private void ApplyTexture()
|
||||
// {
|
||||
// var shaderMaterial = MaterialOverride as ShaderMaterial;
|
||||
//
|
||||
// shaderMaterial.SetShaderParameter("sprite_texture", this.SpriteFrames.GetFrameTexture(Animation, Frame));
|
||||
// }
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://ceiworv2pgl1r
|
||||
44
Scripts/Components/Actors/3D/OutlineEffect.cs
Normal file
44
Scripts/Components/Actors/3D/OutlineEffect.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.Actors._3D;
|
||||
|
||||
[Tool]
|
||||
public partial class OutlineEffect : CompositorEffect
|
||||
{
|
||||
private RenderingDevice RenderingDevice => RenderingServer.GetRenderingDevice();
|
||||
|
||||
private Rid _shader;
|
||||
private Rid _pipeline;
|
||||
|
||||
private Rid _parameterStorageBuffer;
|
||||
|
||||
// tutorial: https://www.youtube.com/watch?v=PQHExF-sbB4
|
||||
|
||||
public OutlineEffect()
|
||||
{
|
||||
var shaderFile = GD.Load<RDShaderFile>("res://Shaders/outline3D.glsl");
|
||||
|
||||
var shaderSpirv = shaderFile.GetSpirV();
|
||||
|
||||
_shader = RenderingDevice.ShaderCreateFromSpirV(shaderSpirv);
|
||||
|
||||
_pipeline = RenderingDevice.ComputePipelineCreate(_shader);
|
||||
|
||||
byte[] parameterData = [0, 0, 0, 0];
|
||||
|
||||
_parameterStorageBuffer = RenderingDevice.StorageBufferCreate((uint)parameterData.Length, parameterData);
|
||||
}
|
||||
|
||||
public override void _RenderCallback(int effectCallbackType, RenderData renderData)
|
||||
{
|
||||
//base._RenderCallback(effectCallbackType, renderData);
|
||||
var renderSceneBuffers = renderData.GetRenderSceneBuffers();
|
||||
|
||||
if (renderSceneBuffers is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//var size = renderSceneBuffers.get
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/Actors/3D/OutlineEffect.cs.uid
Normal file
1
Scripts/Components/Actors/3D/OutlineEffect.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bl63xfdr1e0fo
|
||||
39
Scripts/Components/Actors/3D/PlayerAnimationModule3D.cs
Normal file
39
Scripts/Components/Actors/3D/PlayerAnimationModule3D.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using Cirno.Scripts.Components.FSM;
|
||||
using Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.Actors._3D;
|
||||
|
||||
public partial class PlayerAnimationModule3D : ModuleBase<PlayerState, CharacterBody3D>
|
||||
{
|
||||
|
||||
[Export] public PlayerAnimationProvider3D AnimationProvider { get; private set; }
|
||||
|
||||
[Export] public IsoPlayerStorageModule Storage { get; private set; }
|
||||
|
||||
public override void EnterState(PlayerState state)
|
||||
{
|
||||
AnimationProvider.ShowSprite();
|
||||
}
|
||||
|
||||
public override void ExitState(PlayerState state)
|
||||
{
|
||||
|
||||
AnimationProvider.SetAnimationSpeed(Vector2.Zero);
|
||||
}
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void Process(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcess(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://cu4gsbprip0r
|
||||
180
Scripts/Components/Actors/3D/PlayerAnimationProvider3D.cs
Normal file
180
Scripts/Components/Actors/3D/PlayerAnimationProvider3D.cs
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
using Godot;
|
||||
using GTweens.Builders;
|
||||
using GTweens.Tweens;
|
||||
using GTweensGodot.Extensions;
|
||||
|
||||
namespace Cirno.Scripts.Components.Actors._3D;
|
||||
|
||||
public partial class PlayerAnimationProvider3D : Node3D
|
||||
{
|
||||
[Export] public AnimatedSprite3D AnimatedSprite {get; private set;}
|
||||
|
||||
[ExportCategory("Animation Names")]
|
||||
[Export]
|
||||
public StringName WalkRightAnimationName {get; private set;} = "walk_right";
|
||||
|
||||
[Export]
|
||||
public StringName WalkLeftAnimationName {get; private set;} = "walk_left";
|
||||
[Export]
|
||||
public StringName WalkDownAnimationName {get; private set;} = "walk_down";
|
||||
[Export]
|
||||
public StringName WalkUpAnimationName {get; private set;} = "walk_up";
|
||||
|
||||
[Export]
|
||||
public StringName DrowningAnimationName {get; private set;} = "Drowning";
|
||||
|
||||
[ExportCategory("Shaders")]
|
||||
[Export] public ShaderMaterial BlinkMaterial {get; private set;}
|
||||
|
||||
[Export] public StringName BlinkShaderPropertyName { get; private set; } = new("blink_intensity");
|
||||
[Export] public StringName TeleportProgressPropertyName { get; private set; } = new("teleport_progress");
|
||||
[Export] public StringName ScanlineDensityPropertyName { get; private set; } = new("scanline_density");
|
||||
|
||||
private GTween _blinkTween;
|
||||
|
||||
[Signal] public delegate void OnAnimationEndedEventHandler(StringName animationName);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
AnimatedSprite.AnimationFinished += () => EmitSignal(SignalName.OnAnimationEnded, AnimatedSprite.Animation);
|
||||
}
|
||||
|
||||
public void ShowSprite()
|
||||
{
|
||||
this.Show();
|
||||
}
|
||||
|
||||
public void HideSprite()
|
||||
{
|
||||
this.Hide();
|
||||
}
|
||||
|
||||
public void SetAnimationSpeed(Vector2 velocity)
|
||||
{
|
||||
if (velocity.Length() == 0)
|
||||
{
|
||||
AnimatedSprite.SpeedScale = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (velocity.Length() > 40)
|
||||
{
|
||||
AnimatedSprite.SpeedScale = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
AnimatedSprite.SpeedScale = 0.8f;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void SetAnimation(Vector2 direction)
|
||||
{
|
||||
if (direction == Vector2.Zero)
|
||||
return;
|
||||
|
||||
float angle = Mathf.RadToDeg(direction.Angle());
|
||||
|
||||
// Normalize to 0-360
|
||||
if (angle < 0)
|
||||
angle += 360;
|
||||
|
||||
string animToPlay;
|
||||
|
||||
switch (angle)
|
||||
{
|
||||
case >= 45 and < 135:
|
||||
AnimatedSprite.Play(WalkDownAnimationName);
|
||||
break;
|
||||
case >= 135 and < 225:
|
||||
AnimatedSprite.Play(WalkLeftAnimationName);
|
||||
break;
|
||||
case >= 225 and < 315:
|
||||
AnimatedSprite.Play(WalkUpAnimationName);
|
||||
break;
|
||||
default:
|
||||
AnimatedSprite.Play(WalkRightAnimationName);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void PlayDeathAnimation()
|
||||
{
|
||||
// if (_deathParticles is null) return;
|
||||
// this.CreateSibling<AutodeleteParticle>(_deathParticles, this.GlobalPosition);
|
||||
// _animatedSprite.Visible = false;
|
||||
}
|
||||
|
||||
public void PlayDrowningAnimation()
|
||||
{
|
||||
this.Visible = true;
|
||||
AnimatedSprite.Play(DrowningAnimationName);
|
||||
AnimatedSprite.SpeedScale = 1;
|
||||
}
|
||||
|
||||
public void Blink()
|
||||
{
|
||||
if (BlinkMaterial == null) return;
|
||||
AnimatedSprite.MaterialOverride = BlinkMaterial;
|
||||
|
||||
var material = ((ShaderMaterial)AnimatedSprite.MaterialOverride);
|
||||
|
||||
_blinkTween?.Kill();
|
||||
|
||||
_blinkTween = GTweenSequenceBuilder.New()
|
||||
.Append(material.TweenPropertyFloat(BlinkShaderPropertyName, 1f, 0f))
|
||||
.Append(material.TweenPropertyFloat(BlinkShaderPropertyName, 0f, 0.5f))
|
||||
.Build();
|
||||
|
||||
_blinkTween.Play();
|
||||
}
|
||||
|
||||
public void PlayTeleportAnimation()
|
||||
{
|
||||
if (BlinkMaterial == null) return;
|
||||
AnimatedSprite.MaterialOverride = BlinkMaterial;
|
||||
var material = ((ShaderMaterial)AnimatedSprite.MaterialOverride);
|
||||
_blinkTween?.Kill();
|
||||
_blinkTween = GTweenSequenceBuilder.New()
|
||||
.Append(material.TweenPropertyFloat(TeleportProgressPropertyName, 0f, 0f))
|
||||
.Append(material.TweenPropertyFloat(ScanlineDensityPropertyName, 0f, 0f))
|
||||
.Append(material.TweenPropertyFloat(ScanlineDensityPropertyName,50f,0.5f))
|
||||
.Join(material.TweenPropertyFloat(TeleportProgressPropertyName, 1f,0.5f))
|
||||
.Build();
|
||||
|
||||
_blinkTween.Play();
|
||||
}
|
||||
|
||||
public void PlayUnteleportAnimation()
|
||||
{
|
||||
if (BlinkMaterial == null) return;
|
||||
AnimatedSprite.MaterialOverride = BlinkMaterial;
|
||||
var material = ((ShaderMaterial)AnimatedSprite.MaterialOverride);
|
||||
_blinkTween?.Kill();
|
||||
_blinkTween = GTweenSequenceBuilder.New()
|
||||
.Append(material.TweenPropertyFloat(TeleportProgressPropertyName, 1f, 0f))
|
||||
.Append(material.TweenPropertyFloat(ScanlineDensityPropertyName, 50f, 0f))
|
||||
.Append(material.TweenPropertyFloat(ScanlineDensityPropertyName,0f,0.5f))
|
||||
.Join(material.TweenPropertyFloat(TeleportProgressPropertyName, 0f,0.5f))
|
||||
.Build();
|
||||
|
||||
_blinkTween.Play();
|
||||
}
|
||||
|
||||
private void SetShaderTeleportProgress(float value)
|
||||
{
|
||||
((ShaderMaterial)AnimatedSprite.MaterialOverride).SetShaderParameter("teleport_progress", value);
|
||||
}
|
||||
|
||||
private void SetShaderScanlineDensity(float value)
|
||||
{
|
||||
((ShaderMaterial)AnimatedSprite.MaterialOverride).SetShaderParameter("scanline_density", value);
|
||||
}
|
||||
|
||||
private void SetShaderBlinkIntensity(float newValue)
|
||||
{
|
||||
((ShaderMaterial)AnimatedSprite.MaterialOverride).SetShaderParameter("blink_intensity", newValue);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://cq1joxgoj3jdp
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Components.Actors._3D;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
|
|
@ -10,6 +11,9 @@ public partial class Active : BaseState<PlayerState, CharacterBody3D>
|
|||
|
||||
private CharacterBody3D _player;
|
||||
[Export] private InputProvider _inputProvider;
|
||||
[Export] public PlayerAnimationProvider3D AnimationProvider { get; private set; }
|
||||
|
||||
[Export] public IsoPlayerStorageModule Storage { get; private set; }
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
|
|
@ -49,7 +53,7 @@ public partial class Active : BaseState<PlayerState, CharacterBody3D>
|
|||
// enable sprite
|
||||
// enable crosshair
|
||||
//_crosshairProvider.Show();
|
||||
//_animationProvider.ShowSprite();
|
||||
AnimationProvider.ShowSprite();
|
||||
//_damageReceiver.Enabled = true;
|
||||
//_activationProvider.Enabled = true;
|
||||
//_interactionController.Enabled = true;
|
||||
|
|
@ -60,7 +64,7 @@ public partial class Active : BaseState<PlayerState, CharacterBody3D>
|
|||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
// _animationProvider.SetAnimationSpeed(Vector2.Zero);
|
||||
AnimationProvider.SetAnimationSpeed(Vector2.Zero);
|
||||
// //_animationProvider.SetAnimation(Vector2.Zero);
|
||||
// _crosshairProvider.Hide();
|
||||
// _hitboxSpriteProvider.Hide();
|
||||
|
|
@ -88,6 +92,9 @@ public partial class Active : BaseState<PlayerState, CharacterBody3D>
|
|||
base.ProcessState(delta);
|
||||
|
||||
HandleInputHotkeys();
|
||||
|
||||
AnimationProvider.SetAnimationSpeed(new Vector2(MainObject.Velocity.X, MainObject.Velocity.Z));
|
||||
AnimationProvider.SetAnimation(Storage.FacingDirection);
|
||||
}
|
||||
|
||||
private void HandleInputHotkeys()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue