- [3D Mesh] Fix crash when loading node?

This commit is contained in:
Tanasart 2024-05-03 17:18:44 +07:00
parent f8d04e0b75
commit 57951203af
6 changed files with 68 additions and 55 deletions

View file

@ -141,6 +141,7 @@ function LOAD_AT(path, readonly = false, override = false) { #region
try { try {
var _node_list = _load_content.nodes; var _node_list = _load_content.nodes;
for(var i = 0, n = array_length(_node_list); i < n; i++) { for(var i = 0, n = array_length(_node_list); i < n; i++) {
// printIf(log, $" >> Loading nodes : {_node_list[i].type}");
var _node = nodeLoad(_node_list[i]); var _node = nodeLoad(_node_list[i]);
if(_node) ds_list_add(create_list, _node); if(_node) ds_list_add(create_list, _node);
} }

View file

@ -225,7 +225,7 @@ function Node_3D_Mesh_Obj(_x, _y, _group = noone) : Node_3D_Mesh(_x, _y, _group)
return _object; return _object;
} #endregion } #endregion
static getPreviewValues = function() { return array_safe_get_fast(all_inputs, in_mesh + 2, noone); } static getPreviewValues = function() { return array_safe_get_fast(all_inputs, in_mesh + 3, noone); }
static onDrawNodeOver = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region static onDrawNodeOver = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
if(!obj_reading) return; if(!obj_reading) return;

View file

@ -8,7 +8,8 @@ function Node_create_Image(_x, _y, _group = noone) { #region
var node = new Node_Image(_x, _y, _group); var node = new Node_Image(_x, _y, _group);
node.inputs[| 0].setValue(path); node.inputs[| 0].setValue(path);
node.doUpdate(); if(path != "") node.doUpdate();
return node; return node;
} #endregion } #endregion

View file

@ -99,6 +99,7 @@ function NodeObject(_name, _spr, _node, _create, tooltip = "", tags = []) constr
var _buildCon = createNode[0]; var _buildCon = createNode[0];
if(createUseParam) _param = struct_append(_param, createNode[2]); if(createUseParam) _param = struct_append(_param, createNode[2]);
print($" >> building {node}");
var _node = noone; var _node = noone;
if(_buildCon) _node = new createNode[1](_x, _y, _group, _param); if(_buildCon) _node = new createNode[1](_x, _y, _group, _param);
else _node = createNode[1](_x, _y, _group, _param); else _node = createNode[1](_x, _y, _group, _param);

View file

@ -1704,7 +1704,7 @@ function NodeValue(_name, _node, _connect, _type, _value, _tooltip = "") constru
var _val = array_create(array_length(val)); var _val = array_create(array_length(val));
for( var i = 0, n = array_length(val); i < n; i++ ) for( var i = 0, n = array_length(val); i < n; i++ )
_val[i] = def_val.clone(value[i]); _val[i] = def_val.clone(val[i]);
return _val; return _val;
} }

View file

@ -34,7 +34,7 @@ function readObj_file() {
var l = file_text_readln(obj_read_file); var l = file_text_readln(obj_read_file);
l = string_trim(l); l = string_trim(l);
var sep = string_split(l, " "); var sep = string_split(l, " ", true);
if(array_length(sep) == 0) continue; if(array_length(sep) == 0) continue;
switch(sep[0]) { switch(sep[0]) {
@ -146,9 +146,18 @@ function readObj_file() {
function readObj_cent() { function readObj_cent() {
#region centralize vertex #region centralize vertex
_bmin = v[| 0]; var _v0 = v[| 0];
_bmax = v[| 0]; var _bminx = _v0[0];
cv = [0, 0, 0]; var _bminy = _v0[1];
var _bminz = _v0[2];
var _bmaxx = _v0[0];
var _bmaxy = _v0[1];
var _bmaxz = _v0[2];
var cvx = 0;
var cvy = 0;
var cvz = 0;
vertex = ds_list_size(v); vertex = ds_list_size(v);
@ -158,42 +167,35 @@ function readObj_cent() {
var _v1 = _v[1]; var _v1 = _v[1];
var _v2 = _v[2]; var _v2 = _v[2];
cv[0] += _v0; cvx += _v0;
cv[1] += _v1; cvy += _v1;
cv[2] += _v2; cvz += _v2;
_bmin = [ _bminx = min(_bminx, _v0);
min(_bmin[0], _v0), _bminy = min(_bminy, _v1);
min(_bmin[1], _v1), _bminz = min(_bminz, _v2);
min(_bmin[2], _v2),
]; _bmaxx = max(_bmaxx, _v0);
_bmax = [ _bmaxy = max(_bmaxy, _v1);
max(_bmax[0], _v0), _bmaxz = max(_bmaxz, _v2);
max(_bmax[1], _v1),
max(_bmax[2], _v2),
];
} }
cv[0] /= vertex; cvx /= vertex;
cv[1] /= vertex; cvy /= vertex;
cv[2] /= vertex; cvz /= vertex;
obj_size = new __vec3( obj_size = new __vec3(
_bmax[0] - _bmin[0], _bmaxx - _bminx,
_bmax[1] - _bmin[1], _bmaxy - _bminy,
_bmax[2] - _bmin[2], _bmaxz - _bminz,
); );
//print($"{obj_size}"); for( var i = 0; i < vertex; i++ ) {
var _v = v[| i];
var sc = 1; _v[0] = _v[0] - cvx;
//var span = max(abs(_size.x), abs(_size.y), abs(_size.z)); _v[1] = _v[1] - cvy;
//if(span > 10) sc = span / 10; _v[2] = _v[2] - cvz;
for( var i = 0, n = ds_list_size(v); i < n; i++ ) {
v[| i][0] = (v[| i][0] - cv[0]) / sc;
v[| i][1] = (v[| i][1] - cv[1]) / sc;
v[| i][2] = (v[| i][2] - cv[2]) / sc;
} }
#endregion #endregion
@ -238,24 +240,32 @@ function readObj_buff() {
_pft[k] = vt[| _vTindex]; _pft[k] = vt[| _vTindex];
} }
if(_vlen >= 3) { var _pf0 = _pf[0], _pf1 = _pf[1], _pf2 = _pf[2];
vertex_add_pntc(VB, _pf[0], _pfn[0], _pft[0]); var _pfn0 = _pfn[0], _pfn1 = _pfn[1], _pfn2 = _pfn[2];
vertex_add_pntc(VB, _pf[2], _pfn[2], _pft[2]); var _pft0 = _pft[0], _pft1 = _pft[1], _pft2 = _pft[2];
vertex_add_pntc(VB, _pf[1], _pfn[1], _pft[1]);
ds_list_add(_v, new __vertex(_pf[0][0], _pf[0][1], _pf[0][2]).setNormal(_pfn[0][0], _pfn[0][1]).setUV(_pft[0][0], _pft[0][1])); if(_vlen >= 3) {
ds_list_add(_v, new __vertex(_pf[2][0], _pf[2][1], _pf[2][2]).setNormal(_pfn[2][0], _pfn[2][1]).setUV(_pft[2][0], _pft[2][1])); vertex_add_pntc(VB, _pf0, _pfn0, _pft0);
ds_list_add(_v, new __vertex(_pf[1][0], _pf[1][1], _pf[1][2]).setNormal(_pfn[1][0], _pfn[1][1]).setUV(_pft[1][0], _pft[1][1])); vertex_add_pntc(VB, _pf2, _pfn2, _pft2);
vertex_add_pntc(VB, _pf1, _pfn1, _pft1);
ds_list_add(_v, new __vertex(_pf0[0], _pf0[1], _pf0[2]).setNormal(_pfn0[0], _pfn0[1]).setUV(_pft0[0], _pft0[1]));
ds_list_add(_v, new __vertex(_pf2[0], _pf2[1], _pf2[2]).setNormal(_pfn2[0], _pfn2[1]).setUV(_pft2[0], _pft2[1]));
ds_list_add(_v, new __vertex(_pf1[0], _pf1[1], _pf1[2]).setNormal(_pfn1[0], _pfn1[1]).setUV(_pft1[0], _pft1[1]));
} }
if(_vlen >= 4) { if(_vlen >= 4) {
vertex_add_pntc(VB, _pf[0], _pfn[0], _pft[0]); var _pf3 = _pf[3];
vertex_add_pntc(VB, _pf[3], _pfn[3], _pft[3]); var _pfn3 = _pfn[3];
vertex_add_pntc(VB, _pf[2], _pfn[2], _pft[2]); var _pft3 = _pft[3];
ds_list_add(_v, new __vertex(_pf[0][0], _pf[0][1], _pf[0][2]).setNormal(_pfn[0][0], _pfn[0][1]).setUV(_pft[0][0], _pft[0][1])); vertex_add_pntc(VB, _pf0, _pfn0, _pft0);
ds_list_add(_v, new __vertex(_pf[3][0], _pf[3][1], _pf[3][2]).setNormal(_pfn[3][0], _pfn[3][1]).setUV(_pft[3][0], _pft[3][1])); vertex_add_pntc(VB, _pf3, _pfn3, _pft3);
ds_list_add(_v, new __vertex(_pf[2][0], _pf[2][1], _pf[2][2]).setNormal(_pfn[2][0], _pfn[2][1]).setUV(_pft[2][0], _pft[2][1])); vertex_add_pntc(VB, _pf2, _pfn2, _pft2);
ds_list_add(_v, new __vertex(_pf0[0], _pf0[1], _pf0[2]).setNormal(_pfn0[0], _pfn0[1]).setUV(_pft0[0], _pft0[1]));
ds_list_add(_v, new __vertex(_pf3[0], _pf3[1], _pf3[2]).setNormal(_pfn3[0], _pfn3[1]).setUV(_pft3[0], _pft3[1]));
ds_list_add(_v, new __vertex(_pf2[0], _pf2[1], _pf2[2]).setNormal(_pfn2[0], _pfn2[1]).setUV(_pft2[0], _pft2[1]));
} }
} }