khora_core/asset/materials/mod.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Defines the core traits and material types for the rendering system.
16
17mod alpha_mode;
18mod emissive;
19mod standard;
20mod unlit;
21mod wireframe;
22
23pub use alpha_mode::*;
24pub use emissive::*;
25pub use standard::*;
26pub use unlit::*;
27pub use wireframe::*;
28
29use std::any::Any;
30
31use super::Asset;
32
33/// Helper trait to allow downcasting `dyn Material` trait objects to their concrete types.
34pub trait AsAny {
35 /// Returns a reference to the inner value as `&dyn Any`.
36 fn as_any(&self) -> &dyn Any;
37}
38
39impl<T: Any> AsAny for T {
40 fn as_any(&self) -> &dyn Any {
41 self
42 }
43}
44
45/// A trait for types that can be used as a material.
46///
47/// A material defines the surface properties of an object being rendered,
48/// influencing how it interacts with light and determining which shader
49/// (`RenderPipeline`) is used to draw it.
50pub trait Material: Asset + AsAny {
51 /// Returns the base color (albedo or diffuse) of the material.
52 /// Default implementation is White.
53 fn base_color(&self) -> crate::math::LinearRgba {
54 crate::math::LinearRgba::WHITE
55 }
56
57 /// Returns the emissive color of the material.
58 /// Default implementation is Black.
59 fn emissive_color(&self) -> crate::math::LinearRgba {
60 crate::math::LinearRgba::BLACK
61 }
62
63 /// Returns the specular power or roughness conversion for the material.
64 /// Default implementation is 32.0.
65 fn specular_power(&self) -> f32 {
66 32.0
67 }
68
69 /// Returns the ambient color modifier for the material.
70 /// Default implementation is (0.1, 0.1, 0.1, 0.0).
71 fn ambient_color(&self) -> crate::math::LinearRgba {
72 crate::math::LinearRgba::new(0.1, 0.1, 0.1, 0.0)
73 }
74}
75
76/// This is the key to our type-erased material handle system.
77/// We explicitly tell the compiler that a boxed, dynamic Material trait
78/// object can itself be treated as a valid Asset. This allows it to be
79/// stored inside an AssetHandle.
80impl Asset for Box<dyn Material> {}