Pixel-Composer/scripts/mtl_reader/mtl_reader.gml

50 lines
1.4 KiB
Text
Raw Normal View History

2022-12-12 09:08:03 +01:00
function MTLmaterial(name) constructor {
self.name = name;
self.refc = 0;
2024-05-01 15:35:42 +02:00
self.diff = c_white;
2022-12-12 09:08:03 +01:00
self.spec = 0;
self.refc_path = "";
self.diff_path = "";
self.spec_path = "";
}
2023-01-01 02:06:02 +01:00
function str_strip_nr(str) {
str = string_replace_all(str, "\n", "");
str = string_replace_all(str, "\r", "");
str = string_replace_all(str, "\\", "/");
return str;
}
2022-12-12 09:08:03 +01:00
function readMtl(path) {
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(path)) return [];
2022-12-12 09:08:03 +01:00
var mat = [];
var cur_mat = noone;
var file = file_text_open_read(path);
while(!file_text_eof(file)) {
var l = file_text_readln(file);
l = string_trim(l);
2022-12-12 09:08:03 +01:00
var sep = string_splice(l, " ");
if(array_length(sep) == 0 || sep[0] == "") continue;
switch(sep[0]) {
case "newmtl" :
2023-01-01 02:06:02 +01:00
cur_mat = new MTLmaterial(str_strip_nr(sep[1]));
2022-12-12 09:08:03 +01:00
array_push(mat, cur_mat);
break;
case "Ka" : cur_mat.refc = colorFromRGBArray([sep[1], sep[2], sep[3]]); break;
case "Kd" : cur_mat.diff = colorFromRGBArray([sep[1], sep[2], sep[3]]); break;
case "Ks" : cur_mat.spec = colorFromRGBArray([sep[1], sep[2], sep[3]]); break;
2023-01-01 02:06:02 +01:00
case "map_Ka": cur_mat.refc_path = filename_dir(path) + "/" + str_strip_nr(sep[1]); break;
case "map_Kd": cur_mat.diff_path = filename_dir(path) + "/" + str_strip_nr(sep[1]); break;
case "map_Ks": cur_mat.spec_path = filename_dir(path) + "/" + str_strip_nr(sep[1]); break;
2022-12-12 09:08:03 +01:00
}
}
file_text_close(file);
2022-12-12 09:08:03 +01:00
return mat;
}