2019-08-02 08:17:32 +02:00
|
|
|
tool
|
2019-07-31 20:53:00 +02:00
|
|
|
extends MMGenBase
|
|
|
|
class_name MMGenMaterial
|
|
|
|
|
2019-08-02 08:17:32 +02:00
|
|
|
var texture_list
|
|
|
|
|
2019-08-09 08:16:24 +02:00
|
|
|
var material : SpatialMaterial
|
2019-08-02 08:17:32 +02:00
|
|
|
var generated_textures = {}
|
|
|
|
|
|
|
|
const TEXTURE_LIST = [
|
|
|
|
{ port=0, texture="albedo" },
|
|
|
|
{ port=3, texture="emission" },
|
2019-09-16 20:45:47 +02:00
|
|
|
{ port=4, texture="normal_texture" },
|
2019-10-01 23:15:36 +02:00
|
|
|
{ ports=[5, 2, 1], default_values=["1.0", "1.0", "1.0"], texture="orm" },
|
2019-09-16 20:45:47 +02:00
|
|
|
{ port=6, texture="depth_texture" }
|
2019-08-02 08:17:32 +02:00
|
|
|
]
|
|
|
|
|
2019-10-01 08:34:08 +02:00
|
|
|
func _ready():
|
|
|
|
texture_list = TEXTURE_LIST
|
|
|
|
for t in texture_list:
|
|
|
|
generated_textures[t.texture] = null
|
|
|
|
material = SpatialMaterial.new()
|
|
|
|
|
2019-10-19 01:30:25 +02:00
|
|
|
func can_be_deleted() -> bool:
|
|
|
|
return false
|
|
|
|
|
2019-08-12 15:58:42 +02:00
|
|
|
func get_type():
|
|
|
|
return "material"
|
|
|
|
|
2019-09-16 20:45:47 +02:00
|
|
|
func get_type_name():
|
|
|
|
return "Material"
|
|
|
|
|
2019-09-14 09:14:27 +02:00
|
|
|
func get_parameter_defs():
|
|
|
|
return [
|
|
|
|
{ name="albedo_color", label="Albedo", type="color", default={ r=1.0, g=1.0, b=1.0, a=1.0} },
|
2019-09-24 22:25:46 +02:00
|
|
|
{ name="metallic", label="Metallic", type="float", min=0.0, max=1.0, step=0.05, default=1.0 },
|
|
|
|
{ name="roughness", label="Roughness", type="float", min=0.0, max=1.0, step=0.05, default=1.0 },
|
|
|
|
{ name="emission_energy", label="Emission", type="float", min=0.0, max=8.0, step=0.05, default=1.0 },
|
|
|
|
{ name="normal_scale", label="Normal", type="float", min=0.0, max=8.0, step=0.05, default=1.0 },
|
|
|
|
{ name="ao_light_affect", label="Ambient occlusion", type="float", min=0.0, max=1.0, step=0.05, default=1.0 },
|
2019-10-14 20:42:11 +02:00
|
|
|
{ name="depth_scale", label="Depth", type="float", min=0.0, max=1.0, step=0.05, default=1.0 },
|
|
|
|
{ name="size", label="Size", type="size", first=7, last=11, default=9 }
|
2019-09-14 09:14:27 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
func get_input_defs():
|
|
|
|
return [
|
|
|
|
{ name="albedo_texture", label="", type="rgb" },
|
|
|
|
{ name="metallic_texture", label="", type="f" },
|
|
|
|
{ name="roughness_texture", label="", type="f" },
|
2019-09-16 20:45:47 +02:00
|
|
|
{ name="emission_texture", label="", type="rgb" },
|
|
|
|
{ name="normal_texture", label="", type="rgb" },
|
2019-09-24 22:25:46 +02:00
|
|
|
{ name="ao_texture", label="", type="f" },
|
2019-09-16 20:45:47 +02:00
|
|
|
{ name="depth_texture", label="", type="f" }
|
2019-09-14 09:14:27 +02:00
|
|
|
]
|
|
|
|
|
2019-10-14 20:42:11 +02:00
|
|
|
func get_image_size():
|
|
|
|
var rv : int
|
|
|
|
if parameters.has("size"):
|
|
|
|
rv = int(pow(2, parameters.size+7))
|
|
|
|
else:
|
|
|
|
rv = 512
|
|
|
|
return rv
|
|
|
|
|
2019-10-02 23:06:20 +02:00
|
|
|
func update_preview():
|
|
|
|
var graph_edit = self
|
|
|
|
while graph_edit is MMGenBase:
|
|
|
|
graph_edit = graph_edit.get_parent()
|
|
|
|
if graph_edit.has_method("send_changed_signal"):
|
|
|
|
graph_edit.send_changed_signal()
|
|
|
|
|
|
|
|
func set_parameter(p, v):
|
|
|
|
.set_parameter(p, v)
|
|
|
|
update_preview()
|
|
|
|
|
2019-10-01 08:34:08 +02:00
|
|
|
func source_changed(input_index : int):
|
2019-10-02 23:06:20 +02:00
|
|
|
update_preview()
|
2019-07-31 20:53:00 +02:00
|
|
|
|
2019-09-13 08:07:46 +02:00
|
|
|
func render_textures(renderer : MMGenRenderer):
|
|
|
|
for t in texture_list:
|
2019-10-01 23:15:36 +02:00
|
|
|
var texture = null
|
2019-09-25 08:04:36 +02:00
|
|
|
if t.has("port"):
|
|
|
|
var source = get_source(t.port)
|
|
|
|
if source != null:
|
2019-10-14 20:42:11 +02:00
|
|
|
var result = source.generator.render(source.output_index, renderer, get_image_size())
|
2019-10-05 11:04:50 +02:00
|
|
|
while result is GDScriptFunctionState:
|
|
|
|
result = yield(result, "completed")
|
2019-09-25 08:04:36 +02:00
|
|
|
texture = ImageTexture.new()
|
2019-10-05 11:04:50 +02:00
|
|
|
result.copy_to_texture(texture)
|
|
|
|
result.release()
|
2019-10-19 22:40:28 +02:00
|
|
|
# To work, this must be set after calling `copy_to_texture()`
|
|
|
|
texture.flags |= ImageTexture.FLAG_ANISOTROPIC_FILTER
|
2019-10-01 23:15:36 +02:00
|
|
|
elif t.has("ports"):
|
|
|
|
var context : MMGenContext = MMGenContext.new(renderer)
|
|
|
|
var code = []
|
|
|
|
var shader_textures = {}
|
|
|
|
for i in range(t.ports.size()):
|
|
|
|
var source = get_source(t.ports[i])
|
|
|
|
if source != null:
|
|
|
|
var status = source.generator.get_shader_code("UV", source.output_index, context)
|
|
|
|
while status is GDScriptFunctionState:
|
|
|
|
status = yield(status, "completed")
|
|
|
|
code.push_back(status)
|
|
|
|
for t in status.textures.keys():
|
|
|
|
shader_textures[t] = code.textures[t]
|
|
|
|
else:
|
|
|
|
code.push_back({ defs="", code="", f=t.default_values[i] })
|
|
|
|
var shader : String = renderer.generate_combined_shader(code[0], code[1], code[2])
|
2019-10-14 20:42:11 +02:00
|
|
|
var result = renderer.render_shader(shader, shader_textures, get_image_size())
|
2019-10-05 11:04:50 +02:00
|
|
|
while result is GDScriptFunctionState:
|
|
|
|
result = yield(result, "completed")
|
2019-10-01 23:15:36 +02:00
|
|
|
texture = ImageTexture.new()
|
2019-10-05 11:04:50 +02:00
|
|
|
result.copy_to_texture(texture)
|
|
|
|
result.release()
|
2019-10-19 22:40:28 +02:00
|
|
|
# To work, this must be set after calling `copy_to_texture()`
|
|
|
|
texture.flags |= ImageTexture.FLAG_ANISOTROPIC_FILTER
|
|
|
|
|
2019-10-01 23:15:36 +02:00
|
|
|
generated_textures[t.texture] = texture
|
2019-09-13 08:07:46 +02:00
|
|
|
|
|
|
|
func update_materials(material_list):
|
|
|
|
for m in material_list:
|
|
|
|
update_spatial_material(m)
|
2019-08-02 08:17:32 +02:00
|
|
|
|
|
|
|
func get_generated_texture(slot, file_prefix = null):
|
|
|
|
if file_prefix != null:
|
|
|
|
var file_name = "%s_%s.png" % [ file_prefix, slot ]
|
|
|
|
if File.new().file_exists(file_name):
|
|
|
|
return load(file_name)
|
|
|
|
else:
|
|
|
|
return null
|
|
|
|
else:
|
2019-09-13 08:07:46 +02:00
|
|
|
return generated_textures[slot]
|
2019-08-02 08:17:32 +02:00
|
|
|
|
|
|
|
func update_spatial_material(m, file_prefix = null):
|
|
|
|
var texture
|
2019-09-14 09:14:27 +02:00
|
|
|
m.albedo_color = parameters.albedo_color
|
2019-08-02 08:17:32 +02:00
|
|
|
m.albedo_texture = get_generated_texture("albedo", file_prefix)
|
2019-09-14 09:14:27 +02:00
|
|
|
m.metallic = parameters.metallic
|
|
|
|
m.roughness = parameters.roughness
|
2019-10-01 23:15:36 +02:00
|
|
|
# Metallic
|
|
|
|
texture = get_generated_texture("orm", file_prefix)
|
|
|
|
m.metallic_texture = texture
|
|
|
|
m.metallic_texture_channel = SpatialMaterial.TEXTURE_CHANNEL_BLUE
|
|
|
|
# Roughness
|
|
|
|
m.roughness_texture = texture
|
|
|
|
m.roughness_texture_channel = SpatialMaterial.TEXTURE_CHANNEL_GREEN
|
|
|
|
# Emission
|
2019-08-02 08:17:32 +02:00
|
|
|
texture = get_generated_texture("emission", file_prefix)
|
|
|
|
if texture != null:
|
|
|
|
m.emission_enabled = true
|
2019-09-14 09:14:27 +02:00
|
|
|
m.emission_energy = parameters.emission_energy
|
2019-08-02 08:17:32 +02:00
|
|
|
m.emission_texture = texture
|
|
|
|
else:
|
|
|
|
m.emission_enabled = false
|
2019-10-01 23:15:36 +02:00
|
|
|
# Normal map
|
2019-09-16 20:45:47 +02:00
|
|
|
texture = get_generated_texture("normal_texture", file_prefix)
|
2019-08-02 08:17:32 +02:00
|
|
|
if texture != null:
|
|
|
|
m.normal_enabled = true
|
|
|
|
m.normal_texture = texture
|
|
|
|
else:
|
|
|
|
m.normal_enabled = false
|
2019-10-01 23:15:36 +02:00
|
|
|
# Ambient occlusion
|
|
|
|
if get_source(5) != null:
|
|
|
|
m.ao_enabled = true
|
|
|
|
m.ao_light_affect = parameters.ao_light_affect
|
|
|
|
m.ao_texture = m.metallic_texture
|
|
|
|
m.ao_texture_channel = SpatialMaterial.TEXTURE_CHANNEL_RED
|
2019-08-02 08:17:32 +02:00
|
|
|
else:
|
2019-10-01 23:15:36 +02:00
|
|
|
m.ao_enabled = false
|
|
|
|
# Depth
|
2019-09-16 20:45:47 +02:00
|
|
|
texture = get_generated_texture("depth_texture", file_prefix)
|
2019-08-02 08:17:32 +02:00
|
|
|
if texture != null:
|
|
|
|
m.depth_enabled = true
|
2019-09-24 22:25:46 +02:00
|
|
|
m.depth_deep_parallax = true
|
2019-09-29 10:07:32 +02:00
|
|
|
m.depth_scale = parameters.depth_scale * 0.2
|
2019-08-02 08:17:32 +02:00
|
|
|
m.depth_texture = texture
|
|
|
|
else:
|
|
|
|
m.depth_enabled = false
|
|
|
|
|
2019-10-01 08:34:08 +02:00
|
|
|
func export_textures(prefix, editor_interface = null):
|
2019-08-02 08:17:32 +02:00
|
|
|
for t in texture_list:
|
2019-09-29 10:07:32 +02:00
|
|
|
var texture = generated_textures[t.texture]
|
2019-08-02 08:17:32 +02:00
|
|
|
if texture != null:
|
|
|
|
var image = texture.get_data()
|
|
|
|
image.save_png("%s_%s.png" % [ prefix, t.texture ])
|
2019-10-16 23:15:11 +02:00
|
|
|
if Engine.editor_hint and editor_interface != null:
|
2019-10-01 08:34:08 +02:00
|
|
|
var resource_filesystem = editor_interface.get_resource_filesystem()
|
2019-08-02 08:17:32 +02:00
|
|
|
resource_filesystem.scan()
|
|
|
|
yield(resource_filesystem, "filesystem_changed")
|
|
|
|
var new_material = SpatialMaterial.new()
|
|
|
|
update_spatial_material(new_material, prefix)
|
|
|
|
ResourceSaver.save("%s.tres" % [ prefix ], new_material)
|
|
|
|
resource_filesystem.scan()
|
2019-10-01 08:34:08 +02:00
|
|
|
return new_material
|
2019-09-09 22:00:18 +02:00
|
|
|
|
|
|
|
func _serialize(data):
|
|
|
|
return data
|