diff --git a/DungeonShooting_Godot/project.godot b/DungeonShooting_Godot/project.godot index 59850ad..2cd411b 100644 --- a/DungeonShooting_Godot/project.godot +++ b/DungeonShooting_Godot/project.godot @@ -11,7 +11,7 @@ [application] config/name="DungeonShooting" -run/main_scene="res://scene/test/TestPerfectPixel.tscn" +run/main_scene="res://scene/Main.tscn" config/features=PackedStringArray("4.2", "C#") config/icon="res://icon.png" diff --git a/DungeonShooting_Godot/scene/Main.tscn b/DungeonShooting_Godot/scene/Main.tscn index 16b7e48..346e221 100644 --- a/DungeonShooting_Godot/scene/Main.tscn +++ b/DungeonShooting_Godot/scene/Main.tscn @@ -2,22 +2,10 @@ [ext_resource type="Script" path="res://src/game/GameApplication.cs" id="1_mh1cq"] [ext_resource type="Script" path="res://src/game/camera/GameCamera.cs" id="2_2j367"] +[ext_resource type="Shader" path="res://resource/material/OffsetVertex.gdshader" id="2_fxoum"] -[sub_resource type="Shader" id="1"] -code = "shader_type canvas_item; - -uniform vec2 offset = vec2(0.0 , 0.0); - -void vertex() { - - VERTEX += offset; - -} - -" - -[sub_resource type="ShaderMaterial" id="2"] -shader = SubResource("1") +[sub_resource type="ShaderMaterial" id="ShaderMaterial_pjtkw"] +shader = ExtResource("2_fxoum") shader_parameter/offset = Vector2(0, 0) [node name="Main" type="Node2D" node_paths=PackedStringArray("SubViewport", "SubViewportContainer", "SceneRoot", "GlobalNodeRoot")] @@ -32,7 +20,7 @@ layer = -1 [node name="SubViewportContainer" type="SubViewportContainer" parent="ViewCanvas"] -material = SubResource("2") +material = SubResource("ShaderMaterial_pjtkw") offset_right = 480.0 offset_bottom = 270.0 scale = Vector2(4, 4) @@ -40,8 +28,6 @@ [node name="SubViewport" type="SubViewport" parent="ViewCanvas/SubViewportContainer"] disable_3d = true handle_input_locally = false -snap_2d_transforms_to_pixel = true -snap_2d_vertices_to_pixel = true use_hdr_2d = true canvas_item_default_texture_filter = 0 size = Vector2i(320, 180) diff --git a/DungeonShooting_Godot/src/game/GameApplication.cs b/DungeonShooting_Godot/src/game/GameApplication.cs index 86b0d3d..35272b1 100644 --- a/DungeonShooting_Godot/src/game/GameApplication.cs +++ b/DungeonShooting_Godot/src/game/GameApplication.cs @@ -31,7 +31,7 @@ /// 全局根节点 /// [Export] public Node2D GlobalNodeRoot; - + /// /// 游戏目标帧率 /// @@ -111,7 +111,6 @@ //Engine.TimeScale = 0.2f; //调整窗口分辨率 OnWindowSizeChanged(); - RefreshSubViewportSize(); //窗体大小改变 GetWindow().SizeChanged += OnWindowSizeChanged; @@ -274,11 +273,12 @@ private void RefreshSubViewportSize() { var s = new Vector2I((int)ViewportSize.X, (int)ViewportSize.Y); - s.X = s.X / 2 * 2; - s.Y = s.Y / 2 * 2; + s.X = s.X / 2 * 2 + 2; + s.Y = s.Y / 2 * 2 + 2; SubViewport.Size = s; SubViewportContainer.Scale = new Vector2(PixelScale, PixelScale); SubViewportContainer.Size = s; + SubViewportContainer.Position = new Vector2(-PixelScale, -PixelScale); } //初始化鼠标 diff --git a/DungeonShooting_Godot/src/game/camera/GameCamera.cs b/DungeonShooting_Godot/src/game/camera/GameCamera.cs index 74a355b..a4a851d 100644 --- a/DungeonShooting_Godot/src/game/camera/GameCamera.cs +++ b/DungeonShooting_Godot/src/game/camera/GameCamera.cs @@ -64,6 +64,8 @@ private Vector2 _camPos; private Vector2 _shakeOffset = Vector2.Zero; + + public ShaderMaterial _offsetShader; public GameCamera() { @@ -72,6 +74,7 @@ public override void _Ready() { + _offsetShader = (ShaderMaterial)GameApplication.Instance.SubViewportContainer.Material; _camPos = GlobalPosition; } @@ -81,36 +84,29 @@ var newDelta = (float)delta; _Shake(newDelta); - // 3.5 写法 - // var player = GameApplication.Instance.RoomManager.Player; - // var viewportContainer = GameApplication.Instance.SubViewportContainer; - // var camPos = player.GlobalPosition; - // _camPos = _camPos.Lerp(camPos, Mathf.Min(6 * newDelta, 1)) + _shakeOffset; - // SubPixelPosition = _camPos.Round() - _camPos; - // (viewportContainer.Material as ShaderMaterial)?.SetShaderParameter("offset", SubPixelPosition); - // GlobalPosition = _camPos.Round(); - - var world = GameApplication.Instance.World; if (world != null && !world.Pause && _followTarget != null) { var mousePosition = InputManager.CursorPosition; var targetPosition = _followTarget.GlobalPosition; - //if (targetPosition.DistanceSquaredTo(mousePosition) >= 39999.992F) // >= (60 / 0.3f) * (60 / 0.3f) if (targetPosition.DistanceSquaredTo(mousePosition) >= (60 / FollowsMouseAmount) * (60 / FollowsMouseAmount)) { _camPos = targetPosition.MoveToward(mousePosition, 60); } else { - //targetPos = targetPosition.Lerp(mousePosition, 0.3f); //这里的0.3就是上面的 (60 / 0.3f) * (60 / 0.3f) 中的 0.3 _camPos = targetPosition.Lerp(mousePosition, FollowsMouseAmount); } - //_camPos = _camPos.Lerp(targetPos, 20 * newDelta); - GlobalPosition = _camPos.Round(); + var cameraPosition = _camPos; + //var cameraPosition = _camPos + _shakeOffset; + var roundPos = cameraPosition.Round(); + var offset = roundPos - cameraPosition; + _offsetShader.SetShaderParameter("offset", offset); + GlobalPosition = roundPos; + Offset = _shakeOffset.Round(); - + //调用相机更新事件 if (OnPositionUpdateEvent != null) { @@ -200,9 +196,10 @@ { var distance = _CalculateDistanceSquared(delta); distance = new Vector2(Mathf.Sqrt(distance.X), Mathf.Sqrt(distance.Y)); + var offset = Offset; _shakeOffset += _processDirection + new Vector2( - (float)GD.RandRange(-distance.X, distance.X) - Offset.X, - (float)GD.RandRange(-distance.Y, distance.Y) - Offset.Y + (float)GD.RandRange(-distance.X, distance.X) - offset.X, + (float)GD.RandRange(-distance.Y, distance.Y) - offset.Y ); _processDistanceSquared = Vector2.Zero; _processDirection = _processDirection.Lerp(Vector2.Zero, RecoveryCoefficient * delta);