material_resource.gd 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 to_json(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_dict = parse_json(json_dict_as_string)
  30. albedo_color.r = json_dict["albedo_color"]["r"]
  31. albedo_color.g = json_dict["albedo_color"]["g"]
  32. albedo_color.b = json_dict["albedo_color"]["b"]
  33. metallic_strength = json_dict["metallic_strength"]
  34. roughness_strength = json_dict["roughness_strength"]
  35. # Make a SpatialMaterial using our variables.
  36. func make_material():
  37. var mat = SpatialMaterial.new()
  38. mat.albedo_color = albedo_color
  39. mat.metallic = metallic_strength
  40. mat.roughness = roughness_strength
  41. return mat