Pixel-Composer/scripts/mtl_reader/mtl_reader.gml

48 lines
1.3 KiB
Plaintext
Raw Normal View History

2022-12-12 09:08:03 +01:00
function MTLmaterial(name) constructor {
self.name = name;
self.refc = 0;
self.diff = 0;
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) {
if(!file_exists(path)) return [];
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_replace_all(l, "\n", "");
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
}
}
return mat;
}