Repository navigation
Expand file tree
/
Copy pathMirrorInstance.gd
More file actions
130 lines (108 loc) · 12.8 KB
/
Copy pathMirrorInstance.gd
File metadata and controls
130 lines (108 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
tool
extends MeshInstance
onready var dummy_cam = $DummyCam
onready var mirror_cam = $View/Camera
var a = {} #dictionary to remember Meshinstances' cull masks
export(int, 1, 20, 1) var IgnoreLayer = 2
var t = false # used to check for variable changes in a loop only when the variable changes
#For Diagnostics:=========================================================================================================================================================================================================================================
#var BDY
#var Clmsk
#var i = 0
#=========================================================================================================================================================================================================================================================
func _ready():
# print(find_node("Camera").get("cull_mask"))
add_to_group("mirrors")
get("material/0").get("shader_param/refl_tx").set("viewport_path",find_node("View").get_path()) #sets the viewport_path to the right viewport for every instance of this scene ,Mi2's child(0)='View'
#$View.size = Vector2(ProjectSettings.get_setting("display/window/size/width"), ProjectSettings.get_setting("display/window/size/height"))
get_node("View/Camera").set("cull_mask",1048575)#when all the camera's layer bits are turned on, the cull mask value is 1048575: 2^0 + 2^1 + 2^2 +...+2^19=1048575
get_node("View/Camera").set_cull_mask_bit(IgnoreLayer-1,false) #while every layer is on this turns the chosen layer off
#For Diagnostics:=========================================================================================================================================================================================================================================
# Clmsk=(log(1048575-get_node("View/Camera").get("cull_mask"))/log(2)+1) #The cameras' cull mask layer (clmsk) = log2(1048575-Cull_mask_Value)+1
#=========================================================================================================================================================================================================================================================
#The player's camera node calls this function to update the mirror-cameras position
#shout out to Miziziziz who came up with this: https://www.youtube.com/watch?v=xXUVP6sN-tQ
func update_cam(main_cam_transform):
scale.y *= -1
dummy_cam.global_transform = main_cam_transform
scale.y *= -1
mirror_cam.global_transform = dummy_cam.global_transform
mirror_cam.global_transform.basis.x *= -1
#syncs mirror size with game window size but only changes mirror's viewport size when game window size changes instead of constantly updating size every second
if($View.size != get_viewport().size): #checks if game window size is not equal to mirror viewprt size
if(t == false): #interact with 't' to change it to true only if 't' isn't already true
t=true
$View.size = get_viewport().size #only run once whenever 't' is changed
#For Diagnostics:=========================================================================================================================================================================================================================================
# i+=1
# print("view size updated ",i," ", self.name)
#==========================================================================================================================================================================================================================================================
else:
t=false
#recursively goes through a PhysicsBody node's branch to remember MeshInstances' layer mask
#as it replaces it with the layer mask that the mirror's camera ignores
func _remember_MeshInstances(N):
if(N != null):
#For Diagnostics:=========================================================================================================================================================================================================================================
# print(N.name)
#==========================================================================================================================================================================================================================================================
if((N.is_class("MeshInstance"))&&(!a.has(N))): #it checks if a MeshInstance it finds in the entering body is not yet added in the dictionary
#For Diagnostics:==========================================================================================================================================================================================================================================
# print("\n","Before Entering, \"",N.name,"\", from node: \"",BDY,"\", had a layer mask value of: ",N.get_layer_mask()) #shows the meshInstance's name, which physicsBody it belongs to and it's CullMask value before entering the HideArea
#==========================================================================================================================================================================================================================================================
a[N]=N.get_layer_mask() #Stores MeshInstances in dictionary a
N.set_layer_mask(0) #Sets layer Mask bits to be all false except for the chosen layer
N.set_layer_mask_bit(IgnoreLayer-1,true) #the layer mask bits are just 1 less than the actual layers
#For Diagnostics:==========================================================================================================================================================================================================================================
# print("\n","While Inside, \"",N.name,"\", from node: \"",BDY,"\", is set to appear only in layer: ",(log(N.get_layer_mask())/log(2)+1)) #shows the meshInstance's name, which physicsBody it belongs to and it's new CullMask value
# print("This Mirror's cull mask is set to ignore layer: ",Clmsk) # mirrors ignored cull mask layer
# if((N.get_layer_mask()+get_node("View/Camera").get("cull_mask")==1048575)): #the camera's layers are all ON except for the chosen layer to be ignored, the mesh's layers are all OFF except for the chosen layer so it could get ignored
# print("The mirror is correctly ignoring this body when it passed behind it")#if they were missaligned in any way
# else:
# print("I don't know what but something definitely went wrong")
#==========================================================================================================================================================================================================================================================
if(N.get_child_count()!=0): #checks if the node still has children that the code has to sift thru
for i in N.get_child_count():
_remember_MeshInstances(N.get_child(i)) #recursion
#For Diagnostics:==========================================================================================================================================================================================================================================
# else: #shows up at the leaf(end) nodes
# print("=================")
#==========================================================================================================================================================================================================================================================
#Goes thru the exiting body's stuff and restores each MeshInstancs' original layer mask
func _restore_Mesh_mask(N):
if(N != null):
#For Diagnostics:==========================================================================================================================================================================================================================================
# print(N.name)
#==========================================================================================================================================================================================================================================================
if((N.is_class("MeshInstance"))&&(a.has(N))): #checks if the MeshInstance has a saved layer mask in the dictionary
#For Diagnostics:==========================================================================================================================================================================================================================================
# print("\n","Before Exiting, \"",N.name,"\", from node: \"",BDY,"\", had a layer mask value of: ",N.get_layer_mask()) #shows the meshInstance's name, which physicsBody it belongs to and it's CullMask value before it exits the HideArea
#==========================================================================================================================================================================================================================================================
N.set_layer_mask(a.get(N)) #If it does it checks out of the HideArea with it
#For Diagnostics:==========================================================================================================================================================================================================================================
# print("\n","After Exiting, \"",N.name,"\", from node: \"",BDY,"\", has a layer mask value of: ",N.get_layer_mask()) #shows the meshInstance's name, which physicsBody it belongs to and it's CullMask value after it exits the HideArea
#==========================================================================================================================================================================================================================================================
a.erase(N) #the saved layer mask is then erased from the dictionary
if(N.get_child_count()!=0): #checks if the node still has children that the code has to go thru
for i in N.get_child_count():
_restore_Mesh_mask(N.get_child(i)) #recursion
#Sets the PhysicsBody to a layer mask that the mirror's camera ignores
func _on_HideArea_body_entered(body):
#For Diagnostics:==========================================================================================================================================================================================================================================
# BDY=body.name #saves the PhysicsBody being refered to, as the parent node
#==========================================================================================================================================================================================================================================================
_remember_MeshInstances(body)
#For Diagnostics:==========================================================================================================================================================================================================================================
# print("\nNodes currently in dictionary 'a':\n",a.keys()) #printsout the MeshInstances that the dictionary remembers at the moment
# print("\nTheir Cull Mask Values:\n",a.values()) #printsout the Cull Mask values of said MeshInstances
#==========================================================================================================================================================================================================================================================
#puts the original cullmask value back after body exits the hide area
func _on_HideArea_body_exited(body):
#For Diagnostics:==========================================================================================================================================================================================================================================
# BDY=body.name #saves the PhysicsBody being refered to, as the parent node
#==========================================================================================================================================================================================================================================================
_restore_Mesh_mask(body)
#For Diagnostics:==========================================================================================================================================================================================================================================
# print("\nNodes currently in dictionary 'a':\n",a.keys()) #printsout the MeshInstances that the dictionary remembers at the moment
# print("\nTheir Cull Mask Values:\n",a.values()) #printsout the Cull Mask values of said MeshInstances
#=========================================================================================================================================================================================================================================================