Skip to content
53 changes: 15 additions & 38 deletions plugins/addons/material_creator/material_creator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,53 +41,30 @@ func apply_pressed() -> void:


func save_file_selected(path: String) -> bool:
var silly_resource: Variant = _silly_resource_from_values()
# Make a file, store the silly material as a JSON string.
var file := FileAccess.open(path, FileAccess.WRITE)
file.store_string(silly_resource.make_json())

return true
var silly_resource: Resource = _silly_resource_from_values()
# Save the resource as a .tres file using Godot's ResourceSaver.
return ResourceSaver.save(silly_resource, path) == OK


func load_file_selected(path: String) -> bool:
var SpatialMaterial_Silly: StandardMaterial3D = null

# Make a new silly resource (which in this case actually is a node)
# and initialize it.
var silly_resource: Variant = silly_material_resource.new()
#silly_resource.init()

# If the file exists, then open it.
if FileAccess.file_exists(path):
var file := FileAccess.open(path, FileAccess.READ)

# Get the JSON string and convert it into a silly material.
var json_dict_as_string := file.get_line()
if json_dict_as_string != null:
silly_resource.from_json(json_dict_as_string)
else:
return false

$VBoxContainer/AlbedoColorPicker.color = silly_resource.albedo_color
$VBoxContainer/MetallicSlider.value = silly_resource.metallic_strength
$VBoxContainer/RoughnessSlider.value = silly_resource.roughness_strength

# Return `true` to indicate success.
return true

# If the file does not exist, then return `false` to indicate failure.
return false
# Load the resource using Godot's ResourceLoader.
var silly_resource: Resource = ResourceLoader.load(path)
if silly_resource == null:
return false

$VBoxContainer/AlbedoColorPicker.color = silly_resource.albedo_color
$VBoxContainer/MetallicSlider.value = silly_resource.metallic_strength
$VBoxContainer/RoughnessSlider.value = silly_resource.roughness_strength
return true


func _silly_resource_from_values() -> Variant:
func _silly_resource_from_values() -> Resource:
# Get the values from the sliders and color picker.
var color: Color = $VBoxContainer/AlbedoColorPicker.color
var metallic: float = $VBoxContainer/MetallicSlider.value
var roughness: float = $VBoxContainer/RoughnessSlider.value
# Make a new silly resource (which in this case actually is a node) and initialize it.
var silly_resource: Variant = silly_material_resource.new()
#silly_resource.init()

# Make a new silly resource (now a Resource, not a Node).
var silly_resource: Resource = silly_material_resource.new()
# Assign the values.
silly_resource.albedo_color = color
silly_resource.metallic_strength = metallic
Expand Down
26 changes: 11 additions & 15 deletions plugins/addons/material_creator/material_resource.gd
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
@tool
extends Node
# NOTE: In theory, this would extend from Resource, but until saving and loading resources
# works in Godot, we'll stick with extending from Node and using JSON files to save/load data.
#
# See `material_import.gd` for more information.
extends Resource

var albedo_color := Color.BLACK
var metallic_strength := 0.0
var roughness_strength := 0.0
# Use export to make properties visible and serializable in the inspector and for resource saving/loading.
@export var albedo_color: Color = Color.BLACK
@export var metallic_strength: float = 0.0
@export var roughness_strength: float = 0.0


# Create a StandardMaterial3D from the resource's properties.
# Convert our data into an dictionary so we can convert it
# into the JSON format.
func make_json() -> String:
Expand Down Expand Up @@ -41,10 +39,8 @@ func from_json(json_dict_as_string: String) -> void:

# Make a StandardMaterial3D using our variables.
func make_material() -> StandardMaterial3D:
var material := StandardMaterial3D.new()

material.albedo_color = albedo_color
material.metallic = metallic_strength
material.roughness = roughness_strength

return material
var mat = StandardMaterial3D.new()
mat.albedo_color = albedo_color
mat.metallic = metallic_strength
mat.roughness = roughness_strength
return mat