material_resource.vala 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public struct MaterialResource
  8. {
  9. public Database _db;
  10. public Guid _id;
  11. public MaterialResource(Database db, Guid id, string shader)
  12. {
  13. _db = db;
  14. _id = id;
  15. _db.create(_id, OBJECT_TYPE_MATERIAL);
  16. _db.set_string(_id, "shader", shader);
  17. }
  18. public MaterialResource.mesh(Database db
  19. , Guid material_id
  20. , string? albedo_map = null
  21. , string? normal_map = null
  22. , string? metallic_map = null
  23. , string? roughness_map = null
  24. , string? ao_map = null
  25. , string? emission_map = null
  26. , Vector3 albedo = VECTOR3_ONE
  27. , double metallic = 0.0
  28. , double roughness = 1.0
  29. , Vector3 emission_color = VECTOR3_ZERO
  30. , double emission_intensity = 1.0
  31. , string? shader = null
  32. )
  33. {
  34. this(db, material_id, shader != null ? shader : "mesh");
  35. set_vector3("u_albedo", albedo);
  36. set_float("u_metallic", metallic);
  37. set_float("u_roughness", roughness);
  38. set_vector3("u_emission_color", emission_color);
  39. set_float("u_emission_intensity", emission_intensity);
  40. if (albedo_map != null) set_texture("u_albedo_map", albedo_map);
  41. if (normal_map != null) set_texture("u_normal_map", normal_map);
  42. if (metallic_map != null) set_texture("u_metallic_map", metallic_map);
  43. if (roughness_map != null) set_texture("u_roughness_map", roughness_map);
  44. if (ao_map != null) set_texture("u_ao_map", ao_map);
  45. if (emission_map != null) set_texture("u_emission_map", emission_map);
  46. set_float("u_use_albedo_map", (double)(albedo_map != null));
  47. set_float("u_use_normal_map", (double)(normal_map != null));
  48. set_float("u_use_metallic_map", (double)(metallic_map != null));
  49. set_float("u_use_roughness_map", (double)(roughness_map != null));
  50. set_float("u_use_ao_map", (double)(ao_map != null));
  51. set_float("u_use_emission_map", (double)(emission_map != null));
  52. }
  53. public MaterialResource.sprite(Database db
  54. , Guid material_id
  55. , string albedo_map
  56. , Quaternion color = Quaternion(1.0, 1.0, 1.0, 1.0)
  57. )
  58. {
  59. this(db, material_id, "sprite");
  60. set_texture("u_albedo_map", albedo_map);
  61. set_vector4("u_color", color);
  62. }
  63. public MaterialResource.gui(Database db, Guid material_id, string? albedo_map)
  64. {
  65. // FIXME: add defines list to ctor?
  66. string shader = "gui";
  67. if (albedo_map != null)
  68. shader += "+DIFFUSE_MAP";
  69. this(db, material_id, shader);
  70. if (albedo_map != null)
  71. set_texture("u_albedo_map", albedo_map);
  72. }
  73. public void set_float(string uniform_name, double value)
  74. {
  75. _db.set_string(_id
  76. , "uniforms.%s.type".printf(uniform_name)
  77. , "float"
  78. );
  79. _db.set_double(_id
  80. , "uniforms.%s.value".printf(uniform_name)
  81. , value
  82. );
  83. }
  84. public void set_vector2(string uniform_name, Vector2 value)
  85. {
  86. _db.set_string(_id
  87. , "uniforms.%s.type".printf(uniform_name)
  88. , "vector2"
  89. );
  90. _db.set_vector3(_id
  91. , "uniforms.%s.value".printf(uniform_name)
  92. , Vector3(value.x, value.y, 0.0)
  93. );
  94. }
  95. public void set_vector3(string uniform_name, Vector3 value)
  96. {
  97. _db.set_string(_id
  98. , "uniforms.%s.type".printf(uniform_name)
  99. , "vector3"
  100. );
  101. _db.set_vector3(_id
  102. , "uniforms.%s.value".printf(uniform_name)
  103. , value
  104. );
  105. }
  106. public void set_vector4(string uniform_name, Quaternion value)
  107. {
  108. _db.set_string(_id
  109. , "uniforms.%s.type".printf(uniform_name)
  110. , "vector4"
  111. );
  112. _db.set_quaternion(_id
  113. , "uniforms.%s.value".printf(uniform_name)
  114. , value
  115. );
  116. }
  117. public void set_texture(string sampler_name, string texture_name)
  118. {
  119. _db.set_string(_id, "textures.%s".printf(sampler_name), texture_name);
  120. }
  121. public int save(Project project, string resource_name)
  122. {
  123. return _db.save(project.absolute_path(resource_name) + "." + OBJECT_TYPE_MATERIAL, _id);
  124. }
  125. }
  126. } /* namespace Crown */