add grid dot system to raycast

This commit is contained in:
Nitwel 2024-05-23 00:36:49 +02:00
parent 37bb550544
commit 1a2ec79de3
10 changed files with 149 additions and 11 deletions

View File

@ -0,0 +1,46 @@
shader_type spatial;
render_mode unshaded, cull_back;
uniform float dot_size: hint_range(0.0, 1.0, 0.01) = 0.5;
uniform float dot_falloff: hint_range(0.0, 0.4, 0.001) = 0.1;
uniform float size: hint_range(1.0, 32.0, 0.1) = 16.0;
uniform vec2 dot_offset = vec2(0.0);
uniform int supersampling: hint_range(0, 10, 1) = 1;
vec2 random(vec2 st){
return vec2(fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123), fract(sin(dot(st.xy, vec2(-40.644))) * 87502.29935));
}
float sdf_sphere(vec2 position, float center_dist) {
return length(position) - max(0.0, dot_size / size - center_dist * dot_falloff);
}
vec2 sdf_repeat(vec2 position) {
float inv_size = 1.0 / (size * 2.0);
return fract(position * size) / size - inv_size;
}
void fragment() {
float total_dist = 0.0;
for(int i = 0; i < supersampling; i++) {
float center_dist = distance(UV, vec2(0.5, 0.5));
vec2 rand = random(vec2(float(i))) * 0.004 * (0.5 - center_dist);
float dist = sdf_sphere(sdf_repeat(UV - dot_offset - rand), center_dist);
if (dist <= 0.0) {
total_dist += 1.0 / float(supersampling);
}
}
ALBEDO = vec3(1.0);
ALPHA = total_dist;
//if(dist <= 0.0) {
//ALBEDO = vec3(1.0);
//} else {
//ALBEDO = vec3(1.0);
//ALPHA = dist;
//}
}

View File

@ -0,0 +1,13 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://bb4kgiocswfv7"]
[ext_resource type="Shader" path="res://assets/materials/grid.gdshader" id="1_oebjc"]
[resource]
resource_local_to_scene = true
render_priority = 8
shader = ExtResource("1_oebjc")
shader_parameter/dot_size = 0.12
shader_parameter/dot_falloff = 0.024
shader_parameter/size = 12.0
shader_parameter/dot_offset = Vector2(0, 0)
shader_parameter/supersampling = 8

View File

@ -45,6 +45,9 @@ transform = Transform3D(0.967043, 0.24582, -0.0663439, -0.0663439, 0.494837, 0.8
[node name="MiddleTip" parent="XROrigin3D/XRControllerLeft" index="6"] [node name="MiddleTip" parent="XROrigin3D/XRControllerLeft" index="6"]
transform = Transform3D(0.98042, 0.196912, 0.00149799, 0.001498, -0.015065, 0.999885, 0.196912, -0.980305, -0.0150651, -0.00327212, -0.00771427, -0.176318) transform = Transform3D(0.98042, 0.196912, 0.00149799, 0.001498, -0.015065, 0.999885, 0.196912, -0.980305, -0.0150651, -0.00327212, -0.00771427, -0.176318)
[node name="Palm" parent="XROrigin3D/XRControllerLeft" index="7"]
transform = Transform3D(1, 3.12364e-06, -3.13861e-06, -3.12371e-06, 1, -1.97886e-05, 3.13854e-06, 1.97889e-05, 1, 0.0307807, -0.0419721, -0.0399505)
[node name="XRControllerRight" parent="XROrigin3D" instance=ExtResource("7_0b3tc")] [node name="XRControllerRight" parent="XROrigin3D" instance=ExtResource("7_0b3tc")]
unique_name_in_owner = true unique_name_in_owner = true
transform = Transform3D(0.999999, -1.39635e-11, 0, 1.31553e-10, 1, 0, 0, 0, 1, 0.336726, 0.575093, -0.437942) transform = Transform3D(0.999999, -1.39635e-11, 0, 1.31553e-10, 1, 0, 0, 0, 1, 0.336726, 0.575093, -0.437942)

View File

@ -23,7 +23,13 @@ const Miniature = preload ("res://content/system/miniature/miniature.gd")
@onready var ray: RayCast3D = $Raycast @onready var ray: RayCast3D = $Raycast
@onready var quick_actions = $Palm/QuickActions @onready var quick_actions = $Palm/QuickActions
var hand_active = false var hand_active = false:
set(value):
hand_active = value
if pointer != null:
pointer.set_physics_process(value)
var initiator: Initiator = Initiator.new() var initiator: Initiator = Initiator.new()
var collide: Collide var collide: Collide
var pointer: Pointer var pointer: Pointer

View File

@ -16,7 +16,12 @@ const Miniature = preload ("res://content/system/miniature/miniature.gd")
@onready var thumb_tip = $ThumbTip @onready var thumb_tip = $ThumbTip
@onready var middle_tip = $MiddleTip @onready var middle_tip = $MiddleTip
var hand_active = false var hand_active = false:
set(value):
hand_active = value
if pointer != null:
pointer.set_physics_process(value)
var initiator: Initiator = Initiator.new() var initiator: Initiator = Initiator.new()
var collide: Collide var collide: Collide
var pointer: Pointer var pointer: Pointer

View File

@ -7,9 +7,12 @@ const Initiator = preload ("res://lib/utils/pointer/initiator.gd")
@onready var default_cursor: Sprite3D = $Cursor/DefaultCursor @onready var default_cursor: Sprite3D = $Cursor/DefaultCursor
@onready var retro_cursor: Sprite3D = $Cursor/RetroCursor @onready var retro_cursor: Sprite3D = $Cursor/RetroCursor
@onready var decal: Decal = $Decal @onready var decal: Decal = $Decal
@onready var grid: MeshInstance3D = $Grid
@onready var wall_collider: RayCast3D = $WallCollider
@export var is_right: bool = true @export var is_right: bool = true
@export var with_decal: bool = false @export var with_decal: bool = false
@export var with_grid: bool = false
@onready var pointer: Pointer @onready var pointer: Pointer
@onready var visual_ray: MeshInstance3D = $Ray @onready var visual_ray: MeshInstance3D = $Ray
@ -38,17 +41,34 @@ func _ready():
) )
R.effect(func(_arg): R.effect(func(_arg):
match Store.settings.state.cursor_style: var style=Store.settings.state.cursor_style
1:
default_cursor.visible=false default_cursor.visible=style == 0
retro_cursor.visible=true retro_cursor.visible=style == 1
0, _:
default_cursor.visible=true
retro_cursor.visible=false
) )
func _physics_process(_delta): func _physics_process(_delta):
_handle_cursor() _handle_cursor()
_handle_grid()
func _process(delta):
if grid.visible:
grid.material_override.set_shader_parameter("dot_offset", TransformTools.plane_2d_coords(grid.global_transform) / grid.mesh.size.x)
func _handle_grid():
var collider = wall_collider.get_collider()
if collider == null:
grid.visible = false
grid.visible = with_grid
grid.global_position = wall_collider.get_collision_point() + wall_collider.get_collision_normal() * 0.01
if abs(wall_collider.get_collision_normal().dot(Vector3.UP)) > 0.9:
grid.global_transform.basis = Basis.looking_at(wall_collider.get_collision_normal(), Vector3.FORWARD, true)
else:
grid.global_transform.basis = Basis.looking_at(wall_collider.get_collision_normal(), Vector3.UP, true)
func _handle_cursor(): func _handle_cursor():
var collider = get_collider() var collider = get_collider()
@ -69,6 +89,7 @@ func _handle_cursor():
cursor.visible = true cursor.visible = true
decal.visible = false decal.visible = false
cursor.global_transform.origin = get_collision_point() + get_collision_normal() * 0.001 # offset to avoid z-fighting cursor.global_transform.origin = get_collision_point() + get_collision_normal() * 0.001 # offset to avoid z-fighting
if abs(get_collision_normal().dot(Vector3.UP)) > 0.9: if abs(get_collision_normal().dot(Vector3.UP)) > 0.9:

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=3 uid="uid://dscp8x0ari57n"] [gd_scene load_steps=10 format=3 uid="uid://dscp8x0ari57n"]
[ext_resource type="Texture2D" uid="uid://bl33klueufwja" path="res://assets/cursors/pointer.png" id="1_2f2iv"] [ext_resource type="Texture2D" uid="uid://bl33klueufwja" path="res://assets/cursors/pointer.png" id="1_2f2iv"]
[ext_resource type="Script" path="res://content/system/raycast/raycast.gd" id="1_gp8nv"] [ext_resource type="Script" path="res://content/system/raycast/raycast.gd" id="1_gp8nv"]
@ -6,6 +6,7 @@
[ext_resource type="Shader" path="res://content/system/raycast/raycast.gdshader" id="3_lpfe7"] [ext_resource type="Shader" path="res://content/system/raycast/raycast.gdshader" id="3_lpfe7"]
[ext_resource type="Texture2D" uid="uid://cy5dna78q6tn6" path="res://assets/models/ray/ray.png" id="4_fe8o8"] [ext_resource type="Texture2D" uid="uid://cy5dna78q6tn6" path="res://assets/models/ray/ray.png" id="4_fe8o8"]
[ext_resource type="ArrayMesh" uid="uid://d3kriab522o2w" path="res://assets/models/ray/ray.obj" id="5_3o4gy"] [ext_resource type="ArrayMesh" uid="uid://d3kriab522o2w" path="res://assets/models/ray/ray.obj" id="5_3o4gy"]
[ext_resource type="Material" uid="uid://bb4kgiocswfv7" path="res://assets/materials/grid.tres" id="7_tnbkv"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_ov0gt"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_ov0gt"]
render_priority = 0 render_priority = 0
@ -13,6 +14,9 @@ shader = ExtResource("3_lpfe7")
shader_parameter/albedo = Color(1, 1, 1, 1) shader_parameter/albedo = Color(1, 1, 1, 1)
shader_parameter/texture_albedo = ExtResource("4_fe8o8") shader_parameter/texture_albedo = ExtResource("4_fe8o8")
[sub_resource type="PlaneMesh" id="PlaneMesh_cq16e"]
orientation = 2
[node name="Raycast" type="RayCast3D"] [node name="Raycast" type="RayCast3D"]
transform = Transform3D(0.999999, -1.39624e-11, 0, 9.48108e-12, 0.999999, 0, 0, 4.54747e-13, 0.999998, 0, 0, 0) transform = Transform3D(0.999999, -1.39624e-11, 0, 9.48108e-12, 0.999999, 0, 0, 4.54747e-13, 0.999998, 0, 0, 0)
target_position = Vector3(0, 0, -5) target_position = Vector3(0, 0, -5)
@ -49,3 +53,11 @@ texture = ExtResource("3_f5ibs")
transform = Transform3D(1, -1.39624e-11, -6.10317e-19, 0, -4.37114e-08, 1, -1.39624e-11, -1, -4.37114e-08, 0, 0, -0.0182334) transform = Transform3D(1, -1.39624e-11, -6.10317e-19, 0, -4.37114e-08, 1, -1.39624e-11, -1, -4.37114e-08, 0, 0, -0.0182334)
material_override = SubResource("ShaderMaterial_ov0gt") material_override = SubResource("ShaderMaterial_ov0gt")
mesh = ExtResource("5_3o4gy") mesh = ExtResource("5_3o4gy")
[node name="Grid" type="MeshInstance3D" parent="."]
material_override = ExtResource("7_tnbkv")
mesh = SubResource("PlaneMesh_cq16e")
[node name="WallCollider" type="RayCast3D" parent="."]
target_position = Vector3(0, 0, -10)
collision_mask = 16

View File

@ -1,5 +1,13 @@
class_name TransformTools class_name TransformTools
static func plane_2d_coords(transform: Transform3D) -> Vector2:
var point = transform.origin
transform.origin = Vector3.ZERO
point = transform.inverse() * point
return Vector2( - point.x, point.y)
## Calculate the transform that would transform the source position, direction and up vector to the target position, direction and up vector ## Calculate the transform that would transform the source position, direction and up vector to the target position, direction and up vector
## Basically a bit more fancy Inputs for a Transform3D ## Basically a bit more fancy Inputs for a Transform3D
static func calc_delta_transform(source_pos: Vector3, source_dir: Vector3, source_up: Vector3, target_pos: Vector3, target_dir: Vector3, target_up: Vector3) -> Transform3D: static func calc_delta_transform(source_pos: Vector3, source_dir: Vector3, source_up: Vector3, target_pos: Vector3, target_dir: Vector3, target_up: Vector3) -> Transform3D:

View File

@ -0,0 +1,9 @@
@tool
extends Node3D
const GridShader = preload ("res://assets/materials/grid.tres")
@onready var mesh = $MeshInstance3D
func _process(_delta):
GridShader.set_shader_parameter("dot_offset", TransformTools.plane_2d_coords(mesh.global_transform))

View File

@ -0,0 +1,15 @@
[gd_scene load_steps=4 format=3 uid="uid://d26xg5okbaen6"]
[ext_resource type="Script" path="res://test/content/grid_shader.gd" id="1_jgxlt"]
[ext_resource type="Material" uid="uid://bb4kgiocswfv7" path="res://assets/materials/grid.tres" id="2_siin6"]
[sub_resource type="PlaneMesh" id="PlaneMesh_atwo6"]
size = Vector2(1, 1)
[node name="Node3D" type="Node3D"]
script = ExtResource("1_jgxlt")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(-0.843227, -0.537538, -0.00456947, -0.18903, 0.28855, 0.93862, -0.503225, 0.792333, -0.344923, 0.968812, 1.30895, -0.0715666)
material_override = ExtResource("2_siin6")
mesh = SubResource("PlaneMesh_atwo6")