diff --git a/addons/material_maker/engine/gen_base.gd b/addons/material_maker/engine/gen_base.gd index 67d0a52..25f05c8 100644 --- a/addons/material_maker/engine/gen_base.gd +++ b/addons/material_maker/engine/gen_base.gd @@ -7,6 +7,7 @@ Base class for texture generators, that defines their API """ signal parameter_changed +signal update_textures class InputPort: var generator : MMGenBase = null diff --git a/addons/material_maker/engine/gen_material.gd b/addons/material_maker/engine/gen_material.gd index 83a12f2..6f8559d 100644 --- a/addons/material_maker/engine/gen_material.gd +++ b/addons/material_maker/engine/gen_material.gd @@ -25,6 +25,12 @@ const ADDON_TEXTURE_LIST = [ { port=6, texture="depth_texture" } ] +func _ready(): + texture_list = TEXTURE_LIST + for t in texture_list: + generated_textures[t.texture] = null + material = SpatialMaterial.new() + func get_type(): return "material" @@ -53,23 +59,8 @@ func get_input_defs(): { name="depth_texture", label="", type="f" } ] -func _ready(): - texture_list = TEXTURE_LIST - if Engine.editor_hint: - texture_list = ADDON_TEXTURE_LIST - for t in texture_list: - generated_textures[t.texture] = null - material = SpatialMaterial.new() - -func generate_material(renderer : MMGenRenderer): - var source = get_source(0) - if source != null: - var status = source.generator.render(source.output_index, renderer, 512) - while status is GDScriptFunctionState: - status = yield(status, "completed") - renderer.get_texture().get_data().save_png("res://test.png") - material.albedo_texture = load("res://test.png") - return material +func source_changed(input_index : int): + emit_signal("update_textures") func render_textures(renderer : MMGenRenderer): for t in texture_list: @@ -77,7 +68,7 @@ func render_textures(renderer : MMGenRenderer): var source = get_source(t.port) var texture = null if source != null: - var status = source.generator.render(source.output_index, renderer, 512) + var status = source.generator.render(source.output_index, renderer, 1024) while status is GDScriptFunctionState: status = yield(status, "completed") texture = ImageTexture.new() @@ -104,7 +95,7 @@ func update_spatial_material(m, file_prefix = null): m.albedo_texture = get_generated_texture("albedo", file_prefix) m.metallic = parameters.metallic m.roughness = parameters.roughness - if Engine.editor_hint: + if false: texture = get_generated_texture("mrao", file_prefix) m.metallic_texture = texture m.metallic_texture_channel = SpatialMaterial.TEXTURE_CHANNEL_RED @@ -126,7 +117,7 @@ func update_spatial_material(m, file_prefix = null): m.normal_texture = texture else: m.normal_enabled = false - if Engine.editor_hint: + if false: if (generated_textures.mrao.mask & (1 << 2)) != 0: m.ao_enabled = true m.ao_light_affect = parameters.ao_light_affect @@ -151,22 +142,21 @@ func update_spatial_material(m, file_prefix = null): else: m.depth_enabled = false -func export_textures(prefix, size = null): - if size == null: - size = int(pow(2, 8+parameters.resolution)) +func export_textures(prefix, editor_interface = null): for t in texture_list: var texture = generated_textures[t.texture] if texture != null: var image = texture.get_data() image.save_png("%s_%s.png" % [ prefix, t.texture ]) if Engine.editor_hint: - var resource_filesystem = get_parent().editor_interface.get_resource_filesystem() + var resource_filesystem = editor_interface.get_resource_filesystem() resource_filesystem.scan() yield(resource_filesystem, "filesystem_changed") var new_material = SpatialMaterial.new() update_spatial_material(new_material, prefix) ResourceSaver.save("%s.tres" % [ prefix ], new_material) resource_filesystem.scan() + return new_material func _serialize(data): return data diff --git a/addons/material_maker/main_window.gd b/addons/material_maker/main_window.gd index 7b59e1b..dab2864 100644 --- a/addons/material_maker/main_window.gd +++ b/addons/material_maker/main_window.gd @@ -363,3 +363,9 @@ func _on_Projects_tab_changed(tab): new_tab.connect("node_selected", self, "update_preview_2d") current_tab = new_tab update_preview() + +func _on_Preview_show_background_preview(v): + var pv = $VBoxContainer/HBoxContainer/VBoxContainer/Preview/MaterialPreview + var bgpv = $VBoxContainer/HBoxContainer/ProjectsPane/BackgroundPreview/Viewport + bgpv.world = pv.find_world() + $VBoxContainer/HBoxContainer/ProjectsPane/BackgroundPreview.visible = v diff --git a/addons/material_maker/main_window.tscn b/addons/material_maker/main_window.tscn index 2fbe176..7c4c659 100644 --- a/addons/material_maker/main_window.tscn +++ b/addons/material_maker/main_window.tscn @@ -100,7 +100,7 @@ handle_input_locally = false render_target_update_mode = 0 [node name="Camera" type="Camera" parent="VBoxContainer/HBoxContainer/ProjectsPane/BackgroundPreview/Viewport"] -transform = Transform( 1, 0, 0, 0, 0.766044, 0.642788, 0, -0.642788, 0.766044, 0, 2.24976, 2.68116 ) +transform = Transform( 1, 0, 0, 0, 0.766044, 0.642788, 0, -0.642788, 0.766044, 0, 2.24976, 2.68115 ) current = true [node name="Projects" type="Panel" parent="VBoxContainer/HBoxContainer/ProjectsPane"] @@ -123,7 +123,7 @@ debug_path = null [node name="NodeFactory" type="Node" parent="."] script = ExtResource( 6 ) [connection signal="need_update" from="VBoxContainer/HBoxContainer/VBoxContainer/Preview" to="." method="update_preview_3d"] -[connection signal="show_background_preview" from="VBoxContainer/HBoxContainer/VBoxContainer/Preview" to="VBoxContainer/HBoxContainer/ProjectsPane/BackgroundPreview" method="set_visible"] +[connection signal="show_background_preview" from="VBoxContainer/HBoxContainer/VBoxContainer/Preview" to="." method="_on_Preview_show_background_preview"] [connection signal="no_more_tabs" from="VBoxContainer/HBoxContainer/ProjectsPane/Projects" to="." method="new_material"] [connection signal="resized" from="VBoxContainer/HBoxContainer/ProjectsPane/Projects" to="VBoxContainer/HBoxContainer/ProjectsPane/Projects" method="_on_Projects_resized"] [connection signal="tab_changed" from="VBoxContainer/HBoxContainer/ProjectsPane/Projects" to="." method="_on_Projects_tab_changed"] diff --git a/addons/material_maker/nodes/generic.gd b/addons/material_maker/nodes/generic.gd index 4743048..722d5f4 100644 --- a/addons/material_maker/nodes/generic.gd +++ b/addons/material_maker/nodes/generic.gd @@ -102,7 +102,7 @@ func create_parameter_control(p : Dictionary): if p.has("widget") and p.widget == "spinbox": control = SpinBox.new() else: - control = HSlider.new() + control = preload("res://addons/material_maker/widgets/hslider.tscn").instance() control.min_value = p.min control.max_value = p.max control.step = 0.005 if !p.has("step") else p.step diff --git a/addons/material_maker/plugin.gd b/addons/material_maker/plugin.gd index bcb1b25..e4644c9 100644 --- a/addons/material_maker/plugin.gd +++ b/addons/material_maker/plugin.gd @@ -49,8 +49,12 @@ func close_material_maker(): func generate_material(ptex_filename: String) -> Material: var generator = MMGenLoader.load_gen(ptex_filename) add_child(generator) - var material = generator.get_node("Material") - var return_value = material.generate_material(renderer) - while return_value is GDScriptFunctionState: - return_value = yield(return_value, "completed") - return return_value + if generator.has_node("Material"): + var gen_material = generator.get_node("Material") + print(renderer) + var return_value = gen_material.render_textures(renderer) + while return_value is GDScriptFunctionState: + return_value = yield(return_value, "completed") + var prefix = ptex_filename.left(ptex_filename.rfind(".")) + return gen_material.export_textures(prefix, get_editor_interface()) + return null diff --git a/addons/material_maker/preview.gd b/addons/material_maker/preview.gd index 243bf33..4929e13 100644 --- a/addons/material_maker/preview.gd +++ b/addons/material_maker/preview.gd @@ -7,7 +7,7 @@ const ENVIRONMENTS = [ "experiment", "lobby", "night", "park", "schelde" ] -onready var objects = $MaterialPreview/Objects +onready var objects = $MaterialPreview/Preview3d/Objects onready var current_object = objects.get_child(0) signal need_update @@ -20,14 +20,14 @@ func _ready(): var m = o.get_surface_material(0) o.set_surface_material(0, m.duplicate()) $Config/Model.add_item(o.name) - $ObjectRotate.play("rotate") + $MaterialPreview/Preview3d/ObjectRotate.play("rotate") $Preview2D.material = $Preview2D.material.duplicate(true) _on_Environment_item_selected($Config/Environment.selected) _on_Preview_resized() - $MaterialPreview/CameraPivot/Camera/RemoteTransform.set_remote_node("../../../../../../ProjectsPane/BackgroundPreview/Viewport/Camera") + $MaterialPreview/Preview3d/CameraPivot/Camera/RemoteTransform.set_remote_node("../../../../../../../ProjectsPane/BackgroundPreview/Viewport/Camera") func _on_Environment_item_selected(id): - $MaterialPreview/WorldEnvironment.environment.background_sky.panorama = load("res://addons/material_maker/panoramas/"+ENVIRONMENTS[id]+".hdr") + $MaterialPreview/Preview3d/WorldEnvironment.environment.background_sky.panorama = load("res://addons/material_maker/panoramas/"+ENVIRONMENTS[id]+".hdr") func _on_Model_item_selected(id): current_object.visible = false @@ -60,8 +60,8 @@ func _on_Button_toggled(button_pressed): func on_gui_input(event): if event is InputEventMouseButton: - $ObjectRotate.stop() + $MaterialPreview/Preview3d/ObjectRotate.stop() elif event is InputEventMouseMotion: if event.button_mask != 0: - $MaterialPreview/Objects.rotation.y += 0.01*event.relative.x - $MaterialPreview/CameraPivot.rotation.x -= 0.01*event.relative.y + $MaterialPreview/Preview3d/Objects.rotation.y += 0.01*event.relative.x + $MaterialPreview/Preview3d/CameraPivot.rotation.x -= 0.01*event.relative.y diff --git a/addons/material_maker/preview.tscn b/addons/material_maker/preview.tscn index ad55b76..52e1d75 100644 --- a/addons/material_maker/preview.tscn +++ b/addons/material_maker/preview.tscn @@ -1,33 +1,11 @@ -[gd_scene load_steps=9 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://addons/material_maker/preview.gd" type="Script" id=1] -[ext_resource path="res://addons/material_maker/preview_objects.tscn" type="PackedScene" id=2] -[ext_resource path="res://addons/material_maker/panoramas/park.hdr" type="Texture" id=3] +[ext_resource path="res://addons/material_maker/preview_3d.tscn" type="PackedScene" id=2] -[sub_resource type="Animation" id=1] -loop = true -tracks/0/type = "value" -tracks/0/path = NodePath("MaterialPreview/Objects:rotation_degrees") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/keys = { -"times": PoolRealArray( 0, 1 ), -"transitions": PoolRealArray( 1, 1 ), -"update": 0, -"values": [ Vector3( 0, 0, 0 ), Vector3( 0, 360, 0 ) ] -} +[sub_resource type="World" id=4] -[sub_resource type="PanoramaSky" id=2] -radiance_size = 2 -panorama = ExtResource( 3 ) - -[sub_resource type="Environment" id=3] -background_mode = 2 -background_sky = SubResource( 2 ) - -[sub_resource type="Shader" id=4] +[sub_resource type="Shader" id=2] code = "shader_type canvas_item; uniform sampler2D tex; @@ -36,8 +14,8 @@ void fragment() { COLOR = texture(tex, UV); }" -[sub_resource type="ShaderMaterial" id=5] -shader = SubResource( 4 ) +[sub_resource type="ShaderMaterial" id=3] +shader = SubResource( 2 ) [node name="Preview" type="ViewportContainer"] anchor_left = 1.0 @@ -52,36 +30,15 @@ size_flags_vertical = 3 stretch = true script = ExtResource( 1 ) -[node name="ObjectRotate" type="AnimationPlayer" parent="."] -autoplay = "rotate" -playback_speed = 0.1 -anims/rotate = SubResource( 1 ) - [node name="MaterialPreview" type="Viewport" parent="."] size = Vector2( 395, 370 ) +own_world = true +world = SubResource( 4 ) handle_input_locally = false render_target_clear_mode = 1 render_target_update_mode = 3 -[node name="Objects" parent="MaterialPreview" instance=ExtResource( 2 )] -transform = Transform( 0.768253, 0, -0.640147, 0, 1, 0, 0.640147, 0, 0.768253, 0, 0, 0 ) - -[node name="OmniLight" type="OmniLight" parent="MaterialPreview"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.04729, 1.80471, -2.51024 ) -omni_range = 6.46518 - -[node name="WorldEnvironment" type="WorldEnvironment" parent="MaterialPreview"] -environment = SubResource( 3 ) - -[node name="CameraPivot" type="Spatial" parent="MaterialPreview"] -transform = Transform( 1, 0, 0, 0, 0.766044, 0.642788, 0, -0.642788, 0.766044, 0, 0, 0 ) - -[node name="Camera" type="Camera" parent="MaterialPreview/CameraPivot"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3.5 ) -environment = SubResource( 3 ) -current = true - -[node name="RemoteTransform" type="RemoteTransform" parent="MaterialPreview/CameraPivot/Camera"] +[node name="Preview3d" parent="MaterialPreview" instance=ExtResource( 2 )] [node name="Config" type="HBoxContainer" parent="."] anchor_right = 1.0 @@ -115,7 +72,7 @@ toggle_mode = true text = "O" [node name="Preview2D" type="ColorRect" parent="."] -material = SubResource( 5 ) +material = SubResource( 3 ) anchor_top = 1.0 anchor_bottom = 1.0 margin_top = -64.0 diff --git a/addons/material_maker/preview_3d.tscn b/addons/material_maker/preview_3d.tscn new file mode 100644 index 0000000..7b06440 --- /dev/null +++ b/addons/material_maker/preview_3d.tscn @@ -0,0 +1,55 @@ +[gd_scene load_steps=6 format=2] + +[ext_resource path="res://addons/material_maker/preview_objects.tscn" type="PackedScene" id=1] +[ext_resource path="res://addons/material_maker/panoramas/park.hdr" type="Texture" id=2] + +[sub_resource type="PanoramaSky" id=1] +radiance_size = 2 +panorama = ExtResource( 2 ) + +[sub_resource type="Environment" id=2] +background_mode = 2 +background_sky = SubResource( 1 ) + +[sub_resource type="Animation" id=3] +loop = true +tracks/0/type = "value" +tracks/0/path = NodePath("MaterialPreview/Spatial/Objects:rotation_degrees") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0, 1 ), +"transitions": PoolRealArray( 1, 1 ), +"update": 0, +"values": [ Vector3( 0, 0, 0 ), Vector3( 0, 360, 0 ) ] +} + +[node name="Preview3d" type="Spatial"] + +[node name="Objects" type="Spatial" parent="." instance=ExtResource( 1 )] +transform = Transform( -0.799512, 0, 0.60065, 0, 1, 0, -0.60065, 0, -0.799512, 0, 0, 0 ) + +[node name="OmniLight" type="OmniLight" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.345557, 2.74973, -3.97441 ) +omni_range = 6.46518 + +[node name="WorldEnvironment" type="WorldEnvironment" parent="."] +environment = SubResource( 2 ) + +[node name="CameraPivot" type="Spatial" parent="."] +transform = Transform( 1, 0, 0, 0, 0.766044, 0.642788, 0, -0.642788, 0.766044, 0, 0, 0 ) + +[node name="Camera" type="Camera" parent="CameraPivot"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3.5 ) +environment = SubResource( 2 ) +current = true + +[node name="RemoteTransform" type="RemoteTransform" parent="CameraPivot/Camera"] +remote_path = NodePath("../../../../../../ProjectsPane/BackgroundPreview/Viewport/Camera") + +[node name="ObjectRotate" type="AnimationPlayer" parent="."] +autoplay = "rotate" +playback_speed = 0.1 +anims/rotate = SubResource( 3 ) diff --git a/addons/material_maker/preview_world.tres b/addons/material_maker/preview_world.tres new file mode 100644 index 0000000..0488b02 --- /dev/null +++ b/addons/material_maker/preview_world.tres @@ -0,0 +1,3 @@ +[gd_resource type="World" format=2] + +[resource] diff --git a/addons/material_maker/widgets/hslider.gd b/addons/material_maker/widgets/hslider.gd new file mode 100644 index 0000000..1c2b16e --- /dev/null +++ b/addons/material_maker/widgets/hslider.gd @@ -0,0 +1,12 @@ +tool +extends HSlider + +func _ready(): + update_label(value) + +func set_value(v): + .set_value(v) + update_label(v) + +func update_label(v): + $Label.text = "%f" % v \ No newline at end of file diff --git a/addons/material_maker/widgets/hslider.tscn b/addons/material_maker/widgets/hslider.tscn new file mode 100644 index 0000000..62b0d4f --- /dev/null +++ b/addons/material_maker/widgets/hslider.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://addons/material_maker/widgets/hslider.gd" type="Script" id=1] + +[node name="MMHSlider" type="HSlider"] +margin_left = 1.0 +margin_top = 1.0 +margin_right = 134.0 +margin_bottom = 17.0 +ticks_on_borders = true +script = ExtResource( 1 ) + +[node name="Label" type="Label" parent="."] +self_modulate = Color( 1, 1, 1, 0.501961 ) +anchor_right = 1.0 +anchor_bottom = 1.0 +text = "100" +align = 1 +valign = 1 +__meta__ = { +"_edit_lock_": true +} +[connection signal="value_changed" from="." to="." method="update_label"]