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

Add custom shader support to SpriteBase3D #103274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chocola-mint
Copy link
Contributor

@chocola-mint chocola-mint commented Feb 25, 2025

Closes godotengine/godot-proposals#3266

godot-sprite3d-custom-shader-demo.mp4

This PR lets custom shaders (via Material Override) access the sprite texture used by SpriteBase3D. (and its child classes Sprite3D and AnimatedSprite3D by extension)

Validation is implemented to warn the user if a Material Override without the necessary uniforms (namely uniform sampler2D texture_albedo and uniform ivec2 albedo_texture_size) is being used. The "Flags" group properties of SpriteBase3D are also hidden when a Material Override is being used, as those properties are used in the default automatic material generation. (StandardMaterial3D::get_material_for_2d()) The user can reimplement those features in the custom shader code anyway, so they have been hidden.

Side note: This pattern of requiring the shader to have uniforms of specific names and types can be turned into a reusable "shader interface/trait" system if needed. Users could make their own high-level 3D nodes that send data to shaders like SpriteBase3D does. (Maybe a more advanced version of Sprite3D that assigns normal maps and SDF texture maps as uniforms) The main benefit of such a system would be to reduce the amount of builtin shader types, though. (No need to add a new sprite_3d shader type) Though this probably would have to be a separate proposal.


This PR was originally made to add custom shader support for AnimatedSprite3D in particular, but it is evident that implementing the support for SpriteBase3D makes more sense here.

@chocola-mint chocola-mint requested a review from a team as a code owner February 25, 2025 10:14
@AThousandShips AThousandShips added this to the 4.x milestone Feb 25, 2025
@TokageItLab TokageItLab requested review from clayjohn and a team February 26, 2025 08:45
@chocola-mint chocola-mint force-pushed the sprite-3d-custom-shaders branch from 8330368 to 36e80b5 Compare March 4, 2025 04:55
Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally, it works as expected.

Testing project: test_pr_103274.zip

Some feedback:

  • There should be a Sprite3D shader template you can choose from, with the correct uniforms already in place:

image

The shader template could also have render_mode unshaded; below shader_mode spatial;, as most Sprite3Ds are used in an unshaded manner (this is their default property).

Note that if #103312 is merged before this PR, the cache key should vary depending on the custom shader used. It should be possible for meshes with the same ShaderMaterial resource to be reused, but different shaders will require a different cache key.

@chocola-mint chocola-mint force-pushed the sprite-3d-custom-shaders branch from 36e80b5 to 79b7adf Compare March 6, 2025 08:01
@chocola-mint chocola-mint requested a review from a team as a code owner March 6, 2025 08:01
@chocola-mint chocola-mint force-pushed the sprite-3d-custom-shaders branch from 79b7adf to 9be33cc Compare March 6, 2025 08:05
@chocola-mint
Copy link
Contributor Author

chocola-mint commented Mar 6, 2025

Tested locally, it works as expected.

Testing project: test_pr_103274.zip

Some feedback:

  • There should be a Sprite3D shader template you can choose from, with the correct uniforms already in place:

image

The shader template could also have render_mode unshaded; below shader_mode spatial;, as most Sprite3Ds are used in an unshaded manner (this is their default property).

godot-sprite3d-custom-shader-template.mp4
  • The Sprite3D shader template is only shown when the shader mode is set to Spatial.
  • A minimal example that uses vertex color (thus supporting SpriteBase3D.modulate) and the albedo texture is attached.
  • The required uniforms are properly commented as such.

Note that ShaderCreateDialog doesn't seem to support adding custom shader templates in the first place, so I've taken the liberty to make it a bit easier to add hardcoded shader templates. Perhaps a future feature proposal could expand on this and make it more similar to script templates in terms of ergonomics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for custom shaders on AnimatedSprite3D
3 participants