Skip to content

Commit

Permalink
Added compute shader based mesh generation test scene
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Sep 24, 2024
1 parent 4389243 commit c630765
Show file tree
Hide file tree
Showing 12 changed files with 519 additions and 0 deletions.
24 changes: 24 additions & 0 deletions material_maker/panels/preview_3d/meshes/bricks.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[gd_resource type="StandardMaterial3D" load_steps=5 format=3 uid="uid://bpe2pnw4jgdpq"]

[ext_resource type="Texture2D" uid="uid://du2r5sfapuxxx" path="res://material_maker/panels/preview_3d/meshes/bricks_albedo.png" id="1"]
[ext_resource type="Texture2D" uid="uid://dfvy71k6qeu0g" path="res://material_maker/panels/preview_3d/meshes/bricks_orm.png" id="2"]
[ext_resource type="Texture2D" uid="uid://bgqqi03ty5vjy" path="res://material_maker/panels/preview_3d/meshes/bricks_normal.png" id="3"]
[ext_resource type="Texture2D" uid="uid://bx4upg4i1n5db" path="res://material_maker/panels/preview_3d/meshes/bricks_heightmap.png" id="4"]

[resource]
albedo_texture = ExtResource("1")
metallic = 1.0
metallic_texture = ExtResource("2")
metallic_texture_channel = 2
roughness_texture = ExtResource("2")
roughness_texture_channel = 1
normal_enabled = true
normal_texture = ExtResource("3")
ao_enabled = true
ao_texture = ExtResource("2")
heightmap_enabled = true
heightmap_scale = -1.0
heightmap_deep_parallax = true
heightmap_min_layers = 8
heightmap_max_layers = 32
heightmap_texture = ExtResource("4")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions material_maker/panels/preview_3d/meshes/bricks_albedo.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://du2r5sfapuxxx"
path.s3tc="res://.godot/imported/bricks_albedo.png-fd68b4578c80514f4fcddea3e36876b6.s3tc.ctex"
path.etc2="res://.godot/imported/bricks_albedo.png-fd68b4578c80514f4fcddea3e36876b6.etc2.ctex"
metadata={
"imported_formats": ["s3tc_bptc", "etc2_astc"],
"vram_texture": true
}

[deps]

source_file="res://material_maker/panels/preview_3d/meshes/bricks_albedo.png"
dest_files=["res://.godot/imported/bricks_albedo.png-fd68b4578c80514f4fcddea3e36876b6.s3tc.ctex", "res://.godot/imported/bricks_albedo.png-fd68b4578c80514f4fcddea3e36876b6.etc2.ctex"]

[params]

compress/mode=2
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bx4upg4i1n5db"
path.s3tc="res://.godot/imported/bricks_heightmap.png-52ab05e892532e84513e158ffb724e6d.s3tc.ctex"
path.etc2="res://.godot/imported/bricks_heightmap.png-52ab05e892532e84513e158ffb724e6d.etc2.ctex"
metadata={
"imported_formats": ["s3tc_bptc", "etc2_astc"],
"vram_texture": true
}

[deps]

source_file="res://material_maker/panels/preview_3d/meshes/bricks_heightmap.png"
dest_files=["res://.godot/imported/bricks_heightmap.png-52ab05e892532e84513e158ffb724e6d.s3tc.ctex", "res://.godot/imported/bricks_heightmap.png-52ab05e892532e84513e158ffb724e6d.etc2.ctex"]

[params]

compress/mode=2
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=7
roughness/src_normal="res://material_maker/panels/preview_3d/meshes/bricks_normal.png"
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions material_maker/panels/preview_3d/meshes/bricks_normal.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bgqqi03ty5vjy"
path.s3tc="res://.godot/imported/bricks_normal.png-7098b712f0dcca9e6492aad88863210d.s3tc.ctex"
path.etc2="res://.godot/imported/bricks_normal.png-7098b712f0dcca9e6492aad88863210d.etc2.ctex"
metadata={
"imported_formats": ["s3tc_bptc", "etc2_astc"],
"vram_texture": true
}

[deps]

source_file="res://material_maker/panels/preview_3d/meshes/bricks_normal.png"
dest_files=["res://.godot/imported/bricks_normal.png-7098b712f0dcca9e6492aad88863210d.s3tc.ctex", "res://.godot/imported/bricks_normal.png-7098b712f0dcca9e6492aad88863210d.etc2.ctex"]

[params]

compress/mode=2
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=1
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=1
roughness/src_normal="res://material_maker/panels/preview_3d/meshes/bricks_normal.png"
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions material_maker/panels/preview_3d/meshes/bricks_orm.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://dfvy71k6qeu0g"
path.s3tc="res://.godot/imported/bricks_orm.png-1c80d54abc36fdd6f381da55e535c517.s3tc.ctex"
path.etc2="res://.godot/imported/bricks_orm.png-1c80d54abc36fdd6f381da55e535c517.etc2.ctex"
metadata={
"imported_formats": ["s3tc_bptc", "etc2_astc"],
"vram_texture": true
}

[deps]

source_file="res://material_maker/panels/preview_3d/meshes/bricks_orm.png"
dest_files=["res://.godot/imported/bricks_orm.png-1c80d54abc36fdd6f381da55e535c517.s3tc.ctex", "res://.godot/imported/bricks_orm.png-1c80d54abc36fdd6f381da55e535c517.etc2.ctex"]

[params]

compress/mode=2
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=8
roughness/src_normal="res://material_maker/panels/preview_3d/meshes/bricks_normal.png"
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=0
52 changes: 52 additions & 0 deletions material_maker/panels/preview_3d/meshes/compute_plane.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#version 450
// VERTEX COUNT: (size+1)*(size+1)
// INDEX COUNT: 2*size*(size+2)-2

layout(local_size_x = @LOCAL_SIZE, local_size_y = 1, local_size_z = 1) in;

@DECLARATIONS

layout(set = 3, binding = 0, std140) restrict buffer MM {
int mm_chunk_y;
};

void set_point(int x, int y) {
int vertex_index = x+(size+1)*y;
float scale = 1.0/float(size);
mm_out.vertices[3*vertex_index+0] = 2.0*scale*x-1.0;
mm_out.vertices[3*vertex_index+1] = curvature*cos(scale*x-0.5)*cos(scale*y-0.5)-curvature;
mm_out.vertices[3*vertex_index+2] = 2.0*scale*y-1.0;
vec3 tangent = normalize(vec3(2.0, -curvature*scale*sin(scale*x-0.5)*cos(scale*y-0.5), 0.0));
vec3 cotangent = normalize(vec3(0.0, -curvature*scale*cos(scale*x-0.5)*sin(scale*y-0.5), 2.0));
vec3 normal = normalize(cross(cotangent, tangent));
mm_out.normals[3*vertex_index+0] = normal.x;
mm_out.normals[3*vertex_index+1] = normal.y;
mm_out.normals[3*vertex_index+2] = normal.z;
mm_out.tangents[4*vertex_index+0] = tangent.x;
mm_out.tangents[4*vertex_index+1] = tangent.y;
mm_out.tangents[4*vertex_index+2] = tangent.z;
mm_out.tangents[4*vertex_index+3] = 1.0;
mm_out.tex_uvs[2*vertex_index+0] = scale*x;
mm_out.tex_uvs[2*vertex_index+1] = scale*y;
}

void main() {
ivec2 c = ivec2(gl_GlobalInvocationID.xy)+ivec2(0, mm_chunk_y);
set_point(c.x, c.y);
mm_out.indexes[2*(c.x+(size+2)*c.y)] = c.x+(size+1)*(c.y+1);
mm_out.indexes[2*(c.x+(size+2)*c.y)+1] = c.x+(size+1)*c.y;
if (c.x == size-1) {
mm_out.indexes[2*(size+(size+2)*c.y)] = size+(size+1)*(c.y+1);
mm_out.indexes[2*(size+(size+2)*c.y)+1] = size+(size+1)*c.y;
set_point(size, c.y);
if (c.y == size-1) {
set_point(size, size);
} else {
mm_out.indexes[2*(size+(size+2)*c.y)+2] = size+(size+1)*c.y;
mm_out.indexes[2*(size+(size+2)*c.y)+3] = (size+1)*(c.y+2);
}
}
if (c.y == size-1) {
set_point(c.x, size);
}
}
82 changes: 82 additions & 0 deletions material_maker/panels/preview_3d/meshes/mesh_generator.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
extends Node

var shader : MMComputeShader = MMComputeShader.new()
var size : int = 2

@onready var generated_mesh = $Pivot/MeshInstance
@onready var plane = $Pivot/Plane

# Called when the node enters the scene tree for the first time.
func _ready():
var w : Window = get_window()
w.borderless = false
w.size = Vector2i(800, 600)
w.move_to_center()
await setup_shader()
await generate_mesh()

func get_expression_value_from_string(string : String, prefix: String, values: Dictionary) -> int:
var begin : int = string.find(prefix)
if begin == -1:
return 0
begin += prefix.length()
var end : int = string.find("\n", begin)
var expression_string : String = string.substr(begin, end-begin)
var expression : Expression = Expression.new()
var input_names : PackedStringArray = PackedStringArray()
var input_values : Array = []
for k in values.keys():
input_names.append(k)
input_values.append(values[k])
expression.parse(expression_string, input_names)
var rv : int = expression.execute(input_values)
print(prefix+" "+str(rv))
return rv

func setup_shader():
var string : String = FileAccess.open("res://material_maker/panels/preview_3d/meshes/compute_plane.txt", FileAccess.READ).get_as_text()
var vertex_count : int = get_expression_value_from_string(string, "// VERTEX COUNT:", {size=size})
var index_count : int = get_expression_value_from_string(string, "// INDEX COUNT:", {size=size})
shader.local_size = size if size < 32 else 32
shader.clear()
shader.add_parameter_or_texture("size", "int", size)
shader.add_parameter_or_texture("curvature", "float", 0.0)
shader.add_output_parameter("vertices", "float", 3*vertex_count)
shader.add_output_parameter("normals", "float", 3*vertex_count)
shader.add_output_parameter("tangents", "float", 4*vertex_count)
shader.add_output_parameter("tex_uvs", "float", 2*vertex_count)
shader.add_output_parameter("indexes", "int", index_count)
await shader.set_shader_ext(string)

func generate_mesh():
var opv : Dictionary = { vertices_format="vec3", normals_format="vec3", tex_uvs_format="vec2" }
shader.set_parameter("size", size)
shader.set_parameter("curvature", $UI/VBoxContainer/Curvature.value)
await shader.render_ext([], Vector2i(size, size), opv)
var mesh : ArrayMesh = generated_mesh.mesh
mesh.clear_surfaces()
var flags : int = Mesh.ARRAY_FORMAT_VERTEX | Mesh.ARRAY_FORMAT_NORMAL | Mesh.ARRAY_FORMAT_TEX_UV | Mesh.ARRAY_FORMAT_INDEX
var vertices : PackedVector3Array = opv.vertices
var normals : PackedVector3Array = opv.normals
var tangents : PackedFloat32Array = opv.tangents
var tex_uvs : PackedVector2Array = opv.tex_uvs
var indexes : PackedInt32Array = opv.indexes
var arrays : Array = [vertices, normals, tangents, null, tex_uvs, null, null, null, null, null, null, null, indexes]
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLE_STRIP, arrays, [], {}, flags)
print("Done")

func _on_reload_shader_pressed():
await setup_shader()
await generate_mesh()

func _on_model_pressed():
generated_mesh.visible = not generated_mesh.visible
plane.visible = not plane.visible

func _on_size_value_changed(value):
size = 1 << int(value)
await setup_shader()
await generate_mesh()

func _on_curvature_value_changed(value):
await generate_mesh()
Loading

0 comments on commit c630765

Please sign in to comment.