Skip to content

Commit

Permalink
View/export panel updates in the 2D preview panel
Browse files Browse the repository at this point in the history
+ updated rendering code for export
  • Loading branch information
RodZill4 committed Dec 21, 2024
1 parent f552785 commit 74bf37a
Show file tree
Hide file tree
Showing 12 changed files with 4,248 additions and 4,220 deletions.
26 changes: 8 additions & 18 deletions addons/material_maker/engine/nodes/gen_base.gd
Original file line number Diff line number Diff line change
Expand Up @@ -517,37 +517,27 @@ func generate_output_shader(output_index : int, preview : bool = false):
return { shader=shader, output_type=output_type }

func render(object: Object, output_index : int, size : int, preview : bool = false) -> Object:
print("This function is deprecated!")
var output_shader : Dictionary = generate_output_shader(output_index, preview)
var shader : String = output_shader.shader
var output_type : String = output_shader.output_type
var renderer = await mm_renderer.request(object)
renderer = await renderer.render_shader(object, shader, size, output_type != "rgba")
return renderer

func render_output_to_texture(output_index : int, size : int) -> MMTexture:
func render_output_to_texture(output_index : int, size : Vector2i) -> MMTexture:
var context : MMGenContext = MMGenContext.new()
var source : ShaderCode = get_shader_code("uv", output_index, context)
var shader_compute : MMShaderCompute = MMShaderCompute.new()
var shader_status : bool = await shader_compute.set_shader_from_shadercode(source, false)
var compute_shader : MMComputeShader = MMComputeShader.new()
var shader_status : bool = await compute_shader.set_shader_from_shadercode(source, false)
var texture : MMTexture = MMTexture.new()
if shader_status:
var status = await shader_compute.render(texture, size)
var status = await compute_shader.render_ext([texture], size)
return texture

func render_output(output_index : int, size : int) -> Image:
var context : MMGenContext = MMGenContext.new()
var source : ShaderCode = get_shader_code("uv", output_index, context)
var shader_compute : MMShaderCompute = MMShaderCompute.new()
var shader_status : bool = await shader_compute.set_shader_from_shadercode(source, false)
var image : Image
if shader_status:
var texture : MMTexture = MMTexture.new()
var status = await shader_compute.render(texture, size)
if status:
image = (await texture.get_texture()).get_image()
else:
image = Image.new()
return image
func render_output(output_index : int, size : Vector2i) -> Image:
var texture : MMTexture = await render_output_to_texture(output_index, size)
return (await texture.get_texture()).get_image()

func get_shader_code(uv : String, output_index : int, context : MMGenContext) -> ShaderCode:
var rv = _get_shader_code(uv, output_index, context)
Expand Down
2 changes: 1 addition & 1 deletion addons/material_maker/engine/nodes/gen_material.gd
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func export_material(prefix : String, profile : String, size : int = 0) -> void:
saved_files += 1
progress_dialog.set_progress(float(saved_files)/float(total_files))
continue
var result : MMTexture = await render_output_to_texture(output_index, size)
var result : MMTexture = await render_output_to_texture(output_index, Vector2i(size, size))
await result.save_to_file(file_name)
saved_files += 1
progress_dialog.set_progress(float(saved_files)/float(total_files))
Expand Down
2 changes: 1 addition & 1 deletion material_maker/panels/library/library.gd
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func _on_PopupMenu_index_pressed(index):
var current_node = main_window.get_current_node(main_window.get_current_graph_edit())
if current_node == null:
return
var image : Image = await current_node.generator.render_output(0, 64)
var image : Image = await current_node.generator.render_output(0, Vector2i(64, 64))
library_manager.update_item_icon_in_library(library_index, item_path, image)
2: # Delete item
library_manager.remove_item_from_library(library_index, item_path)
Expand Down
24 changes: 15 additions & 9 deletions material_maker/panels/preview_2d/export_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func _on_export_folder_button_pressed() -> void:
#file_dialog.add_filter("*.exr; EXR image file")

if %ExportFolder.text:
file_dialog.current_dir = mm_globals.config.get_value("path", %ExportFolder.text)
var current_dir = mm_globals.config.get_value("path", %ExportFolder.text)
if current_dir is String:
file_dialog.current_dir = current_dir

var files = await file_dialog.select_files()

Expand All @@ -93,14 +95,22 @@ func _on_resolution_item_selected(index: int) -> void:
mm_globals.set_config(export_settings.resolution, index)
%CustomResolutionSection.visible = index == RESOLUTION_CUSTOM
if index != RESOLUTION_CUSTOM:
%CustomResolution.set_value(64 << index)
%CustomResolutionX.set_value(64 << index)
%CustomResolutionY.set_value(64 << index)


func get_export_resolution() -> Vector2i:
if %Resolution.selected == RESOLUTION_CUSTOM:
return Vector2i(int(%CustomResolutionX.value), int(%CustomResolutionY.value))
else:
var export_size : int = 64 << %Resolution.selected
return Vector2i(export_size, export_size)


func _on_image_pressed() -> void:
var path: String = %ExportFolder.text
var file_name: String = %ExportFile.text


if file_name:
file_name = interpret_file_name(file_name, path)

Expand All @@ -125,16 +135,12 @@ func _on_image_pressed() -> void:
if files.size() > 0:
path = files[0]


if file_name:
path = path.path_join(file_name)


if path:
if %Resolution.selected == RESOLUTION_CUSTOM:
owner.export_as_image_file(path, %CustomResolution.get_value())
else:
owner.export_as_image_file(path, 64 << %Resolution.selected)
owner.export_as_image_file(path, get_export_resolution())
export_notification("Exported to " + path)
await get_tree().create_timer(0.5).timeout
update()
Expand All @@ -149,7 +155,7 @@ func _on_taa_render_pressed() -> void:


func _on_reference_pressed() -> void:
owner.export_to_reference(%Resolution.selected)
owner.export_to_reference(get_export_resolution())
export_notification("Exported to Reference")


Expand Down
36 changes: 10 additions & 26 deletions material_maker/panels/preview_2d/preview_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -125,40 +125,24 @@ func export_taa() -> void:
window.popup_centered()


func create_image(renderer_function : String, params : Array, image_size : int) -> void:
var source = MMGenBase.get_default_generated_shader()
func create_image(renderer_function : String, params : Array, image_size : Vector2i) -> void:
if generator != null:
var gen_output_defs = generator.get_output_defs()
if ! gen_output_defs.is_empty():
var context : MMGenContext = MMGenContext.new()
source = generator.get_shader_code("uv", output, context)
if source.output_type == "":
source = MMGenBase.get_default_generated_shader()
# Update shader
var tmp_material = ShaderMaterial.new()
var shader : Shader = Shader.new()
shader.code = MMGenBase.generate_preview_shader(source, source.output_type, "uniform vec2 mm_texture_size;\nuniform float mm_chunk_size = 1.0;\nuniform vec2 mm_chunk_offset = vec2(0.0);\nvoid fragment() {COLOR = preview_2d(mm_chunk_offset+mm_chunk_size*UV);}")
tmp_material.shader = shader
for u in source.uniforms:
tmp_material.set_shader_parameter(u.name, u.value)
mm_deps.material_update_params(MMShaderMaterial.new(tmp_material))
var renderer = await mm_renderer.request(self)
renderer = await renderer.render_material(self, tmp_material, image_size, source.output_type != "rgba")
renderer.callv(renderer_function, params)
renderer.release(self)
var texture : MMTexture = await generator.render_output_to_texture(output, image_size)


func export_as_image_file(file_name : String, image_size : int) -> void:
func export_as_image_file(file_name : String, image_size : Vector2i) -> void:
mm_globals.config.set_value("path", "save_preview", file_name.get_base_dir())
create_image("save_to_file", [ file_name, is_greyscale ], image_size)
if generator != null:
var texture : MMTexture = await generator.render_output_to_texture(output, image_size)
await texture.save_to_file(file_name)
last_export_filename = file_name
last_export_size = image_size


func export_to_reference(resolution_id : int):
var texture : ImageTexture = ImageTexture.new()
await create_image("copy_to_texture", [ texture ], 64 << resolution_id)
mm_globals.main_window.get_panel("Reference").add_reference(texture)
func export_to_reference(image_size : Vector2i):
if generator != null:
var texture : MMTexture = await generator.render_output_to_texture(output, image_size)
mm_globals.main_window.get_panel("Reference").add_reference(await texture.get_texture())


func _on_Preview2D_visibility_changed():
Expand Down
Loading

0 comments on commit 74bf37a

Please sign in to comment.