2018-07-22 17:20:27 +02:00
|
|
|
tool
|
|
|
|
extends "res://addons/procedural_material/node_base.gd"
|
|
|
|
|
2018-08-10 12:31:15 +02:00
|
|
|
var albedo_color
|
|
|
|
var metallic
|
|
|
|
var roughness
|
|
|
|
var emission_energy
|
|
|
|
var normal_scale
|
|
|
|
var ao_light_affect
|
2018-08-12 19:25:18 +02:00
|
|
|
var depth_scale
|
2018-08-10 12:31:15 +02:00
|
|
|
|
2018-08-12 19:25:18 +02:00
|
|
|
var current_material_list = []
|
|
|
|
var generated_textures = {}
|
2018-07-29 23:43:24 +02:00
|
|
|
|
2018-08-03 08:16:38 +02:00
|
|
|
const TEXTURE_LIST = [
|
2018-08-12 19:25:18 +02:00
|
|
|
{ port=0, texture="albedo" },
|
|
|
|
{ port=1, texture="metallic" },
|
|
|
|
{ port=2, texture="roughness" },
|
|
|
|
{ port=3, texture="emission" },
|
|
|
|
{ port=4, texture="normal_map" },
|
|
|
|
{ port=5, texture="ambient_occlusion" },
|
|
|
|
{ port=6, texture="depth_map" }
|
2018-08-03 08:16:38 +02:00
|
|
|
]
|
2018-07-29 23:43:24 +02:00
|
|
|
|
2018-07-22 17:20:27 +02:00
|
|
|
func _ready():
|
2018-08-12 19:25:18 +02:00
|
|
|
for t in TEXTURE_LIST:
|
|
|
|
generated_textures[t.texture] = { shader=null, source=null, texture=null}
|
2018-08-10 12:31:15 +02:00
|
|
|
initialize_properties([ $Albedo/albedo_color, $Metallic/metallic, $Roughness/roughness, $Emission/emission_energy, $NormalMap/normal_scale, $AmbientOcclusion/ao_light_affect, $DepthMap/depth_scale ])
|
2018-07-22 17:20:27 +02:00
|
|
|
|
2018-08-12 19:25:18 +02:00
|
|
|
func _rerender():
|
|
|
|
var has_textures = false
|
|
|
|
for t in TEXTURE_LIST:
|
|
|
|
var shader = generated_textures[t.texture].shader
|
|
|
|
if shader != null:
|
|
|
|
var input_textures = null
|
|
|
|
if generated_textures[t.texture].source != null:
|
|
|
|
input_textures = generated_textures[t.texture].source.get_textures()
|
|
|
|
get_parent().precalculate_shader(shader, input_textures, 1024, generated_textures[t.texture].texture, self, "do_update_materials", [ current_material_list ])
|
|
|
|
has_textures = true
|
|
|
|
if !has_textures:
|
|
|
|
do_update_materials(current_material_list)
|
|
|
|
|
2018-07-26 08:31:28 +02:00
|
|
|
func _get_shader_code(uv):
|
2018-07-25 06:20:36 +02:00
|
|
|
var rv = { defs="", code="", f="0.0" }
|
2018-07-22 17:20:27 +02:00
|
|
|
var src = get_source()
|
2018-07-24 08:04:25 +02:00
|
|
|
if src != null:
|
|
|
|
rv = src.get_shader_code(uv)
|
2018-07-22 23:25:05 +02:00
|
|
|
return rv
|
|
|
|
|
2018-08-03 08:16:38 +02:00
|
|
|
func update_materials(material_list):
|
2018-08-12 19:25:18 +02:00
|
|
|
current_material_list = material_list
|
2018-08-10 12:31:15 +02:00
|
|
|
var has_textures = false
|
2018-08-03 08:16:38 +02:00
|
|
|
for t in TEXTURE_LIST:
|
|
|
|
var source = get_source(t.port)
|
|
|
|
if source == null:
|
2018-08-12 19:25:18 +02:00
|
|
|
generated_textures[t.texture].shader = null
|
|
|
|
generated_textures[t.texture].source = null
|
|
|
|
if generated_textures[t.texture].texture != null:
|
|
|
|
generated_textures[t.texture].texture = null
|
2018-07-29 23:43:24 +02:00
|
|
|
else:
|
2018-08-12 19:25:18 +02:00
|
|
|
generated_textures[t.texture].shader = source.generate_shader()
|
|
|
|
generated_textures[t.texture].source = source
|
|
|
|
if generated_textures[t.texture].texture == null:
|
|
|
|
generated_textures[t.texture].texture = ImageTexture.new()
|
|
|
|
_rerender()
|
2018-08-10 12:31:15 +02:00
|
|
|
|
|
|
|
func do_update_materials(material_list):
|
2018-07-29 23:43:24 +02:00
|
|
|
for m in material_list:
|
|
|
|
if m is SpatialMaterial:
|
2018-08-10 12:31:15 +02:00
|
|
|
m.albedo_color = albedo_color
|
2018-08-12 19:25:18 +02:00
|
|
|
m.albedo_texture = generated_textures.albedo.texture
|
2018-08-10 12:31:15 +02:00
|
|
|
m.metallic = metallic
|
2018-08-12 19:25:18 +02:00
|
|
|
m.metallic_texture = generated_textures.metallic.texture
|
2018-08-10 12:31:15 +02:00
|
|
|
m.roughness = roughness
|
2018-08-12 19:25:18 +02:00
|
|
|
m.roughness_texture = generated_textures.roughness.texture
|
|
|
|
if generated_textures.emission.texture != null:
|
2018-07-29 23:43:24 +02:00
|
|
|
m.emission_enabled = true
|
2018-08-10 12:31:15 +02:00
|
|
|
m.emission_energy = emission_energy
|
2018-08-12 19:25:18 +02:00
|
|
|
m.emission_texture = generated_textures.emission.texture
|
2018-07-29 23:43:24 +02:00
|
|
|
else:
|
|
|
|
m.emission_enabled = false
|
2018-08-12 19:25:18 +02:00
|
|
|
if generated_textures.normal_map.texture != null:
|
2018-07-29 23:43:24 +02:00
|
|
|
m.normal_enabled = true
|
2018-08-12 19:25:18 +02:00
|
|
|
m.normal_texture = generated_textures.normal_map.texture
|
2018-07-29 23:43:24 +02:00
|
|
|
else:
|
|
|
|
m.normal_enabled = false
|
2018-08-12 19:25:18 +02:00
|
|
|
if generated_textures.ambient_occlusion.texture != null:
|
2018-08-10 12:31:15 +02:00
|
|
|
m.ao_enabled = true
|
|
|
|
m.ao_light_affect = ao_light_affect
|
2018-08-12 19:25:18 +02:00
|
|
|
m.ao_texture = generated_textures.ambient_occlusion.texture
|
2018-08-10 12:31:15 +02:00
|
|
|
else:
|
|
|
|
m.ao_enabled = false
|
2018-08-12 19:25:18 +02:00
|
|
|
if generated_textures.depth_map.texture != null:
|
2018-07-29 23:43:24 +02:00
|
|
|
m.depth_enabled = true
|
2018-08-10 12:31:15 +02:00
|
|
|
m.depth_scale = depth_scale
|
2018-08-12 19:25:18 +02:00
|
|
|
m.depth_texture = generated_textures.depth_map.texture
|
2018-07-29 23:43:24 +02:00
|
|
|
else:
|
|
|
|
m.depth_enabled = false
|
|
|
|
|
2018-08-03 08:16:38 +02:00
|
|
|
func export_textures(prefix):
|
|
|
|
for t in TEXTURE_LIST:
|
|
|
|
get_parent().export_texture(get_source(t.port), prefix+"_"+t.texture+".png", 1024)
|