Sharing my shader compile script

friendMaster2000friendMaster2000 Posts: 14Member
edited October 10 in Resources

I'd like to share a very useful script I wrote for my projects. This script uses the shader compile trick to compile material shaders ahead of time to prevent hiccups or hitching that occurs when shaders are compiled during gameplay. The shader compile problem and solution are explained in the video here: The attached script automates implementing the solution explained in the video.

Instructions:

  1. For each material, add one MeshInstance or Particles object that uses the material to a group called "materials". You can add nodes to a group by selecting the node and clicking the "Node" tab in the top right corner. Adding multiple nodes with the same material to the group is fine but will waste resources.

  2. Add a node as a direct child of the camera node.

  3. Assign the attached script: shaderCompile.gd script to the node.

  4. This script assumes the existence of a top-level node called "World" which all scene objects reside in. Change this name as necessary for your project.

  5. There's two variables to play with. existFrames is the number of frames which go by until the quad mesh is hidden. Set this to a number between 1 and 5. verticalDisplacement is the vertical distance of the quad mesh from the camera. Try tweaking both variables if you notice hiccups still occur.

Feedback is welcome.


Tags :

Comments

  • cyberealitycybereality Posts: 2,089Moderator

    That was really awesome and very educational. I wonder if it would be better if there was a way in code to just preload shaders and/or just trigger the compilation? While this trick is nice to know (I will definitely use it) it would be better if it was part of the API.

  • friendMaster2000friendMaster2000 Posts: 14Member
    edited October 10

    @cybereality said:
    That was really awesome and very educational. I wonder if it would be better if there was a way in code to just preload shaders and/or just trigger the compilation? While this trick is nice to know (I will definitely use it) it would be better if it was part of the API.

    It's not a part of the Godot API but that's sorta the purpose of the attached script. It automates the solution described in the video. After the scripts installed, you can implement the solution explained in the video by just adding a mesh instance or particle system which uses the material to a node group called 'materials'. The script iterates through all materials and submaterials in the mesh instance or particle system, and applies each material to a quad mesh which is a child of the camera node. This tricks Godot into compiling the shaders ahead of time. It then hides the quad meshes after a few process ticks have passed.

  • cyberealitycybereality Posts: 2,089Moderator

    I see, okay. Thanks.

  • cyberealitycybereality Posts: 2,089Moderator

    Looks like this will be fixed in Godot 3.5.

  • Erich_LErich_L Posts: 142Member

    @friendMaster2000 Superbly explained. If anything I learned more about the engine's inner workings.

Sign In or Register to comment.