Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Godot 4.0 Update #8

Closed
wants to merge 9 commits into from
88 changes: 49 additions & 39 deletions Demo/DemoWorld.tscn
Original file line number Diff line number Diff line change
@@ -1,63 +1,73 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=10 format=3 uid="uid://0576fim4xnjw"]

[ext_resource path="res://Demo/MainCamera.gd" type="Script" id=1]
[ext_resource path="res://addons/Mirror/Mirror/Mirror.gd" type="Script" id=2]
[ext_resource type="Script" path="res://Demo/MainCamera.gd" id="1"]
[ext_resource type="Script" path="res://addons/Mirror/Mirror/Mirror.gd" id="2"]

[sub_resource type="SphereMesh" id=4]
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_omm3q"]

[sub_resource type="CapsuleMesh" id=7]
[sub_resource type="Sky" id="Sky_bigpx"]
sky_material = SubResource("ProceduralSkyMaterial_omm3q")

[sub_resource type="Environment" id="Environment_y6thg"]
background_mode = 2
sky = SubResource("Sky_bigpx")

[sub_resource type="SphereMesh" id="4"]
radius = 1.0
height = 2.0

[sub_resource type="CapsuleMesh" id="7"]
radius = 0.33
height = 1.66

[sub_resource type="OpenSimplexNoise" id=8]
period = 38.9
[sub_resource type="FastNoiseLite" id="8"]

[sub_resource type="NoiseTexture" id=9]
[sub_resource type="NoiseTexture2D" id="9"]
seamless = true
noise = SubResource( 8 )
noise = SubResource("8")

[node name="DemoWorld" type="Node3D"]

[node name="DemoWorld" type="Spatial"]
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_y6thg")

[node name="MeshInstance2" type="MeshInstance" parent="."]
transform = Transform( 0.4, 0, 0, 0, 0.4, 0, 0, 0, 0.4, 0.311584, -0.508855, 0.519106 )
[node name="MeshInstance2" type="MeshInstance3D" parent="."]
transform = Transform3D(0.4, 0, 0, 0, 0.4, 0, 0, 0, 0.4, 0.311584, -0.508855, 0.519106)
layers = 2
mesh = SubResource( 4 )
material/0 = null
mesh = SubResource("4")

[node name="MeshInstance3" type="MeshInstance" parent="."]
transform = Transform( 0.4, 0, 0, 0, 0.4, 0, 0, 0, 0.4, 0.4, 0.4, 0.773196 )
mesh = SubResource( 4 )
material/0 = null
[node name="MeshInstance3" type="MeshInstance3D" parent="."]
transform = Transform3D(0.4, 0, 0, 0, 0.4, 0, 0, 0, 0.4, 0.4, 0.4, 0.773196)
mesh = SubResource("4")

[node name="MeshInstance4" type="MeshInstance" parent="."]
transform = Transform( 0.3, 0, 0, 0, 0.3, 0, 0, 0, 0.3, 0.396369, 0.461957, -1.66788 )
mesh = SubResource( 4 )
material/0 = null
[node name="MeshInstance4" type="MeshInstance3D" parent="."]
transform = Transform3D(0.3, 0, 0, 0, 0.3, 0, 0, 0, 0.3, 0.396369, 0.461957, -1.66788)
mesh = SubResource("4")

[node name="Player" type="Spatial" parent="."]
transform = Transform( 0.733279, 0, -0.679928, 0, 1, 0, 0.679928, 0, 0.733279, -3.01042, 0.611678, 3.23258 )
script = ExtResource( 1 )
[node name="Player" type="Node3D" parent="."]
transform = Transform3D(0.733279, 0, -0.679928, 0, 1, 0, 0.679928, 0, 0.733279, -3.01042, 0.611678, 3.23258)
script = ExtResource("1")

[node name="CameraRod" type="Spatial" parent="Player"]
[node name="CameraRod" type="Node3D" parent="Player"]

[node name="MainCamera" type="Camera" parent="Player/CameraRod"]
[node name="MainCamera" type="Camera3D" parent="Player/CameraRod"]
near = 0.3

[node name="MeshInstance" type="MeshInstance" parent="Player/CameraRod"]
transform = Transform( 1, 0, 0, 0, -2.98023e-08, -1, 0, 1, -2.98023e-08, 0, -0.4, 0 )
mesh = SubResource( 7 )
[node name="MeshInstance3D" type="MeshInstance3D" parent="Player/CameraRod"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.4, 0)
mesh = SubResource("7")
skeleton = NodePath("../../..")
material/0 = null

[node name="Mirror" type="Spatial" parent="."]
script = ExtResource( 2 )
[node name="Mirror" type="Node3D" parent="."]
script = ExtResource("2")
ResolutionPerUnit = 200
MainCamPath = NodePath("../Player/CameraRod/MainCamera")
MirrorColor = Color( 1, 1, 1, 1 )
MirrorColor = Color(1, 1, 1, 1)

[node name="Mirror2" type="Spatial" parent="."]
transform = Transform( 0.658492, 0, -0.752587, 0, 1, 0, 0.752587, 0, 0.658492, 2.4934, 0, 0.705538 )
script = ExtResource( 2 )
[node name="Mirror2" type="Node3D" parent="."]
transform = Transform3D(0.658492, 0, -0.752587, 0, 1, 0, 0.752587, 0, 0.658492, 2.4934, 0, 0.705538)
script = ExtResource("2")
ResolutionPerUnit = 200
MainCamPath = NodePath("../Player/CameraRod/MainCamera")
MirrorDistortion = 20.0
DistortionTexture = SubResource( 9 )
MirrorDistortion = 20
DistortionTexture = SubResource("9")
34 changes: 17 additions & 17 deletions Demo/MainCamera.gd
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
extends Spatial
extends Node3D

class_name TpsCamera


# Nodes
onready var camera_pivot : Spatial = self
onready var camera_rod : Spatial = get_node("CameraRod")
onready var camera : Camera = get_node("CameraRod/MainCamera")
@onready var camera_pivot : Node3D = self
@onready var camera_rod : Node3D = get_node("CameraRod")
@onready var camera : Camera3D = get_node("CameraRod/MainCamera")

# Movement
export var mouse_sensitivity : float = 0.15
export var camera_min_vertical_rotation : float = -85.0
export var camera_max_vertical_rotation : float = 85.0
@export var mouse_sensitivity : float = 0.15
@export var camera_min_vertical_rotation : float = -85.0
@export var camera_max_vertical_rotation : float = 85.0

# zooming
export var camera_zoom : float = 3.0 setget set_camera_zoom
@export var camera_zoom : float = 3.0 : set = set_camera_zoom
func set_camera_zoom(value): camera_zoom = clamp(value, camera_min_zoom_distance, camera_max_zoom_distance)
export var camera_min_zoom_distance : float = 3.0
export var camera_max_zoom_distance : float = 15.0
export var camera_zoom_step : float = 0.5
@export var camera_min_zoom_distance : float = 3.0
@export var camera_max_zoom_distance : float = 15.0
@export var camera_zoom_step : float = 0.5

# Cursor
onready var is_cursor_visible setget set_is_cursor_visible, get_is_cursor_visible
@onready var is_cursor_visible : get = get_is_cursor_visible, set = set_is_cursor_visible
func set_is_cursor_visible(value): Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE if value else Input.MOUSE_MODE_CAPTURED)
func get_is_cursor_visible(): return Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE

Expand Down Expand Up @@ -56,7 +56,7 @@ func _unhandled_input(event: InputEvent) -> void:


####################
# Camera methods #
# Camera3D methods #
####################


Expand Down Expand Up @@ -91,18 +91,18 @@ func process_mouse_input(event : InputEvent) -> void:
# Cursor movement
if event is InputEventMouseMotion:
var camera_direction = Vector2(
deg2rad(event.relative.x * mouse_sensitivity),
deg2rad(event.relative.y * mouse_sensitivity)
deg_to_rad(event.relative.x * mouse_sensitivity),
deg_to_rad(event.relative.y * mouse_sensitivity)
)
if !self.is_cursor_visible:
rotate_camera(camera_direction)

# Scrolling
elif event is InputEventMouseButton:
if event.is_pressed() and not self.is_cursor_visible:
if event.button_index == BUTTON_WHEEL_UP:
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
self.camera_zoom -= camera_zoom_step
if event.button_index == BUTTON_WHEEL_DOWN:
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
self.camera_zoom += camera_zoom_step


2 changes: 1 addition & 1 deletion Demo/default_env.tres
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_resource type="Environment" load_steps=2 format=2]

[sub_resource type="ProceduralSky" id=1]
[sub_resource type="Sky" id=1]

[resource]
background_mode = 2
Expand Down
48 changes: 24 additions & 24 deletions addons/Mirror/Mirror/Mirror.gd
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
tool
extends Spatial
@tool
extends Node3D

const whitegreen : Color = Color(0.9, 0.97, 0.94)

export var size : Vector2 = Vector2(2, 2)
export var ResolutionPerUnit = 100
export(NodePath) var MainCamPath = ""
export(Array, int) var cullMask = []
export(Color, RGB) var MirrorColor = whitegreen
export(float, 0, 30, 0.01) var MirrorDistortion = 0
export(Texture) var DistortionTexture
@export var size : Vector2 = Vector2(2, 2)
@export var ResolutionPerUnit = 100
@export var MainCamPath: NodePath = ""
@export var cullMask = [] # (Array, int)
@export var MirrorColor = whitegreen # (Color, RGB)
@export var MirrorDistortion = 0 # (float, 0, 30, 0.01)
@export var DistortionTexture: Texture2D

var MainCam : Camera = null
var cam : Camera
var mirror : MeshInstance
var viewport : Viewport
var MainCam : Camera3D = null
var cam : Camera3D
var mirror : MeshInstance3D
var viewport : SubViewport


func _enter_tree():
var node = preload("MirrorContainer.tscn").instance()
var node = preload("MirrorContainer.tscn").instantiate()
add_child(node)


func _ready():
MainCam = get_node_or_null(MainCamPath)
cam = $MirrorContainer/Viewport/Camera
mirror = $MirrorContainer/MeshInstance
viewport = $MirrorContainer/Viewport
cam = $MirrorContainer/SubViewport/Camera3D
mirror = $MirrorContainer/MeshInstance3D
viewport = $MirrorContainer/SubViewport


func _process(delta):
_ready() # need to reload for proper operation when used as a toolscript
if MainCam == null:
# No camera specified for the mirror to operate on
# No camera specified for the mirror to operate checked
return

# Cull camera layers
Expand All @@ -46,14 +46,14 @@ func _process(delta):
viewport.size = size * ResolutionPerUnit

# Set tint color
mirror.get_active_material(0).set_shader_param("tint", MirrorColor)
mirror.get_active_material(0).set_shader_parameter("tint", MirrorColor)

# Set distortion texture
mirror.get_active_material(0).set_shader_param("distort_tex", DistortionTexture)
mirror.get_active_material(0).set_shader_parameter("distort_tex", DistortionTexture)
# Set distortion strength
mirror.get_active_material(0).set_shader_param("distort_strength", MirrorDistortion)
mirror.get_active_material(0).set_shader_parameter("distort_strength", MirrorDistortion)

# Transform the mirror camera to the opposite side of the mirror plane
# Transform3D the mirror camera to the opposite side of the mirror plane
var MirrorNormal = mirror.global_transform.basis.z
var MirrorTransform = Mirror_transform(MirrorNormal, mirror.global_transform.origin)
cam.global_transform = MirrorTransform * MainCam.global_transform
Expand All @@ -76,13 +76,13 @@ func _process(delta):
# n is the normal of the mirror plane
# d is the offset from the plane of the mirrored object
# Gets the transformation that mirrors through the plane with normal n and offset d
func Mirror_transform(n : Vector3, d : Vector3) -> Transform:
func Mirror_transform(n : Vector3, d : Vector3) -> Transform3D:
var basisX : Vector3 = Vector3(1.0, 0, 0) - 2 * Vector3(n.x * n.x, n.x * n.y, n.x * n.z)
var basisY : Vector3 = Vector3(0, 1.0, 0) - 2 * Vector3(n.y * n.x, n.y * n.y, n.y * n.z)
var basisZ : Vector3 = Vector3(0, 0, 1.0) - 2 * Vector3(n.z * n.x, n.z * n.y, n.z * n.z)

var offset = Vector3.ZERO
offset = 2 * n.dot(d)*n

return Transform(Basis(basisX, basisY, basisZ), offset)
return Transform3D(Basis(basisX, basisY, basisZ), offset)
pass
16 changes: 4 additions & 12 deletions addons/Mirror/Mirror/Mirror.gdshader
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
shader_type spatial;
render_mode cull_disabled, unshaded;

uniform vec4 tint : hint_color = vec4(vec3(0.98),1.0);
uniform sampler2D mirror_tex;
uniform sampler2D distort_tex : hint_albedo;
uniform vec4 tint : source_color = vec4(vec3(0.98),1.0);
uniform sampler2D mirror_tex : source_color, repeat_disable;
uniform sampler2D distort_tex : source_color;
uniform float distort_strength : hint_range(0, 30);

void vertex() {
UV = 1.0 - UV;
UV.x = 1.0 - UV.x;
}

//vec3 lin_to_srgb(vec3 col) {
// return mix(
// pow((col + vec3(0.055)) * (1.0 / (1.0 + 0.055)),vec3(2.4)),
// col * (1.0 / 12.92),
// lessThan(col,vec3(0.04045))
// );
//}

void fragment() {
vec2 m_UV = UV;

Expand Down
33 changes: 14 additions & 19 deletions addons/Mirror/Mirror/MirrorContainer.tscn
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=3 format=3 uid="uid://b51uk2h1t8l33"]

[ext_resource path="res://addons/Mirror/Mirror/MirrorMaterial.tres" type="Material" id=1]
[ext_resource type="Material" uid="uid://cp471l6edt4ab" path="res://addons/Mirror/Mirror/MirrorMaterial.tres" id="1"]

[sub_resource type="QuadMesh" id=11]
size = Vector2( 2, 2 )
[sub_resource type="QuadMesh" id="11"]
size = Vector2(2, 2)

[node name="MirrorContainer" type="Spatial"]
__meta__ = {
"_edit_group_": true,
"_edit_lock_": true
}
[node name="MirrorContainer" type="Node3D"]

[node name="Viewport" type="Viewport" parent="."]
size = Vector2( 100, 100 )
keep_3d_linear = true
[node name="SubViewport" type="SubViewport" parent="."]
size = Vector2i(100, 100)

[node name="Camera" type="Camera" parent="Viewport"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.406672, 1.21628, 8.51171 )
[node name="Camera3D" type="Camera3D" parent="SubViewport"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.406672, 1.21628, 8.51171)
keep_aspect = 0
cull_mask = 1048571
projection = 2
size = 3.0
frustum_offset = Vector2( -0.406672, -1.21628 )
frustum_offset = Vector2(-0.406672, -1.21628)
near = 8.51171
far = 10000.0

[node name="MeshInstance" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 2.38419e-07, 0, 1, 0, -2.38419e-07, 0, 1, 0, 0, 0 )
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 2.38419e-07, 0, 1, 0, -2.38419e-07, 0, 1, 0, 0, 0)
layers = 4
mesh = SubResource( 11 )
mesh = SubResource("11")
skeleton = NodePath("../..")
material/0 = ExtResource( 1 )
surface_material_override/0 = ExtResource("1")
18 changes: 9 additions & 9 deletions addons/Mirror/Mirror/MirrorMaterial.tres
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[gd_resource type="ShaderMaterial" load_steps=3 format=2]
[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://cp471l6edt4ab"]

[ext_resource path="res://addons/Mirror/Mirror/Mirror.gdshader" type="Shader" id=1]
[ext_resource type="Shader" path="res://addons/Mirror/Mirror/Mirror.gdshader" id="1"]

[sub_resource type="ViewportTexture" id=12]
flags = 4
viewport_path = NodePath("Viewport")
[sub_resource type="ViewportTexture" id="12"]
viewport_path = NodePath("SubViewport")

[resource]
resource_local_to_scene = true
shader = ExtResource( 1 )
shader_param/tint = Color( 0.98, 0.98, 0.98, 1 )
shader_param/distort_strength = 0.0
shader_param/mirror_tex = SubResource( 12 )
render_priority = 0
shader = ExtResource("1")
shader_parameter/tint = Color(0.98, 0.98, 0.98, 1)
shader_parameter/distort_strength = 0.0
shader_parameter/mirror_tex = SubResource("12")
Loading