create eevee material

pull/34/head
lendo 2020-06-06 22:02:58 +02:00
rodzic e22758c801
commit c3d27c0cc2
1 zmienionych plików z 84 dodań i 56 usunięć

Wyświetl plik

@ -400,106 +400,134 @@ class Material(IntEnum):
# Uses an image cache dictionary to prevent loading the same asset from disk twice. # Uses an image cache dictionary to prevent loading the same asset from disk twice.
# Returns the texture. # Returns the texture.
img_cache = {} img_cache = {}
def create_texture(name, tex_type, filename, use_alpha=True):
def get_Image(filename):
if filename in img_cache: if filename in img_cache:
# Image has been cached already, so just use that. # Image has been cached already, so just use that.
img = img_cache[(filename, use_alpha)] img = img_cache[filename]
else: else:
# We haven't cached this asset yet, so load it from disk. # We haven't cached this asset yet, so load it from disk.
try: try:
img = bpy.data.images.load(filename) img = bpy.data.images.load(filename, check_existing=True)
except: except:
raise IOError("Cannot load image: %s" % filename) raise IOError("Cannot load image: %s" % filename)
# img.use_alpha = use_alpha
img.pack() img.pack()
# Cache the asset # Cache the asset
img_cache[(filename, use_alpha)] = img img_cache[filename] = img
# Create and return a new texture using img return img
tex = bpy.data.textures.new(name, tex_type)
tex.image = img # Returns shader node
return tex 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. # Adds a hull normal map texture slot to a material.
def add_hull_normal_map(mat, hull_normal_colortex): def add_hull_normal_map(mat, hull_normal_map):
mtex = mat.texture_slots.add() ntree = mat.node_tree
mtex.texture = hull_normal_colortex shader = getShaderNode(mat)
mtex.texture_coords = 'GLOBAL' # global UVs, yolo links = ntree.links
mtex.mapping = 'CUBE'
mtex.use_map_color_diffuse = False teximage_node = ntree.nodes.new('ShaderNodeTexImage')
mtex.use_map_normal = True teximage_node.image = hull_normal_map
mtex.normal_factor = 1 teximage_node.image.colorspace_settings.name = 'Raw'
mtex.bump_method = 'BUMP_BEST_QUALITY' 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. # Sets some basic properties for a hull material.
def set_hull_mat_basics(mat, color, hull_normal_colortex): def set_hull_mat_basics(mat, color, hull_normal_map):
mat.specular_intensity = 0.1 shader_node = getShaderNode(mat)
mat.diffuse_color = color shader_node.inputs["Specular"].default_value = 0.1
add_hull_normal_map(mat, hull_normal_colortex) 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. # Creates all our materials and returns them as a list.
def create_materials(): def create_materials():
ret = [] ret = []
for material in Material: 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 # Choose a base color for the spaceship hull
hull_base_color = hls_to_rgb( hull_base_color = hls_to_rgb(
random(), uniform(0.05, 0.5), uniform(0, 0.25)) 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 # Load up the hull normal map
hull_normal_colortex = create_texture( hull_normal_map = get_Image(resource_path('textures', 'hull_normal.png'))
'ColorTex', 'IMAGE', resource_path('textures', 'hull_normal.png'))
hull_normal_colortex.use_normal_map = True
# Build the hull texture # Build the hull texture
mat = ret[Material.hull] 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 # Build the hull_lights texture
mat = ret[Material.hull_lights] 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 # Add a diffuse layer that sets the window color
mtex = mat.texture_slots.add() hull_lights_diffuse_map = get_Image(resource_path('textures', 'hull_lights_diffuse.png'))
mtex.texture = create_texture( teximage_diff_node = ntree.nodes.new('ShaderNodeTexImage')
'ColorTex', 'IMAGE', resource_path('textures', 'hull_lights_diffuse.png')) teximage_diff_node.image = hull_lights_diffuse_map
mtex.texture_coords = 'GLOBAL' teximage_diff_node.projection ='BOX'
mtex.mapping = 'CUBE' links.new(tex_coords_node.outputs['Object'], teximage_diff_node.inputs['Vector'])
mtex.blend_type = 'ADD' RGB_node = ntree.nodes.new('ShaderNodeRGB')
mtex.use_map_color_diffuse = True RGB_node.outputs[0].default_value = hull_base_color
mtex.use_rgb_to_intensity = True mix_node = ntree.nodes.new('ShaderNodeMixRGB')
mtex.color = hls_to_rgb(random(), uniform(0.5, 1), uniform(0, 0.5)) 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 # Add an emissive layer that lights up the windows
mtex = mat.texture_slots.add() hull_lights_emessive_map = get_Image(resource_path('textures', 'hull_lights_emit.png'))
mtex.texture = create_texture( teximage_emit_node = ntree.nodes.new('ShaderNodeTexImage')
'ColorTex', 'IMAGE', resource_path('textures', 'hull_lights_emit.png'), False) teximage_emit_node.image = hull_lights_emessive_map
mtex.texture_coords = 'GLOBAL' teximage_emit_node.projection ='BOX'
mtex.mapping = 'CUBE' links.new(tex_coords_node.outputs['Object'], teximage_emit_node.inputs['Vector'])
mtex.use_map_emit = True links.new(teximage_emit_node.outputs[0], shader_node.inputs["Emission"])
mtex.emit_factor = 2.0
mtex.blend_type = 'ADD'
mtex.use_map_color_diffuse = False
# Build the hull_dark texture # Build the hull_dark texture
mat = ret[Material.hull_dark] 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 # Choose a glow color for the exhaust + glow discs
glow_color = hls_to_rgb(random(), uniform(0.5, 1), 1) 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 = ret[Material.exhaust_burn]
mat.diffuse_color = glow_color shader_node = getShaderNode(mat)
mat.emit = 1.0 shader_node.inputs["Emission"].default_value = glow_color
# Build the glow_disc texture # # Build the glow_disc texture
mat = ret[Material.glow_disc] mat = ret[Material.glow_disc]
mat.diffuse_color = glow_color shader_node = getShaderNode(mat)
mat.emit = 1.0 shader_node.inputs["Emission"].default_value = glow_color
return ret return ret
@ -714,8 +742,8 @@ def generate_spaceship(random_seed='',
# Add materials to the spaceship # Add materials to the spaceship
me = ob.data me = ob.data
# materials = create_materials() materials = create_materials()
materials = [] # materials = []
for mat in materials: for mat in materials:
if assign_materials: if assign_materials:
me.materials.append(mat) me.materials.append(mat)