1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-30 23:19:49 +00:00

Use VisualServer viewport for applying shader

This comes with the benefit that we no longer need a Viewport node in the ShaderEffect dialog, which lets us have the Preview TextureRect at a fixed size. Previously, the preview was scaled based on the image's size, which would make applying shaders to large images very inconvenient, since the dialog would be way too big.
This commit is contained in:
OverloadedOrama 2020-08-13 01:55:31 +03:00
parent 8734659da1
commit fe2ed42819
2 changed files with 65 additions and 50 deletions

View file

@ -2,19 +2,16 @@ extends ConfirmationDialog
var current_cel : Image
var shader : Shader
var params := [] # String[]
onready var viewport : Viewport = $VBoxContainer/ViewportContainer/Viewport
onready var preview : TextureRect = viewport.get_node("Preview")
onready var preview : TextureRect = $VBoxContainer/Preview
onready var shader_loaded_label : Label = $VBoxContainer/ShaderLoadedLabel
onready var shader_params : BoxContainer = $VBoxContainer/ShaderParams
func _on_ShaderEffect_about_to_show() -> void:
current_cel = Global.current_project.frames[Global.current_project.current_frame].cels[Global.current_project.current_layer].image
current_cel.unlock()
viewport.size = Global.current_project.size
# var viewport_texture = viewport.get_texture().get_data()
# viewport_texture.convert(Image.FORMAT_RGBA8)
var preview_image := Image.new()
preview_image.copy_from(current_cel)
@ -24,26 +21,53 @@ func _on_ShaderEffect_about_to_show() -> void:
func _on_ShaderEffect_confirmed() -> void:
var viewport_texture := Image.new()
viewport_texture.copy_from(viewport.get_texture().get_data())
var viewport_texture_size = viewport_texture.get_size()
if viewport_texture_size == Vector2.ZERO:
if !shader:
return
viewport_texture.flip_y()
current_cel.unlock()
var viewport_texture := Image.new()
var size : Vector2 = Global.current_project.size
var vp = VisualServer.viewport_create()
var canvas = VisualServer.canvas_create()
VisualServer.viewport_attach_canvas(vp, canvas)
VisualServer.viewport_set_size(vp, size.x, size.y)
VisualServer.viewport_set_disable_3d(vp, true)
VisualServer.viewport_set_usage(vp, VisualServer.VIEWPORT_USAGE_2D)
VisualServer.viewport_set_hdr(vp, true)
VisualServer.viewport_set_active(vp, true)
VisualServer.viewport_set_transparent_background(vp, true)
var ci_rid = VisualServer.canvas_item_create()
VisualServer.viewport_set_canvas_transform(vp, canvas, Transform())
VisualServer.canvas_item_set_parent(ci_rid, canvas)
var texture = ImageTexture.new()
texture.create_from_image(current_cel)
VisualServer.canvas_item_add_texture_rect(ci_rid, Rect2(Vector2(0, 0), size), texture)
var mat_rid = VisualServer.material_create()
VisualServer.material_set_shader(mat_rid, shader.get_rid())
VisualServer.canvas_item_set_material(ci_rid, mat_rid)
for param in params:
var param_data = preview.material.get_shader_param(param)
VisualServer.material_set_param(mat_rid, param, param_data)
VisualServer.viewport_set_update_mode(vp, VisualServer.VIEWPORT_UPDATE_ONCE)
VisualServer.viewport_set_vflip(vp, true)
VisualServer.force_draw(false)
viewport_texture = VisualServer.texture_get_data(VisualServer.viewport_get_texture(vp))
VisualServer.free_rid(vp)
VisualServer.free_rid(canvas)
VisualServer.free_rid(ci_rid)
VisualServer.free_rid(mat_rid)
print(viewport_texture.data)
viewport_texture.convert(Image.FORMAT_RGBA8)
print(viewport_texture.get_size())
Global.canvas.handle_undo("Draw")
current_cel.copy_from(viewport_texture)
Global.canvas.handle_redo("Draw")
current_cel.lock()
func _on_ShaderEffect_popup_hide() -> void:
current_cel.lock()
Global.dialog_open(false)
yield(get_tree().create_timer(0.2), "timeout")
preview.texture = null
viewport.size = Vector2.ONE
rect_size = Vector2.ONE
func _on_ChooseShader_pressed() -> void:
@ -54,15 +78,17 @@ func _on_ChooseShader_pressed() -> void:
func _on_FileDialog_file_selected(path : String) -> void:
var shader = load(path)
if !shader is Shader:
var _shader = load(path)
if !_shader is Shader:
return
change_shader(shader, path.get_file().get_basename())
change_shader(_shader, path.get_file().get_basename())
func change_shader(shader : Shader, name : String) -> void:
preview.material.shader = shader
func change_shader(_shader : Shader, name : String) -> void:
shader = _shader
preview.material.shader = _shader
shader_loaded_label.text = tr("Shader loaded:") + " " + name
params.clear()
for child in shader_params.get_children():
child.queue_free()
@ -88,6 +114,7 @@ func change_shader(shader : Shader, name : String) -> void:
var u_init = u_left_side[0].split(" ")
var u_type = u_init[1]
var u_name = u_init[2]
params.append(u_name)
if u_type == "float":
var label := Label.new()

View file

@ -32,58 +32,46 @@ margin_bottom = 31.0
text = "This is an experimental feature and may not be included in the stable version"
autowrap = true
[node name="ViewportContainer" type="ViewportContainer" parent="VBoxContainer"]
[node name="Preview" type="TextureRect" parent="VBoxContainer"]
material = SubResource( 1 )
margin_top = 35.0
margin_right = 384.0
margin_bottom = 36.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Viewport" type="Viewport" parent="VBoxContainer/ViewportContainer"]
size = Vector2( 1, 1 )
transparent_bg = true
handle_input_locally = false
disable_3d = true
usage = 0
render_target_update_mode = 0
gui_disable_input = true
[node name="Preview" type="TextureRect" parent="VBoxContainer/ViewportContainer/Viewport"]
material = SubResource( 1 )
anchor_right = 1.0
anchor_bottom = 1.0
margin_bottom = 235.0
rect_min_size = Vector2( 200, 200 )
expand = true
stretch_mode = 6
[node name="ChooseShader" type="Button" parent="VBoxContainer"]
margin_top = 40.0
margin_top = 239.0
margin_right = 384.0
margin_bottom = 60.0
margin_bottom = 259.0
mouse_default_cursor_shape = 2
text = "Choose Shader"
[node name="ShaderLoadedLabel" type="Label" parent="VBoxContainer"]
margin_top = 64.0
margin_top = 263.0
margin_right = 384.0
margin_bottom = 78.0
margin_bottom = 277.0
text = "No shader loaded!"
autowrap = true
[node name="ShaderParams" type="VBoxContainer" parent="VBoxContainer"]
margin_right = 40.0
margin_bottom = 40.0
margin_top = 281.0
margin_right = 384.0
margin_bottom = 281.0
[node name="FileDialog" type="FileDialog" parent="."]
margin_left = 8.0
margin_top = 8.0
margin_right = 392.0
margin_bottom = 264.0
margin_bottom = 289.0
window_title = "Open a File"
resizable = true
mode = 0
access = 2
filters = PoolStringArray( "*shader" )
current_dir = "/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama"
current_path = "/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama/"
current_dir = "/Users/manos/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama"
current_path = "/Users/manos/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama/"
[connection signal="about_to_show" from="." to="." method="_on_ShaderEffect_about_to_show"]
[connection signal="confirmed" from="." to="." method="_on_ShaderEffect_confirmed"]
[connection signal="popup_hide" from="." to="." method="_on_ShaderEffect_popup_hide"]