Pixel-Composer/scripts/node_3d_extrude/node_3d_extrude.gml

308 lines
10 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_3D_Extrude(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2022-12-12 09:08:03 +01:00
name = "3D Extrude";
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, noone);
2022-12-12 09:08:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 1] = nodeValue("Dimension", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, def_surf_size2)
2022-12-12 09:08:03 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("Object position", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 0, 0 ])
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2022-12-12 09:08:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 3] = nodeValue("Object rotation", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 180, 0 ])
2022-12-12 09:08:03 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-02-14 05:32:32 +01:00
inputs[| 4] = nodeValue("Object scale", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1, 0.1 ])
2022-12-12 09:08:03 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-01-01 02:06:02 +01:00
2023-05-03 21:42:17 +02:00
inputs[| 5] = nodeValue("Render position", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0.5, 0.5 ])
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.vector)
2023-05-03 21:42:17 +02:00
.setUnitRef( function() { return inputs[| 1].getValue(); }, VALUE_UNIT.reference);
2022-12-12 09:08:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 6] = nodeValue("Render rotation", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 0, 0 ])
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-02-14 05:32:32 +01:00
inputs[| 7] = nodeValue("Render scale", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1 ])
2022-12-12 09:08:03 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-03-28 06:58:28 +02:00
inputs[| 8] = nodeValue("Manual generate", self, JUNCTION_CONNECT.input, VALUE_TYPE.trigger, 0)
2022-12-21 02:30:23 +01:00
.setDisplay(VALUE_DISPLAY.button, [ function() {
2022-12-12 09:08:03 +01:00
generateMesh();
2023-03-28 06:58:28 +02:00
doUpdate();
2022-12-12 09:08:03 +01:00
}, "Generate"] );
2023-02-14 05:32:32 +01:00
inputs[| 9] = nodeValue("Light direction", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0)
2022-12-12 09:08:03 +01:00
.setDisplay(VALUE_DISPLAY.rotation);
2023-02-14 05:32:32 +01:00
inputs[| 10] = nodeValue("Light height", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0.5)
2022-12-12 09:08:03 +01:00
.setDisplay(VALUE_DISPLAY.slider, [-1, 1, 0.01]);
2023-02-14 05:32:32 +01:00
inputs[| 11] = nodeValue("Light intensity", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 1)
2022-12-12 09:08:03 +01:00
.setDisplay(VALUE_DISPLAY.slider, [0, 1, 0.01]);
2023-02-14 05:32:32 +01:00
inputs[| 12] = nodeValue("Light color", self, JUNCTION_CONNECT.input, VALUE_TYPE.color, c_white);
inputs[| 13] = nodeValue("Ambient color", self, JUNCTION_CONNECT.input, VALUE_TYPE.color, c_grey);
2022-12-12 09:08:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 14] = nodeValue("Height map", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, noone);
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 15] = nodeValue("Always update", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false);
2022-12-13 09:20:36 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 16] = nodeValue("Projection", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
.setDisplay(VALUE_DISPLAY.enum_button, [ "Orthographic", "Perspective" ])
.rejectArray();
2023-01-09 03:14:20 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 17] = nodeValue("Field of view", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 60)
2023-05-03 21:42:17 +02:00
.setDisplay(VALUE_DISPLAY.slider, [ 1, 90, 1 ]);
2023-01-09 03:14:20 +01:00
2023-02-23 07:02:19 +01:00
inputs[| 18] = nodeValue("Scale view with dimension", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true)
input_display_list = [
2023-05-03 21:42:17 +02:00
["Output", false], 1, 18,
2023-01-09 03:14:20 +01:00
["Geometry", false], 0, 8, 14,
2023-01-01 02:06:02 +01:00
["Object transform", false], 2, 3, 4,
2023-01-09 03:14:20 +01:00
["Camera", false], 16, 17, 5, 7, 15,
2023-01-01 02:06:02 +01:00
["Light", false], 9, 10, 11, 12, 13,
2022-12-12 09:08:03 +01:00
];
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2022-12-12 09:08:03 +01:00
2023-05-03 21:42:17 +02:00
outputs[| 1] = nodeValue("3D scene", self, JUNCTION_CONNECT.output, VALUE_TYPE.d3object, function(index) { return submit_vertex(index); });
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
outputs[| 2] = nodeValue("Normal pass", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2023-01-09 03:14:20 +01:00
2023-05-03 21:42:17 +02:00
outputs[| 3] = nodeValue("3D vertex", self, JUNCTION_CONNECT.output, VALUE_TYPE.d3vertex, []);
2023-01-09 03:14:20 +01:00
output_display_list = [
2023-05-03 21:42:17 +02:00
0, 2, 1, 3
2023-01-09 03:14:20 +01:00
]
2023-05-03 21:42:17 +02:00
_3d_node_init(1, /*Transform*/ 5, 7, 2, 3, 4);
2023-01-01 02:06:02 +01:00
2023-05-03 21:42:17 +02:00
vertexObjects = [];
2022-12-12 09:08:03 +01:00
2023-01-09 03:14:20 +01:00
static onValueFromUpdate = function(index) {
2023-01-04 02:30:04 +01:00
if(index == 0 || index == 14)
2022-12-12 09:08:03 +01:00
generateMesh();
}
2022-12-22 03:09:55 +01:00
static getHeight = function(h, gw, gh, i, j) {
var _i = round(i * gw);
var _j = round(j * gh);
_i = clamp(_i, 0, array_length(h) - 1);
_j = clamp(_j, 0, array_length(h[_i]) - 1);
return h[_i][_j];
}
2022-12-12 09:08:03 +01:00
static generateMesh = function() {
var _ins = inputs[| 0].getValue();
2023-01-01 02:06:02 +01:00
if(!is_array(_ins)) _ins = [ _ins ];
2023-05-03 21:42:17 +02:00
for( var i = 0; i < array_length(vertexObjects); i++ ) {
if(vertexObjects[i] == noone) continue;
vertexObjects[i].destroy();
}
vertexObjects = [];
2023-01-09 03:14:20 +01:00
for( var i = 0; i < array_length(_ins); i++ )
2023-05-03 21:42:17 +02:00
vertexObjects[i] = generateMeshIndex(i);
2023-01-09 03:14:20 +01:00
UPDATE |= RENDER_TYPE.full;
2023-05-03 21:42:17 +02:00
outputs[| 3].setValue(vertexObjects);
2023-01-01 02:06:02 +01:00
}
static generateMeshIndex = function(index) {
var _ins = getSingleValue( 0, index);
2023-01-09 03:14:20 +01:00
var _hei = getSingleValue(14, index);
2023-05-03 21:42:17 +02:00
if(!is_surface(_ins)) return noone;
2022-12-12 09:08:03 +01:00
var ww = surface_get_width(_ins);
var hh = surface_get_height(_ins);
var tw = 1 / ww;
var th = 1 / hh;
var sw = -ww / 2 * tw;
var sh = -hh / 2 * th;
2022-12-13 09:20:36 +01:00
var useH = is_surface(_hei);
if(useH) {
2022-12-22 03:09:55 +01:00
var hgw = surface_get_width(_hei);
var hgh = surface_get_height(_hei);
var hgtW = hgw / ww;
var hgtH = hgh / hh;
var height_buffer = buffer_create(hgw * hgh * 4, buffer_fixed, 2);
2022-12-13 09:20:36 +01:00
buffer_get_surface(height_buffer, _hei, 0);
buffer_seek(height_buffer, buffer_seek_start, 0);
2022-12-22 03:09:55 +01:00
var hei = array_create(hgw, hgh);
2022-12-13 09:20:36 +01:00
2022-12-22 03:09:55 +01:00
for( var j = 0; j < hgh; j++ )
for( var i = 0; i < hgw; i++ ) {
2022-12-13 09:20:36 +01:00
var cc = buffer_read(height_buffer, buffer_u32);
var _b = colorBrightness(cc & ~0b11111111);
hei[i][j] = _b;
}
buffer_delete(height_buffer);
}
2022-12-12 09:08:03 +01:00
var surface_buffer = buffer_create(ww * hh * 4, buffer_fixed, 2);
buffer_get_surface(surface_buffer, _ins, 0);
buffer_seek(surface_buffer, buffer_seek_start, 0);
2023-05-03 21:42:17 +02:00
var v = new VertexObject();
2022-12-12 09:08:03 +01:00
var ap = array_create(ww, hh);
for( var j = 0; j < hh; j++ )
for( var i = 0; i < ww; i++ ) {
var cc = buffer_read(surface_buffer, buffer_u32);
var _a = (cc & (0b11111111 << 24)) >> 24;
ap[i][j] = _a;
}
2022-12-13 09:20:36 +01:00
buffer_delete(surface_buffer);
2022-12-12 09:08:03 +01:00
for( var i = 0; i < ww; i++ )
for( var j = 0; j < hh; j++ ) {
if(ap[i][j] == 0) continue;
var i0 = sw + i * tw, i1 = i0 + tw;
var j0 = sh + j * th, j1 = j0 + th;
var tx0 = tw * i, tx1 = tx0 + tw;
var ty0 = th * j, ty1 = ty0 + th;
2022-12-22 03:09:55 +01:00
var dep = (useH? getHeight(hei, hgtW, hgtH, i, j) : 1) * 0.5;
2022-12-13 09:20:36 +01:00
2023-05-03 21:42:17 +02:00
v.addFace( [i1, j0, -dep], [0, 0, -1], [tx1, ty0],
[i0, j0, -dep], [0, 0, -1], [tx0, ty0],
[i1, j1, -dep], [0, 0, -1], [tx1, ty1], true);
2022-12-12 09:08:03 +01:00
2023-05-03 21:42:17 +02:00
v.addFace( [i1, j1, -dep], [0, 0, -1], [tx1, ty1],
[i0, j0, -dep], [0, 0, -1], [tx0, ty0],
[i0, j1, -dep], [0, 0, -1], [tx0, ty1], true);
2022-12-12 09:08:03 +01:00
2023-05-03 21:42:17 +02:00
v.addFace( [i1, j0, dep], [0, 0, 1], [tx1, ty0],
[i0, j0, dep], [0, 0, 1], [tx0, ty0],
[i1, j1, dep], [0, 0, 1], [tx1, ty1], true);
2022-12-12 09:08:03 +01:00
2023-05-03 21:42:17 +02:00
v.addFace( [i1, j1, dep], [0, 0, 1], [tx1, ty1],
[i0, j0, dep], [0, 0, 1], [tx0, ty0],
[i0, j1, dep], [0, 0, 1], [tx0, ty1], true);
2022-12-12 09:08:03 +01:00
2022-12-22 03:09:55 +01:00
if((useH && dep * 2 > getHeight(hei, hgtW, hgtH, i, j - 1)) || (j == 0 || ap[i][j - 1] == 0)) {
2023-05-03 21:42:17 +02:00
v.addFace( [i0, j0, dep], [0, -1, 0], [tx1, ty0],
[i0, j0, -dep], [0, -1, 0], [tx0, ty0],
[i1, j0, dep], [0, -1, 0], [tx1, ty1], true);
2022-12-12 09:08:03 +01:00
2023-05-03 21:42:17 +02:00
v.addFace( [i0, j0, -dep], [0, -1, 0], [tx1, ty1],
[i1, j0, -dep], [0, -1, 0], [tx0, ty0],
[i1, j0, dep], [0, -1, 0], [tx0, ty1], true);
2022-12-12 09:08:03 +01:00
}
2022-12-22 03:09:55 +01:00
if((useH && dep * 2 > getHeight(hei, hgtW, hgtH, i, j + 1)) || (j == hh - 1 || ap[i][j + 1] == 0)) {
2023-05-03 21:42:17 +02:00
v.addFace( [i0, j1, dep], [0, 1, 0], [tx1, ty0],
[i0, j1, -dep], [0, 1, 0], [tx0, ty0],
[i1, j1, dep], [0, 1, 0], [tx1, ty1], true);
v.addFace( [i0, j1, -dep], [0, 1, 0], [tx1, ty1],
[i1, j1, -dep], [0, 1, 0], [tx0, ty0],
[i1, j1, dep], [0, 1, 0], [tx0, ty1], true);
2022-12-12 09:08:03 +01:00
}
2022-12-22 03:09:55 +01:00
if((useH && dep * 2 > getHeight(hei, hgtW, hgtH, i - 1, j)) || (i == 0 || ap[i - 1][j] == 0)) {
2023-05-03 21:42:17 +02:00
v.addFace( [i0, j0, dep], [1, 0, 0], [tx1, ty0],
[i0, j0, -dep], [1, 0, 0], [tx0, ty0],
[i0, j1, dep], [1, 0, 0], [tx1, ty1], true);
v.addFace( [i0, j0, -dep], [1, 0, 0], [tx1, ty1],
[i0, j1, -dep], [1, 0, 0], [tx0, ty0],
[i0, j1, dep], [1, 0, 0], [tx0, ty1], true);
2022-12-12 09:08:03 +01:00
}
2022-12-22 03:09:55 +01:00
if((useH && dep * 2 > getHeight(hei, hgtW, hgtH, i + 1, j)) || (i == ww - 1 || ap[i + 1][j] == 0)) {
2023-05-03 21:42:17 +02:00
v.addFace( [i1, j0, dep], [-1, 0, 0], [tx1, ty0],
[i1, j0, -dep], [-1, 0, 0], [tx0, ty0],
[i1, j1, dep], [-1, 0, 0], [tx1, ty1], true);
v.addFace( [i1, j0, -dep], [-1, 0, 0], [tx1, ty1],
[i1, j1, -dep], [-1, 0, 0], [tx0, ty0],
[i1, j1, dep], [-1, 0, 0], [tx0, ty1], true);
2022-12-12 09:08:03 +01:00
}
}
2023-05-03 21:42:17 +02:00
v.createBuffer();
return v;
2022-12-12 09:08:03 +01:00
}
2022-12-19 13:35:30 +01:00
static drawOverlay = function(active, _x, _y, _s, _mx, _my, _snx, _sny) {
2023-01-01 02:06:02 +01:00
_3d_gizmo(active, _x, _y, _s, _mx, _my, _snx, _sny);
}
2023-01-09 03:14:20 +01:00
static submit_vertex = function(index = 0) {
2023-01-01 02:06:02 +01:00
var _ins = getSingleValue(0, index);
if(!is_surface(_ins)) return;
2023-05-03 21:42:17 +02:00
if(index >= array_length(vertexObjects)) return;
2022-12-12 09:08:03 +01:00
2023-01-01 02:06:02 +01:00
var _lpos = getSingleValue(2, index);
var _lrot = getSingleValue(3, index);
var _lsca = getSingleValue(4, index);
2022-12-12 09:08:03 +01:00
2023-01-01 02:06:02 +01:00
_3d_local_transform(_lpos, _lrot, _lsca);
2023-05-03 21:42:17 +02:00
vertexObjects[index].submit(_ins);
2023-01-01 02:06:02 +01:00
_3d_clear_local_transform();
2022-12-12 09:08:03 +01:00
}
2023-01-01 02:06:02 +01:00
static process_data = function(_outSurf, _data, _output_index, _array_index) {
2023-05-03 21:42:17 +02:00
if(_output_index == 3) return vertexObjects;
2023-01-01 02:06:02 +01:00
var _ins = _data[ 0];
var _dim = _data[ 1];
var _lpos = _data[ 2];
var _lrot = _data[ 3];
var _lsca = _data[ 4];
2022-12-12 09:08:03 +01:00
2023-01-01 02:06:02 +01:00
var _pos = _data[ 5];
var _sca = _data[ 7];
2022-12-12 09:08:03 +01:00
2023-01-01 02:06:02 +01:00
var _ldir = _data[ 9];
var _lhgt = _data[10];
var _lint = _data[11];
var _lclr = _data[12];
var _aclr = _data[13];
2022-12-12 09:08:03 +01:00
2023-01-01 02:06:02 +01:00
var _upda = _data[15];
2022-12-12 09:08:03 +01:00
2023-01-09 03:14:20 +01:00
var _proj = _data[16];
var _fov = _data[17];
2023-02-23 07:02:19 +01:00
var _dimS = _data[18];
2023-01-09 03:14:20 +01:00
inputs[| 17].setVisible(_proj);
_outSurf = surface_verify(_outSurf, _dim[0], _dim[1]);
2022-12-12 09:08:03 +01:00
if(!is_surface(_ins)) return _outSurf;
2023-01-09 03:14:20 +01:00
var pass = "diff";
switch(_output_index) {
case 0 : pass = "diff" break;
case 2 : pass = "norm" break;
}
2023-01-01 02:06:02 +01:00
if(_upda && ANIMATOR.frame_progress)
2022-12-21 02:30:23 +01:00
generateMesh();
2023-02-23 07:02:19 +01:00
var _cam = { projection: _proj, fov: _fov };
var _scale = { local: false, dimension: _dimS };
2023-05-03 21:42:17 +02:00
_outSurf = _3d_pre_setup(_outSurf, _dim, _pos, _sca, _ldir, _lhgt, _lint, _lclr, _aclr, _lpos, _lrot, _lsca, _cam, pass, _scale);
2023-01-01 02:06:02 +01:00
submit_vertex(_array_index);
_3d_post_setup();
2022-12-12 09:08:03 +01:00
return _outSurf;
}
2023-01-09 03:14:20 +01:00
static postConnect = function() {
generateMesh();
}
2022-12-12 09:08:03 +01:00
}