From f136046082f54d663b1b85ddcdd461568b14cd4a Mon Sep 17 00:00:00 2001 From: Rodolphe Suescun Date: Sat, 2 Nov 2019 22:58:29 +0100 Subject: [PATCH] Added support for shader materials and added a tesselated sphere in preview shapes --- addons/material_maker/engine/gen_material.gd | 122 ++++++++++-------- .../preview/preview_objects.tscn | 96 +++++++++++++- 2 files changed, 161 insertions(+), 57 deletions(-) diff --git a/addons/material_maker/engine/gen_material.gd b/addons/material_maker/engine/gen_material.gd index 0cfdb31..dcbc548 100644 --- a/addons/material_maker/engine/gen_material.gd +++ b/addons/material_maker/engine/gen_material.gd @@ -2,8 +2,6 @@ tool extends MMGenBase class_name MMGenMaterial -var texture_list - var material : SpatialMaterial var generated_textures = {} @@ -25,8 +23,7 @@ const TEXTURE_SIZE_MAX = 12 # 4096x4096 const TEXTURE_SIZE_DEFAULT = 10 # 1024x1024 func _ready() -> void: - texture_list = TEXTURE_LIST - for t in texture_list: + for t in TEXTURE_LIST: generated_textures[t.texture] = null material = SpatialMaterial.new() @@ -82,12 +79,19 @@ func set_parameter(p, v) -> void: update_preview() func source_changed(input_index : int) -> void: + 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 update_preview() func render_textures(renderer : MMGenRenderer) -> void: - for t in texture_list: + for t in TEXTURE_LIST: var texture = null if t.has("port"): + if generated_textures[t.texture] != null: + continue var source = get_source(t.port) if source != null: var result = source.generator.render(source.output_index, renderer, get_image_size()) @@ -152,55 +156,71 @@ func get_generated_texture(slot, file_prefix = null) -> ImageTexture: func update_spatial_material(m, file_prefix = null) -> void: var texture - # 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 + 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 + texture = get_generated_texture("normal_texture", file_prefix) + 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 + texture = get_generated_texture("depth_texture", file_prefix) + 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 else: - m.emission_enabled = false - # Normal map - texture = get_generated_texture("normal_texture", file_prefix) - 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 - texture = get_generated_texture("depth_texture", file_prefix) - 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 + 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) + m.set_shader_param("texture_normal", get_generated_texture("normal_texture", file_prefix)) + m.set_shader_param("depth_scale", parameters.depth_scale * 0.2) + m.set_shader_param("texture_depth", get_generated_texture("depth_texture", file_prefix)) func export_textures(prefix, editor_interface = null) -> SpatialMaterial: - for t in texture_list: + for t in TEXTURE_LIST: var texture = generated_textures[t.texture] if texture != null: var image = texture.get_data() diff --git a/addons/material_maker/preview/preview_objects.tscn b/addons/material_maker/preview/preview_objects.tscn index c48cfd7..702e9a0 100644 --- a/addons/material_maker/preview/preview_objects.tscn +++ b/addons/material_maker/preview/preview_objects.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=13 format=2] +[gd_scene load_steps=17 format=2] [ext_resource path="res://rodz_labs_logo.png" type="Texture" id=1] @@ -17,6 +17,8 @@ uv1_scale = Vector3( 2, 2, 2 ) [sub_resource type="SphereMesh" id=5] radius = 1.5 height = 3.0 +radial_segments = 512 +rings = 256 [sub_resource type="SpatialMaterial" id=6] albedo_texture = ExtResource( 1 ) @@ -26,23 +28,95 @@ albedo_texture = ExtResource( 1 ) uv1_scale = Vector3( 2, 2, 2 ) uv1_offset = Vector3( 0, 0.5, 0 ) -[sub_resource type="PlaneMesh" id=8] -size = Vector2( 3, 3 ) +[sub_resource type="PrismMesh" id=8] [sub_resource type="SpatialMaterial" id=9] albedo_texture = ExtResource( 1 ) +uv1_scale = Vector3( 3, 2, 2 ) [sub_resource type="PlaneMesh" id=10] -size = Vector2( 12, 12 ) +size = Vector2( 3, 3 ) [sub_resource type="SpatialMaterial" id=11] albedo_texture = ExtResource( 1 ) + +[sub_resource type="PlaneMesh" id=12] +size = Vector2( 12, 12 ) + +[sub_resource type="SpatialMaterial" id=13] +albedo_texture = ExtResource( 1 ) uv1_scale = Vector3( 4, 4, 4 ) +[sub_resource type="Shader" id=14] +code = "shader_type spatial; +render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx,skip_vertex_transform; +uniform vec4 albedo : hint_color; +uniform sampler2D texture_albedo : hint_albedo; +uniform float specular; +uniform float metallic; +uniform float roughness : hint_range(0,1); +uniform float point_size : hint_range(0,128); +uniform sampler2D texture_metallic : hint_white; +uniform vec4 metallic_texture_channel; +uniform sampler2D texture_roughness : hint_white; +uniform vec4 roughness_texture_channel; +uniform sampler2D texture_emission : hint_black_albedo; +uniform vec4 emission : hint_color; +uniform float emission_energy; +uniform sampler2D texture_normal : hint_normal; +uniform float normal_scale : hint_range(-16,16); +uniform sampler2D texture_depth : hint_white; +uniform float depth_scale : hint_range(0,1); +uniform vec3 uv1_scale; +uniform vec3 uv1_offset; +uniform vec3 uv2_scale; +uniform vec3 uv2_offset; + +void vertex() { + VERTEX = (MODELVIEW_MATRIX * vec4(VERTEX+NORMAL*depth_scale*(1.0-texture(texture_depth, UV).r), 1.0)).xyz; + UV=UV*uv1_scale.xy+uv1_offset.xy; +} + +void fragment() { + vec2 base_uv = UV; + vec4 albedo_tex = texture(texture_albedo,base_uv); + ALBEDO = albedo.rgb * albedo_tex.rgb; + float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel); + METALLIC = metallic_tex * metallic; + float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel); + ROUGHNESS = roughness_tex * roughness; + SPECULAR = specular; + NORMALMAP = texture(texture_normal,base_uv).rgb; + NORMALMAP_DEPTH = normal_scale; + vec3 emission_tex = texture(texture_emission,base_uv).rgb; + EMISSION = (emission.rgb+emission_tex)*emission_energy; +} +" + +[sub_resource type="ShaderMaterial" id=15] +shader = SubResource( 14 ) +shader_param/albedo = Color( 1, 1, 1, 1 ) +shader_param/specular = 0.5 +shader_param/metallic = 0.0 +shader_param/roughness = 1.0 +shader_param/point_size = 1.0 +shader_param/metallic_texture_channel = Plane( 1, 0, 0, 0 ) +shader_param/roughness_texture_channel = Plane( 1, 0, 0, 0 ) +shader_param/emission = Color( 0, 0, 0, 1 ) +shader_param/emission_energy = 1.0 +shader_param/normal_scale = 1.0 +shader_param/depth_scale = null +shader_param/uv1_scale = Vector3( 1, 1, 1 ) +shader_param/uv1_offset = Vector3( 0, 0, 0 ) +shader_param/uv2_scale = Vector3( 1, 1, 1 ) +shader_param/uv2_offset = Vector3( 0, 0, 0 ) +shader_param/texture_albedo = ExtResource( 1 ) + [node name="Objects" type="Spatial"] transform = Transform( -0.685898, 0, 0.727691, 0, 1, 0, -0.727691, 0, -0.685898, 0, 0, 0 ) [node name="Cube" type="MeshInstance" parent="."] +visible = false mesh = SubResource( 1 ) material/0 = SubResource( 2 ) @@ -61,12 +135,22 @@ visible = false mesh = SubResource( 5 ) material/0 = SubResource( 7 ) -[node name="Quad" type="MeshInstance" parent="."] +[node name="Prism" type="MeshInstance" parent="."] visible = false mesh = SubResource( 8 ) material/0 = SubResource( 9 ) -[node name="Plane" type="MeshInstance" parent="."] +[node name="Quad" type="MeshInstance" parent="."] visible = false mesh = SubResource( 10 ) material/0 = SubResource( 11 ) + +[node name="Plane" type="MeshInstance" parent="."] +visible = false +mesh = SubResource( 12 ) +material/0 = SubResource( 13 ) + +[node name="SphereTess" type="MeshInstance" parent="."] +visible = false +mesh = SubResource( 5 ) +material/0 = SubResource( 15 )