use crate::scene::node::constructor::NodeConstructor;
use crate::{
asset::{
embedded_data_source, manager::BuiltInResource, state::LoadError, untyped::ResourceKind,
},
core::{
algebra::{Matrix4, Point3, Vector2, Vector3, Vector4},
color::Color,
log::Log,
math::{aabb::AxisAlignedBoundingBox, frustum::Frustum, ray::Ray, Rect},
pool::Handle,
reflect::prelude::*,
type_traits::prelude::*,
uuid::{uuid, Uuid},
uuid_provider,
variable::InheritableVariable,
visitor::{Visit, VisitResult, Visitor},
},
graph::BaseSceneGraph,
resource::texture::{
CompressionOptions, Texture, TextureImportOptions, TextureKind, TextureMinificationFilter,
TexturePixelKind, TextureResource, TextureResourceExtension, TextureWrapMode,
},
scene::{
base::{Base, BaseBuilder},
debug::SceneDrawingContext,
graph::Graph,
node::{Node, NodeTrait, UpdateContext},
},
};
use fyrox_graph::constructor::ConstructorProvider;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use std::{
fmt::{Display, Formatter},
ops::{Deref, DerefMut},
};
use strum_macros::{AsRefStr, EnumString, VariantNames};
#[derive(Reflect, Clone, Debug, PartialEq, Visit, Serialize, Deserialize)]
pub struct PerspectiveProjection {
#[reflect(min_value = 0.0, max_value = 6.28, step = 0.1)]
pub fov: f32,
#[reflect(min_value = 0.0, step = 0.1)]
pub z_near: f32,
#[reflect(min_value = 0.0, step = 0.1)]
pub z_far: f32,
}
impl Default for PerspectiveProjection {
fn default() -> Self {
Self {
fov: 75.0f32.to_radians(),
z_near: 0.025,
z_far: 2048.0,
}
}
}
impl PerspectiveProjection {
#[inline]
pub fn matrix(&self, frame_size: Vector2<f32>) -> Matrix4<f32> {
let limit = 10.0 * f32::EPSILON;
let z_near = self.z_far.min(self.z_near);
let mut z_far = self.z_far.max(self.z_near);
if z_far - z_near < limit {
z_far += limit;
}
Matrix4::new_perspective(
(frame_size.x / frame_size.y).max(limit),
self.fov,
z_near,
z_far,
)
}
}
#[derive(Reflect, Clone, Debug, PartialEq, Visit, Serialize, Deserialize)]
pub struct OrthographicProjection {
#[reflect(min_value = 0.0, step = 0.1)]
pub z_near: f32,
#[reflect(min_value = 0.0, step = 0.1)]
pub z_far: f32,
#[reflect(step = 0.1)]
pub vertical_size: f32,
}
impl Default for OrthographicProjection {
fn default() -> Self {
Self {
z_near: 0.0,
z_far: 2048.0,
vertical_size: 5.0,
}
}
}
impl OrthographicProjection {
#[inline]
pub fn matrix(&self, frame_size: Vector2<f32>) -> Matrix4<f32> {
fn clamp_to_limit_signed(value: f32, limit: f32) -> f32 {
if value < 0.0 && -value < limit {
-limit
} else if value >= 0.0 && value < limit {
limit
} else {
value
}
}
let limit = 10.0 * f32::EPSILON;
let aspect = (frame_size.x / frame_size.y).max(limit);
let vertical_size = clamp_to_limit_signed(self.vertical_size, limit);
let horizontal_size = clamp_to_limit_signed(aspect * vertical_size, limit);
let z_near = self.z_far.min(self.z_near);
let mut z_far = self.z_far.max(self.z_near);
if z_far - z_near < limit {
z_far += limit;
}
let left = -horizontal_size;
let top = vertical_size;
let right = horizontal_size;
let bottom = -vertical_size;
Matrix4::new_orthographic(left, right, bottom, top, z_near, z_far)
}
}
#[derive(
Reflect,
Clone,
Debug,
PartialEq,
Visit,
AsRefStr,
EnumString,
VariantNames,
Serialize,
Deserialize,
)]
pub enum Projection {
Perspective(PerspectiveProjection),
Orthographic(OrthographicProjection),
}
uuid_provider!(Projection = "0eb5bec0-fc4e-4945-99b6-e6c5392ad971");
impl Projection {
#[inline]
#[must_use]
pub fn with_z_near(mut self, z_near: f32) -> Self {
match self {
Projection::Perspective(ref mut v) => v.z_near = z_near,
Projection::Orthographic(ref mut v) => v.z_near = z_near,
}
self
}
#[inline]
#[must_use]
pub fn with_z_far(mut self, z_far: f32) -> Self {
match self {
Projection::Perspective(ref mut v) => v.z_far = z_far,
Projection::Orthographic(ref mut v) => v.z_far = z_far,
}
self
}
#[inline]
pub fn set_z_near(&mut self, z_near: f32) {
match self {
Projection::Perspective(v) => v.z_near = z_near,
Projection::Orthographic(v) => v.z_near = z_near,
}
}
#[inline]
pub fn set_z_far(&mut self, z_far: f32) {
match self {
Projection::Perspective(v) => v.z_far = z_far,
Projection::Orthographic(v) => v.z_far = z_far,
}
}
#[inline]
pub fn z_near(&self) -> f32 {
match self {
Projection::Perspective(v) => v.z_near,
Projection::Orthographic(v) => v.z_near,
}
}
#[inline]
pub fn z_far(&self) -> f32 {
match self {
Projection::Perspective(v) => v.z_far,
Projection::Orthographic(v) => v.z_far,
}
}
#[inline]
pub fn matrix(&self, frame_size: Vector2<f32>) -> Matrix4<f32> {
match self {
Projection::Perspective(v) => v.matrix(frame_size),
Projection::Orthographic(v) => v.matrix(frame_size),
}
}
#[inline]
pub fn is_perspective(&self) -> bool {
matches!(self, Projection::Perspective(_))
}
#[inline]
pub fn is_orthographic(&self) -> bool {
matches!(self, Projection::Orthographic(_))
}
}
impl Default for Projection {
fn default() -> Self {
Self::Perspective(PerspectiveProjection::default())
}
}
#[derive(Visit, Copy, Clone, PartialEq, Debug, Reflect, AsRefStr, EnumString, VariantNames)]
pub enum Exposure {
Auto {
#[reflect(min_value = 0.0, step = 0.1)]
key_value: f32,
#[reflect(min_value = 0.0, step = 0.1)]
min_luminance: f32,
#[reflect(min_value = 0.0, step = 0.1)]
max_luminance: f32,
},
Manual(f32),
}
uuid_provider!(Exposure = "0e35ee3d-8baa-4b0c-b3dd-6c31a08c121e");
impl Default for Exposure {
fn default() -> Self {
Self::Auto {
key_value: 0.01556,
min_luminance: 0.00778,
max_luminance: 64.0,
}
}
}
#[derive(Debug, Visit, Reflect, Clone, ComponentProvider)]
pub struct Camera {
base: Base,
#[reflect(setter = "set_projection")]
projection: InheritableVariable<Projection>,
#[reflect(setter = "set_viewport")]
viewport: InheritableVariable<Rect<f32>>,
#[reflect(setter = "set_enabled")]
enabled: InheritableVariable<bool>,
#[reflect(setter = "set_skybox")]
sky_box: InheritableVariable<Option<SkyBox>>,
#[reflect(setter = "set_environment")]
environment: InheritableVariable<Option<TextureResource>>,
#[reflect(setter = "set_exposure")]
exposure: InheritableVariable<Exposure>,
#[reflect(setter = "set_color_grading_lut")]
color_grading_lut: InheritableVariable<Option<ColorGradingLut>>,
#[reflect(setter = "set_color_grading_enabled")]
color_grading_enabled: InheritableVariable<bool>,
#[visit(skip)]
#[reflect(hidden)]
view_matrix: Matrix4<f32>,
#[visit(skip)]
#[reflect(hidden)]
projection_matrix: Matrix4<f32>,
}
impl Deref for Camera {
type Target = Base;
fn deref(&self) -> &Self::Target {
&self.base
}
}
impl DerefMut for Camera {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.base
}
}
impl Default for Camera {
fn default() -> Self {
CameraBuilder::new(BaseBuilder::new()).build_camera()
}
}
impl TypeUuidProvider for Camera {
fn type_uuid() -> Uuid {
uuid!("198d3aca-433c-4ce1-bb25-3190699b757f")
}
}
pub enum FitParameters {
Perspective {
position: Vector3<f32>,
distance: f32,
},
Orthographic {
position: Vector3<f32>,
vertical_size: f32,
},
}
impl Camera {
#[inline]
pub fn calculate_matrices(&mut self, frame_size: Vector2<f32>) {
let pos = self.base.global_position();
let look = self.base.look_vector();
let up = self.base.up_vector();
self.view_matrix = Matrix4::look_at_rh(&Point3::from(pos), &Point3::from(pos + look), &up);
self.projection_matrix = self.projection.matrix(frame_size);
}
pub fn set_viewport(&mut self, mut viewport: Rect<f32>) -> Rect<f32> {
viewport.position.x = viewport.position.x.clamp(0.0, 1.0);
viewport.position.y = viewport.position.y.clamp(0.0, 1.0);
viewport.size.x = viewport.size.x.clamp(0.0, 1.0);
viewport.size.y = viewport.size.y.clamp(0.0, 1.0);
self.viewport.set_value_and_mark_modified(viewport)
}
pub fn viewport(&self) -> Rect<f32> {
*self.viewport
}
#[inline]
pub fn viewport_pixels(&self, frame_size: Vector2<f32>) -> Rect<i32> {
Rect::new(
(self.viewport.x() * frame_size.x) as i32,
(self.viewport.y() * frame_size.y) as i32,
((self.viewport.w() * frame_size.x) as i32).max(1),
((self.viewport.h() * frame_size.y) as i32).max(1),
)
}
#[inline]
pub fn view_projection_matrix(&self) -> Matrix4<f32> {
self.projection_matrix * self.view_matrix
}
#[inline]
pub fn projection_matrix(&self) -> Matrix4<f32> {
self.projection_matrix
}
#[inline]
pub fn view_matrix(&self) -> Matrix4<f32> {
self.view_matrix
}
#[inline]
pub fn inv_view_matrix(&self) -> Option<Matrix4<f32>> {
self.view_matrix.try_inverse()
}
#[inline]
pub fn projection(&self) -> &Projection {
&self.projection
}
#[inline]
pub fn projection_value(&self) -> Projection {
(*self.projection).clone()
}
#[inline]
pub fn projection_mut(&mut self) -> &mut Projection {
self.projection.get_value_mut_and_mark_modified()
}
#[inline]
pub fn set_projection(&mut self, projection: Projection) -> Projection {
self.projection.set_value_and_mark_modified(projection)
}
#[inline]
pub fn is_enabled(&self) -> bool {
*self.enabled
}
#[inline]
pub fn set_enabled(&mut self, enabled: bool) -> bool {
self.enabled.set_value_and_mark_modified(enabled)
}
pub fn set_skybox(&mut self, skybox: Option<SkyBox>) -> Option<SkyBox> {
self.sky_box.set_value_and_mark_modified(skybox)
}
pub fn skybox_mut(&mut self) -> Option<&mut SkyBox> {
self.sky_box.get_value_mut_and_mark_modified().as_mut()
}
pub fn skybox_ref(&self) -> Option<&SkyBox> {
self.sky_box.as_ref()
}
pub fn replace_skybox(&mut self, new: Option<SkyBox>) -> Option<SkyBox> {
std::mem::replace(self.sky_box.get_value_mut_and_mark_modified(), new)
}
pub fn set_environment(
&mut self,
environment: Option<TextureResource>,
) -> Option<TextureResource> {
self.environment.set_value_and_mark_modified(environment)
}
pub fn environment_mut(&mut self) -> Option<&mut TextureResource> {
self.environment.get_value_mut_and_mark_modified().as_mut()
}
pub fn environment_ref(&self) -> Option<&TextureResource> {
self.environment.as_ref()
}
pub fn environment_map(&self) -> Option<TextureResource> {
(*self.environment).clone()
}
pub fn make_ray(&self, screen_coord: Vector2<f32>, screen_size: Vector2<f32>) -> Ray {
let viewport = self.viewport_pixels(screen_size);
let nx = screen_coord.x / (viewport.w() as f32) * 2.0 - 1.0;
let ny = (viewport.h() as f32 - screen_coord.y) / (viewport.h() as f32) * 2.0 - 1.0;
let inv_view_proj = self
.view_projection_matrix()
.try_inverse()
.unwrap_or_default();
let near = inv_view_proj * Vector4::new(nx, ny, -1.0, 1.0);
let far = inv_view_proj * Vector4::new(nx, ny, 1.0, 1.0);
let begin = near.xyz().scale(1.0 / near.w);
let end = far.xyz().scale(1.0 / far.w);
Ray::from_two_points(begin, end)
}
#[inline]
#[must_use]
pub fn fit(&self, aabb: &AxisAlignedBoundingBox, aspect_ratio: f32) -> FitParameters {
let look_vector = self
.look_vector()
.try_normalize(f32::EPSILON)
.unwrap_or_default();
match self.projection.deref() {
Projection::Perspective(perspective) => {
let radius = aabb.half_extents().max();
let distance = radius / (perspective.fov * 0.5).sin();
FitParameters::Perspective {
position: aabb.center() - look_vector.scale(distance),
distance,
}
}
Projection::Orthographic(_) => {
let mut min_x = f32::MAX;
let mut min_y = f32::MAX;
let mut max_x = -f32::MAX;
let mut max_y = -f32::MAX;
let inv = self.global_transform().try_inverse().unwrap_or_default();
for point in aabb.corners() {
let local = inv.transform_point(&Point3::from(point));
if local.x < min_x {
min_x = local.x;
}
if local.y < min_y {
min_y = local.y;
}
if local.x > max_x {
max_x = local.x;
}
if local.y > max_y {
max_y = local.y;
}
}
FitParameters::Orthographic {
position: aabb.center() - look_vector.scale((aabb.max - aabb.min).norm()),
vertical_size: (max_y - min_y).max((max_x - min_x) * aspect_ratio),
}
}
}
}
#[inline]
pub fn frustum(&self) -> Frustum {
Frustum::from_view_projection_matrix(self.view_projection_matrix()).unwrap_or_default()
}
pub fn project(
&self,
world_pos: Vector3<f32>,
screen_size: Vector2<f32>,
) -> Option<Vector2<f32>> {
let viewport = self.viewport_pixels(screen_size);
let proj = self.view_projection_matrix()
* Vector4::new(world_pos.x, world_pos.y, world_pos.z, 1.0);
if proj.w != 0.0 && proj.z >= 0.0 {
let k = (1.0 / proj.w) * 0.5;
Some(Vector2::new(
viewport.x() as f32 + viewport.w() as f32 * (proj.x * k + 0.5),
viewport.h() as f32
- (viewport.y() as f32 + viewport.h() as f32 * (proj.y * k + 0.5)),
))
} else {
None
}
}
pub fn set_color_grading_lut(
&mut self,
lut: Option<ColorGradingLut>,
) -> Option<ColorGradingLut> {
self.color_grading_lut.set_value_and_mark_modified(lut)
}
pub fn color_grading_lut(&self) -> Option<ColorGradingLut> {
(*self.color_grading_lut).clone()
}
pub fn color_grading_lut_ref(&self) -> Option<&ColorGradingLut> {
self.color_grading_lut.as_ref()
}
pub fn set_color_grading_enabled(&mut self, enable: bool) -> bool {
self.color_grading_enabled
.set_value_and_mark_modified(enable)
}
pub fn color_grading_enabled(&self) -> bool {
*self.color_grading_enabled
}
pub fn set_exposure(&mut self, exposure: Exposure) -> Exposure {
self.exposure.set_value_and_mark_modified(exposure)
}
pub fn exposure(&self) -> Exposure {
*self.exposure
}
}
impl ConstructorProvider<Node, Graph> for Camera {
fn constructor() -> NodeConstructor {
NodeConstructor::new::<Self>().with_variant("Camera", |_| {
CameraBuilder::new(BaseBuilder::new().with_name("Camera"))
.build_node()
.into()
})
}
}
impl NodeTrait for Camera {
#[inline]
fn local_bounding_box(&self) -> AxisAlignedBoundingBox {
self.base.local_bounding_box()
}
fn world_bounding_box(&self) -> AxisAlignedBoundingBox {
self.base.world_bounding_box()
}
fn id(&self) -> Uuid {
Self::type_uuid()
}
fn update(&mut self, context: &mut UpdateContext) {
self.calculate_matrices(context.frame_size);
}
fn debug_draw(&self, ctx: &mut SceneDrawingContext) {
let transform = self.global_transform.get();
ctx.draw_pyramid(
self.frustum().center(),
self.frustum().right_top_front_corner(),
self.frustum().left_top_front_corner(),
self.frustum().left_bottom_front_corner(),
self.frustum().right_bottom_front_corner(),
Color::GREEN,
transform,
);
}
}
#[derive(Debug)]
pub enum ColorGradingLutCreationError {
NotEnoughData {
required: usize,
current: usize,
},
InvalidPixelFormat(TexturePixelKind),
Texture(LoadError),
}
impl Display for ColorGradingLutCreationError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ColorGradingLutCreationError::NotEnoughData { required, current } => {
write!(
f,
"There is not enough data in provided \
texture to build LUT. Required: {required}, current: {current}.",
)
}
ColorGradingLutCreationError::InvalidPixelFormat(v) => {
write!(
f,
"Pixel format is not supported. It must be either RGB8 \
or RGBA8, but texture has {v:?} pixel format"
)
}
ColorGradingLutCreationError::Texture(v) => {
write!(f, "Texture load error: {v:?}")
}
}
}
}
#[derive(Visit, Clone, Default, PartialEq, Debug, Reflect, Eq)]
pub struct ColorGradingLut {
unwrapped_lut: Option<TextureResource>,
#[visit(skip)]
#[reflect(hidden)]
lut: Option<TextureResource>,
}
uuid_provider!(ColorGradingLut = "bca9c90a-7cde-4960-8814-c132edfc9614");
impl ColorGradingLut {
pub async fn new(unwrapped_lut: TextureResource) -> Result<Self, ColorGradingLutCreationError> {
match unwrapped_lut.await {
Ok(unwrapped_lut) => {
let data = unwrapped_lut.data_ref();
if data.pixel_kind() != TexturePixelKind::RGBA8
&& data.pixel_kind() != TexturePixelKind::RGB8
{
return Err(ColorGradingLutCreationError::InvalidPixelFormat(
data.pixel_kind(),
));
}
let bytes = data.data();
const RGBA8_SIZE: usize = 16 * 16 * 16 * 4;
const RGB8_SIZE: usize = 16 * 16 * 16 * 3;
if data.pixel_kind() == TexturePixelKind::RGBA8 {
if bytes.len() != RGBA8_SIZE {
return Err(ColorGradingLutCreationError::NotEnoughData {
required: RGBA8_SIZE,
current: bytes.len(),
});
}
} else if bytes.len() != RGB8_SIZE {
return Err(ColorGradingLutCreationError::NotEnoughData {
required: RGB8_SIZE,
current: bytes.len(),
});
}
let pixel_size = if data.pixel_kind() == TexturePixelKind::RGBA8 {
4
} else {
3
};
let mut lut_bytes = Vec::with_capacity(16 * 16 * 16 * 3);
for z in 0..16 {
for y in 0..16 {
for x in 0..16 {
let pixel_index = z * 16 + y * 16 * 16 + x;
let pixel_byte_pos = pixel_index * pixel_size;
lut_bytes.push(bytes[pixel_byte_pos]); lut_bytes.push(bytes[pixel_byte_pos + 1]); lut_bytes.push(bytes[pixel_byte_pos + 2]); }
}
}
let lut = TextureResource::from_bytes(
TextureKind::Volume {
width: 16,
height: 16,
depth: 16,
},
TexturePixelKind::RGB8,
lut_bytes,
ResourceKind::Embedded,
)
.unwrap();
let mut lut_ref = lut.data_ref();
lut_ref.set_s_wrap_mode(TextureWrapMode::ClampToEdge);
lut_ref.set_t_wrap_mode(TextureWrapMode::ClampToEdge);
drop(lut_ref);
drop(data);
Ok(Self {
lut: Some(lut),
unwrapped_lut: Some(unwrapped_lut),
})
}
Err(e) => Err(ColorGradingLutCreationError::Texture(e)),
}
}
pub fn unwrapped_lut(&self) -> TextureResource {
self.unwrapped_lut.clone().unwrap()
}
pub fn lut(&self) -> TextureResource {
self.lut.clone().unwrap()
}
pub fn lut_ref(&self) -> &TextureResource {
self.lut.as_ref().unwrap()
}
}
#[derive(Default)]
pub enum SkyBoxKind {
#[default]
Builtin,
None,
Specific(SkyBox),
}
fn load_texture(data: &[u8], id: &str) -> TextureResource {
TextureResource::load_from_memory(
ResourceKind::External(id.into()),
data,
TextureImportOptions::default()
.with_compression(CompressionOptions::NoCompression)
.with_minification_filter(TextureMinificationFilter::Linear),
)
.ok()
.unwrap()
}
lazy_static! {
static ref BUILT_IN_SKYBOX_FRONT: BuiltInResource<Texture> =
BuiltInResource::new(embedded_data_source!("skybox/front.png"), |data| {
load_texture(data, "__BUILT_IN_SKYBOX_FRONT")
});
static ref BUILT_IN_SKYBOX_BACK: BuiltInResource<Texture> =
BuiltInResource::new(embedded_data_source!("skybox/back.png"), |data| {
load_texture(data, "__BUILT_IN_SKYBOX_BACK")
});
static ref BUILT_IN_SKYBOX_TOP: BuiltInResource<Texture> =
BuiltInResource::new(embedded_data_source!("skybox/top.png"), |data| {
load_texture(data, "__BUILT_IN_SKYBOX_TOP")
});
static ref BUILT_IN_SKYBOX_BOTTOM: BuiltInResource<Texture> =
BuiltInResource::new(embedded_data_source!("skybox/bottom.png"), |data| {
load_texture(data, "__BUILT_IN_SKYBOX_BOTTOM")
});
static ref BUILT_IN_SKYBOX_LEFT: BuiltInResource<Texture> =
BuiltInResource::new(embedded_data_source!("skybox/left.png"), |data| {
load_texture(data, "__BUILT_IN_SKYBOX_LEFT")
});
static ref BUILT_IN_SKYBOX_RIGHT: BuiltInResource<Texture> =
BuiltInResource::new(embedded_data_source!("skybox/right.png"), |data| {
load_texture(data, "__BUILT_IN_SKYBOX_RIGHT")
});
static ref BUILT_IN_SKYBOX: SkyBox = SkyBoxKind::make_built_in_skybox();
}
impl SkyBoxKind {
fn make_built_in_skybox() -> SkyBox {
let front = BUILT_IN_SKYBOX_FRONT.resource();
let back = BUILT_IN_SKYBOX_BACK.resource();
let top = BUILT_IN_SKYBOX_TOP.resource();
let bottom = BUILT_IN_SKYBOX_BOTTOM.resource();
let left = BUILT_IN_SKYBOX_LEFT.resource();
let right = BUILT_IN_SKYBOX_RIGHT.resource();
SkyBoxBuilder {
front: Some(front),
back: Some(back),
left: Some(left),
right: Some(right),
top: Some(top),
bottom: Some(bottom),
}
.build()
.unwrap()
}
pub fn built_in_skybox() -> &'static SkyBox {
&BUILT_IN_SKYBOX
}
pub fn built_in_skybox_textures() -> [&'static BuiltInResource<Texture>; 6] {
[
&BUILT_IN_SKYBOX_FRONT,
&BUILT_IN_SKYBOX_BACK,
&BUILT_IN_SKYBOX_TOP,
&BUILT_IN_SKYBOX_BOTTOM,
&BUILT_IN_SKYBOX_LEFT,
&BUILT_IN_SKYBOX_RIGHT,
]
}
}
pub struct CameraBuilder {
base_builder: BaseBuilder,
fov: f32,
z_near: f32,
z_far: f32,
viewport: Rect<f32>,
enabled: bool,
skybox: SkyBoxKind,
environment: Option<TextureResource>,
exposure: Exposure,
color_grading_lut: Option<ColorGradingLut>,
color_grading_enabled: bool,
projection: Projection,
}
impl CameraBuilder {
pub fn new(base_builder: BaseBuilder) -> Self {
Self {
enabled: true,
base_builder,
fov: 75.0f32.to_radians(),
z_near: 0.025,
z_far: 2048.0,
viewport: Rect::new(0.0, 0.0, 1.0, 1.0),
skybox: SkyBoxKind::Builtin,
environment: None,
exposure: Exposure::Manual(std::f32::consts::E),
color_grading_lut: None,
color_grading_enabled: false,
projection: Projection::default(),
}
}
pub fn with_fov(mut self, fov: f32) -> Self {
self.fov = fov;
self
}
pub fn with_z_near(mut self, z_near: f32) -> Self {
self.z_near = z_near;
self
}
pub fn with_z_far(mut self, z_far: f32) -> Self {
self.z_far = z_far;
self
}
pub fn with_viewport(mut self, viewport: Rect<f32>) -> Self {
self.viewport = viewport;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn with_skybox(mut self, skybox: SkyBox) -> Self {
self.skybox = SkyBoxKind::Specific(skybox);
self
}
pub fn with_specific_skybox(mut self, skybox_kind: SkyBoxKind) -> Self {
self.skybox = skybox_kind;
self
}
pub fn with_environment(mut self, environment: TextureResource) -> Self {
self.environment = Some(environment);
self
}
pub fn with_color_grading_lut(mut self, lut: ColorGradingLut) -> Self {
self.color_grading_lut = Some(lut);
self
}
pub fn with_color_grading_enabled(mut self, enabled: bool) -> Self {
self.color_grading_enabled = enabled;
self
}
pub fn with_exposure(mut self, exposure: Exposure) -> Self {
self.exposure = exposure;
self
}
pub fn with_projection(mut self, projection: Projection) -> Self {
self.projection = projection;
self
}
pub fn build_camera(self) -> Camera {
Camera {
enabled: self.enabled.into(),
base: self.base_builder.build_base(),
projection: self.projection.into(),
viewport: self.viewport.into(),
view_matrix: Matrix4::identity(),
projection_matrix: Matrix4::identity(),
sky_box: InheritableVariable::new_modified(match self.skybox {
SkyBoxKind::Builtin => Some(SkyBoxKind::built_in_skybox().clone()),
SkyBoxKind::None => None,
SkyBoxKind::Specific(skybox) => Some(skybox),
}),
environment: self.environment.into(),
exposure: self.exposure.into(),
color_grading_lut: self.color_grading_lut.into(),
color_grading_enabled: self.color_grading_enabled.into(),
}
}
pub fn build_node(self) -> Node {
Node::new(self.build_camera())
}
pub fn build(self, graph: &mut Graph) -> Handle<Node> {
graph.add_node(self.build_node())
}
}
pub struct SkyBoxBuilder {
pub front: Option<TextureResource>,
pub back: Option<TextureResource>,
pub left: Option<TextureResource>,
pub right: Option<TextureResource>,
pub top: Option<TextureResource>,
pub bottom: Option<TextureResource>,
}
impl SkyBoxBuilder {
pub fn with_front(mut self, texture: TextureResource) -> Self {
self.front = Some(texture);
self
}
pub fn with_back(mut self, texture: TextureResource) -> Self {
self.back = Some(texture);
self
}
pub fn with_left(mut self, texture: TextureResource) -> Self {
self.left = Some(texture);
self
}
pub fn with_right(mut self, texture: TextureResource) -> Self {
self.right = Some(texture);
self
}
pub fn with_top(mut self, texture: TextureResource) -> Self {
self.top = Some(texture);
self
}
pub fn with_bottom(mut self, texture: TextureResource) -> Self {
self.bottom = Some(texture);
self
}
pub fn build(self) -> Result<SkyBox, SkyBoxError> {
let mut skybox = SkyBox {
left: self.left,
right: self.right,
top: self.top,
bottom: self.bottom,
front: self.front,
back: self.back,
cubemap: None,
};
skybox.create_cubemap()?;
Ok(skybox)
}
}
#[derive(Debug, Clone, Default, PartialEq, Reflect, Visit, Eq)]
pub struct SkyBox {
#[reflect(setter = "set_front")]
pub(crate) front: Option<TextureResource>,
#[reflect(setter = "set_back")]
pub(crate) back: Option<TextureResource>,
#[reflect(setter = "set_left")]
pub(crate) left: Option<TextureResource>,
#[reflect(setter = "set_right")]
pub(crate) right: Option<TextureResource>,
#[reflect(setter = "set_top")]
pub(crate) top: Option<TextureResource>,
#[reflect(setter = "set_bottom")]
pub(crate) bottom: Option<TextureResource>,
#[reflect(hidden)]
#[visit(skip)]
pub(crate) cubemap: Option<TextureResource>,
}
uuid_provider!(SkyBox = "45f359f1-e26f-4ace-81df-097f63474c72");
#[derive(Debug)]
pub enum SkyBoxError {
UnsupportedTextureKind(TextureKind),
UnableToBuildCubeMap,
NonSquareTexture {
index: usize,
width: u32,
height: u32,
},
DifferentTexture {
expected_width: u32,
expected_height: u32,
expected_pixel_kind: TexturePixelKind,
index: usize,
actual_width: u32,
actual_height: u32,
actual_pixel_kind: TexturePixelKind,
},
TextureIsNotReady {
index: usize,
},
}
impl SkyBox {
pub fn cubemap(&self) -> Option<TextureResource> {
self.cubemap.clone()
}
pub fn cubemap_ref(&self) -> Option<&TextureResource> {
self.cubemap.as_ref()
}
pub fn validate(&self) -> Result<(), SkyBoxError> {
struct TextureInfo {
pixel_kind: TexturePixelKind,
width: u32,
height: u32,
}
let mut first_info: Option<TextureInfo> = None;
for (index, texture) in self.textures().iter().enumerate() {
if let Some(texture) = texture {
if let Some(texture) = texture.state().data() {
if let TextureKind::Rectangle { width, height } = texture.kind() {
if width != height {
return Err(SkyBoxError::NonSquareTexture {
index,
width,
height,
});
}
if let Some(first_info) = first_info.as_mut() {
if first_info.width != width
|| first_info.height != height
|| first_info.pixel_kind != texture.pixel_kind()
{
return Err(SkyBoxError::DifferentTexture {
expected_width: first_info.width,
expected_height: first_info.height,
expected_pixel_kind: first_info.pixel_kind,
index,
actual_width: width,
actual_height: height,
actual_pixel_kind: texture.pixel_kind(),
});
}
} else {
first_info = Some(TextureInfo {
pixel_kind: texture.pixel_kind(),
width,
height,
});
}
}
} else {
return Err(SkyBoxError::TextureIsNotReady { index });
}
}
}
Ok(())
}
pub fn create_cubemap(&mut self) -> Result<(), SkyBoxError> {
self.validate()?;
let (kind, pixel_kind, bytes_per_face) =
self.textures().iter().find(|face| face.is_some()).map_or(
(
TextureKind::Rectangle {
width: 1,
height: 1,
},
TexturePixelKind::R8,
1,
),
|face| {
let face = face.clone().unwrap();
let data = face.data_ref();
(data.kind(), data.pixel_kind(), data.mip_level_data(0).len())
},
);
let (width, height) = match kind {
TextureKind::Rectangle { width, height } => (width, height),
_ => return Err(SkyBoxError::UnsupportedTextureKind(kind)),
};
let mut data = Vec::<u8>::with_capacity(bytes_per_face * 6);
for face in self.textures().iter() {
if let Some(f) = face.clone() {
data.extend(f.data_ref().mip_level_data(0));
} else {
let black_face_data = vec![0; bytes_per_face];
data.extend(black_face_data);
}
}
let cubemap = TextureResource::from_bytes(
TextureKind::Cube { width, height },
pixel_kind,
data,
ResourceKind::Embedded,
)
.ok_or(SkyBoxError::UnableToBuildCubeMap)?;
let mut cubemap_ref = cubemap.data_ref();
cubemap_ref.set_s_wrap_mode(TextureWrapMode::ClampToEdge);
cubemap_ref.set_t_wrap_mode(TextureWrapMode::ClampToEdge);
drop(cubemap_ref);
self.cubemap = Some(cubemap);
Ok(())
}
pub fn textures(&self) -> [Option<TextureResource>; 6] {
[
self.left.clone(),
self.right.clone(),
self.top.clone(),
self.bottom.clone(),
self.front.clone(),
self.back.clone(),
]
}
pub fn set_left(&mut self, texture: Option<TextureResource>) -> Option<TextureResource> {
let prev = std::mem::replace(&mut self.left, texture);
Log::verify(self.create_cubemap());
prev
}
pub fn left(&self) -> Option<TextureResource> {
self.left.clone()
}
pub fn set_right(&mut self, texture: Option<TextureResource>) -> Option<TextureResource> {
let prev = std::mem::replace(&mut self.right, texture);
Log::verify(self.create_cubemap());
prev
}
pub fn right(&self) -> Option<TextureResource> {
self.right.clone()
}
pub fn set_top(&mut self, texture: Option<TextureResource>) -> Option<TextureResource> {
let prev = std::mem::replace(&mut self.top, texture);
Log::verify(self.create_cubemap());
prev
}
pub fn top(&self) -> Option<TextureResource> {
self.top.clone()
}
pub fn set_bottom(&mut self, texture: Option<TextureResource>) -> Option<TextureResource> {
let prev = std::mem::replace(&mut self.bottom, texture);
Log::verify(self.create_cubemap());
prev
}
pub fn bottom(&self) -> Option<TextureResource> {
self.bottom.clone()
}
pub fn set_front(&mut self, texture: Option<TextureResource>) -> Option<TextureResource> {
let prev = std::mem::replace(&mut self.front, texture);
Log::verify(self.create_cubemap());
prev
}
pub fn front(&self) -> Option<TextureResource> {
self.front.clone()
}
pub fn set_back(&mut self, texture: Option<TextureResource>) -> Option<TextureResource> {
let prev = std::mem::replace(&mut self.back, texture);
Log::verify(self.create_cubemap());
prev
}
pub fn back(&self) -> Option<TextureResource> {
self.back.clone()
}
}