mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-28 17:19:17 +01:00
Normalize integer Levels endpoints and preserve scalar channel alpha
This commit is contained in:
1 parent
313bf96d40
commit
83516a4bfa
12 files changed
+154
-62
No files matched your search
@@ -2,20 +2,24 @@ use materialcraft_core::{FunctionGraph, Value, ValueType};
|
||||
use materialcraft_nodes::function::{EvaluationContext, EvaluationState, compile};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum ChannelMode {
|
||||
Opaque,
|
||||
Broadcast,
|
||||
Normalized,
|
||||
}
|
||||
|
||||
pub(crate) fn evaluate(
|
||||
function: &FunctionGraph,
|
||||
expected: ValueType,
|
||||
variables: &mut BTreeMap<String, Value>,
|
||||
seed: u32,
|
||||
size: [u32; 2],
|
||||
allow_scalar: bool,
|
||||
channels: ChannelMode,
|
||||
) -> Result<Value, String> {
|
||||
let program = compile(function, seed)?;
|
||||
let scalar_channel = allow_scalar && expected == ValueType::Float4 && program.output_type() == ValueType::Float as u32;
|
||||
|
||||
// A color parameter also takes a gray level or an opaque RGB triple
|
||||
let widened = expected == ValueType::Float4 && [ValueType::Float as u32, ValueType::Float3 as u32].contains(&program.output_type());
|
||||
if program.output_type() != expected as u32 && !scalar_channel && !widened {
|
||||
if program.output_type() != expected as u32 && !widened {
|
||||
return Err(format!("output must be {}", expected.label()));
|
||||
}
|
||||
|
||||
@@ -29,21 +33,13 @@ pub(crate) fn evaluate(
|
||||
variables,
|
||||
};
|
||||
let mut state = EvaluationState::default();
|
||||
let value = program.evaluate(&context, &mut state)?;
|
||||
let finite = match &value {
|
||||
Value::Float(value) => value.is_finite(),
|
||||
Value::Float2(values) => values.iter().all(|value| value.is_finite()),
|
||||
Value::Float3(values) => values.iter().all(|value| value.is_finite()),
|
||||
Value::Float4(values) | Value::Color(values) => values.iter().all(|value| value.is_finite()),
|
||||
_ => true,
|
||||
let value = match channels {
|
||||
ChannelMode::Normalized => program.evaluate_normalized_color(&context, &mut state)?,
|
||||
_ => program.evaluate(&context, &mut state)?,
|
||||
};
|
||||
|
||||
if !finite {
|
||||
return Err("function returned a non-finite value".into());
|
||||
}
|
||||
|
||||
let value = match value {
|
||||
Value::Float(level) if !scalar_channel && widened => Value::Float4([level, level, level, 1.0]),
|
||||
Value::Float(level) if widened && matches!(channels, ChannelMode::Broadcast) => Value::Float4([level; 4]),
|
||||
Value::Float(level) if widened => Value::Float4([level, level, level, 1.0]),
|
||||
Value::Float3([red, green, blue]) if widened => Value::Float4([red, green, blue, 1.0]),
|
||||
value => value,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user