Skip to content
Open

Cube #17

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README - Copie.md
Original file line number Diff line number Diff line change
@@ -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**
2 changes: 1 addition & 1 deletion examples/cells/example_cells.tscn
Original file line number Diff line number Diff line change
@@ -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" 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")]
Expand Down
7 changes: 7 additions & 0 deletions examples/circles/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files.associations": {
"iostream": "cpp",
"vector": "cpp",
"cmath": "cpp"
}
}
4 changes: 2 additions & 2 deletions examples/circles/circles.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[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="Circles" type="Node2D"]
[node name="Circles2" type="Node2D"]

[node name="ComputeShaderStudio2D" type="Node" parent="." node_paths=PackedStringArray("data")]
script = ExtResource("1_amro2")
Expand All @@ -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")
260 changes: 260 additions & 0 deletions examples/cube2/cube.cpp
Original file line number Diff line number Diff line change
@@ -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 = 0xFFFFFFFF; // 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 = 0xFFFFFFFF; // 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] = 0xFF4C4C4C;
}
}
}

20 changes: 20 additions & 0 deletions examples/cube2/cube.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[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")
WSX = 512
WSY = 256
glsl_file = "res://examples/cube2/cube.cpp"
GLSL_code = ""
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")
Loading