Skip to content

Commit

Permalink
Fixed map generator / smart materials problem
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Dec 8, 2024
1 parent 219af93 commit dd0b7e1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
98 changes: 76 additions & 22 deletions addons/material_maker/map_generator/map_generator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,67 @@ const MAP_DEFINITIONS : Dictionary = {
type="simple",
vertex = "position_vertex",
fragment = "common_fragment",
postprocess=["dilate"]
postprocess=["dilate"],
dependencies=["seams"]
},
normal = { type="simple", vertex = "normal_vertex", fragment = "normal_fragment", postprocess=["dilate"] },
tangent = { type="simple", vertex = "tangent_vertex", fragment = "normal_fragment", postprocess=["dilate"] },
ambient_occlusion = { type="bvh", vertex = "ao_vertex", fragment = "ao_fragment", mode=0, postprocess=["dilate"] },
bent_normals = { type="bvh", vertex = "ao_vertex", fragment = "ao_fragment", mode=1, postprocess=["dilate"] },
thickness = { type="bvh", vertex = "ao_vertex", fragment = "ao_fragment", mode=2, postprocess=["dilate"] },
curvature = { type="curvature", vertex = "curvature_vertex", fragment = "common_fragment", postprocess=["dilate"] },
seams = { type="simple", vertex = "position_vertex", fragment = "common_fragment", postprocess=["seams_1", "seams_2"] },
adjacency = { type="adjacency", vertex = "normal_vertex", fragment = "common_fragment", postprocess=["adjacency_dilate"] },
normal = {
type="simple",
vertex = "normal_vertex",
fragment = "normal_fragment",
postprocess=["dilate"],
dependencies=["seams"]
},
tangent = {
type="simple",
vertex = "tangent_vertex",
fragment = "normal_fragment",
postprocess=["dilate"],
dependencies=["seams"]
},
ambient_occlusion = {
type="bvh",
vertex = "ao_vertex",
fragment = "ao_fragment",
mode=0,
postprocess=["dilate"],
dependencies=["seams"]
},
bent_normals = {
type="bvh",
vertex = "ao_vertex",
fragment = "ao_fragment",
mode=1,
postprocess=["dilate"],
dependencies=["seams"]
},
thickness = {
type="bvh",
vertex = "ao_vertex",
fragment = "ao_fragment",
mode=2,
postprocess=["dilate"],
dependencies=["seams"]
},
curvature = {
type="curvature",
vertex = "curvature_vertex",
fragment = "common_fragment",
postprocess=["dilate"],
dependencies=["seams"]
},
seams = {
type="simple",
vertex = "position_vertex",
fragment = "common_fragment",
postprocess=["seams_1", "seams_2"]
},
adjacency = {
type="adjacency",
vertex = "normal_vertex",
fragment = "common_fragment",
postprocess=["adjacency_dilate"],
dependencies=["seams"]
}
}


Expand Down Expand Up @@ -145,7 +196,7 @@ static func generate(mesh : Mesh, map : String, size : int, texture : MMTexture)
postprocess_pipeline.add_parameter_or_texture("pixels", "int", pixels)
match p:
"adjacency_dilate", "dilate":
var seams_map : MMTexture = await get_map(mesh, "seams", false, true)
var seams_map : MMTexture = mesh_maps[mesh].seams
postprocess_pipeline.add_parameter_or_texture("seams_map", "sampler2D", seams_map)
await postprocess_pipeline.set_shader(load("res://addons/material_maker/map_generator/"+p+"_compute.tres").text, 3)
await postprocess_pipeline.render(texture, Vector2i(size, size))
Expand All @@ -155,7 +206,7 @@ static func generate(mesh : Mesh, map : String, size : int, texture : MMTexture)

static var busy : bool = false

static func get_map(mesh : Mesh, map : String, force_generate : bool = false, parallel : bool = false) -> MMTexture:
static func get_map(mesh : Mesh, map : String, force_generate : bool = false) -> MMTexture:
if mesh == null:
if error_texture == null:
error_texture = MMTexture.new()
Expand All @@ -167,15 +218,18 @@ static func get_map(mesh : Mesh, map : String, force_generate : bool = false, pa
mesh_maps[mesh] = {}
if force_generate:
mesh_maps[mesh].erase(map)
while true:
if mesh_maps[mesh].has(map):
break
if parallel or not busy:
busy = true
var texture : MMTexture = MMTexture.new()
mesh_maps[mesh][map] = texture
await generate(mesh, map, 2048, texture)
busy = false
break
await mm_globals.get_tree().process_frame
if not mesh_maps[mesh].has(map):
if MAP_DEFINITIONS[map].has("dependencies"):
for d in MAP_DEFINITIONS[map].dependencies:
await get_map(mesh, d)
print("Creating map ", map, " for mesh ", mesh)
while not mesh_maps[mesh].has(map):
if busy:
await mm_globals.get_tree().process_frame
else:
busy = true
var texture : MMTexture = MMTexture.new()
await generate(mesh, map, 2048, texture)
mesh_maps[mesh][map] = texture
busy = false
return mesh_maps[mesh][map] as MMTexture
6 changes: 4 additions & 2 deletions material_maker/main_window.gd
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func do_load_project(file_name : String) -> bool:
var status : bool = false
match file_name.get_extension():
"ptex":
status = do_load_material(file_name, false)
status = await do_load_material(file_name, false)
hierarchy.update_from_graph_edit(get_current_graph_edit())
"mmpp":
status = do_load_painting(file_name)
Expand Down Expand Up @@ -623,9 +623,11 @@ func create_new_graph_edit_if_needed() -> MMGraphEdit:

func do_load_material(filename : String, update_hierarchy : bool = true) -> bool:
var graph_edit : MMGraphEdit = create_new_graph_edit_if_needed()
graph_edit.load_file(filename)
await graph_edit.load_file(filename)
if update_hierarchy:
hierarchy.update_from_graph_edit(get_current_graph_edit())
print("Current mesh: ", current_mesh)
print("Top generator: ", graph_edit.top_generator)
if current_mesh and graph_edit.top_generator:
graph_edit.top_generator.set_current_mesh(current_mesh)
return true
Expand Down

0 comments on commit dd0b7e1

Please sign in to comment.