material_creator.gd 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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").connect("pressed", self, "apply_pressed")
  9. get_node("VBoxContainer/SaveButton").connect("pressed", self, "save_pressed")
  10. get_node("VBoxContainer/LoadButton").connect("pressed", self, "load_pressed")
  11. get_node("SaveMaterialDialog").connect("file_selected", self, "save_file_selected")
  12. get_node("LoadMaterialDialog").connect("file_selected", self, "load_file_selected")
  13. VisualServer.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 MeshInstance 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, then close the file.
  34. var file = File.new()
  35. file.open(path, File.WRITE)
  36. file.store_string(silly_resource.make_json())
  37. file.close()
  38. return true
  39. func load_file_selected(path):
  40. var file = File.new()
  41. var SpatialMaterial_Silly = null
  42. # Make a new silly resource (which in this case actually is a node)
  43. # and initialize it
  44. var silly_resource = silly_material_resource.new()
  45. silly_resource.init()
  46. # If the file exists, then open it
  47. if file.file_exists(path):
  48. file.open(path, File.READ)
  49. # Get the JSON string and convert it into a silly material.
  50. var json_dict_as_string = file.get_line()
  51. if json_dict_as_string != null:
  52. silly_resource.from_json(json_dict_as_string)
  53. else:
  54. file.close()
  55. return false
  56. get_node("VBoxContainer/AlbedoColorPicker").color = silly_resource.albedo_color
  57. get_node("VBoxContainer/MetallicSlider").value = silly_resource.metallic_strength
  58. get_node("VBoxContainer/RoughnessSlider").value = silly_resource.roughness_strength
  59. # Close the file and return true (success!)
  60. file.close()
  61. return true
  62. #else: If the file does not exist, then return false (failure)
  63. return false
  64. func _silly_resource_from_values():
  65. # Get the values from the sliders and color picker
  66. var color = get_node("VBoxContainer/AlbedoColorPicker").color
  67. var metallic = get_node("VBoxContainer/MetallicSlider").value
  68. var roughness = get_node("VBoxContainer/RoughnessSlider").value
  69. # Make a new silly resource (which in this case actually is a node) and initialize it
  70. var silly_resource = silly_material_resource.new()
  71. silly_resource.init()
  72. # Assign the values
  73. silly_resource.albedo_color = color
  74. silly_resource.metallic_strength = metallic
  75. silly_resource.roughness_strength = roughness
  76. return silly_resource