From c3d27c0cc281f18f131249b51b617fd73993062f Mon Sep 17 00:00:00 2001 From: lendo Date: Sat, 6 Jun 2020 22:02:58 +0200 Subject: [PATCH] create eevee material --- spaceship_generator.py | 140 ++++++++++++++++++++++++----------------- 1 file changed, 84 insertions(+), 56 deletions(-) diff --git a/spaceship_generator.py b/spaceship_generator.py index 57a1460..0efff39 100644 --- a/spaceship_generator.py +++ b/spaceship_generator.py @@ -400,106 +400,134 @@ class Material(IntEnum): # Uses an image cache dictionary to prevent loading the same asset from disk twice. # Returns the texture. img_cache = {} -def create_texture(name, tex_type, filename, use_alpha=True): + +def get_Image(filename): if filename in img_cache: # Image has been cached already, so just use that. - img = img_cache[(filename, use_alpha)] + img = img_cache[filename] else: # We haven't cached this asset yet, so load it from disk. try: - img = bpy.data.images.load(filename) + img = bpy.data.images.load(filename, check_existing=True) except: raise IOError("Cannot load image: %s" % filename) - - # img.use_alpha = use_alpha img.pack() # Cache the asset - img_cache[(filename, use_alpha)] = img + img_cache[filename] = img - # Create and return a new texture using img - tex = bpy.data.textures.new(name, tex_type) - tex.image = img - return tex + return img + +# Returns shader node +def getShaderNode(mat): + ntree = mat.node_tree + node_out = ntree.get_output_node('EEVEE') + shader_node = node_out.inputs['Surface'].links[0].from_node + return shader_node + +def getShaderInput(mat, name): + shaderNode = getShaderNode(mat) + return shaderNode.inputs[name] # Adds a hull normal map texture slot to a material. -def add_hull_normal_map(mat, hull_normal_colortex): - mtex = mat.texture_slots.add() - mtex.texture = hull_normal_colortex - mtex.texture_coords = 'GLOBAL' # global UVs, yolo - mtex.mapping = 'CUBE' - mtex.use_map_color_diffuse = False - mtex.use_map_normal = True - mtex.normal_factor = 1 - mtex.bump_method = 'BUMP_BEST_QUALITY' +def add_hull_normal_map(mat, hull_normal_map): + ntree = mat.node_tree + shader = getShaderNode(mat) + links = ntree.links + + teximage_node = ntree.nodes.new('ShaderNodeTexImage') + teximage_node.image = hull_normal_map + teximage_node.image.colorspace_settings.name = 'Raw' + teximage_node.projection ='BOX' + tex_coords_node = ntree.nodes.new('ShaderNodeTexCoord') + links.new(tex_coords_node.outputs['Object'], teximage_node.inputs['Vector']) + normalMap_node = ntree.nodes.new('ShaderNodeNormalMap') + links.new(teximage_node.outputs[0], normalMap_node.inputs['Color']) + links.new(normalMap_node.outputs['Normal'], shader.inputs['Normal']) + return tex_coords_node + + # Sets some basic properties for a hull material. -def set_hull_mat_basics(mat, color, hull_normal_colortex): - mat.specular_intensity = 0.1 - mat.diffuse_color = color - add_hull_normal_map(mat, hull_normal_colortex) +def set_hull_mat_basics(mat, color, hull_normal_map): + shader_node = getShaderNode(mat) + shader_node.inputs["Specular"].default_value = 0.1 + shader_node.inputs["Base Color"].default_value = color + + return add_hull_normal_map(mat, hull_normal_map) # Creates all our materials and returns them as a list. def create_materials(): ret = [] + for material in Material: - ret.append(bpy.data.materials.new(material.name)) + mat = bpy.data.materials.new(name=material.name) + mat.use_nodes = True + ret.append(mat) # Choose a base color for the spaceship hull hull_base_color = hls_to_rgb( random(), uniform(0.05, 0.5), uniform(0, 0.25)) + hull_base_color = (hull_base_color[0], hull_base_color[1], hull_base_color[2], 1.0) # Load up the hull normal map - hull_normal_colortex = create_texture( - 'ColorTex', 'IMAGE', resource_path('textures', 'hull_normal.png')) - hull_normal_colortex.use_normal_map = True + hull_normal_map = get_Image(resource_path('textures', 'hull_normal.png')) + # Build the hull texture mat = ret[Material.hull] - set_hull_mat_basics(mat, hull_base_color, hull_normal_colortex) + set_hull_mat_basics(mat, hull_base_color, hull_normal_map) # Build the hull_lights texture mat = ret[Material.hull_lights] - set_hull_mat_basics(mat, hull_base_color, hull_normal_colortex) + tex_coords_node = set_hull_mat_basics(mat, hull_base_color, hull_normal_map) + ntree = mat.node_tree + shader_node = getShaderNode(mat) + links = ntree.links # Add a diffuse layer that sets the window color - mtex = mat.texture_slots.add() - mtex.texture = create_texture( - 'ColorTex', 'IMAGE', resource_path('textures', 'hull_lights_diffuse.png')) - mtex.texture_coords = 'GLOBAL' - mtex.mapping = 'CUBE' - mtex.blend_type = 'ADD' - mtex.use_map_color_diffuse = True - mtex.use_rgb_to_intensity = True - mtex.color = hls_to_rgb(random(), uniform(0.5, 1), uniform(0, 0.5)) + hull_lights_diffuse_map = get_Image(resource_path('textures', 'hull_lights_diffuse.png')) + teximage_diff_node = ntree.nodes.new('ShaderNodeTexImage') + teximage_diff_node.image = hull_lights_diffuse_map + teximage_diff_node.projection ='BOX' + links.new(tex_coords_node.outputs['Object'], teximage_diff_node.inputs['Vector']) + RGB_node = ntree.nodes.new('ShaderNodeRGB') + RGB_node.outputs[0].default_value = hull_base_color + mix_node = ntree.nodes.new('ShaderNodeMixRGB') + links.new(RGB_node.outputs[0], mix_node.inputs[1]) + links.new(teximage_diff_node.outputs[0], mix_node.inputs[2]) + links.new(teximage_diff_node.outputs[1], mix_node.inputs[0]) + links.new(mix_node.outputs[0], shader_node.inputs["Base Color"]) + + # Add an emissive layer that lights up the windows - mtex = mat.texture_slots.add() - mtex.texture = create_texture( - 'ColorTex', 'IMAGE', resource_path('textures', 'hull_lights_emit.png'), False) - mtex.texture_coords = 'GLOBAL' - mtex.mapping = 'CUBE' - mtex.use_map_emit = True - mtex.emit_factor = 2.0 - mtex.blend_type = 'ADD' - mtex.use_map_color_diffuse = False + hull_lights_emessive_map = get_Image(resource_path('textures', 'hull_lights_emit.png')) + teximage_emit_node = ntree.nodes.new('ShaderNodeTexImage') + teximage_emit_node.image = hull_lights_emessive_map + teximage_emit_node.projection ='BOX' + links.new(tex_coords_node.outputs['Object'], teximage_emit_node.inputs['Vector']) + links.new(teximage_emit_node.outputs[0], shader_node.inputs["Emission"]) + + # Build the hull_dark texture mat = ret[Material.hull_dark] - set_hull_mat_basics(mat, [0.3 * x for x in hull_base_color], hull_normal_colortex) + set_hull_mat_basics(mat, [0.3 * x for x in hull_base_color], hull_normal_map) # Choose a glow color for the exhaust + glow discs glow_color = hls_to_rgb(random(), uniform(0.5, 1), 1) + glow_color = (glow_color[0], glow_color[1], glow_color[2], 1.0) - # Build the exhaust_burn texture + # # Build the exhaust_burn texture mat = ret[Material.exhaust_burn] - mat.diffuse_color = glow_color - mat.emit = 1.0 + shader_node = getShaderNode(mat) + shader_node.inputs["Emission"].default_value = glow_color - # Build the glow_disc texture + # # Build the glow_disc texture mat = ret[Material.glow_disc] - mat.diffuse_color = glow_color - mat.emit = 1.0 + shader_node = getShaderNode(mat) + shader_node.inputs["Emission"].default_value = glow_color return ret @@ -714,8 +742,8 @@ def generate_spaceship(random_seed='', # Add materials to the spaceship me = ob.data - # materials = create_materials() - materials = [] + materials = create_materials() + # materials = [] for mat in materials: if assign_materials: me.materials.append(mat)