Skip to content

Commit 48ca2c2

Browse files
committed
add more custom game script default scripts
1 parent 0002e5d commit 48ca2c2

1 file changed

Lines changed: 60 additions & 12 deletions

File tree

xr_injector/custom_scripts/custom_game_script.gd

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ var right_controller : XRController3D
77
# Convenience VR Camera / HMD reference, do not modify, will be set in xr_scene.gd automatically
88
var hmd : XRCamera3D
99
# Convenience reference to Primary (weapon/turning hand) controller set by user, do not modify, will be set in xr_scene.gd automatically
10-
var primary_controller : XRController3D
10+
var primary_controller
1111
# Convenience reference to Secondary (movement/off-hand) controller set by user, do not modify, will be set in xr_scene.gd automatically
12-
var secondary_controller : XRController3D
12+
var secondary_controller
1313
# Convenience XR Scene reference (the parent node of all of UGVR), do not modify, will be set in xr_scene.gd
1414
var xr_scene : Node3D = null
1515
# Convenience reference to the node at the top of the scene tree in any game, allows finding or getting other nodes in game scene tree
@@ -18,15 +18,34 @@ var scene_root = null
1818
var active_flat_screen_camera3D = null
1919
# Track whether single use function has already been called
2020
var on_xr_setup_already_run : bool = false
21+
# Basic clear 2D shader (for use in trying to replace problematic canvas item shaders)
22+
var default_2d_transparent_shader = """
23+
shader_type canvas_item;
24+
25+
void fragment(){
26+
COLOR = vec4(1.0, 1.0, 1.0, 0.0);
27+
}
28+
"""
29+
# Basic clear 3D mesh shader (for use in trying to replace problematic mesh shaders)
30+
var default_3d_transparent_mesh_shader = """
31+
shader_type spatial;
32+
render_mode unshaded, transparent;
33+
34+
void fragment() {
35+
ALBEDO = vec3(1.0); // RGB = white
36+
ALPHA = 0.0; // Fully transparent
37+
}
38+
"""
39+
2140
# Put game specific custom variables here for your code, e.g., var game_name_important_node = null
2241

2342

24-
# Called when the node enters the scene tree for the first time. Can't use controller references yet as they will not be set up yet.
43+
# Called when the node enters the scene tree for the first time. Can't use convenience references yet as they will not be set up yet.
2544
func _ready():
2645
pass
2746

2847
# Called only once after xr scene and all convenience variables are set, insert any code you want to run then here
29-
# Note that you can now access any of the xr scene variables directly, example: xr_scene.xr_pointer.enabled=false
48+
# Note that you can now access any of the xr scene variables directly at this point, example: xr_scene.xr_pointer.enabled=false
3049
func _on_xr_setup_run_once():
3150
pass
3251

@@ -121,10 +140,10 @@ func reparent_game_node_to_controller(game_node : Node3D, controller : XRControl
121140
remote_transform.remote_path = game_node.get_path()
122141

123142
# Convenience function to move game node to track HMD, may not work in all instances, offset is x, y, z relative to HMD
124-
func reparent_game_node_to_hmd(game_node : Node3D, hmd : XRCamera3D, offset: Vector3 = Vector3(0,0,0)) -> void:
143+
func reparent_game_node_to_hmd(game_node : Node3D, hmd_node : XRCamera3D, offset: Vector3 = Vector3(0,0,0)) -> void:
125144
var remote_transform : RemoteTransform3D = RemoteTransform3D.new()
126145
var node_holder : Node3D = Node3D.new()
127-
hmd.add_child(node_holder)
146+
hmd_node.add_child(node_holder)
128147
node_holder.transform.origin = offset
129148
node_holder.add_child(remote_transform)
130149
remote_transform.update_scale = false
@@ -135,37 +154,66 @@ func remove_shader_from_mesh(mesh_node : MeshInstance3D)-> void :
135154
var surface_material = mesh_node.get_surface_override_material(0)
136155
if surface_material:
137156
if surface_material.is_class("ShaderMaterial"):
138-
surface_material.get_shader().set_code("")
157+
surface_material.get_shader().set_code(default_3d_transparent_mesh_shader)
139158

140159
# Convenience funcrion to try to delete a problematic shader from a 2D canvas object (like a rectangle on the screen), may cause game crashes if game code depends on setting variables in the shader
141160
func remove_shader_from_UI_object(canvas_item : CanvasItem)-> void :
142161
var surface_material = canvas_item.material
143162
if surface_material:
144163
if surface_material.is_class("ShaderMaterial"):
145-
surface_material.get_shader().set_code("")
164+
surface_material.get_shader().set_code(default_2d_transparent_shader)
165+
166+
# Convenience function to completely remove a potential problematic material from a mesh. May have unintended consequences or create unwarranted visual artifacts.
167+
func remove_material_from_mesh(mesh_node : MeshInstance3D) -> void:
168+
var number_of_surface_materials = mesh_node.get_surface_override_material_count()
169+
for i in range(number_of_surface_materials):
170+
mesh_node.set_surface_override_material(i, null)
171+
var mesh = mesh_node.mesh
172+
if mesh:
173+
var mesh_surface_count = mesh.get_surface_count()
174+
for i in range(mesh_surface_count):
175+
mesh.surface_set_material(i, null)
176+
177+
# Convenience function to completely remove a potential problematic material from a 2D item (like a UI item). May have unintended consequences or create unwarranted visual artifacts.
178+
func remove_material_from_UI_object(canvas_item : CanvasItem) -> void:
179+
canvas_item.material = null
146180

147181
# Convenience function to hide a node. May need to be run in _process if other game code may dynamically hide and show the element
148182
func hide_node(node : Node) -> void:
149183
if node.has_method("hide"):
150184
node.hide()
151185
else:
152186
for property_dictionary in node.get_property_list():
153-
if "visible" in property_dictionary.name:
187+
if "visible" in property_dictionary["name"]:
154188
node.visible = false
155189
break
156190

157191
# Convenience function to show a hidden node. May need to be run in _process if other game code may dynamically hide and show the element
158192
func show_node(node : Node) -> void:
159-
node.show()
160-
node.visible = true
161193
if node.has_method("show"):
162194
node.show()
163195
else:
164196
for property_dictionary in node.get_property_list():
165-
if "visible" in property_dictionary.name:
197+
if "visible" in property_dictionary["name"]:
166198
node.visible = true
167199
break
168200

201+
# Convenience function to print a scene tree of the scene at the time the function is called to the existing log for the game
202+
# The log is usually found in Users/userscomputername/AppData/Roaming/Godot/app_userdata/game_name, but some games maintain their user data with logs in other folders
203+
# This can be used to find potential nodes for reparenting, adjusting shaders, etc., without having to try to decompile the flat screen game
204+
func print_scene_tree_pretty_to_game_log() -> void:
205+
get_tree().current_scene.print_tree_pretty()
206+
207+
# Convenience function to print a scene tree of the scene at the time the function is called to a text file dedicated to this purpose in the XRConfigs folder. Note that by default the file does NOT clear. Set to true instead when calling the function to just print the latest scene tree to the file.
208+
# This can be used to find potential nodes for reparenting, adjusting shaders, etc., without having to try to decompile the flat screen game
209+
func print_scene_tree_pretty_to_text_file(full_file_path : String = "", clear_previous_contents : bool = false) -> void:
210+
var text_file = full_file_path if full_file_path != "" else xr_scene.xr_config_handler.cfg_base_path + "/xr_scene_tree_print_log.txt"
211+
if clear_previous_contents == true and FileAccess.file_exists(text_file):
212+
OS.move_to_trash(text_file)
213+
var file = FileAccess.open(text_file, FileAccess.WRITE)
214+
var node_tree_string = get_tree().current_scene.get_tree_string_pretty() + "\n\n"
215+
file.store_string(node_tree_string)
216+
169217
# Setter function for xr_scene reference, called in xr_scene.gd automatically
170218
func set_xr_scene(new_xr_scene) -> void:
171219
xr_scene = new_xr_scene

0 commit comments

Comments
 (0)