Pixel-Composer/scripts/node_3d_displace/node_3d_displace.gml

181 lines
6.2 KiB
Plaintext
Raw Normal View History

2023-05-03 21:42:17 +02:00
function Node_3D_Displace(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
name = "3D Displace";
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Dimension", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, def_surf_size2)
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-02-14 05:32:32 +01:00
inputs[| 1] = 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);
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("Object 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[| 3] = nodeValue("Object scale", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1, 1 ])
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-05-03 21:42:17 +02:00
inputs[| 4] = 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[| 0].getValue(); }, VALUE_UNIT.reference);
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 5] = nodeValue("Render scale", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1 ])
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-02-14 05:32:32 +01:00
inputs[| 6] = nodeValue("Light direction", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0)
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.rotation);
2023-02-14 05:32:32 +01:00
inputs[| 7] = nodeValue("Light height", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0.5)
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.slider, [-1, 1, 0.01]);
2023-02-14 05:32:32 +01:00
inputs[| 8] = nodeValue("Light intensity", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 1)
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.slider, [0, 1, 0.01]);
2023-02-14 05:32:32 +01:00
inputs[| 9] = nodeValue("Light color", self, JUNCTION_CONNECT.input, VALUE_TYPE.color, c_white);
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 10] = nodeValue("Ambient color", self, JUNCTION_CONNECT.input, VALUE_TYPE.color, c_grey);
2023-01-01 02:06:02 +01:00
2023-05-03 21:42:17 +02:00
inputs[| 11] = nodeValue("3D vertex", self, JUNCTION_CONNECT.input, VALUE_TYPE.d3vertex, [])
2023-01-01 02:06:02 +01:00
.setVisible(true, true);
2023-02-14 05:32:32 +01:00
inputs[| 12] = 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[| 13] = 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-05-03 21:42:17 +02:00
inputs[| 14] = nodeValue("Scale view with dimension", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
inputs[| 15] = nodeValue("Displacement map", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, noone);
inputs[| 16] = nodeValue("Strength", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 1)
.setDisplay(VALUE_DISPLAY.slider, [ 0, 4, 0.01 ]);
2023-02-23 07:02:19 +01:00
input_display_list = [ 11,
2023-05-03 21:42:17 +02:00
["Output", true], 0, 14,
["Displace", false], 15, 16,
["Object transform", true], 1, 2, 3,
["Camera", true], 12, 13, 4, 5,
["Light", true], 6, 7, 8, 9, 10,
2023-01-01 02:06:02 +01:00
];
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Surface out", 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[| 1] = nodeValue("3D scene", self, JUNCTION_CONNECT.output, VALUE_TYPE.d3object, function() { return submit_vertex(); });
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-06-13 14:42:06 +02:00
attributes.auto_update = true;
2023-05-03 21:42:17 +02:00
array_push(attributeEditors, ["Auto Update", "auto_update", new checkBox(function() { attribute[? "auto_update"] = !attribute[? "auto_update"]; }, false)]);
vertexObjects = [];
_3d_node_init(1, /*Transform*/ 4, 5, 1, 2, 3);
2023-01-01 02:06:02 +01:00
static drawOverlay = function(active, _x, _y, _s, _mx, _my, _snx, _sny) {
_3d_gizmo(active, _x, _y, _s, _mx, _my, _snx, _sny);
}
static submit_vertex = function() {
var _lpos = inputs[| 1].getValue();
var _lrot = inputs[| 2].getValue();
var _lsca = inputs[| 3].getValue();
_3d_local_transform(_lpos, _lrot, _lsca);
2023-05-03 21:42:17 +02:00
for( var i = 0; i < array_length(vertexObjects); i++ )
vertexObjects[i].submit();
2023-01-01 02:06:02 +01:00
_3d_clear_local_transform();
}
2023-02-14 05:32:32 +01:00
static step = function() {
var _proj = inputs[| 12].getValue();
inputs[| 13].setVisible(_proj);
}
static process_data = function(_outSurf, _data, _output_index, _array_index) {
2023-05-03 21:42:17 +02:00
if(_output_index == 1) return undefined;
if(_output_index == 3) return vertexObjects;
2023-02-14 05:32:32 +01:00
var _dim = _data[0];
var _lpos = _data[1];
var _lrot = _data[2];
var _lsca = _data[3];
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
var _pos = _data[4];
var _sca = _data[5];
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
var _ldir = _data[ 6];
var _lhgt = _data[ 7];
var _lint = _data[ 8];
var _lclr = _data[ 9];
var _aclr = _data[10];
2023-01-09 03:14:20 +01:00
2023-02-14 05:32:32 +01:00
var _proj = _data[12];
var _fov = _data[13];
2023-02-23 07:02:19 +01:00
var _dimS = _data[14];
2023-01-09 03:14:20 +01:00
2023-05-03 21:42:17 +02:00
var _dspTex = _data[15];
var _dspStr = _data[16];
2023-06-13 14:42:06 +02:00
if(_output_index == 0 && attributes.auto_update) {
2023-05-03 21:42:17 +02:00
var _vert = _data[11];
for( var i = 0; i < array_length(vertexObjects); i++ )
vertexObjects[i].destroy();
vertexObjects = [];
for( var i = 0; i < array_length(_vert); i++ ) {
var v = _vert[i].clone(false);
for( var j = 0; j < array_length(v.faces); j++ ) {
var face = v.faces[j];
var _posI = face[0];
var _norI = face[1];
var _texI = face[2];
var str = 1;
if(is_surface(_dspTex)) {
var c = surface_getpixel(_dspTex, v.textures[_texI][0] * surface_get_width(_dspTex), v.textures[_texI][1] * surface_get_height(_dspTex));
var r = color_get_red(c) / 255;
var g = color_get_green(c) / 255;
var b = color_get_blue(c) / 255;
str = 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
v.positions[@ _posI][@ 0] += v.normals[_norI][0] * str * _dspStr;
v.positions[@ _posI][@ 1] += v.normals[_norI][1] * str * _dspStr;
v.positions[@ _posI][@ 2] += v.normals[_norI][2] * str * _dspStr;
}
v.createBuffer();
vertexObjects[i] = v;
}
}
2023-02-14 05:32:32 +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
2023-06-13 14:42:06 +02:00
var _transform = new __3d_transform(_pos,, _sca, _lpos, _lrot, _lsca, true, _dimS );
var _light = new __3d_light(_ldir, _lhgt, _lint, _lclr, _aclr);
var _cam = new __3d_camera(_proj, _fov);
2023-02-23 07:02:19 +01:00
2023-06-13 14:42:06 +02:00
_outSurf = _3d_pre_setup(_outSurf, _dim, _transform, _light, _cam, pass);
2023-05-03 21:42:17 +02:00
for( var i = 0; i < array_length(vertexObjects); i++ )
vertexObjects[i].submit();
2023-02-14 05:32:32 +01:00
_3d_post_setup();
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
return _outSurf;
2023-01-01 02:06:02 +01:00
}
}