Done with textures

This commit is contained in:
Wito Wiala 2026-09-01 23:04:46 +01:00
parent aba574219c
commit 2448ab890e
5 changed files with 113 additions and 39 deletions

View file

@ -39,8 +39,12 @@ impl FloatBilerpTexture {
} }
} }
pub fn evaluate(&self, _ctx: &TextureEvalContext) -> Float { pub fn evaluate(&self, ctx: &TextureEvalContext) -> Float {
todo!() let c = self.mapping.map(ctx);
(1. - c.st[0]) * (1. - c.st[1]) * self.v00
+ c.st[0] * (1. - c.st[1]) * self.v10
+ (1. - c.st[0]) * c.st[1] * self.v01
+ c.st[0] * c.st[1] * self.v11
} }
} }

View file

@ -23,6 +23,24 @@ pub struct MarbleTexture {
} }
impl MarbleTexture { impl MarbleTexture {
pub fn new(
mapping: TextureMapping3D,
octaves: i32,
omega: Float,
scale: Float,
variation: Float,
colorspace: Ptr<RGBColorSpace>,
) -> Self {
Self {
mapping,
octaves: octaves.try_into().unwrap(),
omega,
scale,
variation,
colorspace,
}
}
pub fn evaluate( pub fn evaluate(
&self, &self,
ctx: &TextureEvalContext, ctx: &TextureEvalContext,

View file

@ -9,8 +9,8 @@ use shared::core::geometry::Vector3f;
use shared::core::image::WrapMode; use shared::core::image::WrapMode;
use shared::core::texture::SpectrumType; use shared::core::texture::SpectrumType;
use shared::core::texture::{ use shared::core::texture::{
CylindricalMapping, PlanarMapping, PointTransformMapping, SphericalMapping, CylindricalMapping, PlanarMapping, PointTransformMapping, SphericalMapping, TextureEvalContext,
TextureEvalContext, TextureMapping2D, TextureMapping3D, UVMapping, TextureMapping2D, TextureMapping3D, UVMapping,
}; };
use shared::spectra::{SampledSpectrum, SampledWavelengths}; use shared::spectra::{SampledSpectrum, SampledWavelengths};
use shared::textures::{ use shared::textures::{
@ -115,26 +115,53 @@ impl SpectrumTexture {
arena: &Arena, arena: &Arena,
) -> Result<Self> { ) -> Result<Self> {
match name { match name {
"constant" => { "constant" => SpectrumConstantTexture::create(
SpectrumConstantTexture::create(render_from_texture, params, spectrum_type, loc, arena) render_from_texture,
} params,
"scale" => { spectrum_type,
SpectrumScaledTexture::create(render_from_texture, params, spectrum_type, loc, arena) loc,
} arena,
"mix" => SpectrumMixTexture::create(render_from_texture, params, spectrum_type, loc, arena), ),
"directionmix" => { "scale" => SpectrumScaledTexture::create(
SpectrumDirectionMixTexture::create(render_from_texture, params, spectrum_type, loc, arena) render_from_texture,
} params,
"bilerp" => { spectrum_type,
SpectrumBilerpTexture::create(render_from_texture, params, spectrum_type, loc, arena) loc,
arena,
),
"mix" => {
SpectrumMixTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"directionmix" => SpectrumDirectionMixTexture::create(
render_from_texture,
params,
spectrum_type,
loc,
arena,
),
"bilerp" => SpectrumBilerpTexture::create(
render_from_texture,
params,
spectrum_type,
loc,
arena,
),
"imagemap" => { "imagemap" => {
SpectrumImageTexture::create(render_from_texture, params, spectrum_type, loc, arena) SpectrumImageTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"checkerboard" => { "checkerboard" => SpectrumCheckerboardTexture::create(
SpectrumCheckerboardTexture::create(render_from_texture, params, spectrum_type, loc, arena) render_from_texture,
params,
spectrum_type,
loc,
arena,
),
"dots" => {
SpectrumDotsTexture::create(render_from_texture, params, spectrum_type, loc, arena)
}
"marble" => {
MarbleTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"dots" => SpectrumDotsTexture::create(render_from_texture, params, spectrum_type, loc, arena),
_ => Err(anyhow!( _ => Err(anyhow!(
"Spectrum texture type '{}' unknown at {}", "Spectrum texture type '{}' unknown at {}",
name, name,
@ -209,7 +236,6 @@ impl CreateTextureMapping for TextureMapping3D {
let mapping = PointTransformMapping::new(render_from_texture.inverse()); let mapping = PointTransformMapping::new(render_from_texture.inverse());
Ok(TextureMapping3D::PointTransform(mapping)) Ok(TextureMapping3D::PointTransform(mapping))
} }
} }
pub static TEXTURE_CACHE: OnceLock<Mutex<HashMap<TexInfo, Arc<MIPMap>>>> = OnceLock::new(); pub static TEXTURE_CACHE: OnceLock<Mutex<HashMap<TexInfo, Arc<MIPMap>>>> = OnceLock::new();

View file

@ -1,19 +1,29 @@
use crate::core::texture::{CreateSpectrumTexture, SpectrumTexture};
use crate::utils::{FileLoc, TextureParameterDictionary};
use crate::Arena; use crate::Arena;
use crate::core::texture::{CreateSpectrumTexture, CreateTextureMapping, SpectrumTexture};
use crate::spectra::get_colorspace_device;
use crate::utils::{FileLoc, TextureParameterDictionary};
use anyhow::Result; use anyhow::Result;
use shared::Transform; use shared::Transform;
use shared::core::texture::SpectrumType; use shared::core::texture::{SpectrumType, TextureMapping3D};
use shared::textures::MarbleTexture; use shared::textures::MarbleTexture;
impl CreateSpectrumTexture for MarbleTexture { impl CreateSpectrumTexture for MarbleTexture {
fn create( fn create(
_render_from_texture: Transform, render_from_texture: Transform,
_parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, spectrum_type: SpectrumType,
_loc: FileLoc, loc: FileLoc,
_arena: &Arena, _arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
todo!() let map = TextureMapping3D::create(&parameters, &render_from_texture, &loc)?;
let tex = MarbleTexture::new(
map,
parameters.get_one_int("octaves", 8)?,
parameters.get_one_float("roughness", 0.5)?,
parameters.get_one_float("scale", 1.)?,
parameters.get_one_float("variation", 0.2)?,
get_colorspace_device().srgb,
);
Ok(SpectrumTexture::Marble(tex))
} }
} }

View file

@ -4,8 +4,9 @@ use crate::utils::{FileLoc, TextureParameterDictionary};
use anyhow::Result; use anyhow::Result;
use shared::Float; use shared::Float;
use shared::core::geometry::{Vector3f, VectorLike}; use shared::core::geometry::{Vector3f, VectorLike};
use shared::core::spectrum::Spectrum;
use shared::core::texture::{SpectrumType, TextureEvalContext}; use shared::core::texture::{SpectrumType, TextureEvalContext};
use shared::spectra::{SampledSpectrum, SampledWavelengths}; use shared::spectra::{ConstantSpectrum, SampledSpectrum, SampledWavelengths};
use shared::utils::Transform; use shared::utils::Transform;
use std::sync::Arc; use std::sync::Arc;
@ -90,11 +91,11 @@ impl CreateSpectrumTexture for SpectrumMixTexture {
.expect("default supplied") .expect("default supplied")
}; };
let tex = SpectrumMixTexture::new( let tex = SpectrumMixTexture {
tex("tex1", zero), tex1: tex("tex1", zero),
tex("tex2", one), tex2: tex("tex2", one),
parameters.get_float_texture("amount", 0.5)?, amount: parameters.get_float_texture("amount", 0.5)?,
); };
Ok(SpectrumTexture::Mix(tex)) Ok(SpectrumTexture::Mix(tex))
} }
} }
@ -108,12 +109,27 @@ pub struct SpectrumDirectionMixTexture {
impl CreateSpectrumTexture for SpectrumDirectionMixTexture { impl CreateSpectrumTexture for SpectrumDirectionMixTexture {
fn create( fn create(
_render_from_texture: Transform, render_from_texture: Transform,
_parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, spectrum_type: SpectrumType,
_loc: FileLoc, loc: FileLoc,
_arena: &Arena, _arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
todo!() let dir_raw = parameters.get_one_vector3f("dir", Vector3f::new(0., 1., 0.))?;
let dir = render_from_texture.apply_to_vector(dir_raw).normalize();
let tex = |name: &str, def: Spectrum| {
parameters
.get_spectrum_texture(name, Some(def), spectrum_type)
.expect("default supplied")
};
let zero = Spectrum::Constant(ConstantSpectrum::new(0.));
let one = Spectrum::Constant(ConstantSpectrum::new(1.));
let tex = SpectrumDirectionMixTexture {
tex1: tex("tex1", zero),
tex2: tex("tex2", one),
dir,
};
Ok(SpectrumTexture::DirectionMix(tex))
} }
} }