material_data.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. let _material_data_uid_counter: i32 = 0;
  2. function material_data_create(raw: material_data_t, file: string = ""): material_data_t {
  3. raw._ = {};
  4. raw._.uid = ++_material_data_uid_counter; // Start from 1
  5. let ref: string[] = string_split(raw.shader, "/");
  6. let object_file: string = "";
  7. let data_ref: string = "";
  8. if (ref.length == 2) { // File reference
  9. object_file = ref[0];
  10. data_ref = ref[1];
  11. }
  12. else { // Local data
  13. object_file = file;
  14. data_ref = raw.shader;
  15. }
  16. let b: shader_data_t = data_get_shader(object_file, data_ref);
  17. raw._.shader = b;
  18. for (let i: i32 = 0; i < raw.contexts.length; ++i) {
  19. let c: material_context_t = raw.contexts[i];
  20. material_context_load(c);
  21. }
  22. return raw;
  23. }
  24. function material_data_parse(file: string, name: string): material_data_t {
  25. let format: scene_t = data_get_scene_raw(file);
  26. let raw: material_data_t = material_data_get_raw_by_name(format.material_datas, name);
  27. if (raw == null) {
  28. iron_log("Material data '" + name + "' not found!");
  29. return null;
  30. }
  31. return material_data_create(raw, file);
  32. }
  33. function material_data_get_raw_by_name(datas: material_data_t[], name: string): material_data_t {
  34. if (name == "") {
  35. return datas[0];
  36. }
  37. for (let i: i32 = 0; i < datas.length; ++i) {
  38. if (datas[i].name == name) {
  39. return datas[i];
  40. }
  41. }
  42. return null;
  43. }
  44. function material_data_get_context(raw: material_data_t, name: string): material_context_t {
  45. for (let i: i32 = 0; i < raw.contexts.length; ++i) {
  46. let c: material_context_t = raw.contexts[i];
  47. if (c.name == name) {
  48. return c;
  49. }
  50. }
  51. return null;
  52. }
  53. function material_context_load(raw: material_context_t) {
  54. raw._ = {};
  55. if (raw.bind_textures != null && raw.bind_textures.length > 0) {
  56. raw._.textures = [];
  57. for (let i: i32 = 0; i < raw.bind_textures.length; ++i) {
  58. let tex: bind_tex_t = raw.bind_textures[i];
  59. if (tex.file == "") { // Empty texture
  60. continue;
  61. }
  62. let image: gpu_texture_t = data_get_image(tex.file);
  63. array_push(raw._.textures, image);
  64. }
  65. }
  66. }