material_resource.gd 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. @tool
  2. extends Node
  3. # NOTE: in theory this would extend from resource, but until saving and loading resources
  4. # works in godot, we'll stick with extending from node
  5. # and using JSON files to save/load data
  6. #
  7. # See material_import.gd for more information
  8. var albedo_color
  9. var metallic_strength
  10. var roughness_strength
  11. func init():
  12. albedo_color = Color()
  13. metallic_strength = 0
  14. roughness_strength = 0
  15. # Convert our data into an dictonary so we can convert it
  16. # into the JSON format
  17. func make_json():
  18. var json_dict = {}
  19. json_dict["albedo_color"] = {}
  20. json_dict["albedo_color"]["r"] = albedo_color.r
  21. json_dict["albedo_color"]["g"] = albedo_color.g
  22. json_dict["albedo_color"]["b"] = albedo_color.b
  23. json_dict["metallic_strength"] = metallic_strength
  24. json_dict["roughness_strength"] = roughness_strength
  25. return JSON.new().stringify(json_dict)
  26. # Convert the passed in string to a json dictonary, and then
  27. # fill in our data.
  28. func from_json(json_dict_as_string):
  29. var json = JSON.new()
  30. json.parse(json_dict_as_string)
  31. var json_dict = json.get_data()
  32. albedo_color.r = json_dict["albedo_color"]["r"]
  33. albedo_color.g = json_dict["albedo_color"]["g"]
  34. albedo_color.b = json_dict["albedo_color"]["b"]
  35. metallic_strength = json_dict["metallic_strength"]
  36. roughness_strength = json_dict["roughness_strength"]
  37. # Make a StandardMaterial3D using our variables.
  38. func make_material():
  39. var mat = StandardMaterial3D.new()
  40. mat.albedo_color = albedo_color
  41. mat.metallic = metallic_strength
  42. mat.roughness = roughness_strength
  43. return mat