Skip to content

Commit

Permalink
Hull material colours are now directly editable properties,
Browse files Browse the repository at this point in the history
randomly (re)initialized every time a new spaceship is created,
rather than indirectly depending on an editable random seed.
  • Loading branch information
Lawrence D'Oliveiro committed Apr 29, 2020
1 parent b35dc67 commit c737433
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 51 deletions.
44 changes: 33 additions & 11 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"name" : "Spaceship Generator",
"author" : "Michael Davies, Lawrence D'Oliveiro",
"version" : (1, 4, 6),
"version" : (1, 5, 0),
"blender" : (2, 82, 0),
"location" : "View3D > Add > Mesh",
"description" : "Procedurally generate 3D spaceships from a random seed.",
Expand All @@ -25,6 +25,7 @@
from bpy.props import \
BoolProperty, \
FloatProperty, \
FloatVectorProperty, \
IntProperty, \
StringProperty

Expand All @@ -40,11 +41,6 @@ class GenerateSpaceship(bpy.types.Operator) :
default = df.geom_ranseed,
name = "Geometry Seed"
)
mat_ranseed : StringProperty \
(
default = df.mat_ranseed,
name = "Material Seed"
)
num_hull_segments_min : IntProperty \
(
default = df.num_hull_segments_min,
Expand Down Expand Up @@ -103,6 +99,31 @@ class GenerateSpaceship(bpy.types.Operator) :
default = df.create_materials,
name = "Assign Materials"
)
hull_base_color : FloatVectorProperty \
(
subtype = "COLOR",
default = df.hull_base_color,
name = "Hull Base Color"
)
hull_darken : FloatProperty \
(
default = df.hull_darken,
min = 0,
max = 1,
name = "Hull Darken Factor"
)
hull_emissive_color : FloatVectorProperty \
(
subtype = "COLOR",
default = df.hull_emissive_color,
name = "Window Emissive Color"
)
glow_color : FloatVectorProperty \
(
subtype = "COLOR",
default = df.glow_color,
name = "Engine/Disc Glow Color"
)
grunge_factor : FloatProperty \
(
default = df.grunge_factor,
Expand All @@ -114,10 +135,7 @@ class GenerateSpaceship(bpy.types.Operator) :

def draw(self, context) :
main = self.layout
sub = main.box()
sub.label(text = "Random Seeds")
sub.prop(self, "geom_ranseed", text = "Geometry")
sub.prop(self, "mat_ranseed", text = "Material")
main.prop(self, "geom_ranseed", text = "Geometry Seed")
sub = main.box()
sub.label(text = "Hull Segments")
row = sub.row()
Expand All @@ -142,6 +160,10 @@ def draw(self, context) :
if self.create_materials :
sub = main.box()
sub.prop(self, "create_materials", text = "Create Materials")
sub.prop(self, "hull_base_color", text = "Hull Base")
sub.prop(self, "hull_darken", text = "Hull Darken")
sub.prop(self, "hull_emissive_color", text = "Hull Emissive")
sub.prop(self, "glow_color", text = "Engine/Disc Glow")
sub.prop(self, "grunge_factor", text = "Grunge")
else :
main.prop(self, "create_materials", text = "Create Materials")
Expand All @@ -154,7 +176,7 @@ def invoke(self, context, event) :
# default. Users can always replace seeds with
# anything they like.
self.geom_ranseed = str(random.randrange(maxseed))
self.mat_ranseed = str(random.randrange(maxseed))
spaceship_generator.randomize_colors(self, random.Random())
spaceship_generator.generate_spaceship(self)
return {"FINISHED"}
#end invoke
Expand Down
72 changes: 32 additions & 40 deletions spaceship_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class MATERIAL(IntEnum) :
GLOW_DISC = 4 # Emissive landing pad disc material
#end MATERIAL

def create_materials(parms, mat_random) :
def create_materials(parms) :
# Creates all our materials and returns them as a list.

def define_tex_coords_common() :
Expand Down Expand Up @@ -328,7 +328,7 @@ def set_hull_mat_basics(mat, base_color) :
save_pos = ctx.pos
color_mix = ctx.node("ShaderNodeGroup", ctx.step_down(200))
color_mix.node_tree = hull_color_common
color_mix.inputs["Color"].default_value = base_color
color_mix.inputs["Color"].default_value = tuple(base_color) + (1,)
normal_map = ctx.node("ShaderNodeGroup", ctx.step_across(200))
normal_map.node_tree = normals_common
ctx.pos = (ctx.pos[0], save_pos[1])
Expand Down Expand Up @@ -357,7 +357,7 @@ def set_hull_mat_basics(mat, base_color) :
def set_hull_mat_emissive(mat, color, strength) :
# does common setup for very basic emissive hull materials (engines, landing discs)
main_shader = find_main_shader(mat)
main_shader.inputs["Emission"].default_value = tuple(c * strength for c in color)
main_shader.inputs["Emission"].default_value = tuple(c * strength for c in color[:3]) + (1,)
deselect_all(mat.node_tree)
#end set_hull_mat_emissive

Expand All @@ -370,21 +370,8 @@ def set_hull_mat_emissive(mat, color, strength) :
materials.append(mat)
#end for

# Choose a base color for the spaceship hull
hull_base_color = \
(
hls_to_rgb
(
h = mat_random.random(),
l = mat_random.uniform(0.05, 0.5),
s = mat_random.uniform(0, 0.25)
)
+
(1,)
)

# Build the hull texture
set_hull_mat_basics(materials[MATERIAL.HULL], hull_base_color)
set_hull_mat_basics(materials[MATERIAL.HULL], parms.hull_base_color)

ctx = NodeContext(materials[MATERIAL.HULL_LIGHTS].node_tree, (-600, 0), clear = True)
normal_map = ctx.node("ShaderNodeGroup", ctx.step_down(200))
Expand All @@ -402,22 +389,12 @@ def set_hull_mat_emissive(mat, color, strength) :
mixer1.blend_type = "MULTIPLY"
mixer1.inputs[0].default_value = 1.0
ctx.link(base_window, mixer1.inputs[1])
mixer1.inputs[2].default_value = \
(
hls_to_rgb
(
h = mat_random.random(),
l = mat_random.uniform(0.5, 1),
s = mat_random.uniform(0, 0.5)
)
+
(1,)
)
mixer1.inputs[2].default_value = tuple(parms.hull_emissive_color) + (1,)
mixer2 = ctx.node("ShaderNodeMixRGB", ctx.step_across(200))
mixer2.blend_type = "ADD"
mixer2.inputs[0].default_value = 1.0
ctx.link(mixer1.outputs[0], mixer2.inputs[1])
mixer2.inputs[2].default_value = hull_base_color
mixer2.inputs[2].default_value = tuple(parms.hull_base_color) + (1,)
color_mix = ctx.node("ShaderNodeGroup", ctx.step_across(200))
color_mix.node_tree = hull_color_common
ctx.link(mixer2.outputs[0], color_mix.inputs[0])
Expand Down Expand Up @@ -448,17 +425,14 @@ def set_hull_mat_emissive(mat, color, strength) :
set_hull_mat_basics \
(
materials[MATERIAL.HULL_DARK],
tuple(0.3 * x for x in hull_base_color[:3]) + (1,)
tuple(parms.hull_darken * x for x in parms.hull_base_color[:3])
)

# Choose a glow color for the exhaust + glow discs
glow_color = hls_to_rgb(h = mat_random.random(), l = mat_random.uniform(0.5, 1), s = 1) + (1,)

# Build the exhaust_burn texture
set_hull_mat_emissive(materials[MATERIAL.EXHAUST_BURN], glow_color, 1.0)
set_hull_mat_emissive(materials[MATERIAL.EXHAUST_BURN], parms.glow_color, 1.0)

# Build the glow_disc texture
set_hull_mat_emissive(materials[MATERIAL.GLOW_DISC], glow_color, 1.0)
set_hull_mat_emissive(materials[MATERIAL.GLOW_DISC], parms.glow_color, 1.0)

return materials
#end create_materials
Expand All @@ -477,9 +451,31 @@ class parms_defaults :
allow_vertical_symmetry = False
add_bevel_modifier = True
create_materials = True
hull_base_color = (0.5, 0.5, 0.5)
hull_darken = 0.3
hull_emissive_color = (0.75, 0.75, 0.75)
glow_color = (1, 1, 1)
grunge_factor = 0.5
#end parms_defaults

def randomize_colors(parms, mat_random) :
# Choose a base color for the spaceship hull
parms.hull_base_color = hls_to_rgb \
(
h = mat_random.random(),
l = mat_random.uniform(0.05, 0.5),
s = mat_random.uniform(0, 0.25)
)
parms.hull_emissive_color = hls_to_rgb \
(
h = mat_random.random(),
l = mat_random.uniform(0.5, 1),
s = mat_random.uniform(0, 0.5)
)
# Choose a glow color for the exhaust + glow discs
parms.glow_color = hls_to_rgb(h = mat_random.random(), l = mat_random.uniform(0.5, 1), s = 1)
#end randomize_colors

def generate_spaceship(parms) :
# Generates a textured spaceship mesh and returns the object.
# Just uses global cube texture coordinates rather than generating UVs.
Expand Down Expand Up @@ -1144,12 +1140,8 @@ def add_disc_to_face(bm, face) :

# Add materials to the spaceship
me = ob.data
mat_random = Random()
if parms.mat_ranseed != "" :
mat_random.seed(parms.mat_ranseed)
#end if
if parms.create_materials :
materials = create_materials(parms, mat_random)
materials = create_materials(parms)
for mat in materials :
me.materials.append(mat)
#end for
Expand Down

0 comments on commit c737433

Please sign in to comment.