2019-08-02 08:17:32 +02:00
|
|
|
tool
|
2019-07-31 20:53:00 +02:00
|
|
|
extends MMGenBase
|
|
|
|
class_name MMGenMaterial
|
|
|
|
|
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-11-06 22:33:01 +01:00
|
|
|
{ port=4, texture="normal" },
|
2019-10-01 23:15:36 +02:00
|
|
|
{ ports=[5, 2, 1], default_values=["1.0", "1.0", "1.0"], texture="orm" },
|
2019-11-06 22:33:01 +01:00
|
|
|
{ port=6, texture="depth" },
|
|
|
|
{ port=7, texture="subsurf_scatter" }
|
2019-08-02 08:17:32 +02:00
|
|
|
]
|
|
|
|
|
2019-10-20 20:16:48 +02:00
|
|
|
# The minimum allowed texture size as a power-of-two exponent
|
|
|
|
const TEXTURE_SIZE_MIN = 4 # 16x16
|
|
|
|
|
|
|
|
# The maximum allowed texture size as a power-of-two exponent
|
|
|
|
const TEXTURE_SIZE_MAX = 12 # 4096x4096
|
|
|
|
|
|
|
|
# The default texture size as a power-of-two exponent
|
|
|
|
const TEXTURE_SIZE_DEFAULT = 10 # 1024x1024
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _ready() -> void:
|
2019-11-02 22:58:29 +01:00
|
|
|
for t in TEXTURE_LIST:
|
2019-10-01 08:34:08 +02:00
|
|
|
generated_textures[t.texture] = null
|
|
|
|
material = SpatialMaterial.new()
|
|
|
|
|
2019-10-19 01:30:25 +02:00
|
|
|
func can_be_deleted() -> bool:
|
|
|
|
return false
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_type() -> String:
|
2019-08-12 15:58:42 +02:00
|
|
|
return "material"
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_type_name() -> String:
|
2019-09-16 20:45:47 +02:00
|
|
|
return "Material"
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_parameter_defs() -> Array:
|
2019-09-14 09:14:27 +02:00
|
|
|
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 },
|
2019-11-06 22:33:01 +01:00
|
|
|
{ name="subsurf_scatter_strength", label="Subsurf. Scatter.", type="float", min=0.0, max=1.0, step=0.05, default=0.0 },
|
2019-10-20 20:16:48 +02:00
|
|
|
{ name="size", label="Size", type="size", first=TEXTURE_SIZE_MIN, last=TEXTURE_SIZE_MAX, default=TEXTURE_SIZE_DEFAULT }
|
2019-09-14 09:14:27 +02:00
|
|
|
]
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_input_defs() -> Array:
|
2019-09-14 09:14:27 +02:00
|
|
|
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-11-06 22:33:01 +01:00
|
|
|
{ name="depth_texture", label="", type="f" },
|
|
|
|
{ name="subsurf_scatter_texture", label="", type="f" }
|
2019-09-14 09:14:27 +02:00
|
|
|
]
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_image_size() -> int:
|
2019-10-14 20:42:11 +02:00
|
|
|
var rv : int
|
|
|
|
if parameters.has("size"):
|
2019-10-21 23:58:14 +02:00
|
|
|
rv = int(pow(2, parameters.size))
|
2019-10-14 20:42:11 +02:00
|
|
|
else:
|
2019-10-20 20:16:48 +02:00
|
|
|
rv = int(pow(2, TEXTURE_SIZE_DEFAULT))
|
2019-10-14 20:42:11 +02:00
|
|
|
return rv
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func update_preview() -> void:
|
2019-10-02 23:06:20 +02:00
|
|
|
var graph_edit = self
|
|
|
|
while graph_edit is MMGenBase:
|
|
|
|
graph_edit = graph_edit.get_parent()
|
2019-10-21 23:58:14 +02:00
|
|
|
if graph_edit != null and graph_edit.has_method("send_changed_signal"):
|
2019-10-02 23:06:20 +02:00
|
|
|
graph_edit.send_changed_signal()
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func set_parameter(p, v) -> void:
|
2019-10-02 23:06:20 +02:00
|
|
|
.set_parameter(p, v)
|
|
|
|
update_preview()
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func source_changed(input_index : int) -> void:
|
2019-11-02 22:58:29 +01:00
|
|
|
for t in TEXTURE_LIST:
|
|
|
|
if t.has("port") and t.port == input_index:
|
|
|
|
generated_textures[t.texture] = null
|
|
|
|
elif t.has("ports") and t.ports.has(input_index):
|
|
|
|
generated_textures[t.texture] = null
|
2019-10-02 23:06:20 +02:00
|
|
|
update_preview()
|
2019-07-31 20:53:00 +02:00
|
|
|
|
2019-11-06 22:33:01 +01:00
|
|
|
func render_textures() -> void:
|
2019-11-02 22:58:29 +01:00
|
|
|
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"):
|
2019-11-02 22:58:29 +01:00
|
|
|
if generated_textures[t.texture] != null:
|
|
|
|
continue
|
2019-09-25 08:04:36 +02:00
|
|
|
var source = get_source(t.port)
|
|
|
|
if source != null:
|
2019-11-06 22:33:01 +01:00
|
|
|
var result = source.generator.render(source.output_index, 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-20 21:16:20 +02:00
|
|
|
|
|
|
|
# Disable filtering for small textures, as they're considered to be used
|
|
|
|
# for a pixel art style
|
|
|
|
if texture.get_size().x <= 128:
|
|
|
|
texture.flags ^= ImageTexture.FLAG_FILTER
|
2019-10-01 23:15:36 +02:00
|
|
|
elif t.has("ports"):
|
2019-11-06 22:33:01 +01:00
|
|
|
var context : MMGenContext = MMGenContext.new()
|
2019-10-01 23:15:36 +02:00
|
|
|
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():
|
2019-11-06 22:33:01 +01:00
|
|
|
shader_textures[t] = status.textures[t]
|
2019-10-01 23:15:36 +02:00
|
|
|
else:
|
|
|
|
code.push_back({ defs="", code="", f=t.default_values[i] })
|
2019-11-06 22:33:01 +01:00
|
|
|
var shader : String = mm_renderer.generate_combined_shader(code[0], code[1], code[2])
|
|
|
|
var result = mm_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-20 21:16:20 +02:00
|
|
|
# Disable filtering for small textures, as they're considered to be used
|
|
|
|
# for a pixel art style
|
|
|
|
if texture.get_size().x <= 128:
|
|
|
|
texture.flags ^= ImageTexture.FLAG_FILTER
|
|
|
|
|
2019-10-01 23:15:36 +02:00
|
|
|
generated_textures[t.texture] = texture
|
2019-09-13 08:07:46 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func update_materials(material_list) -> void:
|
2019-09-13 08:07:46 +02:00
|
|
|
for m in material_list:
|
|
|
|
update_spatial_material(m)
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_generated_texture(slot, file_prefix = null) -> ImageTexture:
|
2019-08-02 08:17:32 +02:00
|
|
|
if file_prefix != null:
|
|
|
|
var file_name = "%s_%s.png" % [ file_prefix, slot ]
|
|
|
|
if File.new().file_exists(file_name):
|
2019-11-10 15:02:00 +01:00
|
|
|
var texture = load(file_name)
|
|
|
|
return texture
|
2019-08-02 08:17:32 +02:00
|
|
|
else:
|
|
|
|
return null
|
|
|
|
else:
|
2019-09-13 08:07:46 +02:00
|
|
|
return generated_textures[slot]
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func update_spatial_material(m, file_prefix = null) -> void:
|
2019-08-02 08:17:32 +02:00
|
|
|
var texture
|
2019-10-20 19:30:51 +02:00
|
|
|
|
2019-11-02 22:58:29 +01:00
|
|
|
if m is SpatialMaterial:
|
|
|
|
# Make the material double-sided for better visiblity in the preview
|
|
|
|
m.params_cull_mode = SpatialMaterial.CULL_DISABLED
|
|
|
|
m.albedo_color = parameters.albedo_color
|
|
|
|
m.albedo_texture = get_generated_texture("albedo", file_prefix)
|
|
|
|
m.metallic = parameters.metallic
|
|
|
|
m.roughness = parameters.roughness
|
|
|
|
# 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
|
|
|
|
texture = get_generated_texture("emission", file_prefix)
|
|
|
|
if texture != null:
|
|
|
|
m.emission_enabled = true
|
|
|
|
m.emission_energy = parameters.emission_energy
|
|
|
|
m.emission_texture = texture
|
|
|
|
else:
|
|
|
|
m.emission_enabled = false
|
|
|
|
# Normal map
|
2019-11-06 22:33:01 +01:00
|
|
|
texture = get_generated_texture("normal", file_prefix)
|
2019-11-02 22:58:29 +01:00
|
|
|
if texture != null:
|
|
|
|
m.normal_enabled = true
|
|
|
|
m.normal_texture = texture
|
|
|
|
else:
|
|
|
|
m.normal_enabled = false
|
|
|
|
# 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
|
|
|
|
else:
|
|
|
|
m.ao_enabled = false
|
|
|
|
# Depth
|
2019-11-06 22:33:01 +01:00
|
|
|
texture = get_generated_texture("depth", file_prefix)
|
2019-11-02 22:58:29 +01:00
|
|
|
if texture != null:
|
|
|
|
m.depth_enabled = true
|
|
|
|
m.depth_deep_parallax = true
|
|
|
|
m.depth_scale = parameters.depth_scale * 0.2
|
|
|
|
m.depth_texture = texture
|
|
|
|
else:
|
|
|
|
m.depth_enabled = false
|
2019-11-06 22:33:01 +01:00
|
|
|
# Subsurface scattering
|
|
|
|
texture = get_generated_texture("subsurf_scatter", file_prefix)
|
|
|
|
if texture != null:
|
|
|
|
m.subsurf_scatter_enabled = true
|
|
|
|
m.subsurf_scatter_strength = parameters.subsurf_scatter_strength
|
|
|
|
m.subsurf_scatter_texture = texture
|
|
|
|
else:
|
|
|
|
m.subsurf_scatter_enabled = false
|
2019-08-02 08:17:32 +02:00
|
|
|
else:
|
2019-11-02 22:58:29 +01:00
|
|
|
m.set_shader_param("albedo", parameters.albedo_color)
|
|
|
|
m.set_shader_param("texture_albedo", get_generated_texture("albedo", file_prefix))
|
|
|
|
m.set_shader_param("metallic", parameters.metallic)
|
|
|
|
m.set_shader_param("roughness", parameters.roughness)
|
|
|
|
m.set_shader_param("texture_metallic", get_generated_texture("orm", file_prefix))
|
|
|
|
m.set_shader_param("metallic_texture_channel", PoolRealArray([0.0, 0.0, 1.0, 0.0]))
|
|
|
|
m.set_shader_param("texture_roughness", get_generated_texture("orm", file_prefix))
|
|
|
|
m.set_shader_param("roughness_texture_channel", PoolRealArray([0.0, 1.0, 0.0, 0.0]))
|
|
|
|
m.set_shader_param("emission_energy", parameters.emission_energy)
|
|
|
|
m.set_shader_param("texture_emission", get_generated_texture("emission", file_prefix))
|
|
|
|
m.set_shader_param("normal_scale", parameters.normal_scale)
|
2019-11-06 22:33:01 +01:00
|
|
|
m.set_shader_param("texture_normal", get_generated_texture("normal", file_prefix))
|
2019-11-02 22:58:29 +01:00
|
|
|
m.set_shader_param("depth_scale", parameters.depth_scale * 0.2)
|
2019-11-06 22:33:01 +01:00
|
|
|
m.set_shader_param("texture_depth", get_generated_texture("depth", file_prefix))
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func export_textures(prefix, editor_interface = null) -> SpatialMaterial:
|
2019-11-02 22:58:29 +01: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()
|
2019-11-10 15:02:00 +01:00
|
|
|
yield(resource_filesystem, "resources_reimported")
|
|
|
|
print("resources_reimported")
|
2019-08-02 08:17:32 +02:00
|
|
|
var new_material = SpatialMaterial.new()
|
|
|
|
update_spatial_material(new_material, prefix)
|
2019-11-10 15:02:00 +01:00
|
|
|
var file_name : String = "%s.tres" % [ prefix ]
|
|
|
|
ResourceSaver.save(file_name, new_material)
|
|
|
|
resource_filesystem.update_file(file_name)
|
2019-10-01 08:34:08 +02:00
|
|
|
return new_material
|
2019-09-09 22:00:18 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
return null
|
|
|
|
|
|
|
|
func _serialize(data: Dictionary) -> Dictionary:
|
2019-09-09 22:00:18 +02:00
|
|
|
return data
|