Godot Version
Godot v4.4_beta1
Question
I have a dropdown shadow shader for Sprite2D node for static objects and I use a 2D average pooling to make the edge smooth. The computation increase with the pooling size, so the shader is quite computation expensive. Since the object is static, the shadow is static too so it only need to calculate once. Is there a way to make a texture out of the rendered texture and replace it as the new texture, then disable the shader material?
following is the solution given by Deepseek, but the final_texture
is empty. The 2.png
is also an empty image.
func bake_texture():
var original_material = material as ShaderMaterial
if not original_material:
return
var viewport = SubViewport.new()
viewport.size = texture.get_size()
viewport.transparent_bg = true #
#viewport.render_target_update_mode = SubViewport.UPDATE_ONCE
add_child(viewport)
var sprite_copy = Sprite2D.new()
sprite_copy.texture = texture
sprite_copy.material = original_material.duplicate()
viewport.add_child(sprite_copy)
sprite_copy.texture.get_image().save_png("1.png")
await get_tree().process_frame
var rendered_texture = viewport.get_texture()
var image = rendered_texture.get_image()
var final_texture = ImageTexture.create_from_image(image)
final_texture.get_image().save_png('2.png')
texture = final_texture
material = null
viewport.queue_free()
baked = true