Add orbit camera and hovering stuff

This commit is contained in:
Sofia 2024-08-20 00:33:24 +03:00
parent f6b7f9e319
commit bf10022d3b
10 changed files with 163 additions and 46 deletions

View File

@ -126,6 +126,11 @@ interact={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":69,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null)
]
}
drag_orbit={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":4,"position":Vector2(272, 24),"global_position":Vector2(286, 94),"factor":1.0,"button_index":3,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
[internationalization]

View File

@ -1,20 +0,0 @@
[gd_scene load_steps=4 format=3 uid="uid://bmewhw3f0n2s8"]
[ext_resource type="Script" path="res://scripts/BuildingBlock.cs" id="1_htnym"]
[ext_resource type="Shader" path="res://textures/building_block.gdshader" id="2_dc3y0"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_q3u5a"]
render_priority = 0
shader = ExtResource("2_dc3y0")
shader_parameter/albedo = Color(0.417896, 0.417896, 0.417896, 1)
shader_parameter/roughness = 0.533
shader_parameter/specular = 0.45
shader_parameter/metallic = null
shader_parameter/uv1_scale = Vector3(1, 1, 1)
shader_parameter/uv1_offset = null
[node name="BuildingBlockScene" type="RigidBody3D"]
script = ExtResource("1_htnym")
[node name="MeshInstance" type="MeshInstance3D" parent="."]
material_override = SubResource("ShaderMaterial_q3u5a")

View File

@ -1,8 +1,10 @@
[gd_scene load_steps=8 format=3 uid="uid://d02cqylu3xwos"]
[gd_scene load_steps=10 format=3 uid="uid://d02cqylu3xwos"]
[ext_resource type="PackedScene" uid="uid://diwlyi146eroa" path="res://models/table.fbx" id="1_1soa3"]
[ext_resource type="Script" path="res://scripts/Table.cs" id="1_cb7s1"]
[ext_resource type="Material" uid="uid://bq5oqyuwekryv" path="res://textures/building_block.tres" id="2_1s35y"]
[ext_resource type="Material" uid="uid://pu02smf107o8" path="res://textures/building_block_hover.tres" id="3_m64id"]
[ext_resource type="Script" path="res://scripts/Orbit.cs" id="4_nyqjn"]
[sub_resource type="BoxShape3D" id="BoxShape3D_iiew7"]
size = Vector3(1.26587, 1.10037, 2.3989)
@ -16,13 +18,17 @@ size = Vector3(2.4, 5, 0.01)
[sub_resource type="BoxShape3D" id="BoxShape3D_3kmm7"]
size = Vector3(1.3, 0.2, 2.4)
[node name="table" type="StaticBody3D" node_paths=PackedStringArray("SpawnPoint")]
[node name="table" type="RigidBody3D" node_paths=PackedStringArray("SpawnPoint", "Orbit")]
collision_layer = 7
collision_mask = 7
lock_rotation = true
freeze = true
script = ExtResource("1_cb7s1")
BlockMaterial = ExtResource("2_1s35y")
BlockHoverMaterial = ExtResource("3_m64id")
SpawnPoint = NodePath("SpawnPoint")
BlockMask = 2
BlockMask = 10
Orbit = NodePath("Orbit")
InteractName = "build"
[node name="table" parent="." instance=ExtResource("1_1soa3")]
@ -57,3 +63,13 @@ shape = SubResource("BoxShape3D_5k7xn")
[node name="Roof" type="CollisionShape3D" parent="TableWalls"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 6, 0)
shape = SubResource("BoxShape3D_3kmm7")
[node name="Orbit" type="Node3D" parent="." node_paths=PackedStringArray("Camera")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.12311, 0)
script = ExtResource("4_nyqjn")
Camera = NodePath("OrbitCamera")
[node name="OrbitCamera" type="Camera3D" parent="Orbit"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1)
[connection signal="Interacted" from="." to="." method="Interact"]

View File

@ -1,13 +1,18 @@
using Godot;
using Godot.Collections;
using System;
namespace Gmtk24 {
public partial class BuildingBlock : RigidBody3D {
public partial class BuildingBlock : Interactible {
public BaseBlock Base;
public ShaderMaterial Material;
public ShaderMaterial HoverMaterial;
public float RelativeScale;
private MeshInstance3D MeshInstance;
private ShaderMaterial[] NormalMaterialArr;
private ShaderMaterial[] HoverMaterialArr;
public BuildingBlock() {
Name = "BuildingBlock";
}
@ -15,22 +20,37 @@ namespace Gmtk24 {
public override void _Ready() {
base._Ready();
var meshInstance = new MeshInstance3D() {
HoverMaterial ??= Material;
Hovered += OnHover;
MeshInstance = new MeshInstance3D() {
Mesh = Base.Mesh,
Scale = Vector3.One * RelativeScale,
};
AddChild(meshInstance);
AddChild(MeshInstance);
for (int i = 0; i < meshInstance.GetSurfaceOverrideMaterialCount(); i++) {
var replacedMaterial = meshInstance.Mesh.SurfaceGetMaterial(i);
int count = MeshInstance.GetSurfaceOverrideMaterialCount();
NormalMaterialArr = new ShaderMaterial[count];
HoverMaterialArr = new ShaderMaterial[count];
for (int i = 0; i < MeshInstance.GetSurfaceOverrideMaterialCount(); i++) {
var replacedMaterial = MeshInstance.Mesh.SurfaceGetMaterial(i);
var transparency = replacedMaterial.Get("transparency");
if (((int)transparency) != 0) {
ShaderMaterial newMat = (ShaderMaterial)Material.Duplicate();
newMat.SetShaderParameter("texture_albedo_for_alpha", replacedMaterial.Get("albedo_texture"));
meshInstance.SetSurfaceOverrideMaterial(i, newMat);
ShaderMaterial newHoverMat = (ShaderMaterial)HoverMaterial.Duplicate();
newHoverMat.SetShaderParameter("texture_albedo_for_alpha", replacedMaterial.Get("albedo_texture"));
NormalMaterialArr[i] = newMat;
HoverMaterialArr[i] = newHoverMat;
} else {
meshInstance.SetSurfaceOverrideMaterial(i, Material);
NormalMaterialArr[i] = Material;
}
MeshInstance.SetSurfaceOverrideMaterial(i, NormalMaterialArr[i]);
}
foreach (var shape in Base.Colliders) {
@ -47,6 +67,15 @@ namespace Gmtk24 {
}
}
public void OnHover(bool hovered) {
for (int i = 0; i < MeshInstance.GetSurfaceOverrideMaterialCount(); i++) {
if (hovered)
MeshInstance.SetSurfaceOverrideMaterial(i, HoverMaterialArr[i]);
else
MeshInstance.SetSurfaceOverrideMaterial(i, NormalMaterialArr[i]);
}
}
public struct BaseBlock {
public Mesh Mesh;
public Array<CollisionShape3D> Colliders;

View File

@ -14,19 +14,20 @@ namespace Gmtk24 {
if (Player == null)
return;
GodotObject collider;
GodotObject collider = null;
if (Input.MouseMode == Input.MouseModeEnum.Captured) {
collider = Player.LookingAt.GetCollider();
if (Player.Eye.Current)
collider = Player.LookingAt.GetCollider();
} else {
var viewport = GetViewport();
var camera = GetViewport().GetCamera3D();
var origin = camera.ProjectRayOrigin(viewport.GetMousePosition());
var normal = camera.ProjectRayNormal(viewport.GetMousePosition());
var normal = camera.ProjectRayNormal(viewport.GetMousePosition()) * 10000;
var ray = new PhysicsRayQueryParameters3D {
From = origin,
To = normal,
CollideWithBodies = true,
CollisionMask = 0b100,
CollisionMask = 0b1000,
HitBackFaces = false,
HitFromInside = false,
};
@ -34,13 +35,16 @@ namespace Gmtk24 {
if (results.ContainsKey("collider")) {
collider = (GodotObject)results["collider"];
} else {
collider = null;
}
}
if (collider is Interactible interactible) {
if (interactible != Hovered) {
Hovered?.SetHovered(false);
interactible.SetHovered(true);
}
Hovered = interactible;
StringBuilder Builder = new StringBuilder();
@ -50,6 +54,7 @@ namespace Gmtk24 {
HoverText.Text = Builder.ToString();
} else {
Hovered?.SetHovered(false);
Hovered = null;
HoverText.Text = "";
}

View File

@ -2,15 +2,21 @@
using Godot;
namespace Gmtk24 {
public partial class Interactible : StaticBody3D {
public partial class Interactible : RigidBody3D {
[Signal]
public delegate void OnInteractedEventHandler(InputEvent e);
public delegate void InteractedEventHandler(InputEvent e);
[Signal]
public delegate void HoveredEventHandler(bool hovered);
[Export]
public string InteractName;
public void HandleInput(InputEvent e) {
EmitSignal(SignalName.OnInteracted, e);
EmitSignal(SignalName.Interacted, e);
}
public void SetHovered(bool hovered) {
EmitSignal(SignalName.Hovered, hovered);
}
}
}

57
scripts/Orbit.cs Normal file
View File

@ -0,0 +1,57 @@
using Godot;
using System;
namespace Gmtk24 {
public partial class Orbit : Node3D {
[Export(PropertyHint.Range, "0,10,0.1")]
public float MaxCameraDistance = 1f;
[Export(PropertyHint.Range, "0,10,0.1")]
public float MinCameraDistance = 0.1f;
[Export]
public Camera3D Camera;
[Export]
public bool IsEnabled { private set; get; } = false;
private float CurrentYaw = 0;
private float CurrentPitch = 0;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) {
Quaternion = new Quaternion(Vector3.Up, CurrentYaw) * new Quaternion(Vector3.Right, CurrentPitch);
}
public void SetEnabled(bool enabled) {
IsEnabled = enabled;
Camera.Current = enabled;
if (IsEnabled)
Input.MouseMode = Input.MouseModeEnum.Visible;
else
Input.MouseMode = Input.MouseModeEnum.Captured;
}
public override void _UnhandledInput(InputEvent @event) {
if (!IsEnabled)
return;
if (@event.IsActionPressed("drag_orbit"))
Input.MouseMode = Input.MouseModeEnum.Captured;
if (@event.IsActionReleased("drag_orbit"))
Input.MouseMode = Input.MouseModeEnum.Visible;
if (Input.IsActionPressed("drag_orbit") && @event is InputEventMouseMotion mouseMotion) {
var cameraSensitivity = UserSettings.Singleton.GetCameraSpeedMultipliers();
var mouseMultiplier = 0.0003f;
CurrentYaw -= mouseMotion.ScreenRelative.X * mouseMultiplier * cameraSensitivity.X;
CurrentPitch -= mouseMotion.ScreenRelative.Y * mouseMultiplier * cameraSensitivity.Y;
CurrentPitch = Mathf.Clamp(CurrentPitch, -Mathf.Pi * 0.49f, 0);
}
}
}
}

View File

@ -15,7 +15,7 @@ namespace Gmtk24 {
public float Gravity = 20;
[ExportCategory("Vision")]
[Export]
public Node3D Eye;
public Camera3D Eye;
[Export]
public RayCast3D LookingAt;
[ExportCategory("Noises")]

View File

@ -10,10 +10,15 @@ namespace Gmtk24 {
[Export]
public ShaderMaterial BlockMaterial;
[Export]
public ShaderMaterial BlockHoverMaterial;
[Export]
public Node3D SpawnPoint;
[Export(PropertyHint.LayersAvoidance)]
public uint BlockMask;
[Export]
public Orbit Orbit;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
Vector3 SpawnPos = Vector3.Zero;
@ -22,10 +27,6 @@ namespace Gmtk24 {
SpawnPos = SpawnPoint.GlobalPosition - GlobalPosition;
SpawnBlocks(SpawnPos);
OnInteracted += (e) => {
GD.Print("hello!");
};
}
public void SpawnBlocks(Vector3 position) {
@ -39,6 +40,7 @@ namespace Gmtk24 {
var block = new BuildingBlock() {
Base = baseBlock,
Material = BlockMaterial,
HoverMaterial = BlockHoverMaterial,
Position = position + Vector3.One * (rng.Randf() * 0.5f - 0.25f) + Vector3.Up * rng.Randf(),
CollisionMask = BlockMask,
CollisionLayer = BlockMask,
@ -49,5 +51,9 @@ namespace Gmtk24 {
}
}
public void Interact(InputEvent _) {
Orbit.SetEnabled(true);
}
}
}
}

View File

@ -0,0 +1,13 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://pu02smf107o8"]
[ext_resource type="Shader" path="res://textures/building_block.gdshader" id="1_fkxdu"]
[resource]
render_priority = 0
shader = ExtResource("1_fkxdu")
shader_parameter/albedo = Color(0.356373, 0.507339, 1, 1)
shader_parameter/roughness = 0.533
shader_parameter/specular = 0.45
shader_parameter/metallic = null
shader_parameter/uv1_scale = Vector3(1, 1, 1)
shader_parameter/uv1_offset = null