Skip to content

Commit

Permalink
Force shaders for blaster to be precompiled
Browse files Browse the repository at this point in the history
Resolves #3.
  • Loading branch information
jspahrsummers committed Jun 27, 2024
1 parent aa3f268 commit 6d40ae3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
18 changes: 18 additions & 0 deletions effects/environment.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[gd_resource type="Environment" load_steps=3 format=3 uid="uid://b7t3opwr35xen"]

[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_i3osh"]
sky_top_color = Color(1, 1, 1, 1)
sky_horizon_color = Color(0.75, 0.75, 0.75, 1)
ground_bottom_color = Color(0, 0, 0, 1)
ground_horizon_color = Color(0.75, 0.75, 0.75, 1)

[sub_resource type="Sky" id="Sky_qvksn"]
sky_material = SubResource("ProceduralSkyMaterial_i3osh")

[resource]
background_mode = 1
sky = SubResource("Sky_qvksn")
ambient_light_source = 2
ambient_light_color = Color(1, 1, 1, 1)
ambient_light_energy = 0.25
reflected_light_source = 1
42 changes: 23 additions & 19 deletions game.tscn
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
[gd_scene load_steps=36 format=3 uid="uid://sunhu71swcs2"]
[gd_scene load_steps=37 format=3 uid="uid://sunhu71swcs2"]

[ext_resource type="Script" path="res://utils/shader_precompiler.gd" id="1_kgmvv"]
[ext_resource type="Script" path="res://galaxy/hyperspace_controller.gd" id="2_5gj7y"]
[ext_resource type="PackedScene" uid="uid://b7tckdwyjwpso" path="res://weapons/blaster/blaster.tscn" id="2_bp704"]
[ext_resource type="Script" path="res://effects/main_camera.gd" id="2_v5hns"]
[ext_resource type="Resource" uid="uid://bcva4l4cpt70c" path="res://galaxy/main_galaxy.tres" id="3_j76v0"]
[ext_resource type="Material" uid="uid://tpd4rufhhe4h" path="res://celestials/starfield/starfield.tres" id="3_vksuy"]
[ext_resource type="Script" path="res://effects/hyperspace/hyperspace_effect.gd" id="4_bmy66"]
[ext_resource type="Script" path="res://galaxy/star_systems/star_system.gd" id="4_oiv78"]
[ext_resource type="AudioStream" uid="uid://cmjfgigj57eie" path="res://effects/hyperspace/hyperspace.ogg" id="5_jhe1g"]
[ext_resource type="PackedScene" uid="uid://cqxv07b8bwiev" path="res://ships/ship.tscn" id="6_8inyn"]
[ext_resource type="Environment" uid="uid://b7t3opwr35xen" path="res://effects/environment.tres" id="6_srnxb"]
[ext_resource type="Script" path="res://ships/player.gd" id="7_6citj"]
[ext_resource type="Resource" uid="uid://dc468i445pkgd" path="res://ships/corvette01/corvette01.tres" id="8_s4vsk"]
[ext_resource type="PackedScene" uid="uid://2mdsbbko7baw" path="res://galaxy/star_systems/sol.tscn" id="10_vhe4b"]
Expand All @@ -24,28 +27,14 @@
[ext_resource type="Script" path="res://gui/game_over.gd" id="22_g1tis"]
[ext_resource type="Script" path="res://gui/fps_counter.gd" id="23_ncnr7"]

[sub_resource type="World3D" id="World3D_pvg5q"]
environment = ExtResource("6_srnxb")

[sub_resource type="Resource" id="Resource_5wg57"]
script = ExtResource("4_oiv78")
name = &"Sol"
connections = Array[StringName]([&"Alpha Centauri", &"Barnard\'s Star", &"Wolf 359"])

[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_i3osh"]
sky_top_color = Color(1, 1, 1, 1)
sky_horizon_color = Color(0.75, 0.75, 0.75, 1)
ground_bottom_color = Color(0, 0, 0, 1)
ground_horizon_color = Color(0.75, 0.75, 0.75, 1)

[sub_resource type="Sky" id="Sky_qvksn"]
sky_material = SubResource("ProceduralSkyMaterial_i3osh")

[sub_resource type="Environment" id="Environment_5laul"]
background_mode = 1
sky = SubResource("Sky_qvksn")
ambient_light_source = 2
ambient_light_color = Color(1, 1, 1, 1)
ambient_light_energy = 0.25
reflected_light_source = 1

[sub_resource type="ViewportTexture" id="ViewportTexture_u0kwm"]
viewport_path = NodePath("HyperspaceViewport")

Expand Down Expand Up @@ -94,13 +83,28 @@ shadow_size = 0

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

[node name="ShaderPrecompiler" type="SubViewport" parent="."]
world_3d = SubResource("World3D_pvg5q")
gui_disable_input = true
size = Vector2i(2048, 2048)
render_target_update_mode = 4
script = ExtResource("1_kgmvv")
precompile = Array[PackedScene]([ExtResource("2_bp704")])

[node name="Camera3D" type="Camera3D" parent="ShaderPrecompiler"]
transform = Transform3D(1, -6.97574e-16, -1.5246e-23, -1.5246e-23, -4.37114e-08, 1, -6.97574e-16, -1, -4.37114e-08, 2.08165e-12, 10, 2.08165e-12)
projection = 1
current = true
near = 1.0
far = 20.0

[node name="HyperspaceController" type="Node" parent="."]
script = ExtResource("2_5gj7y")
galaxy = ExtResource("3_j76v0")
current_system = SubResource("Resource_5wg57")

[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_5laul")
environment = ExtResource("6_srnxb")

[node name="MainCamera" type="Camera3D" parent="." node_paths=PackedStringArray("follow_target")]
transform = Transform3D(1, -6.97574e-16, 4.15924e-24, 4.15924e-24, 1.19249e-08, 1, -6.97574e-16, -1, 1.19249e-08, 2.08165e-12, 10, 2.08165e-12)
Expand Down
19 changes: 19 additions & 0 deletions utils/shader_precompiler.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends SubViewport

## Hacky way to precompile shaders and avoid hiccups when instantiating objects later.
##
## The listed scenes are prerendered to an offscreen texture, forcing shaders to be
## compiled and everything loaded into memory. For some reason, this still doesn't
## cut out _all_ of the hiccup later, but at least reduces it meaningfully.

@export var precompile: Array[PackedScene] = []

func _ready() -> void:
self.get_texture()

for scene in self.precompile:
var node := scene.instantiate()
self.add_child(node)

await RenderingServer.frame_post_draw
self.queue_free()

0 comments on commit 6d40ae3

Please sign in to comment.