From e1e4ab8db8e774abb2abf8fd1d2310eca65b1d66 Mon Sep 17 00:00:00 2001 From: Abdelkader MAHBOUBI Date: Tue, 11 Mar 2025 11:50:02 +0100 Subject: [PATCH 01/14] truc de prof --- examples/cells/example_cells.tscn | 2 +- examples/example_1.tscn | 2 +- examples/example_2.tscn | 2 +- examples/example_3.tscn | 4 ++-- examples/example_4.tscn | 2 +- examples/example_5.tscn | 2 +- examples/example_lenia_test.tscn | 4 ++-- examples/example_mandelbrot.cpp | 0 examples/example_mandelbrot.tscn | 4 ++-- examples/simple_circle/simple_circle.tscn | 3 +-- 10 files changed, 12 insertions(+), 13 deletions(-) mode change 100755 => 100644 examples/example_mandelbrot.cpp diff --git a/examples/cells/example_cells.tscn b/examples/cells/example_cells.tscn index 6d35843..1d38454 100644 --- a/examples/cells/example_cells.tscn +++ b/examples/cells/example_cells.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://cutxgtalwsp6q"] -[ext_resource type="Script" uid="uid://bs2f5sxok3d3i" path="res://examples/cells/cells.gd" id="1_dw4h8"] +[ext_resource type="Script" uid="uid://cq3uippbckyky" path="res://examples/cells/cells.gd" id="1_dw4h8"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_oji6h"] [node name="Cells" type="Control" node_paths=PackedStringArray("data")] diff --git a/examples/example_1.tscn b/examples/example_1.tscn index 88ce9e3..b4d997a 100644 --- a/examples/example_1.tscn +++ b/examples/example_1.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=5 format=3 uid="uid://ddr6qtwy1pesd"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_6846p"] +[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_6846p"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_s3fct"] [sub_resource type="FastNoiseLite" id="FastNoiseLite_dmk8h"] diff --git a/examples/example_2.tscn b/examples/example_2.tscn index a843389..fc10638 100644 --- a/examples/example_2.tscn +++ b/examples/example_2.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://cotswmubn7uvm"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_2cs40"] +[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_2cs40"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_j6qu8"] [node name="Example2" type="Node2D"] diff --git a/examples/example_3.tscn b/examples/example_3.tscn index 680ce1e..257445a 100644 --- a/examples/example_3.tscn +++ b/examples/example_3.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://cmucgsppcoo5e"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_eimw3"] -[ext_resource type="Script" uid="uid://jw3o2qu3a0pl" path="res://examples/LabelStepPass.gd" id="2_4aq4t"] +[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_eimw3"] +[ext_resource type="Script" uid="uid://bwwru4dq5vo22" path="res://examples/LabelStepPass.gd" id="2_4aq4t"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_4upxj"] [node name="CompShadStudioEx3" type="Node2D"] diff --git a/examples/example_4.tscn b/examples/example_4.tscn index 1d7e26f..2eb7796 100644 --- a/examples/example_4.tscn +++ b/examples/example_4.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://b2ip5eftk3aij"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_ke3fj"] +[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_ke3fj"] [ext_resource type="Texture2D" uid="uid://2behgeplwycn" path="res://examples/grid_512x512.png" id="2_flqlh"] [node name="CompShadStudioEx4" type="Node2D"] diff --git a/examples/example_5.tscn b/examples/example_5.tscn index b1d6e72..38e64a6 100644 --- a/examples/example_5.tscn +++ b/examples/example_5.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=4 format=3 uid="uid://cets11mlsd8hb"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_cogap"] +[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_cogap"] [sub_resource type="FastNoiseLite" id="FastNoiseLite_pm7bb"] diff --git a/examples/example_lenia_test.tscn b/examples/example_lenia_test.tscn index 118ec4d..50bef2e 100644 --- a/examples/example_lenia_test.tscn +++ b/examples/example_lenia_test.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://cljj33x767odp"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_wci4f"] -[ext_resource type="Script" uid="uid://jw3o2qu3a0pl" path="res://examples/LabelStepPass.gd" id="2_ndkd3"] +[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_wci4f"] +[ext_resource type="Script" uid="uid://bwwru4dq5vo22" path="res://examples/LabelStepPass.gd" id="2_ndkd3"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="3_orsvs"] [node name="CompShadStudioEx3" type="Node2D"] diff --git a/examples/example_mandelbrot.cpp b/examples/example_mandelbrot.cpp old mode 100755 new mode 100644 diff --git a/examples/example_mandelbrot.tscn b/examples/example_mandelbrot.tscn index 1b0fb27..b3ef14f 100644 --- a/examples/example_mandelbrot.tscn +++ b/examples/example_mandelbrot.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://d1hhshnvrt86e"] -[ext_resource type="Script" uid="uid://bib00xk6p8oo4" path="res://examples/example_mandelbrot.gd" id="1_5gp20"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_t5cx1"] +[ext_resource type="Script" uid="uid://3ydbk1bjdq0m" path="res://examples/example_mandelbrot.gd" id="1_5gp20"] +[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_t5cx1"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_dyrad"] [node name="example_mandelbrot" type="Node2D"] diff --git a/examples/simple_circle/simple_circle.tscn b/examples/simple_circle/simple_circle.tscn index 8c68dd5..ee22334 100644 --- a/examples/simple_circle/simple_circle.tscn +++ b/examples/simple_circle/simple_circle.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://c63rtjh0eurgm"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_rlapi"] +[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_rlapi"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_y7slp"] [node name="SimpleCircle" type="Node2D"] @@ -9,7 +9,6 @@ script = ExtResource("1_rlapi") glsl_file = "res://examples/simple_circle/simple_circle.cpp" data = [NodePath("../Icon")] -metadata/_custom_type_script = "uid://c8esqdv0y26yp" [node name="Icon" type="Sprite2D" parent="."] position = Vector2(465.5, 268.5) From f98cc23e0bcf002b518e1551a9dfa0fdcc51ebe1 Mon Sep 17 00:00:00 2001 From: Abdelkader MAHBOUBI Date: Tue, 11 Mar 2025 11:57:01 +0100 Subject: [PATCH 02/14] Create README - Copie.md --- README - Copie.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 README - Copie.md diff --git a/README - Copie.md b/README - Copie.md new file mode 100644 index 0000000..28b6388 --- /dev/null +++ b/README - Copie.md @@ -0,0 +1,12 @@ +# Compute Shader Studio + A plugin for Godot to create compute shaders + +![logo](screenshots/compute_shader_studio_headline.png) + +**[Compute Shader Studio](https://virtulab.univ-brest.fr) is an addon for Godot Engine that enables you to write Compute Shaders in a minute:** + +![logo](screenshots/compute_shader_studio_ex2.png) + +**Read [the tutorial](doc/ComputeShaderStudio.pdf) or look at the [2 minutes video](https://www.youtube.com/watch?v=3bEgPawi7fQ) to make your first Compute Shader in Godot** + +**Several examples are available: test them to understand how they work and what can be done** From 263e93ee28da713b3587d095d5d30b870b5316a5 Mon Sep 17 00:00:00 2001 From: Abdelkader MAHBOUBI Date: Tue, 11 Mar 2025 12:23:27 +0100 Subject: [PATCH 03/14] quasi fini --- examples/cells/example_cells.tscn | 2 +- examples/example_1.tscn | 2 +- examples/example_2.tscn | 2 +- examples/example_3.tscn | 4 +- examples/example_4.tscn | 2 +- examples/example_5.tscn | 2 +- examples/example_lenia_test.tscn | 4 +- examples/example_mandelbrot.tscn | 4 +- examples/simple_circle/simple_circle.cpp | 185 ++++++++++++++++++++-- examples/simple_circle/simple_circle.tscn | 2 +- project.godot | 4 +- 11 files changed, 182 insertions(+), 31 deletions(-) diff --git a/examples/cells/example_cells.tscn b/examples/cells/example_cells.tscn index 1d38454..6959c31 100644 --- a/examples/cells/example_cells.tscn +++ b/examples/cells/example_cells.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://cutxgtalwsp6q"] -[ext_resource type="Script" uid="uid://cq3uippbckyky" path="res://examples/cells/cells.gd" id="1_dw4h8"] +[ext_resource type="Script" path="res://examples/cells/cells.gd" id="1_dw4h8"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_oji6h"] [node name="Cells" type="Control" node_paths=PackedStringArray("data")] diff --git a/examples/example_1.tscn b/examples/example_1.tscn index b4d997a..7d67949 100644 --- a/examples/example_1.tscn +++ b/examples/example_1.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=5 format=3 uid="uid://ddr6qtwy1pesd"] -[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_6846p"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_6846p"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_s3fct"] [sub_resource type="FastNoiseLite" id="FastNoiseLite_dmk8h"] diff --git a/examples/example_2.tscn b/examples/example_2.tscn index fc10638..766e825 100644 --- a/examples/example_2.tscn +++ b/examples/example_2.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://cotswmubn7uvm"] -[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_2cs40"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_2cs40"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_j6qu8"] [node name="Example2" type="Node2D"] diff --git a/examples/example_3.tscn b/examples/example_3.tscn index 257445a..df2b8a6 100644 --- a/examples/example_3.tscn +++ b/examples/example_3.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://cmucgsppcoo5e"] -[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_eimw3"] -[ext_resource type="Script" uid="uid://bwwru4dq5vo22" path="res://examples/LabelStepPass.gd" id="2_4aq4t"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_eimw3"] +[ext_resource type="Script" path="res://examples/LabelStepPass.gd" id="2_4aq4t"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_4upxj"] [node name="CompShadStudioEx3" type="Node2D"] diff --git a/examples/example_4.tscn b/examples/example_4.tscn index 2eb7796..c777972 100644 --- a/examples/example_4.tscn +++ b/examples/example_4.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://b2ip5eftk3aij"] -[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_ke3fj"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_ke3fj"] [ext_resource type="Texture2D" uid="uid://2behgeplwycn" path="res://examples/grid_512x512.png" id="2_flqlh"] [node name="CompShadStudioEx4" type="Node2D"] diff --git a/examples/example_5.tscn b/examples/example_5.tscn index 38e64a6..43aa420 100644 --- a/examples/example_5.tscn +++ b/examples/example_5.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=4 format=3 uid="uid://cets11mlsd8hb"] -[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_cogap"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_cogap"] [sub_resource type="FastNoiseLite" id="FastNoiseLite_pm7bb"] diff --git a/examples/example_lenia_test.tscn b/examples/example_lenia_test.tscn index 50bef2e..6c2caef 100644 --- a/examples/example_lenia_test.tscn +++ b/examples/example_lenia_test.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://cljj33x767odp"] -[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_wci4f"] -[ext_resource type="Script" uid="uid://bwwru4dq5vo22" path="res://examples/LabelStepPass.gd" id="2_ndkd3"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_wci4f"] +[ext_resource type="Script" path="res://examples/LabelStepPass.gd" id="2_ndkd3"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="3_orsvs"] [node name="CompShadStudioEx3" type="Node2D"] diff --git a/examples/example_mandelbrot.tscn b/examples/example_mandelbrot.tscn index b3ef14f..a5e532c 100644 --- a/examples/example_mandelbrot.tscn +++ b/examples/example_mandelbrot.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://d1hhshnvrt86e"] -[ext_resource type="Script" uid="uid://3ydbk1bjdq0m" path="res://examples/example_mandelbrot.gd" id="1_5gp20"] -[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_t5cx1"] +[ext_resource type="Script" path="res://examples/example_mandelbrot.gd" id="1_5gp20"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_t5cx1"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_dyrad"] [node name="example_mandelbrot" type="Node2D"] diff --git a/examples/simple_circle/simple_circle.cpp b/examples/simple_circle/simple_circle.cpp index 3d4cf17..d971317 100644 --- a/examples/simple_circle/simple_circle.cpp +++ b/examples/simple_circle/simple_circle.cpp @@ -1,21 +1,172 @@ -void main() { - - uint x = gl_GlobalInvocationID.x; - uint y = gl_GlobalInvocationID.y; - uint p = x + y*WSX; - - if (step == 0) // init - data_0[p] = int(0xFF0000FF - y) ; // Fond +void main() +{ + int x = int(gl_GlobalInvocationID.x); + int y = int(gl_GlobalInvocationID.y); + int p = x + y * int(WSX); + + // Parameters for rotation + float A = step * 0.05; // Rotation X + float B = step * 0.05; // Rotation Y + float C = step * 0.01; // Rotation Z + + float cubeWidth = 20.0; + int width = int(WSX); + int height = int(WSY); + + // Convert pixel coordinates to normalized coordinates + float nx = (float(x) / float(width) - 0.5) * 2.0; + float ny = (float(y) / float(height) - 0.5) * 2.0; + + // Camera distance and scale factor + float distanceFromCam = 100.0; + float K1 = 40.0; + + // Drawing parameters + float incrementSpeed = 0.6; + float minDist = 999999.0; + int color = 0xFF000000; // Default color (black) + + // Initialize background on first step + if (step == 0) { + data_0[p] = 0xFF000000; + } else { - float dx = float(mousex) - float(x) ; - float dy = float(mousey) - float(y) ; - float dist = sqrt( dx*dx + dy*dy ); - - if (dist < 10) { - data_0[p] = 0xFF00FF00; // Cercle vert + // For each face of the cube, check if the current pixel is on this face + for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) { + for (float cubeY = -cubeWidth; cubeY < cubeWidth; cubeY += incrementSpeed) { + // Calculate 3D coordinates for different faces of the cube + // Face 1: Z = -cubeWidth + float x1 = cubeX * sin(A) * sin(B) * cos(C) - (-cubeWidth) * cos(A) * sin(B) * cos(C) + + cubeX * cos(A) * sin(C) + (-cubeWidth) * sin(A) * sin(C) + cubeY * cos(B) * cos(C); + float y1 = cubeX * cos(A) * cos(C) + (-cubeWidth) * sin(A) * cos(C) - + cubeX * sin(A) * sin(B) * sin(C) + (-cubeWidth) * cos(A) * sin(B) * sin(C) - + cubeY * cos(B) * sin(C); + float z1 = (-cubeWidth) * cos(A) * cos(B) - cubeX * sin(A) * cos(B) + cubeY * sin(B) + distanceFromCam; + + if (z1 > 0.0) { + float ooz1 = 1.0 / z1; + float px1 = (width / 2.0) + K1 * ooz1 * x1 * 2.0; + float py1 = (height / 2.0) + K1 * ooz1 * y1; + + float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); + if (dist1 < 1.0 && dist1 < minDist) { + minDist = dist1; + color = 0xFF0000FF; // Face 1: Red + } + } + + // Face 2: X = cubeWidth + float x2 = cubeY * sin(A) * sin(B) * cos(C) - cubeWidth * cos(A) * sin(B) * cos(C) + + cubeY * cos(A) * sin(C) + cubeWidth * sin(A) * sin(C) + cubeWidth * cos(B) * cos(C); + float y2 = cubeY * cos(A) * cos(C) + cubeWidth * sin(A) * cos(C) - + cubeY * sin(A) * sin(B) * sin(C) + cubeWidth * cos(A) * sin(B) * sin(C) - + cubeWidth * cos(B) * sin(C); + float z2 = cubeWidth * cos(A) * cos(B) - cubeY * sin(A) * cos(B) + cubeWidth * sin(B) + distanceFromCam; + + if (z2 > 0.0) { + float ooz2 = 1.0 / z2; + float px2 = (width / 2.0) + K1 * ooz2 * x2 * 2.0; + float py2 = (height / 2.0) + K1 * ooz2 * y2; + + float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); + if (dist2 < 1.0 && dist2 < minDist) { + minDist = dist2; + color = 0xFF00FF00; // Face 2: Green + } + } + + // Face 3: X = -cubeWidth + float x3 = cubeY * sin(A) * sin(B) * cos(C) - (-cubeWidth) * cos(A) * sin(B) * cos(C) + + cubeY * cos(A) * sin(C) + (-cubeWidth) * sin(A) * sin(C) + (-cubeWidth) * cos(B) * cos(C); + float y3 = cubeY * cos(A) * cos(C) + (-cubeWidth) * sin(A) * cos(C) - + cubeY * sin(A) * sin(B) * sin(C) + (-cubeWidth) * cos(A) * sin(B) * sin(C) - + (-cubeWidth) * cos(B) * sin(C); + float z3 = (-cubeWidth) * cos(A) * cos(B) - cubeY * sin(A) * cos(B) + (-cubeWidth) * sin(B) + distanceFromCam; + + if (z3 > 0.0) { + float ooz3 = 1.0 / z3; + float px3 = (width / 2.0) + K1 * ooz3 * x3 * 2.0; + float py3 = (height / 2.0) + K1 * ooz3 * y3; + + float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); + if (dist3 < 1.0 && dist3 < minDist) { + minDist = dist3; + color = 0xFF0080FF; // Face 3: Light blue + } + } + + // Face 4: Z = cubeWidth + float x4 = cubeX * sin(A) * sin(B) * cos(C) - cubeWidth * cos(A) * sin(B) * cos(C) + + cubeX * cos(A) * sin(C) + cubeWidth * sin(A) * sin(C) + cubeY * cos(B) * cos(C); + float y4 = cubeX * cos(A) * cos(C) + cubeWidth * sin(A) * cos(C) - + cubeX * sin(A) * sin(B) * sin(C) + cubeWidth * cos(A) * sin(B) * sin(C) - + cubeY * cos(B) * sin(C); + float z4 = cubeWidth * cos(A) * cos(B) - cubeX * sin(A) * cos(B) + cubeY * sin(B) + distanceFromCam; + + if (z4 > 0.0) { + float ooz4 = 1.0 / z4; + float px4 = (width / 2.0) + K1 * ooz4 * x4 * 2.0; + float py4 = (height / 2.0) + K1 * ooz4 * y4; + + float dist4 = (px4 - float(x)) * (px4 - float(x)) + (py4 - float(y)) * (py4 - float(y)); + if (dist4 < 1.0 && dist4 < minDist) { + minDist = dist4; + color = 0xFFFF00FF; // Face 4: Magenta + } + } + + // Face 5: Y = -cubeWidth + float x5 = cubeX * sin(A) * sin(B) * cos(C) - cubeY * cos(A) * sin(B) * cos(C) + + cubeX * cos(A) * sin(C) + cubeY * sin(A) * sin(C) + (-cubeWidth) * cos(B) * cos(C); + float y5 = cubeX * cos(A) * cos(C) + cubeY * sin(A) * cos(C) - + cubeX * sin(A) * sin(B) * sin(C) + cubeY * cos(A) * sin(B) * sin(C) - + (-cubeWidth) * cos(B) * sin(C); + float z5 = cubeY * cos(A) * cos(B) - cubeX * sin(A) * cos(B) + (-cubeWidth) * sin(B) + distanceFromCam; + + if (z5 > 0.0) { + float ooz5 = 1.0 / z5; + float px5 = (width / 2.0) + K1 * ooz5 * x5 * 2.0; + float py5 = (height / 2.0) + K1 * ooz5 * y5; + + float dist5 = (px5 - float(x)) * (px5 - float(x)) + (py5 - float(y)) * (py5 - float(y)); + if (dist5 < 1.0 && dist5 < minDist) { + minDist = dist5; + color = 0xFFFFFF00; // Face 5: Yellow + } + } + + // Face 6: Y = cubeWidth + float x6 = cubeX * sin(A) * sin(B) * cos(C) - cubeY * cos(A) * sin(B) * cos(C) + + cubeX * cos(A) * sin(C) + cubeY * sin(A) * sin(C) + cubeWidth * cos(B) * cos(C); + float y6 = cubeX * cos(A) * cos(C) + cubeY * sin(A) * cos(C) - + cubeX * sin(A) * sin(B) * sin(C) + cubeY * cos(A) * sin(B) * sin(C) - + cubeWidth * cos(B) * sin(C); + float z6 = cubeY * cos(A) * cos(B) - cubeX * sin(A) * cos(B) + cubeWidth * sin(B) + distanceFromCam; + + if (z6 > 0.0) { + float ooz6 = 1.0 / z6; + float px6 = (width / 2.0) + K1 * ooz6 * x6 * 2.0; + float py6 = (height / 2.0) + K1 * ooz6 * y6; + + float dist6 = (px6 - float(x)) * (px6 - float(x)) + (py6 - float(y)) * (py6 - float(y)); + if (dist6 < 1.0 && dist6 < minDist) { + minDist = dist6; + color = 0xFF00FFFF; // Face 6: Cyan + } + } + } + } + + // Update the pixel color if we found a close face + if (minDist < 999999.0) { + data_0[p] = color; } else { - data_0[p] = int(0xFF0000FF - y) ; // Fond + // Gradually darken the background + int pix = data_0[p]; + int col = pix & 0x00FFFFFF; + if (col > 0) { + data_0[p] = pix - 1; + } } - } -} +} \ No newline at end of file diff --git a/examples/simple_circle/simple_circle.tscn b/examples/simple_circle/simple_circle.tscn index ee22334..d10ce3d 100644 --- a/examples/simple_circle/simple_circle.tscn +++ b/examples/simple_circle/simple_circle.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://c63rtjh0eurgm"] -[ext_resource type="Script" uid="uid://dso2opt580igs" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_rlapi"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_rlapi"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_y7slp"] [node name="SimpleCircle" type="Node2D"] diff --git a/project.godot b/project.godot index 1617345..6192edd 100644 --- a/project.godot +++ b/project.godot @@ -11,8 +11,8 @@ config_version=5 [application] config/name="compute_shader_studio" -run/main_scene="res://examples/example_1.tscn" -config/features=PackedStringArray("4.4", "Forward Plus") +run/main_scene="res://examples/circles/circles.tscn" +config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://addons/compute_shader_studio/icon.png" [editor_plugins] From 87bd61faf6b3d3105cb8613f09bab119f96ad9a2 Mon Sep 17 00:00:00 2001 From: Abdelkader MAHBOUBI Date: Wed, 12 Mar 2025 16:15:35 +0100 Subject: [PATCH 04/14] Update example_mandelbrot.cpp --- examples/example_mandelbrot.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 examples/example_mandelbrot.cpp diff --git a/examples/example_mandelbrot.cpp b/examples/example_mandelbrot.cpp old mode 100755 new mode 100644 From 8ccdf86788f4c845527aaae11b3f226432783c00 Mon Sep 17 00:00:00 2001 From: Alexandre COUTE Date: Wed, 12 Mar 2025 16:37:16 +0100 Subject: [PATCH 05/14] cube trop gros --- examples/example_mandelbrot.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 examples/example_mandelbrot.cpp diff --git a/examples/example_mandelbrot.cpp b/examples/example_mandelbrot.cpp old mode 100755 new mode 100644 From 1d916f24aaa56943a56886b1b7108a580c025ad7 Mon Sep 17 00:00:00 2001 From: Alexandre COUTE Date: Wed, 12 Mar 2025 16:38:58 +0100 Subject: [PATCH 06/14] cube trop gros (vraiment) --- examples/circles/.vscode/settings.json | 7 + examples/circles/circles.tscn | 2 +- examples/cube2/cube.cpp | 260 +++++++++++++++++++++++++ examples/cube2/cube.tscn | 17 ++ examples/example_mandelbrot.gd | 27 ++- examples/example_mandelbrot.tscn | 2 +- project.godot | 5 + 7 files changed, 304 insertions(+), 16 deletions(-) create mode 100644 examples/circles/.vscode/settings.json create mode 100644 examples/cube2/cube.cpp create mode 100644 examples/cube2/cube.tscn diff --git a/examples/circles/.vscode/settings.json b/examples/circles/.vscode/settings.json new file mode 100644 index 0000000..1d1a361 --- /dev/null +++ b/examples/circles/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "iostream": "cpp", + "vector": "cpp", + "cmath": "cpp" + } +} \ No newline at end of file diff --git a/examples/circles/circles.tscn b/examples/circles/circles.tscn index c66e724..3143139 100644 --- a/examples/circles/circles.tscn +++ b/examples/circles/circles.tscn @@ -27,6 +27,6 @@ horizontal_alignment = 1 vertical_alignment = 1 [node name="Icon" type="Sprite2D" parent="."] -position = Vector2(605.5, 411) +position = Vector2(592, 361) scale = Vector2(5.63281, 3.17969) texture = ExtResource("2_ntl1q") diff --git a/examples/cube2/cube.cpp b/examples/cube2/cube.cpp new file mode 100644 index 0000000..471048d --- /dev/null +++ b/examples/cube2/cube.cpp @@ -0,0 +1,260 @@ +void main() +{ + int x = int(gl_GlobalInvocationID.x); + int y = int(gl_GlobalInvocationID.y); + int p = x + y * int(WSX); + + // Parameters for rotation + float A = step * 0.02; // Rotation X + float B = step * 0.02; // Rotation Y + float C = step * 0.005; // Rotation Z + + // Cube size + float cubeWidth = 45.0; + int width = int(WSX); + int height = int(WSY); + + // Convert pixel coordinates to normalized coordinates + float nx = (float(x) / float(width) - 0.5) * 2.0; + float ny = (float(y) / float(height) - 0.5) * 2.0; + + // Camera distance and scale factor + float distanceFromCam = 90.0; + float K1 = 70.0; + + // Drawing parameters + float incrementSpeed = 5; + float minDist = 999999.0; + int color = 0xFF000000; // Default color (black) + bool isOnCube = false; // Flag to track if the pixel is currently on the cube + + // For the first frame, initialize everything to black + if (step == 0) { + data_0[p] = 0xFF000000; + } + else { + // Fixed rotation to show 3 faces instead of using animated rotation + // This ensures we always see the 3 faces properly + float fixedA = 0.5; // Fixed angle to show 3 faces + float fixedB = 0.3; + float fixedC = 0.1; + + // Use a combination of fixed rotation and animated rotation + float rotA = fixedA + A * 0.2; + float rotB = fixedB + B * 0.2; + float rotC = fixedC + C * 0.2; + + float threshold = 0.6; + + // Calculate faces with consistent dimensions to ensure proper cube shape + for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) { + for (float cubeY = -cubeWidth; cubeY < cubeWidth; cubeY += incrementSpeed) { + // Face 1: Right face (X = cubeWidth) + { + float x1 = cubeWidth; + float y1 = cubeY; + float z1 = cubeX; + + // Apply rotation + float rotX1 = x1 * cos(rotB) * cos(rotC) + + z1 * sin(rotB) - + y1 * cos(rotB) * sin(rotC); + float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z1 * sin(rotA) * cos(rotB); + float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z1 * cos(rotA) * cos(rotB); + + rotZ1 += distanceFromCam; + + if (rotZ1 > 0.0) { + float ooz1 = 1.0 / rotZ1; + float px1 = (width / 2.0) + K1 * ooz1 * rotX1; + float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + + float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); + if (dist1 < threshold && dist1 < minDist) { + minDist = dist1; + color = 0xFF0000FF; // Face 1: Blue + isOnCube = true; + } + } + } + + // Face 2: Top face (Y = -cubeWidth) + { + float x2 = cubeX; + float y2 = -cubeWidth; + float z2 = cubeY; + + // Apply rotation + float rotX2 = x2 * cos(rotB) * cos(rotC) + + z2 * sin(rotB) - + y2 * cos(rotB) * sin(rotC); + float rotY2 = x2 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y2 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z2 * sin(rotA) * cos(rotB); + float rotZ2 = x2 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y2 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z2 * cos(rotA) * cos(rotB); + + rotZ2 += distanceFromCam; + + if (rotZ2 > 0.0) { + float ooz2 = 1.0 / rotZ2; + float px2 = (width / 2.0) + K1 * ooz2 * rotX2; + float py2 = (height / 2.0) + K1 * ooz2 * rotY2; + + float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); + if (dist2 < threshold && dist2 < minDist) { + minDist = dist2; + color = 0xFF00FF00; // Face 2: Green + isOnCube = true; + } + } + } + + // Face 3: Front face (Z = cubeWidth) + { + float x3 = cubeX; + float y3 = cubeY; + float z3 = cubeWidth; + + // Apply rotation + float rotX3 = x3 * cos(rotB) * cos(rotC) + + z3 * sin(rotB) - + y3 * cos(rotB) * sin(rotC); + float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z3 * sin(rotA) * cos(rotB); + float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z3 * cos(rotA) * cos(rotB); + + rotZ3 += distanceFromCam; + + if (rotZ3 > 0.0) { + float ooz3 = 1.0 / rotZ3; + float px3 = (width / 2.0) + K1 * ooz3 * rotX3; + float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + + float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); + if (dist3 < threshold && dist3 < minDist) { + minDist = dist3; + color = 0xFFFF8000; // Face 3: Orange + isOnCube = true; + } + } + } + + // Face 4: Right face (X = cubeWidth) + { + float x1 = -cubeWidth; + float y1 = cubeY; + float z1 = cubeX; + + // Apply rotation + float rotX1 = x1 * cos(rotB) * cos(rotC) + + z1 * sin(rotB) - + y1 * cos(rotB) * sin(rotC); + float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z1 * sin(rotA) * cos(rotB); + float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z1 * cos(rotA) * cos(rotB); + + rotZ1 += distanceFromCam; + + if (rotZ1 > 0.0) { + float ooz1 = 1.0 / rotZ1; + float px1 = (width / 2.0) + K1 * ooz1 * rotX1; + float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + + float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); + if (dist1 < threshold && dist1 < minDist) { + minDist = dist1; + color = 0xFF0000FF; // Face 1: Blue + isOnCube = true; + } + } + } + + // Face 5: Top face (Y = -cubeWidth) + { + float x2 = cubeX; + float y2 = cubeWidth; + float z2 = cubeY; + + // Apply rotation + float rotX2 = x2 * cos(rotB) * cos(rotC) + + z2 * sin(rotB) - + y2 * cos(rotB) * sin(rotC); + float rotY2 = x2 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y2 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z2 * sin(rotA) * cos(rotB); + float rotZ2 = x2 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y2 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z2 * cos(rotA) * cos(rotB); + + rotZ2 += distanceFromCam; + + if (rotZ2 > 0.0) { + float ooz2 = 1.0 / rotZ2; + float px2 = (width / 2.0) + K1 * ooz2 * rotX2; + float py2 = (height / 2.0) + K1 * ooz2 * rotY2; + + float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); + if (dist2 < threshold && dist2 < minDist) { + minDist = dist2; + color = 0xFF00FF00; // Face 2: Green + isOnCube = true; + } + } + } + + // Face 6: Front face (Z = cubeWidth) + { + float x3 = cubeX; + float y3 = cubeY; + float z3 = -cubeWidth; + + // Apply rotation + float rotX3 = x3 * cos(rotB) * cos(rotC) + + z3 * sin(rotB) - + y3 * cos(rotB) * sin(rotC); + float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z3 * sin(rotA) * cos(rotB); + float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z3 * cos(rotA) * cos(rotB); + + rotZ3 += distanceFromCam; + + if (rotZ3 > 0.0) { + float ooz3 = 1.0 / rotZ3; + float px3 = (width / 2.0) + K1 * ooz3 * rotX3; + float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + + float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); + if (dist3 < threshold && dist3 < minDist) { + minDist = dist3; + color = 0xFFFF8000; // Face 3: Orange + isOnCube = true; + } + } + } + } + } + + // No trailing effect + if (isOnCube) { + data_0[p] = color; + } else { + data_0[p] = 0xFF000000; + } + } +} + diff --git a/examples/cube2/cube.tscn b/examples/cube2/cube.tscn new file mode 100644 index 0000000..ba0e55d --- /dev/null +++ b/examples/cube2/cube.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=3 uid="uid://bn86hxa8xefmh"] + +[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_hegqh"] +[ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_hegqh"] + +[node name="Cube" type="Node2D"] + +[node name="ComputeShaderStudio2D" type="Node" parent="." node_paths=PackedStringArray("data")] +script = ExtResource("1_hegqh") +glsl_file = "res://examples/cube2/cube.cpp" +data = [NodePath("../Icon")] +metadata/_custom_type_script = "uid://c8esqdv0y26yp" + +[node name="Icon" type="Sprite2D" parent="."] +position = Vector2(592, 361) +scale = Vector2(5.63281, 3.17969) +texture = ExtResource("2_hegqh") diff --git a/examples/example_mandelbrot.gd b/examples/example_mandelbrot.gd index d869fa7..506717b 100644 --- a/examples/example_mandelbrot.gd +++ b/examples/example_mandelbrot.gd @@ -1,10 +1,8 @@ extends Node var css : Node - var zoom : float = 1.0 -var pos : Vector2 = Vector2(-0.75, 0) - +var pos : Vector2 = Vector2(0, 0) var grab : Vector2 var grabbing : bool = false @@ -15,26 +13,27 @@ func screen_to_world(screen : Vector2): func _ready(): css = get_node("ComputeShaderStudio2D") - css.uniform_user_data.resize(4*3) + # Augmenter la performance + css.material.set_shader_parameter("num_iterations", 1) func _process(_delta): - css.uniform_user_data.encode_float(0, zoom) - css.uniform_user_data.encode_float(4, pos.x) - css.uniform_user_data.encode_float(8, pos.y) + # Pas besoin de passer des uniformes + pass func _input(event): if event is InputEventMouseButton: - if event.button_index == MOUSE_BUTTON_WHEEL_UP : + if event.button_index == MOUSE_BUTTON_WHEEL_UP: zoom *= 1.1 - elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN : + elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN: zoom /= 1.1 - elif event.button_index == MOUSE_BUTTON_LEFT : - if event.pressed : # Start panning + elif event.button_index == MOUSE_BUTTON_LEFT: + if event.pressed: # Start panning grab = screen_to_world(event.position) grabbing = true - else : # Stop panning + else: # Stop panning grabbing = false - elif event is InputEventMouse and grabbing : # Panning - var delta : Vector2 = screen_to_world(event.position) - grab + elif event is InputEventMouseMotion and grabbing: # Panning + var world_pos = screen_to_world(event.position) + var delta = world_pos - grab pos -= delta diff --git a/examples/example_mandelbrot.tscn b/examples/example_mandelbrot.tscn index a5e532c..3198647 100644 --- a/examples/example_mandelbrot.tscn +++ b/examples/example_mandelbrot.tscn @@ -11,7 +11,7 @@ script = ExtResource("1_5gp20") script = ExtResource("1_t5cx1") WSX = 512 WSY = 512 -glsl_file = "res://examples/example_mandelbrot.cpp" +glsl_file = "res://examples/cube/circles.cpp" data = [NodePath("../mandelbrot"), NodePath("../julia")] [node name="mandelbrot" type="Sprite2D" parent="."] diff --git a/project.godot b/project.godot index 6192edd..3bfcaf7 100644 --- a/project.godot +++ b/project.godot @@ -11,8 +11,13 @@ config_version=5 [application] config/name="compute_shader_studio" +<<<<<<< Updated upstream run/main_scene="res://examples/circles/circles.tscn" config/features=PackedStringArray("4.2", "Forward Plus") +======= +run/main_scene="uid://bn86hxa8xefmh" +config/features=PackedStringArray("4.4", "Forward Plus") +>>>>>>> Stashed changes config/icon="res://addons/compute_shader_studio/icon.png" [editor_plugins] From ee23c1bee41d3849af9a0b7bcaaa362647409012 Mon Sep 17 00:00:00 2001 From: Abdelkader MAHBOUBI Date: Wed, 12 Mar 2025 16:46:41 +0100 Subject: [PATCH 07/14] =?UTF-8?q?tr=C3=A8s=20fort=20!!!=20cube3=20omg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/circles/circles.tscn | 2 +- examples/cube2/cube.tscn | 2 +- examples/cube3/circles.cpp | 31 +++++++++++++++++++++++++++++++ examples/cube3/circles.tscn | 32 ++++++++++++++++++++++++++++++++ project.godot | 6 +----- 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 examples/cube3/circles.cpp create mode 100644 examples/cube3/circles.tscn diff --git a/examples/circles/circles.tscn b/examples/circles/circles.tscn index 3143139..3e471e9 100644 --- a/examples/circles/circles.tscn +++ b/examples/circles/circles.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://c1ux4sgouixua"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_amro2"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_amro2"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_ntl1q"] [node name="Circles" type="Node2D"] diff --git a/examples/cube2/cube.tscn b/examples/cube2/cube.tscn index ba0e55d..e6f8a20 100644 --- a/examples/cube2/cube.tscn +++ b/examples/cube2/cube.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://bn86hxa8xefmh"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_hegqh"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_hegqh"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_hegqh"] [node name="Cube" type="Node2D"] diff --git a/examples/cube3/circles.cpp b/examples/cube3/circles.cpp new file mode 100644 index 0000000..3dcaa32 --- /dev/null +++ b/examples/cube3/circles.cpp @@ -0,0 +1,31 @@ +#define RADIUS 40 + +void main() +{ + int x = int(gl_GlobalInvocationID.x); + int y = int(gl_GlobalInvocationID.y); + int p = x + y * int(WSX); + + // initial background is black & opac + if (step == 0) { + data_0[p] = 0xFF000000 + y; + } else { // draw a fading red circle + float dx = float(mousex - x); + float dy = float(mousey - y); + float dist = sqrt(dx*dx+dy*dy); + int pix = data_0[p]; + int col = pix & 0x00FFFFFF ; + float r = RADIUS + 10*sin(step/10.0f); + if( dist < r-1 ) { // interior + col = col + 0x04 ; + data_0[p] = 0xFF000000 + col ; + } + if( dist >= r-1 && dist <= r+1 ) { // perimeter + data_0[p] = 0xFF0000FF ; + } + if( dist > r + 1) { // ouside + if (col > 0) + data_0[p] = pix - 1; + } + } +} diff --git a/examples/cube3/circles.tscn b/examples/cube3/circles.tscn new file mode 100644 index 0000000..c3f8650 --- /dev/null +++ b/examples/cube3/circles.tscn @@ -0,0 +1,32 @@ +[gd_scene load_steps=3 format=3 uid="uid://e5gixrwap30u"] + +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_t2x41"] +[ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_woahi"] + +[node name="Circles" type="Node2D"] + +[node name="ComputeShaderStudio2D" type="Node" parent="." node_paths=PackedStringArray("data")] +script = ExtResource("1_t2x41") +WSX = 512 +WSY = 256 +glsl_file = "res://examples/cube2/cube.cpp" +GLSL_code = "" +data = [NodePath("../Icon")] +metadata/_custom_type_script = "uid://c8esqdv0y26yp" + +[node name="Label" type="Label" parent="."] +offset_left = 278.0 +offset_top = -1.0 +offset_right = 679.0 +offset_bottom = 101.0 +scale = Vector2(1.72, 1.72) +text = "Compute Shader Studio - Example Circles +- uses an external GLSL file +- and a Sprite2D" +horizontal_alignment = 1 +vertical_alignment = 1 + +[node name="Icon" type="Sprite2D" parent="."] +position = Vector2(592, 361) +scale = Vector2(5.63281, 3.17969) +texture = ExtResource("2_woahi") diff --git a/project.godot b/project.godot index 3bfcaf7..26509ff 100644 --- a/project.godot +++ b/project.godot @@ -11,14 +11,10 @@ config_version=5 [application] config/name="compute_shader_studio" -<<<<<<< Updated upstream run/main_scene="res://examples/circles/circles.tscn" config/features=PackedStringArray("4.2", "Forward Plus") -======= -run/main_scene="uid://bn86hxa8xefmh" -config/features=PackedStringArray("4.4", "Forward Plus") ->>>>>>> Stashed changes config/icon="res://addons/compute_shader_studio/icon.png" +<<<<<< Date: Wed, 12 Mar 2025 16:57:57 +0100 Subject: [PATCH 08/14] et oui mon gars --- examples/circles/circles.tscn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/circles/circles.tscn b/examples/circles/circles.tscn index 3e471e9..5d44f9d 100644 --- a/examples/circles/circles.tscn +++ b/examples/circles/circles.tscn @@ -3,7 +3,7 @@ [ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_amro2"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_ntl1q"] -[node name="Circles" type="Node2D"] +[node name="Circles2" type="Node2D"] [node name="ComputeShaderStudio2D" type="Node" parent="." node_paths=PackedStringArray("data")] script = ExtResource("1_amro2") From d2e6cef22048604f2707cf41d074f669d331dc3f Mon Sep 17 00:00:00 2001 From: Abdelkader MAHBOUBI Date: Wed, 12 Mar 2025 16:59:31 +0100 Subject: [PATCH 09/14] cube dans cube3 --- examples/cube3/circles.cpp | 31 ----- examples/cube3/circles.tscn | 2 +- examples/cube3/cube.cpp | 260 ++++++++++++++++++++++++++++++++++++ 3 files changed, 261 insertions(+), 32 deletions(-) delete mode 100644 examples/cube3/circles.cpp create mode 100644 examples/cube3/cube.cpp diff --git a/examples/cube3/circles.cpp b/examples/cube3/circles.cpp deleted file mode 100644 index 3dcaa32..0000000 --- a/examples/cube3/circles.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#define RADIUS 40 - -void main() -{ - int x = int(gl_GlobalInvocationID.x); - int y = int(gl_GlobalInvocationID.y); - int p = x + y * int(WSX); - - // initial background is black & opac - if (step == 0) { - data_0[p] = 0xFF000000 + y; - } else { // draw a fading red circle - float dx = float(mousex - x); - float dy = float(mousey - y); - float dist = sqrt(dx*dx+dy*dy); - int pix = data_0[p]; - int col = pix & 0x00FFFFFF ; - float r = RADIUS + 10*sin(step/10.0f); - if( dist < r-1 ) { // interior - col = col + 0x04 ; - data_0[p] = 0xFF000000 + col ; - } - if( dist >= r-1 && dist <= r+1 ) { // perimeter - data_0[p] = 0xFF0000FF ; - } - if( dist > r + 1) { // ouside - if (col > 0) - data_0[p] = pix - 1; - } - } -} diff --git a/examples/cube3/circles.tscn b/examples/cube3/circles.tscn index c3f8650..9343776 100644 --- a/examples/cube3/circles.tscn +++ b/examples/cube3/circles.tscn @@ -9,7 +9,7 @@ script = ExtResource("1_t2x41") WSX = 512 WSY = 256 -glsl_file = "res://examples/cube2/cube.cpp" +glsl_file = "res://examples/cube3/cube.cpp" GLSL_code = "" data = [NodePath("../Icon")] metadata/_custom_type_script = "uid://c8esqdv0y26yp" diff --git a/examples/cube3/cube.cpp b/examples/cube3/cube.cpp new file mode 100644 index 0000000..471048d --- /dev/null +++ b/examples/cube3/cube.cpp @@ -0,0 +1,260 @@ +void main() +{ + int x = int(gl_GlobalInvocationID.x); + int y = int(gl_GlobalInvocationID.y); + int p = x + y * int(WSX); + + // Parameters for rotation + float A = step * 0.02; // Rotation X + float B = step * 0.02; // Rotation Y + float C = step * 0.005; // Rotation Z + + // Cube size + float cubeWidth = 45.0; + int width = int(WSX); + int height = int(WSY); + + // Convert pixel coordinates to normalized coordinates + float nx = (float(x) / float(width) - 0.5) * 2.0; + float ny = (float(y) / float(height) - 0.5) * 2.0; + + // Camera distance and scale factor + float distanceFromCam = 90.0; + float K1 = 70.0; + + // Drawing parameters + float incrementSpeed = 5; + float minDist = 999999.0; + int color = 0xFF000000; // Default color (black) + bool isOnCube = false; // Flag to track if the pixel is currently on the cube + + // For the first frame, initialize everything to black + if (step == 0) { + data_0[p] = 0xFF000000; + } + else { + // Fixed rotation to show 3 faces instead of using animated rotation + // This ensures we always see the 3 faces properly + float fixedA = 0.5; // Fixed angle to show 3 faces + float fixedB = 0.3; + float fixedC = 0.1; + + // Use a combination of fixed rotation and animated rotation + float rotA = fixedA + A * 0.2; + float rotB = fixedB + B * 0.2; + float rotC = fixedC + C * 0.2; + + float threshold = 0.6; + + // Calculate faces with consistent dimensions to ensure proper cube shape + for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) { + for (float cubeY = -cubeWidth; cubeY < cubeWidth; cubeY += incrementSpeed) { + // Face 1: Right face (X = cubeWidth) + { + float x1 = cubeWidth; + float y1 = cubeY; + float z1 = cubeX; + + // Apply rotation + float rotX1 = x1 * cos(rotB) * cos(rotC) + + z1 * sin(rotB) - + y1 * cos(rotB) * sin(rotC); + float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z1 * sin(rotA) * cos(rotB); + float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z1 * cos(rotA) * cos(rotB); + + rotZ1 += distanceFromCam; + + if (rotZ1 > 0.0) { + float ooz1 = 1.0 / rotZ1; + float px1 = (width / 2.0) + K1 * ooz1 * rotX1; + float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + + float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); + if (dist1 < threshold && dist1 < minDist) { + minDist = dist1; + color = 0xFF0000FF; // Face 1: Blue + isOnCube = true; + } + } + } + + // Face 2: Top face (Y = -cubeWidth) + { + float x2 = cubeX; + float y2 = -cubeWidth; + float z2 = cubeY; + + // Apply rotation + float rotX2 = x2 * cos(rotB) * cos(rotC) + + z2 * sin(rotB) - + y2 * cos(rotB) * sin(rotC); + float rotY2 = x2 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y2 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z2 * sin(rotA) * cos(rotB); + float rotZ2 = x2 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y2 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z2 * cos(rotA) * cos(rotB); + + rotZ2 += distanceFromCam; + + if (rotZ2 > 0.0) { + float ooz2 = 1.0 / rotZ2; + float px2 = (width / 2.0) + K1 * ooz2 * rotX2; + float py2 = (height / 2.0) + K1 * ooz2 * rotY2; + + float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); + if (dist2 < threshold && dist2 < minDist) { + minDist = dist2; + color = 0xFF00FF00; // Face 2: Green + isOnCube = true; + } + } + } + + // Face 3: Front face (Z = cubeWidth) + { + float x3 = cubeX; + float y3 = cubeY; + float z3 = cubeWidth; + + // Apply rotation + float rotX3 = x3 * cos(rotB) * cos(rotC) + + z3 * sin(rotB) - + y3 * cos(rotB) * sin(rotC); + float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z3 * sin(rotA) * cos(rotB); + float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z3 * cos(rotA) * cos(rotB); + + rotZ3 += distanceFromCam; + + if (rotZ3 > 0.0) { + float ooz3 = 1.0 / rotZ3; + float px3 = (width / 2.0) + K1 * ooz3 * rotX3; + float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + + float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); + if (dist3 < threshold && dist3 < minDist) { + minDist = dist3; + color = 0xFFFF8000; // Face 3: Orange + isOnCube = true; + } + } + } + + // Face 4: Right face (X = cubeWidth) + { + float x1 = -cubeWidth; + float y1 = cubeY; + float z1 = cubeX; + + // Apply rotation + float rotX1 = x1 * cos(rotB) * cos(rotC) + + z1 * sin(rotB) - + y1 * cos(rotB) * sin(rotC); + float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z1 * sin(rotA) * cos(rotB); + float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z1 * cos(rotA) * cos(rotB); + + rotZ1 += distanceFromCam; + + if (rotZ1 > 0.0) { + float ooz1 = 1.0 / rotZ1; + float px1 = (width / 2.0) + K1 * ooz1 * rotX1; + float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + + float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); + if (dist1 < threshold && dist1 < minDist) { + minDist = dist1; + color = 0xFF0000FF; // Face 1: Blue + isOnCube = true; + } + } + } + + // Face 5: Top face (Y = -cubeWidth) + { + float x2 = cubeX; + float y2 = cubeWidth; + float z2 = cubeY; + + // Apply rotation + float rotX2 = x2 * cos(rotB) * cos(rotC) + + z2 * sin(rotB) - + y2 * cos(rotB) * sin(rotC); + float rotY2 = x2 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y2 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z2 * sin(rotA) * cos(rotB); + float rotZ2 = x2 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y2 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z2 * cos(rotA) * cos(rotB); + + rotZ2 += distanceFromCam; + + if (rotZ2 > 0.0) { + float ooz2 = 1.0 / rotZ2; + float px2 = (width / 2.0) + K1 * ooz2 * rotX2; + float py2 = (height / 2.0) + K1 * ooz2 * rotY2; + + float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); + if (dist2 < threshold && dist2 < minDist) { + minDist = dist2; + color = 0xFF00FF00; // Face 2: Green + isOnCube = true; + } + } + } + + // Face 6: Front face (Z = cubeWidth) + { + float x3 = cubeX; + float y3 = cubeY; + float z3 = -cubeWidth; + + // Apply rotation + float rotX3 = x3 * cos(rotB) * cos(rotC) + + z3 * sin(rotB) - + y3 * cos(rotB) * sin(rotC); + float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z3 * sin(rotA) * cos(rotB); + float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z3 * cos(rotA) * cos(rotB); + + rotZ3 += distanceFromCam; + + if (rotZ3 > 0.0) { + float ooz3 = 1.0 / rotZ3; + float px3 = (width / 2.0) + K1 * ooz3 * rotX3; + float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + + float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); + if (dist3 < threshold && dist3 < minDist) { + minDist = dist3; + color = 0xFFFF8000; // Face 3: Orange + isOnCube = true; + } + } + } + } + } + + // No trailing effect + if (isOnCube) { + data_0[p] = color; + } else { + data_0[p] = 0xFF000000; + } + } +} + From b7652c03ee9babbb79d26fc6d0c031f789ebc122 Mon Sep 17 00:00:00 2001 From: Tom Yvinec Date: Thu, 13 Mar 2025 16:54:57 +0100 Subject: [PATCH 10/14] Ajout de plusieurs cubes --- examples/cube3/circles.tscn | 4 +- examples/cube3/cube.cpp | 484 ++++++++++++++++++++---------------- project.godot | 2 +- 3 files changed, 275 insertions(+), 215 deletions(-) diff --git a/examples/cube3/circles.tscn b/examples/cube3/circles.tscn index 9343776..99f13bf 100644 --- a/examples/cube3/circles.tscn +++ b/examples/cube3/circles.tscn @@ -27,6 +27,6 @@ horizontal_alignment = 1 vertical_alignment = 1 [node name="Icon" type="Sprite2D" parent="."] -position = Vector2(592, 361) -scale = Vector2(5.63281, 3.17969) +position = Vector2(575, 323.5) +scale = Vector2(8.95313, 5.03906) texture = ExtResource("2_woahi") diff --git a/examples/cube3/cube.cpp b/examples/cube3/cube.cpp index 471048d..4c99974 100644 --- a/examples/cube3/cube.cpp +++ b/examples/cube3/cube.cpp @@ -4,257 +4,317 @@ void main() int y = int(gl_GlobalInvocationID.y); int p = x + y * int(WSX); - // Parameters for rotation - float A = step * 0.02; // Rotation X - float B = step * 0.02; // Rotation Y - float C = step * 0.005; // Rotation Z - - // Cube size - float cubeWidth = 45.0; + // Paramètres généraux int width = int(WSX); int height = int(WSY); - - // Convert pixel coordinates to normalized coordinates - float nx = (float(x) / float(width) - 0.5) * 2.0; - float ny = (float(y) / float(height) - 0.5) * 2.0; - - // Camera distance and scale factor - float distanceFromCam = 90.0; + float distanceFromCam = 150.0; float K1 = 70.0; - // Drawing parameters - float incrementSpeed = 5; - float minDist = 999999.0; - int color = 0xFF000000; // Default color (black) - bool isOnCube = false; // Flag to track if the pixel is currently on the cube - - // For the first frame, initialize everything to black + // Pour le premier frame, initialiser tout en noir if (step == 0) { data_0[p] = 0xFF000000; } else { - // Fixed rotation to show 3 faces instead of using animated rotation - // This ensures we always see the 3 faces properly - float fixedA = 0.5; // Fixed angle to show 3 faces - float fixedB = 0.3; - float fixedC = 0.1; + // Nombre de cubes + const int numCubes = 3; - // Use a combination of fixed rotation and animated rotation - float rotA = fixedA + A * 0.2; - float rotB = fixedB + B * 0.2; - float rotC = fixedC + C * 0.2; + float minDist = 999999.0; + int color = 0xFF000000; // Couleur par défaut (noir) + bool isOnCube = false; // Indicateur si le pixel est sur un cube - float threshold = 0.6; + // Paramètres individuels pour chaque cube + float cubeWidth[numCubes] = float[](35.0, 35.0, 35.0); - // Calculate faces with consistent dimensions to ensure proper cube shape - for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) { - for (float cubeY = -cubeWidth; cubeY < cubeWidth; cubeY += incrementSpeed) { - // Face 1: Right face (X = cubeWidth) - { - float x1 = cubeWidth; - float y1 = cubeY; - float z1 = cubeX; - - // Apply rotation - float rotX1 = x1 * cos(rotB) * cos(rotC) + - z1 * sin(rotB) - - y1 * cos(rotB) * sin(rotC); - float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + - y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - - z1 * sin(rotA) * cos(rotB); - float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + - y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + - z1 * cos(rotA) * cos(rotB); - - rotZ1 += distanceFromCam; - - if (rotZ1 > 0.0) { - float ooz1 = 1.0 / rotZ1; - float px1 = (width / 2.0) + K1 * ooz1 * rotX1; - float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + // Espacer les cubes horizontalement et ajouter mouvement aléatoire + // Utiliser différentes fonctions de bruit pseudo-aléatoire basées sur le temps + vec3 cubePositions[numCubes] = vec3[]( + // Cube 1: position de base à gauche + mouvement aléatoire + vec3(-100.0 + sin(step * 0.023) * 20.0 + cos(step * 0.019) * 15.0, + sin(step * 0.017) * 25.0 + cos(step * 0.011) * 10.0, + sin(step * 0.013) * 15.0 + cos(step * 0.027) * 20.0), + + // Cube 2: position de base au centre + mouvement aléatoire + vec3(0.0 + sin(step * 0.021) * 15.0 + cos(step * 0.015) * 25.0, + sin(step * 0.012) * 20.0 + cos(step * 0.025) * 15.0, + sin(step * 0.018) * 25.0 + cos(step * 0.022) * 10.0), + + // Cube 3: position de base à droite + mouvement aléatoire + vec3(100.0 + sin(step * 0.016) * 25.0 + cos(step * 0.024) * 15.0, + sin(step * 0.026) * 15.0 + cos(step * 0.014) * 20.0, + sin(step * 0.022) * 20.0 + cos(step * 0.018) * 25.0) + ); + + // Rotations différentes pour chaque cube + vec3 cubeRotations[numCubes] = vec3[]( + vec3(step * 0.017, step * 0.019, step * 0.013), + vec3(step * 0.015, step * 0.021, step * 0.018), + vec3(step * 0.012, step * 0.023, step * 0.016) + ); + + int cubeColors[numCubes][3] = int[3][]( + int[](0xFF0000FF, 0xFF00FF00, 0xFFFF8000), // Cube 1: Bleu, Vert, Orange + int[](0xFFFF0000, 0xFF00FFFF, 0xFFFF00FF), // Cube 2: Rouge, Cyan, Magenta + int[](0xFFFFFF00, 0xFF8000FF, 0xFF00FF80) // Cube 3: Jaune, Violet, Vert clair + ); + + // Paramètres de dessin + float incrementSpeed = 5.0; + float threshold = 0.1; // Points encore plus gros + + // Traiter chaque cube + for (int cubeIndex = 0; cubeIndex < numCubes; cubeIndex++) { + // Rotation fixe + animée + float fixedA = 0.5; // Angle fixe pour montrer 3 faces + float fixedB = 0.3; + float fixedC = 0.1; + + vec3 rot = cubeRotations[cubeIndex]; + float rotA = fixedA + rot.x * 0.2; + float rotB = fixedB + rot.y * 0.2; + float rotC = fixedC + rot.z * 0.2; + + // Position du cube + vec3 cubePos = cubePositions[cubeIndex]; + float currentCubeWidth = cubeWidth[cubeIndex]; + + // Calculer les faces avec des dimensions cohérentes + for (float cubeX = -currentCubeWidth; cubeX < currentCubeWidth; cubeX += incrementSpeed) { + for (float cubeY = -currentCubeWidth; cubeY < currentCubeWidth; cubeY += incrementSpeed) { + // Face 1: Face droite (X = currentCubeWidth) + { + float x1 = currentCubeWidth; + float y1 = cubeY; + float z1 = cubeX; + + // Ajouter le décalage de position + x1 += cubePos.x; + y1 += cubePos.y; + z1 += cubePos.z; - float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); - if (dist1 < threshold && dist1 < minDist) { - minDist = dist1; - color = 0xFF0000FF; // Face 1: Blue - isOnCube = true; + // Appliquer la rotation + float rotX1 = x1 * cos(rotB) * cos(rotC) + + z1 * sin(rotB) - + y1 * cos(rotB) * sin(rotC); + float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z1 * sin(rotA) * cos(rotB); + float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z1 * cos(rotA) * cos(rotB); + + rotZ1 += distanceFromCam; + + if (rotZ1 > 0.0) { + float ooz1 = 1.0 / rotZ1; + float px1 = (width / 2.0) + K1 * ooz1 * rotX1; + float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + + float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); + if (dist1 < threshold && dist1 < minDist) { + minDist = dist1; + color = cubeColors[cubeIndex][0]; // Face 1 color + isOnCube = true; + } } } - } - - // Face 2: Top face (Y = -cubeWidth) - { - float x2 = cubeX; - float y2 = -cubeWidth; - float z2 = cubeY; - // Apply rotation - float rotX2 = x2 * cos(rotB) * cos(rotC) + - z2 * sin(rotB) - - y2 * cos(rotB) * sin(rotC); - float rotY2 = x2 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + - y2 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - - z2 * sin(rotA) * cos(rotB); - float rotZ2 = x2 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + - y2 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + - z2 * cos(rotA) * cos(rotB); - - rotZ2 += distanceFromCam; - - if (rotZ2 > 0.0) { - float ooz2 = 1.0 / rotZ2; - float px2 = (width / 2.0) + K1 * ooz2 * rotX2; - float py2 = (height / 2.0) + K1 * ooz2 * rotY2; + // Face 2: Face supérieure (Y = -currentCubeWidth) + { + float x2 = cubeX; + float y2 = -currentCubeWidth; + float z2 = cubeY; + + // Ajouter le décalage de position + x2 += cubePos.x; + y2 += cubePos.y; + z2 += cubePos.z; - float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); - if (dist2 < threshold && dist2 < minDist) { - minDist = dist2; - color = 0xFF00FF00; // Face 2: Green - isOnCube = true; + // Appliquer la rotation + float rotX2 = x2 * cos(rotB) * cos(rotC) + + z2 * sin(rotB) - + y2 * cos(rotB) * sin(rotC); + float rotY2 = x2 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y2 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z2 * sin(rotA) * cos(rotB); + float rotZ2 = x2 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y2 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z2 * cos(rotA) * cos(rotB); + + rotZ2 += distanceFromCam; + + if (rotZ2 > 0.0) { + float ooz2 = 1.0 / rotZ2; + float px2 = (width / 2.0) + K1 * ooz2 * rotX2; + float py2 = (height / 2.0) + K1 * ooz2 * rotY2; + + float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); + if (dist2 < threshold && dist2 < minDist) { + minDist = dist2; + color = cubeColors[cubeIndex][1]; // Face 2 color + isOnCube = true; + } } } - } - - // Face 3: Front face (Z = cubeWidth) - { - float x3 = cubeX; - float y3 = cubeY; - float z3 = cubeWidth; - - // Apply rotation - float rotX3 = x3 * cos(rotB) * cos(rotC) + - z3 * sin(rotB) - - y3 * cos(rotB) * sin(rotC); - float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + - y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - - z3 * sin(rotA) * cos(rotB); - float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + - y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + - z3 * cos(rotA) * cos(rotB); - - rotZ3 += distanceFromCam; - if (rotZ3 > 0.0) { - float ooz3 = 1.0 / rotZ3; - float px3 = (width / 2.0) + K1 * ooz3 * rotX3; - float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + // Face 3: Face avant (Z = currentCubeWidth) + { + float x3 = cubeX; + float y3 = cubeY; + float z3 = currentCubeWidth; - float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); - if (dist3 < threshold && dist3 < minDist) { - minDist = dist3; - color = 0xFFFF8000; // Face 3: Orange - isOnCube = true; + // Ajouter le décalage de position + x3 += cubePos.x; + y3 += cubePos.y; + z3 += cubePos.z; + + // Appliquer la rotation + float rotX3 = x3 * cos(rotB) * cos(rotC) + + z3 * sin(rotB) - + y3 * cos(rotB) * sin(rotC); + float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z3 * sin(rotA) * cos(rotB); + float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z3 * cos(rotA) * cos(rotB); + + rotZ3 += distanceFromCam; + + if (rotZ3 > 0.0) { + float ooz3 = 1.0 / rotZ3; + float px3 = (width / 2.0) + K1 * ooz3 * rotX3; + float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + + float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); + if (dist3 < threshold && dist3 < minDist) { + minDist = dist3; + color = cubeColors[cubeIndex][2]; // Face 3 color + isOnCube = true; + } } } - } - - // Face 4: Right face (X = cubeWidth) - { - float x1 = -cubeWidth; - float y1 = cubeY; - float z1 = cubeX; - - // Apply rotation - float rotX1 = x1 * cos(rotB) * cos(rotC) + - z1 * sin(rotB) - - y1 * cos(rotB) * sin(rotC); - float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + - y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - - z1 * sin(rotA) * cos(rotB); - float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + - y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + - z1 * cos(rotA) * cos(rotB); - - rotZ1 += distanceFromCam; - - if (rotZ1 > 0.0) { - float ooz1 = 1.0 / rotZ1; - float px1 = (width / 2.0) + K1 * ooz1 * rotX1; - float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + // Face 1: Face droite (X = currentCubeWidth) + { + float x1 = -currentCubeWidth; + float y1 = cubeY; + float z1 = cubeX; + + // Ajouter le décalage de position + x1 += cubePos.x; + y1 += cubePos.y; + z1 += cubePos.z; + + // Appliquer la rotation + float rotX1 = x1 * cos(rotB) * cos(rotC) + + z1 * sin(rotB) - + y1 * cos(rotB) * sin(rotC); + float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z1 * sin(rotA) * cos(rotB); + float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z1 * cos(rotA) * cos(rotB); - float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); - if (dist1 < threshold && dist1 < minDist) { - minDist = dist1; - color = 0xFF0000FF; // Face 1: Blue - isOnCube = true; + rotZ1 += distanceFromCam; + + if (rotZ1 > 0.0) { + float ooz1 = 1.0 / rotZ1; + float px1 = (width / 2.0) + K1 * ooz1 * rotX1; + float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + + float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); + if (dist1 < threshold && dist1 < minDist) { + minDist = dist1; + color = cubeColors[cubeIndex][0]; // Face 1 color + isOnCube = true; + } } } - } - - // Face 5: Top face (Y = -cubeWidth) - { - float x2 = cubeX; - float y2 = cubeWidth; - float z2 = cubeY; - // Apply rotation - float rotX2 = x2 * cos(rotB) * cos(rotC) + - z2 * sin(rotB) - - y2 * cos(rotB) * sin(rotC); - float rotY2 = x2 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + - y2 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - - z2 * sin(rotA) * cos(rotB); - float rotZ2 = x2 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + - y2 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + - z2 * cos(rotA) * cos(rotB); - - rotZ2 += distanceFromCam; - - if (rotZ2 > 0.0) { - float ooz2 = 1.0 / rotZ2; - float px2 = (width / 2.0) + K1 * ooz2 * rotX2; - float py2 = (height / 2.0) + K1 * ooz2 * rotY2; + // Face 2: Face supérieure (Y = -currentCubeWidth) + { + float x2 = cubeX; + float y2 = currentCubeWidth; + float z2 = cubeY; + + // Ajouter le décalage de position + x2 += cubePos.x; + y2 += cubePos.y; + z2 += cubePos.z; - float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); - if (dist2 < threshold && dist2 < minDist) { - minDist = dist2; - color = 0xFF00FF00; // Face 2: Green - isOnCube = true; + // Appliquer la rotation + float rotX2 = x2 * cos(rotB) * cos(rotC) + + z2 * sin(rotB) - + y2 * cos(rotB) * sin(rotC); + float rotY2 = x2 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y2 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z2 * sin(rotA) * cos(rotB); + float rotZ2 = x2 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y2 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z2 * cos(rotA) * cos(rotB); + + rotZ2 += distanceFromCam; + + if (rotZ2 > 0.0) { + float ooz2 = 1.0 / rotZ2; + float px2 = (width / 2.0) + K1 * ooz2 * rotX2; + float py2 = (height / 2.0) + K1 * ooz2 * rotY2; + + float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); + if (dist2 < threshold && dist2 < minDist) { + minDist = dist2; + color = cubeColors[cubeIndex][1]; // Face 2 color + isOnCube = true; + } } } - } - - // Face 6: Front face (Z = cubeWidth) - { - float x3 = cubeX; - float y3 = cubeY; - float z3 = -cubeWidth; - // Apply rotation - float rotX3 = x3 * cos(rotB) * cos(rotC) + - z3 * sin(rotB) - - y3 * cos(rotB) * sin(rotC); - float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + - y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - - z3 * sin(rotA) * cos(rotB); - float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + - y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + - z3 * cos(rotA) * cos(rotB); - - rotZ3 += distanceFromCam; - - if (rotZ3 > 0.0) { - float ooz3 = 1.0 / rotZ3; - float px3 = (width / 2.0) + K1 * ooz3 * rotX3; - float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + // Face 3: Face avant (Z = currentCubeWidth) + { + float x3 = cubeX; + float y3 = cubeY; + float z3 = -currentCubeWidth; + + // Ajouter le décalage de position + x3 += cubePos.x; + y3 += cubePos.y; + z3 += cubePos.z; + + // Appliquer la rotation + float rotX3 = x3 * cos(rotB) * cos(rotC) + + z3 * sin(rotB) - + y3 * cos(rotB) * sin(rotC); + float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z3 * sin(rotA) * cos(rotB); + float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z3 * cos(rotA) * cos(rotB); + + rotZ3 += distanceFromCam; - float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); - if (dist3 < threshold && dist3 < minDist) { - minDist = dist3; - color = 0xFFFF8000; // Face 3: Orange - isOnCube = true; + if (rotZ3 > 0.0) { + float ooz3 = 1.0 / rotZ3; + float px3 = (width / 2.0) + K1 * ooz3 * rotX3; + float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + + float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); + if (dist3 < threshold && dist3 < minDist) { + minDist = dist3; + color = cubeColors[cubeIndex][2]; // Face 3 color + isOnCube = true; + } } } } } } - // No trailing effect + // Pas d'effet de traînée if (isOnCube) { data_0[p] = color; } else { - data_0[p] = 0xFF000000; + data_0[p] = 0xFF000000; // Noir pour les pixels qui ne sont pas sur un cube } } -} - +} \ No newline at end of file diff --git a/project.godot b/project.godot index 26509ff..5c85153 100644 --- a/project.godot +++ b/project.godot @@ -11,7 +11,7 @@ config_version=5 [application] config/name="compute_shader_studio" -run/main_scene="res://examples/circles/circles.tscn" +run/main_scene="res://examples/cube3/circles.tscn" config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://addons/compute_shader_studio/icon.png" <<<<<< Date: Thu, 13 Mar 2025 17:09:50 +0100 Subject: [PATCH 11/14] background --- examples/cube3/cube.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/cube3/cube.cpp b/examples/cube3/cube.cpp index 4c99974..9bb8af3 100644 --- a/examples/cube3/cube.cpp +++ b/examples/cube3/cube.cpp @@ -12,14 +12,14 @@ void main() // Pour le premier frame, initialiser tout en noir if (step == 0) { - data_0[p] = 0xFF000000; + data_0[p] = 0xFF260B42; } else { // Nombre de cubes const int numCubes = 3; float minDist = 999999.0; - int color = 0xFF000000; // Couleur par défaut (noir) + int color = 0xFF260B42; // Couleur par défaut (noir) bool isOnCube = false; // Indicateur si le pixel est sur un cube // Paramètres individuels pour chaque cube @@ -314,7 +314,7 @@ void main() if (isOnCube) { data_0[p] = color; } else { - data_0[p] = 0xFF000000; // Noir pour les pixels qui ne sont pas sur un cube + data_0[p] = 0xFF641E16; // Noir pour les pixels qui ne sont pas sur un cube } } } \ No newline at end of file From 53d0f2df5fe6d06a39e4bf36f43016b8d44cd679 Mon Sep 17 00:00:00 2001 From: Alexandre1422 Date: Mon, 17 Mar 2025 23:15:05 +0100 Subject: [PATCH 12/14] Retour cube de base (cube2) --- examples/circles/circles.tscn | 2 +- examples/cube2/cube.cpp | 4 ++-- examples/cube2/cube.tscn | 5 ++++- examples/cube3/circles.tscn | 2 +- project.godot | 4 ++-- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/circles/circles.tscn b/examples/circles/circles.tscn index 5d44f9d..6135dc6 100644 --- a/examples/circles/circles.tscn +++ b/examples/circles/circles.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://c1ux4sgouixua"] -[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_amro2"] +[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_amro2"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_ntl1q"] [node name="Circles2" type="Node2D"] diff --git a/examples/cube2/cube.cpp b/examples/cube2/cube.cpp index 471048d..68c35b5 100644 --- a/examples/cube2/cube.cpp +++ b/examples/cube2/cube.cpp @@ -109,7 +109,7 @@ void main() float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); if (dist2 < threshold && dist2 < minDist) { minDist = dist2; - color = 0xFF00FF00; // Face 2: Green + color = 0xFFFFFFFF; // Face 2: Green isOnCube = true; } } @@ -208,7 +208,7 @@ void main() float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); if (dist2 < threshold && dist2 < minDist) { minDist = dist2; - color = 0xFF00FF00; // Face 2: Green + color = 0xFFFFFFFF; // Face 2: Green isOnCube = true; } } diff --git a/examples/cube2/cube.tscn b/examples/cube2/cube.tscn index e6f8a20..82301ba 100644 --- a/examples/cube2/cube.tscn +++ b/examples/cube2/cube.tscn @@ -1,13 +1,16 @@ [gd_scene load_steps=3 format=3 uid="uid://bn86hxa8xefmh"] -[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_hegqh"] +[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_hegqh"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_hegqh"] [node name="Cube" type="Node2D"] [node name="ComputeShaderStudio2D" type="Node" parent="." node_paths=PackedStringArray("data")] script = ExtResource("1_hegqh") +WSX = 512 +WSY = 256 glsl_file = "res://examples/cube2/cube.cpp" +GLSL_code = "" data = [NodePath("../Icon")] metadata/_custom_type_script = "uid://c8esqdv0y26yp" diff --git a/examples/cube3/circles.tscn b/examples/cube3/circles.tscn index 9343776..e5ccaba 100644 --- a/examples/cube3/circles.tscn +++ b/examples/cube3/circles.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://e5gixrwap30u"] -[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_t2x41"] +[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_t2x41"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_woahi"] [node name="Circles" type="Node2D"] diff --git a/project.godot b/project.godot index 26509ff..54c5aa4 100644 --- a/project.godot +++ b/project.godot @@ -11,8 +11,8 @@ config_version=5 [application] config/name="compute_shader_studio" -run/main_scene="res://examples/circles/circles.tscn" -config/features=PackedStringArray("4.2", "Forward Plus") +run/main_scene="uid://bn86hxa8xefmh" +config/features=PackedStringArray("4.4", "Forward Plus") config/icon="res://addons/compute_shader_studio/icon.png" <<<<<< Date: Mon, 17 Mar 2025 23:23:34 +0100 Subject: [PATCH 13/14] Fond transparent (cube2) --- examples/cube2/cube.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/cube2/cube.cpp b/examples/cube2/cube.cpp index 68c35b5..67a251d 100644 --- a/examples/cube2/cube.cpp +++ b/examples/cube2/cube.cpp @@ -253,7 +253,7 @@ void main() if (isOnCube) { data_0[p] = color; } else { - data_0[p] = 0xFF000000; + data_0[p] = 0xFF4C4C4C; } } } From e7e504c414ce3f2a23224ce9872b318a2cc49af3 Mon Sep 17 00:00:00 2001 From: Alexandre1422 Date: Tue, 18 Mar 2025 00:16:35 +0100 Subject: [PATCH 14/14] Version Tom sans warnings, rename cube --- examples/cube3/cube.cpp | 269 ++++++++++----------- examples/cube3/{circles.tscn => cube.tscn} | 0 examples/example_mandelbrot.gd.uid | 1 + project.godot | 2 +- 4 files changed, 134 insertions(+), 138 deletions(-) rename examples/cube3/{circles.tscn => cube.tscn} (100%) create mode 100644 examples/example_mandelbrot.gd.uid diff --git a/examples/cube3/cube.cpp b/examples/cube3/cube.cpp index 9bb8af3..a000b51 100644 --- a/examples/cube3/cube.cpp +++ b/examples/cube3/cube.cpp @@ -4,67 +4,71 @@ void main() int y = int(gl_GlobalInvocationID.y); int p = x + y * int(WSX); - // Paramètres généraux + // General parameters int width = int(WSX); int height = int(WSY); float distanceFromCam = 150.0; float K1 = 70.0; - // Pour le premier frame, initialiser tout en noir + // Background color constant + int backgroundColor = 0xFF641E16; + + // For the first frame, initialize everything with background color if (step == 0) { - data_0[p] = 0xFF260B42; + data_0[p] = backgroundColor; } else { - // Nombre de cubes + // Number of cubes const int numCubes = 3; float minDist = 999999.0; - int color = 0xFF260B42; // Couleur par défaut (noir) - bool isOnCube = false; // Indicateur si le pixel est sur un cube + int color = backgroundColor; // Default color (background) + bool isOnCube = false; // Indicator if pixel is on a cube - // Paramètres individuels pour chaque cube + // Individual parameters for each cube float cubeWidth[numCubes] = float[](35.0, 35.0, 35.0); - - // Espacer les cubes horizontalement et ajouter mouvement aléatoire - // Utiliser différentes fonctions de bruit pseudo-aléatoire basées sur le temps + + // Space cubes horizontally and add more lateral movement vec3 cubePositions[numCubes] = vec3[]( - // Cube 1: position de base à gauche + mouvement aléatoire - vec3(-100.0 + sin(step * 0.023) * 20.0 + cos(step * 0.019) * 15.0, - sin(step * 0.017) * 25.0 + cos(step * 0.011) * 10.0, - sin(step * 0.013) * 15.0 + cos(step * 0.027) * 20.0), - - // Cube 2: position de base au centre + mouvement aléatoire - vec3(0.0 + sin(step * 0.021) * 15.0 + cos(step * 0.015) * 25.0, - sin(step * 0.012) * 20.0 + cos(step * 0.025) * 15.0, - sin(step * 0.018) * 25.0 + cos(step * 0.022) * 10.0), - - // Cube 3: position de base à droite + mouvement aléatoire - vec3(100.0 + sin(step * 0.016) * 25.0 + cos(step * 0.024) * 15.0, - sin(step * 0.026) * 15.0 + cos(step * 0.014) * 20.0, - sin(step * 0.022) * 20.0 + cos(step * 0.018) * 25.0) + // Cube 1: rotated 90 up + vec3(-100.0 + sin(step * 0.010) * 50.0, + sin(step * 0.006) * 15.0, + cos(step * 0.015) * 40.0), + + // Cube 2: rotated 90 up + vec3(0.0 + sin(step * 0.011) * 60.0, + sin(step * 0.009) * 20.0, + cos(step * 0.007) * 45.0), + + // Cube 3: rotated 90 up + vec3(100.0 + sin(step * 0.008) * 55.0, + sin(step * 0.011) * 25.0, + cos(step * 0.014) * 35.0) ); + + - // Rotations différentes pour chaque cube + // Different rotations for each cube - reduced for smoother movement vec3 cubeRotations[numCubes] = vec3[]( - vec3(step * 0.017, step * 0.019, step * 0.013), - vec3(step * 0.015, step * 0.021, step * 0.018), - vec3(step * 0.012, step * 0.023, step * 0.016) + vec3(step * 0.008, step * 0.009, step * 0.007), + vec3(step * 0.007, step * 0.010, step * 0.009), + vec3(step * 0.006, step * 0.011, step * 0.008) ); - int cubeColors[numCubes][3] = int[3][]( - int[](0xFF0000FF, 0xFF00FF00, 0xFFFF8000), // Cube 1: Bleu, Vert, Orange - int[](0xFFFF0000, 0xFF00FFFF, 0xFFFF00FF), // Cube 2: Rouge, Cyan, Magenta - int[](0xFFFFFF00, 0xFF8000FF, 0xFF00FF80) // Cube 3: Jaune, Violet, Vert clair + int cubeColors[numCubes][6] = int[3][]( + int[](0xFF0000FF, 0xFF00FF00, 0xFFFF8000, 0xFF0080FF, 0xFFFF00FF, 0xFF80FF00), // Cube 1 + int[](0xFFFF0000, 0xFF00FFFF, 0xFFFF00FF, 0xFF8000FF, 0xFF00FF80, 0xFFFFFF00), // Cube 2 + int[](0xFFFFFF00, 0xFF8000FF, 0xFF00FF80, 0xFFFF0000, 0xFF00FFFF, 0xFFFF8000) // Cube 3 ); - // Paramètres de dessin - float incrementSpeed = 5.0; - float threshold = 0.1; // Points encore plus gros + // IMPROVED drawing parameters + float incrementSpeed = 5.0; // Much smaller for better density + float threshold = 0.3; // Larger for slightly overlapping points - // Traiter chaque cube + // Process each cube for (int cubeIndex = 0; cubeIndex < numCubes; cubeIndex++) { - // Rotation fixe + animée - float fixedA = 0.5; // Angle fixe pour montrer 3 faces + // Fixed + animated rotation + float fixedA = 0.5; // Fixed angle to show 3 faces float fixedB = 0.3; float fixedC = 0.1; @@ -73,25 +77,24 @@ void main() float rotB = fixedB + rot.y * 0.2; float rotC = fixedC + rot.z * 0.2; - // Position du cube + // Cube position vec3 cubePos = cubePositions[cubeIndex]; float currentCubeWidth = cubeWidth[cubeIndex]; - // Calculer les faces avec des dimensions cohérentes - for (float cubeX = -currentCubeWidth; cubeX < currentCubeWidth; cubeX += incrementSpeed) { - for (float cubeY = -currentCubeWidth; cubeY < currentCubeWidth; cubeY += incrementSpeed) { - // Face 1: Face droite (X = currentCubeWidth) + // Calculate all 6 cube faces for complete representation + for (float cubeX = -currentCubeWidth; cubeX <= currentCubeWidth; cubeX += incrementSpeed) { + for (float cubeY = -currentCubeWidth; cubeY <= currentCubeWidth; cubeY += incrementSpeed) { + // Face 1: Right face (X = currentCubeWidth) { float x1 = currentCubeWidth; float y1 = cubeY; float z1 = cubeX; - // Ajouter le décalage de position x1 += cubePos.x; y1 += cubePos.y; z1 += cubePos.z; - // Appliquer la rotation + // Apply rotation float rotX1 = x1 * cos(rotB) * cos(rotC) + z1 * sin(rotB) - y1 * cos(rotB) * sin(rotC); @@ -118,18 +121,54 @@ void main() } } - // Face 2: Face supérieure (Y = -currentCubeWidth) + // Face 2: Left face (X = -currentCubeWidth) + { + float x1 = -currentCubeWidth; + float y1 = cubeY; + float z1 = cubeX; + + x1 += cubePos.x; + y1 += cubePos.y; + z1 += cubePos.z; + + // Apply rotation + float rotX1 = x1 * cos(rotB) * cos(rotC) + + z1 * sin(rotB) - + y1 * cos(rotB) * sin(rotC); + float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z1 * sin(rotA) * cos(rotB); + float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z1 * cos(rotA) * cos(rotB); + + rotZ1 += distanceFromCam; + + if (rotZ1 > 0.0) { + float ooz1 = 1.0 / rotZ1; + float px1 = (width / 2.0) + K1 * ooz1 * rotX1; + float py1 = (height / 2.0) + K1 * ooz1 * rotY1; + + float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); + if (dist1 < threshold && dist1 < minDist) { + minDist = dist1; + color = cubeColors[cubeIndex][1]; // Face 2 color + isOnCube = true; + } + } + } + + // Face 3: Top face (Y = -currentCubeWidth) { float x2 = cubeX; float y2 = -currentCubeWidth; float z2 = cubeY; - // Ajouter le décalage de position x2 += cubePos.x; y2 += cubePos.y; z2 += cubePos.z; - // Appliquer la rotation + // Apply rotation float rotX2 = x2 * cos(rotB) * cos(rotC) + z2 * sin(rotB) - y2 * cos(rotB) * sin(rotC); @@ -150,99 +189,23 @@ void main() float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); if (dist2 < threshold && dist2 < minDist) { minDist = dist2; - color = cubeColors[cubeIndex][1]; // Face 2 color - isOnCube = true; - } - } - } - - // Face 3: Face avant (Z = currentCubeWidth) - { - float x3 = cubeX; - float y3 = cubeY; - float z3 = currentCubeWidth; - - // Ajouter le décalage de position - x3 += cubePos.x; - y3 += cubePos.y; - z3 += cubePos.z; - - // Appliquer la rotation - float rotX3 = x3 * cos(rotB) * cos(rotC) + - z3 * sin(rotB) - - y3 * cos(rotB) * sin(rotC); - float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + - y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - - z3 * sin(rotA) * cos(rotB); - float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + - y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + - z3 * cos(rotA) * cos(rotB); - - rotZ3 += distanceFromCam; - - if (rotZ3 > 0.0) { - float ooz3 = 1.0 / rotZ3; - float px3 = (width / 2.0) + K1 * ooz3 * rotX3; - float py3 = (height / 2.0) + K1 * ooz3 * rotY3; - - float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); - if (dist3 < threshold && dist3 < minDist) { - minDist = dist3; color = cubeColors[cubeIndex][2]; // Face 3 color isOnCube = true; } } } - // Face 1: Face droite (X = currentCubeWidth) - { - float x1 = -currentCubeWidth; - float y1 = cubeY; - float z1 = cubeX; - - // Ajouter le décalage de position - x1 += cubePos.x; - y1 += cubePos.y; - z1 += cubePos.z; - - // Appliquer la rotation - float rotX1 = x1 * cos(rotB) * cos(rotC) + - z1 * sin(rotB) - - y1 * cos(rotB) * sin(rotC); - float rotY1 = x1 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + - y1 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - - z1 * sin(rotA) * cos(rotB); - float rotZ1 = x1 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + - y1 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + - z1 * cos(rotA) * cos(rotB); - - rotZ1 += distanceFromCam; - - if (rotZ1 > 0.0) { - float ooz1 = 1.0 / rotZ1; - float px1 = (width / 2.0) + K1 * ooz1 * rotX1; - float py1 = (height / 2.0) + K1 * ooz1 * rotY1; - - float dist1 = (px1 - float(x)) * (px1 - float(x)) + (py1 - float(y)) * (py1 - float(y)); - if (dist1 < threshold && dist1 < minDist) { - minDist = dist1; - color = cubeColors[cubeIndex][0]; // Face 1 color - isOnCube = true; - } - } - } - // Face 2: Face supérieure (Y = -currentCubeWidth) + // Face 4: Bottom face (Y = currentCubeWidth) { float x2 = cubeX; float y2 = currentCubeWidth; float z2 = cubeY; - // Ajouter le décalage de position x2 += cubePos.x; y2 += cubePos.y; z2 += cubePos.z; - // Appliquer la rotation + // Apply rotation float rotX2 = x2 * cos(rotB) * cos(rotC) + z2 * sin(rotB) - y2 * cos(rotB) * sin(rotC); @@ -263,24 +226,60 @@ void main() float dist2 = (px2 - float(x)) * (px2 - float(x)) + (py2 - float(y)) * (py2 - float(y)); if (dist2 < threshold && dist2 < minDist) { minDist = dist2; - color = cubeColors[cubeIndex][1]; // Face 2 color + color = cubeColors[cubeIndex][3]; // Face 4 color isOnCube = true; } } } - // Face 3: Face avant (Z = currentCubeWidth) + // Face 5: Front face (Z = currentCubeWidth) + { + float x3 = cubeX; + float y3 = cubeY; + float z3 = currentCubeWidth; + + x3 += cubePos.x; + y3 += cubePos.y; + z3 += cubePos.z; + + // Apply rotation + float rotX3 = x3 * cos(rotB) * cos(rotC) + + z3 * sin(rotB) - + y3 * cos(rotB) * sin(rotC); + float rotY3 = x3 * (sin(rotA) * sin(rotB) * cos(rotC) + cos(rotA) * sin(rotC)) + + y3 * (cos(rotA) * cos(rotC) - sin(rotA) * sin(rotB) * sin(rotC)) - + z3 * sin(rotA) * cos(rotB); + float rotZ3 = x3 * (sin(rotA) * sin(rotC) - cos(rotA) * sin(rotB) * cos(rotC)) + + y3 * (cos(rotA) * sin(rotB) * sin(rotC) + sin(rotA) * cos(rotC)) + + z3 * cos(rotA) * cos(rotB); + + rotZ3 += distanceFromCam; + + if (rotZ3 > 0.0) { + float ooz3 = 1.0 / rotZ3; + float px3 = (width / 2.0) + K1 * ooz3 * rotX3; + float py3 = (height / 2.0) + K1 * ooz3 * rotY3; + + float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); + if (dist3 < threshold && dist3 < minDist) { + minDist = dist3; + color = cubeColors[cubeIndex][4]; // Face 5 color + isOnCube = true; + } + } + } + + // Face 6: Back face (Z = -currentCubeWidth) { float x3 = cubeX; float y3 = cubeY; float z3 = -currentCubeWidth; - // Ajouter le décalage de position x3 += cubePos.x; y3 += cubePos.y; z3 += cubePos.z; - // Appliquer la rotation + // Apply rotation float rotX3 = x3 * cos(rotB) * cos(rotC) + z3 * sin(rotB) - y3 * cos(rotB) * sin(rotC); @@ -301,7 +300,7 @@ void main() float dist3 = (px3 - float(x)) * (px3 - float(x)) + (py3 - float(y)) * (py3 - float(y)); if (dist3 < threshold && dist3 < minDist) { minDist = dist3; - color = cubeColors[cubeIndex][2]; // Face 3 color + color = cubeColors[cubeIndex][5]; // Face 6 color isOnCube = true; } } @@ -310,11 +309,7 @@ void main() } } - // Pas d'effet de traînée - if (isOnCube) { - data_0[p] = color; - } else { - data_0[p] = 0xFF641E16; // Noir pour les pixels qui ne sont pas sur un cube - } + // Apply color to current pixel + data_0[p] = isOnCube ? color : backgroundColor; } } \ No newline at end of file diff --git a/examples/cube3/circles.tscn b/examples/cube3/cube.tscn similarity index 100% rename from examples/cube3/circles.tscn rename to examples/cube3/cube.tscn diff --git a/examples/example_mandelbrot.gd.uid b/examples/example_mandelbrot.gd.uid new file mode 100644 index 0000000..d22223e --- /dev/null +++ b/examples/example_mandelbrot.gd.uid @@ -0,0 +1 @@ +uid://bib00xk6p8oo4 diff --git a/project.godot b/project.godot index 54c5aa4..3cbff81 100644 --- a/project.godot +++ b/project.godot @@ -11,7 +11,7 @@ config_version=5 [application] config/name="compute_shader_studio" -run/main_scene="uid://bn86hxa8xefmh" +run/main_scene="uid://e5gixrwap30u" config/features=PackedStringArray("4.4", "Forward Plus") config/icon="res://addons/compute_shader_studio/icon.png" <<<<<<