Skip to content

Commit

Permalink
added collision fade
Browse files Browse the repository at this point in the history
this adds fade upon collision
based upon @Malcolmnixon s work
https://github.com/Malcolmnixon/godot-xr-tools-experiments

Co-Authored-By: Malcolm Nixon <[email protected]>
  • Loading branch information
DigitalN8m4r3 and Malcolmnixon committed Mar 31, 2024
1 parent 60d7cb1 commit 6e7e16f
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 28 deletions.
6 changes: 6 additions & 0 deletions assets/meshes/collision_fade/scenes/fade_area.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://b3o3tygsi5vw"]

[ext_resource type="Script" path="res://assets/meshes/collision_fade/scripts/fade_area.gd" id="1_m3jgp"]

[node name="FadeArea" type="Node3D"]
script = ExtResource("1_m3jgp")
6 changes: 6 additions & 0 deletions assets/meshes/collision_fade/scenes/fade_collision.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://dlivaur4w2m12"]

[ext_resource type="Script" path="res://assets/meshes/collision_fade/scripts/fade_collision.gd" id="1_2vuvb"]

[node name="FadeCollision" type="Node3D"]
script = ExtResource("1_2vuvb")
6 changes: 6 additions & 0 deletions assets/meshes/collision_fade/scenes/fade_in_out.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://dapr8mrnkgum0"]

[ext_resource type="Script" path="res://assets/meshes/collision_fade/scripts/fade_in_out.gd" id="1_gqyql"]

[node name="FadeInOut" type="Node"]
script = ExtResource("1_gqyql")
18 changes: 18 additions & 0 deletions assets/meshes/collision_fade/scenes/fader.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[gd_scene load_steps=5 format=3 uid="uid://w82xvrc74u1b"]

[ext_resource type="Script" path="res://assets/meshes/collision_fade/scripts/fader.gd" id="1_r8ais"]
[ext_resource type="Shader" path="res://assets/meshes/collision_fade/shader/fade.gdshader" id="2_oa6c6"]

[sub_resource type="QuadMesh" id="QuadMesh_iv1ir"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_tmf72"]
render_priority = 127
shader = ExtResource("2_oa6c6")
shader_parameter/albedo = Color(0, 0, 0, 0)

[node name="Fader" type="Node3D"]
script = ExtResource("1_r8ais")

[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("QuadMesh_iv1ir")
surface_material_override/0 = SubResource("ShaderMaterial_tmf72")
72 changes: 72 additions & 0 deletions assets/meshes/collision_fade/scripts/fade_area.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
extends Node3D

## Rate to obscure
##
## @desc:
## This property controls the rate the fader will adjust to obscure the
## view. Larger numeric values will obscure faster. For example a value of 3
## will fully obscure the scene in 1/3rd of a second.
@export var obscure_rate := 3.0

## Rate to reveal
##
## @desc:
## This property controls the rate the fader will adjust to reveal the
## view. Larger numeric values will reveal faster. For example a value of
## 3 will fully reveal the scene in 1/3rd of a second.
@export var reveal_rate := 1.0

## Default fade if not in a fade area
##
## @desc:
## This property sets the default fade if the player is not in a fade area
@export var default_fade := 1.0

## Layers to check
##
## @desc:
## This property sets the layers this fade area checks for.
@export var fade_area_layers := 2 # (int, LAYERS_3D_PHYSICS)

# Current fade contribution [0..1] - used by Fader
var fade_contribution := 0.0

# World space to use for collision detection
var space : PhysicsDirectSpaceState3D = null

# Called when the node enters the scene tree for the first time.
func _ready():
# Get the space to test collisions in
space = get_world_3d().get_direct_space_state()

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
add_to_group("fade_contributor")
# Find all area collisions
var collisions = PhysicsRayQueryParameters3D.new()
collisions.from = global_transform.origin
#collisions.to = 32
collisions.exclude = []

collisions.collision_mask = 2
var result = space.intersect_ray(collisions)
if result:
return false
return true
# Calculate the fade
var fade = default_fade
var fade_priority = -1;
for c in collisions:
var area = c["collider"]
if area.is_in_group("fade_area"):
if area.priority > fade_priority:
fade = area.fade_level
fade_priority = area.priority;

# Adjust the contribution
if fade_contribution < fade:
# Fade in to target (obscure)
fade_contribution = min(fade_contribution + obscure_rate * delta, fade)
elif fade_contribution > fade:
# Fade out to target (reveal)
fade_contribution = max(fade_contribution - reveal_rate * delta, fade)
65 changes: 65 additions & 0 deletions assets/meshes/collision_fade/scripts/fade_collision.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
extends Node3D

## Layers to collide with
##
## @desc:
## This property sets the layers this fade collision checks for.
@export var collision_layers : int= 2

## Collision distance at which fading begins
##
## @desc:
## This distance sets how far away from the camera a collision must be to
## begin obscuring the view
@export var fade_start_distance := 0.3

## Collision distance for totally obscuring the view
##
## @desc:
## This distance sets how far away from the camera a collision must be to
## totally obscure the view
@export var fade_full_distance := 0.15

# Current fade contribution [0..1] - used by Fader
var fade_contribution := 0.0

# Shape to use for collision detection
var collision_shape : Shape3D = null

# Parameters to use for collision detection
var collision_parameters : PhysicsShapeQueryParameters3D = null

# World space to use for collision detection
var space : PhysicsDirectSpaceState3D = null

# Called when the node enters the scene tree for the first time.
func _ready():
add_to_group("fade_contributor")
# Construct a sphere for the collision shape
collision_shape = SphereShape3D.new()
collision_shape.radius = fade_start_distance

# Construct the collosion parameters
collision_parameters = PhysicsShapeQueryParameters3D.new()
collision_parameters.collision_mask = collision_layers
collision_parameters.set_shape(collision_shape)

# Get the space to test collisions in
space = get_world_3d().direct_space_state

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
# Update the collision parameters to include our global location
collision_parameters.transform = global_transform
# Find closest collision
var results = space.get_rest_info(collision_parameters)
if "point" in results:
# Collision detected, calculate distance to closet collision point
var delta_pos = global_transform.origin - results["point"]
var length = delta_pos.length()

# Fade based on distance
fade_contribution = inverse_lerp(fade_start_distance, fade_full_distance, length)
else:
# No collision
fade_contribution = 0.0
50 changes: 50 additions & 0 deletions assets/meshes/collision_fade/scripts/fade_in_out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
extends Node

## Rate to obscure
##
## @desc:
## This property controls the rate the fader will adjust to obscure the
## view. Larger numeric values will obscure faster. For example a value of 3
## will fully obscure the scene in 1/3rd of a second.
@export var obscure_rate := 1.0

## Rate to reveal
##
## @desc:
## This property controls the rate the fader will adjust to reveal the
## view. Larger numeric values will reveal faster. For example a value of
## 3 will fully reveal the scene in 1/3rd of a second.
@export var reveal_rate := 1.0

## Initial fade contribution [0..1]
##
## @desc:
## This property contains the initial fade level at start.
@export var initial_fade := 1.0

## Current fade target [0..1]
##
## @desc:
## This property contains the target fade for this fade function. The
## 'fade_contribution' will slew to this target based on the 'fade_in_rate'
## and 'fade_out_rate' properties. The user can set this value for an initial
## fade target at start.
@export var fade_target := 0.0

# Current fade contribution [0..1] - used by Fader
var fade_contribution := 1.0

# Called when the node enters the scene tree for the first time.
func _ready():
add_to_group("fade_contributor")
fade_contribution = initial_fade

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
# Adjust the fade
if fade_contribution < fade_target:
# Fade in to target (obscure)
fade_contribution = min(fade_contribution + obscure_rate * delta, fade_target)
elif fade_contribution > fade_target:
# Fade out to target (reveal)
fade_contribution = max(fade_contribution - reveal_rate * delta, fade_target)
38 changes: 38 additions & 0 deletions assets/meshes/collision_fade/scripts/fader.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
extends Node3D

# Current fade level [0..1]
var current_fade := 0.0

# Material on fade mesh
var fade_material : Material = null

# Array of fade-contributors
var fade_contributors = Array()

# Called when the node enters the scene tree for the first time.
func _ready():
# Get the fade material
fade_material = $MeshInstance3D.get_surface_override_material(0)

# Get all fade contributor nodes
fade_contributors = get_tree().get_nodes_in_group("fade_contributor")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
# Get the highest fade from all contributors [0..1]
var fade = 0.0
for f in fade_contributors:
fade = max(fade, f.fade_contribution)

# Clamp the fade to ensure legal range
fade = clampf(fade, 0.0, 1.0)

# Adjust the fade level if necessary
if fade != current_fade:
# Update the current fade
current_fade = fade

# Set the fade mesh alpha channel
fade_material.set_shader_parameter("albedo", Color(0.0, 0.0, 0.0, fade))
# Enable the fade mesh only if we have anything to fade
$MeshInstance3D.visible = current_fade > 0
12 changes: 12 additions & 0 deletions assets/meshes/collision_fade/shader/fade.gdshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_disabled,unshaded,depth_test_disabled;
uniform vec4 albedo : source_color;

void vertex() {
POSITION = vec4(VERTEX.xy * 2.0, 1.0, 1.0);
}

void fragment() {
ALBEDO = albedo.rgb;
ALPHA = albedo.a;
}
Loading

0 comments on commit 6e7e16f

Please sign in to comment.