From a4035b52bf7b96e8a0442df77aa51307535b3cfb Mon Sep 17 00:00:00 2001 From: MakhamDev Date: Sat, 28 Oct 2023 18:31:13 +0700 Subject: [PATCH] 3d particle --- PixelComposer.resource_order | 3 +- PixelComposer.yyp | 2 + .../Shaders/3dInstance/3dInstancePS.hlsl | 11 ++--- .../Shaders/3dInstance/3dInstanceVS.hlsl | 44 ++++++++++++++----- scripts/__3d_particle/__3d_particle.gml | 23 ++++++++++ scripts/__3d_particle/__3d_particle.yy | 11 +++++ .../d3d_object_instancer.gml | 21 ++++++++- scripts/node_3d_particle/node_3d_particle.gml | 42 +++++++++++++++--- scripts/node_3d_particle/node_3d_particle.yy | 4 +- 9 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 scripts/__3d_particle/__3d_particle.gml create mode 100644 scripts/__3d_particle/__3d_particle.yy diff --git a/PixelComposer.resource_order b/PixelComposer.resource_order index 89e5eef93..fa0277f14 100644 --- a/PixelComposer.resource_order +++ b/PixelComposer.resource_order @@ -220,6 +220,7 @@ {"name":"biterator","order":2,"path":"folders/VCT/biterator.yy",}, {"name":"widget","order":3,"path":"folders/VCT/widget.yy",}, {"name":"widgets","order":5,"path":"folders/widgets.yy",}, + {"name":"d3d particle","order":19,"path":"folders/nodes/data/3D/d3d particle.yy",}, ], "ResourceOrderSettings": [ {"name":"s_node_corner","order":16,"path":"sprites/s_node_corner/s_node_corner.yy",}, @@ -648,7 +649,6 @@ {"name":"sh_3d_extrude_filler","order":8,"path":"shaders/sh_3d_extrude_filler/sh_3d_extrude_filler.yy",}, {"name":"quarternionBox","order":32,"path":"scripts/quarternionBox/quarternionBox.yy",}, {"name":"s_window_exit","order":2,"path":"sprites/s_window_exit/s_window_exit.yy",}, - {"name":"node_3d_particle","order":4,"path":"scripts/node_3d_particle/node_3d_particle.yy",}, {"name":"s_node_color_remove","order":7,"path":"sprites/s_node_color_remove/s_node_color_remove.yy",}, {"name":"sh_average","order":7,"path":"shaders/sh_average/sh_average.yy",}, {"name":"sh_warp_4points_pers","order":10,"path":"shaders/sh_warp_4points_pers/sh_warp_4points_pers.yy",}, @@ -1536,6 +1536,7 @@ {"name":"__matrix3","order":5,"path":"scripts/__matrix3/__matrix3.yy",}, {"name":"draw_fit","order":12,"path":"scripts/draw_fit/draw_fit.yy",}, {"name":"node_armature_sample","order":5,"path":"scripts/node_armature_sample/node_armature_sample.yy",}, + {"name":"__3d_particle","order":1,"path":"scripts/__3d_particle/__3d_particle.yy",}, {"name":"sh_pb_shade","order":3,"path":"shaders/sh_pb_shade/sh_pb_shade.yy",}, {"name":"s_node_morph_surface","order":56,"path":"sprites/s_node_morph_surface/s_node_morph_surface.yy",}, {"name":"fd_rectangle_get_velocity_dissipation_type","order":20,"path":"scripts/fd_rectangle_get_velocity_dissipation_type/fd_rectangle_get_velocity_dissipation_type.yy",}, diff --git a/PixelComposer.yyp b/PixelComposer.yyp index 7a2aad904..40ecf05a3 100644 --- a/PixelComposer.yyp +++ b/PixelComposer.yyp @@ -253,6 +253,7 @@ {"resourceType":"GMFolder","resourceVersion":"1.0","name":"biterator","folderPath":"folders/VCT/biterator.yy",}, {"resourceType":"GMFolder","resourceVersion":"1.0","name":"widget","folderPath":"folders/VCT/widget.yy",}, {"resourceType":"GMFolder","resourceVersion":"1.0","name":"widgets","folderPath":"folders/widgets.yy",}, + {"resourceType":"GMFolder","resourceVersion":"1.0","name":"d3d particle","folderPath":"folders/nodes/data/3D/d3d particle.yy",}, ], "IncludedFiles": [ {"resourceType":"GMIncludedFile","resourceVersion":"1.0","name":"ApolloHelp.html","ConfigValues":{"Itch":{"CopyToMask":"0",},},"CopyToMask":-1,"filePath":"datafiles",}, @@ -2280,6 +2281,7 @@ {"id":{"name":"__matrix3","path":"scripts/__matrix3/__matrix3.yy",},}, {"id":{"name":"draw_fit","path":"scripts/draw_fit/draw_fit.yy",},}, {"id":{"name":"node_armature_sample","path":"scripts/node_armature_sample/node_armature_sample.yy",},}, + {"id":{"name":"__3d_particle","path":"scripts/__3d_particle/__3d_particle.yy",},}, {"id":{"name":"sh_pb_shade","path":"shaders/sh_pb_shade/sh_pb_shade.yy",},}, {"id":{"name":"ds_map","path":"scripts/ds_map/ds_map.yy",},}, {"id":{"name":"s_node_morph_surface","path":"sprites/s_node_morph_surface/s_node_morph_surface.yy",},}, diff --git a/datafiles/Shaders/3dInstance/3dInstancePS.hlsl b/datafiles/Shaders/3dInstance/3dInstancePS.hlsl index 79f6907c1..ef4ab9e5c 100644 --- a/datafiles/Shaders/3dInstance/3dInstancePS.hlsl +++ b/datafiles/Shaders/3dInstance/3dInstancePS.hlsl @@ -1,23 +1,20 @@ #include "CommonPS.hlsl" -struct VS_out -{ +struct VS_out { float4 Position : SV_POSITION; float3 Normal : NORMAL0; float4 Color : COLOR0; float2 TexCoord : TEXCOORD0; }; -struct PS_out -{ +struct PS_out { float4 Color : SV_Target0; }; -void main(in VS_out IN, out PS_out OUT) -{ +void main(in VS_out IN, out PS_out OUT) { OUT.Color = gm_BaseTextureObject.Sample(gm_BaseTexture, IN.TexCoord); float3 N = normalize(IN.Normal); - float3 L = normalize(float3(1.0, 1.0, 1.0)); + float3 L = normalize(float3(1.0, 1.0, 2.0)); float NdotL = saturate(dot(N, L)); OUT.Color.rgb *= lerp(0.2, 1.0, NdotL); } diff --git a/datafiles/Shaders/3dInstance/3dInstanceVS.hlsl b/datafiles/Shaders/3dInstance/3dInstanceVS.hlsl index f698b2430..fe72dc854 100644 --- a/datafiles/Shaders/3dInstance/3dInstanceVS.hlsl +++ b/datafiles/Shaders/3dInstance/3dInstanceVS.hlsl @@ -1,7 +1,6 @@ #include "CommonVS.hlsl" -struct VS_in -{ +struct VS_in { float3 Position : POSITION; float3 Normal : NORMAL0; float4 Color : COLOR0; @@ -9,22 +8,47 @@ struct VS_in uint InstanceID : SV_InstanceID; }; -struct VS_out -{ +struct VS_out { float4 Position : SV_POSITION; float3 Normal : NORMAL0; float4 Color : COLOR0; float2 TexCoord : TEXCOORD0; }; -cbuffer Data : register(b10) -{ - float4 InstancePosition[4096]; +struct Transform { + float4 position; + float4 rotation; + float4 scale; + float4 reserved; }; -void main(in VS_in IN, out VS_out OUT) -{ - float3 position = IN.Position.xyz + InstancePosition[IN.InstanceID].xyz; +cbuffer Data : register(b10) { + Transform InstanceTransforms[128]; +}; + +// float4x4 EulerToMatrix(float3 eulerAngles) { +// float3 c = cos(eulerAngles); +// float3 s = sin(eulerAngles); + +// float4x4 rotationMatrix; + +// rotationMatrix[0] = float4(c.y * c.z, -c.x * s.z + s.x * s.y * c.z, s.x * s.z + c.x * s.y * c.z, 0.0); +// rotationMatrix[1] = float4(c.y * s.z, c.x * c.z + s.x * s.y * s.z, -s.x * c.z + c.x * s.y * s.z, 0.0); +// rotationMatrix[2] = float4(-s.y, s.x * c.y, c.x * c.y, 0.0); +// rotationMatrix[3] = float4(0.0, 0.0, 0.0, 1.0); + +// return rotationMatrix; +// } + +void main(in VS_in IN, out VS_out OUT) { + float3 position = IN.Position.xyz; + //float4x4 rotation = EulerToMatrix(InstanceTransforms[IN.InstanceID].rotation.xyz); + float3 scale = InstanceTransforms[IN.InstanceID].scale.xyz; + + //position = mul(rotation, float4(position, 1.)).xyz; + position = position * scale; + position = position + InstanceTransforms[IN.InstanceID].position.xyz; + OUT.Position = mul(gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION], float4(position.xyz, 1.0)); OUT.Normal = mul(gm_Matrices[MATRIX_WORLD], float4(IN.Normal.xyz, 0.0)); OUT.Color = IN.Color; diff --git a/scripts/__3d_particle/__3d_particle.gml b/scripts/__3d_particle/__3d_particle.gml new file mode 100644 index 000000000..e77e830c5 --- /dev/null +++ b/scripts/__3d_particle/__3d_particle.gml @@ -0,0 +1,23 @@ +function __3DVFX() constructor { + active = true; + position = [ 0, 0, 0 ]; + rotation = [ 0, 0, 0 ]; + scale = [ 0, 0, 0 ]; + + seed = 0; + velocity = [ 0, 0, 0 ]; + + static reset = function(seed) { + self.seed = seed; + + random_set_seed(seed); + position[0] = random_range(-4, 4); + position[1] = random_range(-4, 4); + position[2] = random_range(-4, 4); + } + + static step = function() { + for( var i = 0; i < 3; i++ ) + position[i] += velocity[i]; + } +} \ No newline at end of file diff --git a/scripts/__3d_particle/__3d_particle.yy b/scripts/__3d_particle/__3d_particle.yy new file mode 100644 index 000000000..8918d3e56 --- /dev/null +++ b/scripts/__3d_particle/__3d_particle.yy @@ -0,0 +1,11 @@ +{ + "resourceType": "GMScript", + "resourceVersion": "1.0", + "name": "__3d_particle", + "isCompatibility": false, + "isDnD": false, + "parent": { + "name": "d3d particle", + "path": "folders/nodes/data/3D/d3d particle.yy", + }, +} \ No newline at end of file diff --git a/scripts/d3d_object_instancer/d3d_object_instancer.gml b/scripts/d3d_object_instancer/d3d_object_instancer.gml index 1bf17c517..a6d7559f5 100644 --- a/scripts/d3d_object_instancer/d3d_object_instancer.gml +++ b/scripts/d3d_object_instancer/d3d_object_instancer.gml @@ -21,21 +21,40 @@ function __3dObjectInstancer() : __3dObject() constructor { size = new __vec3(1); positions = []; + rotations = []; + scales = []; static setData = function() { #region d3d11_cbuffer_begin(); - d3d11_cbuffer_add_float(4 * object_counts); + d3d11_cbuffer_add_float(16 * object_counts); object_data = d3d11_cbuffer_end(); var _buffer = buffer_create(d3d11_cbuffer_get_size(object_data), buffer_fixed, 1); for(var i = 0; i < object_counts; i++) { var pos = array_safe_get(positions, i, 0); + var rot = array_safe_get(rotations, i, 0); + var sca = array_safe_get(scales, i, 0); buffer_write(_buffer, buffer_f32, array_safe_get(pos, 0, 0)); buffer_write(_buffer, buffer_f32, array_safe_get(pos, 1, 0)); buffer_write(_buffer, buffer_f32, array_safe_get(pos, 2, 0)); buffer_write(_buffer, buffer_f32, 0); + + buffer_write(_buffer, buffer_f32, array_safe_get(rot, 0, 0)); + buffer_write(_buffer, buffer_f32, array_safe_get(rot, 1, 0)); + buffer_write(_buffer, buffer_f32, array_safe_get(rot, 2, 0)); + buffer_write(_buffer, buffer_f32, 0); + + buffer_write(_buffer, buffer_f32, array_safe_get(sca, 0, 0)); + buffer_write(_buffer, buffer_f32, array_safe_get(sca, 1, 0)); + buffer_write(_buffer, buffer_f32, array_safe_get(sca, 2, 0)); + buffer_write(_buffer, buffer_f32, 0); + + buffer_write(_buffer, buffer_f32, 0); + buffer_write(_buffer, buffer_f32, 0); + buffer_write(_buffer, buffer_f32, 0); + buffer_write(_buffer, buffer_f32, 0); } d3d11_cbuffer_update(object_data, _buffer); diff --git a/scripts/node_3d_particle/node_3d_particle.gml b/scripts/node_3d_particle/node_3d_particle.gml index 0bc722aa5..9f5fe9b9e 100644 --- a/scripts/node_3d_particle/node_3d_particle.gml +++ b/scripts/node_3d_particle/node_3d_particle.gml @@ -1,11 +1,31 @@ function Node_3D_Particle(_x, _y, _group = noone) : Node_3D_Modifier(_x, _y, _group) constructor { name = "3D Particle"; + update_on_frame = true; inputs[| in_mesh + 0] = nodeValue("Amounts", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 1); - inputs[| in_mesh + 1] = nodeValue("Positions", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 0, 0 ]) - .setDisplay(VALUE_DISPLAY.vector) - .setArrayDepth(1); + part_pool_size = 128; + parts = array_create(part_pool_size); + for( var i = 0; i < part_pool_size; i++ ) + parts[i] = new __3DVFX(); + + part_pos = array_create(part_pool_size); + part_rot = array_create(part_pool_size); + part_sca = array_create(part_pool_size); + + seed = irandom_range(100000, 999999); + + static processData_prebatch = function() { + if(CURRENT_FRAME == 0) { + var _sed = seed; + + for( var i = 0; i < part_pool_size; i++ ) + parts[i].reset(_sed++); + } else { + for( var i = 0; i < part_pool_size; i++ ) + parts[i].step(); + } + } static processData = function(_output, _data, _output_index, _array_index = 0) { var _obj = _data[0]; @@ -13,13 +33,25 @@ function Node_3D_Particle(_x, _y, _group = noone) : Node_3D_Modifier(_x, _y, _gr if(_obj.VF != global.VF_POS_NORM_TEX_COL) return noone; var _amo = _data[in_mesh + 0]; - var _pos = _data[in_mesh + 1]; if(_amo <= 0) return noone; var _res = new __3dObjectInstancer(); + var _scaleZero = [ 0, 0, 0 ]; _res.object_counts = max(1, _amo); - _res.positions = _pos; + + for( var i = 0; i < part_pool_size; i++ ) { + var _part = parts[i]; + + part_pos[i] = _part.position; + part_rot[i] = _part.rotation; + part_sca[i] = _part.active? _part.scale : _scaleZero; + } + + _res.positions = part_pos; + _res.rotations = part_rot; + _res.scales = part_sca; + _res.object_counts = part_pool_size; _res.vertex = _obj.vertex; _res.VB = _obj.VB; diff --git a/scripts/node_3d_particle/node_3d_particle.yy b/scripts/node_3d_particle/node_3d_particle.yy index 41bbbe45d..d4e5e2c08 100644 --- a/scripts/node_3d_particle/node_3d_particle.yy +++ b/scripts/node_3d_particle/node_3d_particle.yy @@ -5,7 +5,7 @@ "isCompatibility": false, "isDnD": false, "parent": { - "name": "d3d modifier", - "path": "folders/nodes/data/3D/d3d modifier.yy", + "name": "d3d particle", + "path": "folders/nodes/data/3D/d3d particle.yy", }, } \ No newline at end of file