Skip to content

Commit

Permalink
packed alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilaa3 committed Oct 26, 2024
1 parent 03feb19 commit e50e6a8
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions fast64_internal/f3d/bsdf_converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class AbstractedN64Texture:
repeat: bool = False
set_color: bool = False
set_alpha: bool = False
color_is_alpha: bool = False
packed_alpha: bool = False
alpha_as_color: bool = False # tex0 alpha, gathered in bsdf to f3d


@dataclasses.dataclass
Expand Down Expand Up @@ -210,7 +211,7 @@ def to_offset(low: float, tex_size: int):
abstracted_tex.offset = (to_offset(f3d_tex.S.low, size[0]), to_offset(f3d_tex.T.low, size[1]))
abstracted_tex.scale = (2.0 ** (f3d_tex.S.shift * -1.0), 2.0 ** (f3d_tex.T.shift * -1.0))
abstracted_tex.set_color, abstracted_tex.set_alpha = set_color, set_alpha
abstracted_tex.color_is_alpha = f3d_tex.tex_format in {"I4", "I8"}
abstracted_tex.packed_alpha = f3d_tex.tex_format in {"I4", "I8"}

return abstracted_tex

Expand Down Expand Up @@ -437,7 +438,7 @@ def create_node(typ: T, name: str, location=False, x_offset=0, y_offset=0):
if abstracted_tex.set_color:
links.new(tex_node.outputs[0], tex_color_input)
if abstracted_tex.set_alpha:
if abstracted_tex.color_is_alpha: # i4/i8
if abstracted_tex.packed_alpha: # i4/i8
links.new(tex_node.outputs[0], tex_alpha_input)
else:
links.new(tex_node.outputs[1], tex_alpha_input)
Expand Down Expand Up @@ -619,13 +620,29 @@ def get_vtx_layer(nodes):
alpha_shader,
lambda node: node.bl_idname == "ShaderNodeTexImage",
specific_input_sockets={alpha_inp},
specific_output_sockets={"Alpha"},
)
packed_textures = find_linked_nodes( # textures that use color as alpha
alpha_shader,
lambda node: node.bl_idname == "ShaderNodeTexImage",
specific_input_sockets={alpha_inp},
specific_output_sockets={"Color"},
)
color_textures = find_linked_nodes(
color_shader,
lambda node: node.bl_idname == "ShaderNodeTexImage",
specific_input_sockets={color_inp},
specific_output_sockets={"Color"},
)
alpha_as_color_textures = find_linked_nodes(
color_shader,
lambda node: node.bl_idname == "ShaderNodeTexImage",
specific_input_sockets={color_inp},
specific_output_sockets={"Alpha"},
)
textures: list[ShaderNodeTexImage] = list(
dict.fromkeys(color_textures + alpha_as_color_textures + packed_textures + alpha_textures).keys()
)
textures: list[ShaderNodeTexImage] = list(dict.fromkeys(color_textures + alpha_textures).keys())
if len(textures) > 2:
print(f"WARNING: More than 2 textures connected to {material.name}.")
for tex_node in textures[:2]:
Expand Down Expand Up @@ -658,6 +675,8 @@ def get_vtx_layer(nodes):
abstracted_tex.repeat = tex_node.extension == "REPEAT"
abstracted_tex.set_color = tex_node in color_textures
abstracted_tex.set_alpha = tex_node in alpha_textures
abstracted_tex.packed_alpha = tex_node in packed_textures
abstracted_tex.alpha_as_color = tex_node in alpha_as_color_textures
if abstracted_tex.set_color:
abstracted_mat.texture_sets_col = True
if abstracted_tex.set_alpha:
Expand Down Expand Up @@ -725,6 +744,8 @@ def material_to_f3d(
t.low = abstracted_tex.offset[1]
s.shift = int(-math.log2(abstracted_tex.scale[0]))
t.shift = int(-math.log2(abstracted_tex.scale[1]))
if abstracted_tex.packed_alpha: # if color is being used as alpha, assume intensity texture
f3d_tex.tex_format = "I8"

combiner1: CombinerProperty = f3d_mat.combiner1
combiner2: CombinerProperty = f3d_mat.combiner2
Expand Down Expand Up @@ -757,6 +778,8 @@ def set_combiner_cycle(inputs: list[str], suffix=""):
color_inputs.append(f"TEXEL{i}")
if abstracted_tex.set_alpha:
alpha_inputs.append(f"TEXEL{i}")
if abstracted_tex.alpha_as_color:
color_inputs.append(f"TEXEL{i}_ALPHA")
if abstracted_mat.color[:3] != [1, 1, 1]:
if lights_for_colors and abstracted_mat.lighting:
color_inputs.append("SHADE")
Expand Down

0 comments on commit e50e6a8

Please sign in to comment.