Skip to content

Commit

Permalink
Fix material importing erroring in Blender 4.3
Browse files Browse the repository at this point in the history
Also could have resulted in broken materials in Blender 4.2. Fixes #194.
  • Loading branch information
lasa01 committed Dec 4, 2024
1 parent 6cd4b37 commit 0612a07
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
72 changes: 33 additions & 39 deletions src/asset/material/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,7 @@ impl MaterialBuilder {
}

fn build_nodraw_material() -> BuiltMaterialData {
let mut builder = MaterialBuilder::new(&shaders::TRANSPARENT);

builder
.property("blend_method", Value::Enum("CLIP"))
.property("shadow_method", Value::Enum("CLIP"));
let builder = MaterialBuilder::new(&shaders::TRANSPARENT);

builder.build()
}
Expand All @@ -332,8 +328,6 @@ fn build_water_material(
let mut builder = MaterialBuilder::new(&shaders::GLASS);

builder
.property("blend_method", Value::Enum("BLEND"))
.property("shadow_method", Value::Enum("HASHED"))
.socket_value("IOR", Value::Float(1.333))
.socket_value("Roughness", Value::Float(0.3));

Expand Down Expand Up @@ -824,8 +818,7 @@ impl<'a, 'b, 'c, 'd> NormalMaterialBuilder<'a, 'b, 'c, 'd> {
}

self.builder
.property("blend_method", Value::Enum("BLEND"))
.property("shadow_method", Value::Enum("HASHED"));
.property("use_transparent_shadow", Value::Bool(true));

if self.builder.has_input("$basetexture") {
let output = self.builder.output("Alpha", "$basetexture", "alpha");
Expand All @@ -849,28 +842,43 @@ impl<'a, 'b, 'c, 'd> NormalMaterialBuilder<'a, 'b, 'c, 'd> {
}

self.builder
.property("blend_method", Value::Enum("CLIP"))
.property("shadow_method", Value::Enum("CLIP"));
.property("use_transparent_shadow", Value::Bool(true));

let reference = self.vmt.extract_param("$alphatestreference").unwrap_or(0.7);
self.builder
.property("alpha_threshold", Value::Float(reference));

if self.vmt.extract_param_or_default("$allowalphatocoverage") {
self.builder.property("blend_method", Value::Enum("HASHED"));
}

if self.builder.has_input("$basetexture") {
let output = self.builder.output("Alpha", "$basetexture", "alpha");
let mut input_linked = false;

if let Some(alpha) = self.vmt.extract_param("$alpha") {
output
.push(&groups::MULTIPLY_VALUE)
.link_input(&groups::MULTIPLY_VALUE, "value")
.link(&groups::MULTIPLY_VALUE, "fac", Value::Float(alpha));

input_linked = true;
}

if !self
.vmt
.extract_param_or_default::<bool>("$allowalphatocoverage")
{
output.push(&groups::CLIP_ALPHA).link(
&groups::CLIP_ALPHA,
"ref",
Value::Float(reference),
);

if !input_linked {
output.link_input(&groups::CLIP_ALPHA, "value");
}
}
} else if let Some(alpha) = self.vmt.extract_param("$alpha") {
if reference >= alpha {
self.builder.socket_value("Alpha", Value::Float(0.0));
} else {
self.builder.socket_value("Alpha", Value::Float(1.0));
}
} else {
self.handle_alpha();
}

true
Expand All @@ -882,8 +890,7 @@ impl<'a, 'b, 'c, 'd> NormalMaterialBuilder<'a, 'b, 'c, 'd> {
}

self.builder
.property("blend_method", Value::Enum("BLEND"))
.property("shadow_method", Value::Enum("HASHED"));
.property("use_transparent_shadow", Value::Bool(true));

let output = self.builder.output("Alpha", "vertex_color", "alpha");

Expand Down Expand Up @@ -993,8 +1000,7 @@ impl<'a, 'b, 'c, 'd> NormalMaterialBuilder<'a, 'b, 'c, 'd> {

fn build_normal(&mut self) {
self.builder
.property("blend_method", Value::Enum("OPAQUE"))
.property("shadow_method", Value::Enum("OPAQUE"))
.property("use_transparent_shadow", Value::Bool(false))
.socket_value("Specular", Value::Float(0.1))
.socket_value("Roughness", Value::Float(0.9));

Expand Down Expand Up @@ -1068,8 +1074,7 @@ impl<'a, 'b, 'c, 'd> NormalMaterialBuilder<'a, 'b, 'c, 'd> {
}

self.builder
.property("blend_method", Value::Enum("BLEND"))
.property("shadow_method", Value::Enum("HASHED"));
.property("use_transparent_shadow", Value::Bool(true));

if self.builder.has_input("$basetexture") {
self.builder.output("Alpha", "$basetexture", "alpha");
Expand All @@ -1086,16 +1091,7 @@ impl<'a, 'b, 'c, 'd> NormalMaterialBuilder<'a, 'b, 'c, 'd> {
}

self.builder
.property("blend_method", Value::Enum("CLIP"))
.property("shadow_method", Value::Enum("CLIP"));

let reference = self.vmt.extract_param("$alphatestreference").unwrap_or(0.7);
self.builder
.property("alpha_threshold", Value::Float(reference));

if self.vmt.extract_param_or_default("$allowalphatocoverage") {
self.builder.property("blend_method", Value::Enum("HASHED"));
}
.property("use_transparent_shadow", Value::Bool(true));

if self.builder.has_input("$basetexture") {
self.builder.output("Alpha", "$basetexture", "alpha");
Expand Down Expand Up @@ -1172,8 +1168,7 @@ impl<'a, 'b, 'c, 'd> NormalMaterialBuilder<'a, 'b, 'c, 'd> {

fn build_simple(&mut self) {
self.builder
.property("blend_method", Value::Enum("OPAQUE"))
.property("shadow_method", Value::Enum("OPAQUE"))
.property("use_transparent_shadow", Value::Bool(false))
.socket_value("Specular", Value::Float(0.1))
.socket_value("Roughness", Value::Float(0.9));

Expand Down Expand Up @@ -1496,8 +1491,7 @@ impl<'a, 'b, 'c, 'd> NormalMaterialBuilder<'a, 'b, 'c, 'd> {

fn build_fwb(&mut self) {
self.builder
.property("blend_method", Value::Enum("OPAQUE"))
.property("shadow_method", Value::Enum("OPAQUE"))
.property("use_transparent_shadow", Value::Bool(false))
.socket_value("Specular", Value::Float(0.1))
.socket_value("Roughness", Value::Float(0.9));

Expand Down
16 changes: 16 additions & 0 deletions src/asset/material/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,21 @@ pub mod groups {
outputs: &[("val", NodeSocketRef::new("mix2", Position(0)))],
..NodeGroup::default()
};

pub static CLIP_ALPHA: NodeGroup = NodeGroup {
nodes: &[Node {
kind: &nodes::MATH,
id: "clip",
properties: &[("operation", Value::Enum("GREATER_THAN"))],
..Node::default()
}],
inputs: &[
("value", NodeSocketRef::new("clip", Position(0))),
("ref", NodeSocketRef::new("clip", Position(1))),
],
outputs: &[("value", NodeSocketRef::new("clip", Position(0)))],
..NodeGroup::default()
};
}

#[cfg(test)]
Expand Down Expand Up @@ -1079,6 +1094,7 @@ mod tests {
&groups::MULTIBLEND_TEXTURE,
&groups::MULTIBLEND_VALUE,
&groups::BLEND_3_VALUES,
&groups::CLIP_ALPHA,
];

#[test]
Expand Down
8 changes: 4 additions & 4 deletions src/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl PyImporter {
let mut settings = HandlerSettings::default();

if let Some(kwargs) = kwargs {
for (key, value) in kwargs.iter() {
for (key, value) in kwargs {
if value.is_none() {
continue;
}
Expand Down Expand Up @@ -182,7 +182,7 @@ impl PyImporter {
let mut settings = VmfConfig::new(self.material_config);

if let Some(kwargs) = kwargs {
for (key, value) in kwargs.iter() {
for (key, value) in kwargs {
match key.extract()? {
"import_brushes" => {
import_brushes = value.extract()?;
Expand Down Expand Up @@ -337,7 +337,7 @@ impl PyImporter {
fn process_assets(&self, py: Python) {
let callback_ref = self.callback_obj.as_ref(py);

for asset in self.receiver.iter() {
for asset in &self.receiver {
let kind = asset.kind();
let id = asset.id();

Expand Down Expand Up @@ -373,7 +373,7 @@ impl PyImporter {
let mut settings = MdlConfig::new(self.material_config);

if let Some(kwargs) = kwargs {
for (key, value) in kwargs.iter() {
for (key, value) in kwargs {
match key.extract()? {
"import_animations" => settings.import_animations = value.extract()?,
_ => return Err(PyTypeError::new_err("unexpected kwarg")),
Expand Down

0 comments on commit 0612a07

Please sign in to comment.