material_creator.gd 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. @tool
  2. extends Panel
  3. # In this file, the word "silly" is used to make it obvious that the name is arbitrary.
  4. var silly_material_resource = preload("res://addons/material_creator/material_resource.gd")
  5. var editor_interface
  6. func _ready():
  7. # Connect all of the signals we'll need to save and load silly materials.
  8. get_node(^"VBoxContainer/ApplyButton").pressed.connect(self.apply_pressed)
  9. get_node(^"VBoxContainer/SaveButton").pressed.connect(self.save_pressed)
  10. get_node(^"VBoxContainer/LoadButton").pressed.connect(self.load_pressed)
  11. get_node(^"SaveMaterialDialog").file_selected.connect(self.save_file_selected)
  12. get_node(^"LoadMaterialDialog").file_selected.connect(self.load_file_selected)
  13. RenderingServer.canvas_item_set_clip(get_canvas_item(), true)
  14. func save_pressed():
  15. get_node(^"SaveMaterialDialog").popup_centered()
  16. func load_pressed():
  17. get_node(^"LoadMaterialDialog").popup_centered()
  18. func apply_pressed():
  19. # Using the passed in editor interface, get the selected nodes in the editor.
  20. var editor_selection = editor_interface.get_selection()
  21. var selected_nodes = editor_selection.get_selected_nodes()
  22. if selected_nodes.size() == 0:
  23. printerr("Material Creator: Can't apply the material, because there are no nodes selected!")
  24. var material = _silly_resource_from_values().make_material()
  25. # Go through the selected nodes and see if they have the "set_surface_material"
  26. # function (which only MeshInstance3D has by default). If they do, then set the material
  27. # to the silly material.
  28. for node in selected_nodes:
  29. if node.has_method("set_surface_material"):
  30. node.set_surface_material(0, material)
  31. func save_file_selected(path):
  32. var silly_resource = _silly_resource_from_values()
  33. # Make a file, store the silly material as a JSON string.
  34. var file = FileAccess.open(path, FileAccess.WRITE)
  35. file.store_string(silly_resource.make_json())
  36. return true
  37. func load_file_selected(path):
  38. var SpatialMaterial_Silly = null
  39. # Make a new silly resource (which in this case actually is a node)
  40. # and initialize it.
  41. var silly_resource = silly_material_resource.new()
  42. silly_resource.init()
  43. # If the file exists, then open it.
  44. if FileAccess.file_exists(path):
  45. var file = FileAccess.open(path, FileAccess.READ)
  46. # Get the JSON string and convert it into a silly material.
  47. var json_dict_as_string = file.get_line()
  48. if json_dict_as_string != null:
  49. silly_resource.from_json(json_dict_as_string)
  50. else:
  51. return false
  52. get_node(^"VBoxContainer/AlbedoColorPicker").color = silly_resource.albedo_color
  53. get_node(^"VBoxContainer/MetallicSlider").value = silly_resource.metallic_strength
  54. get_node(^"VBoxContainer/RoughnessSlider").value = silly_resource.roughness_strength
  55. # Return `true` to indicate success.
  56. return true
  57. # If the file does not exist, then return `false` to indicate failure.
  58. return false
  59. func _silly_resource_from_values():
  60. # Get the values from the sliders and color picker.
  61. var color = get_node(^"VBoxContainer/AlbedoColorPicker").color
  62. var metallic = get_node(^"VBoxContainer/MetallicSlider").value
  63. var roughness = get_node(^"VBoxContainer/RoughnessSlider").value
  64. # Make a new silly resource (which in this case actually is a node) and initialize it.
  65. var silly_resource = silly_material_resource.new()
  66. silly_resource.init()
  67. # Assign the values.
  68. silly_resource.albedo_color = color
  69. silly_resource.metallic_strength = metallic
  70. silly_resource.roughness_strength = roughness
  71. return silly_resource