use crate::scene::node::constructor::NodeConstructor;
use crate::scene::node::RdcControlFlow;
use crate::{
core::{
algebra::{Vector2, Vector3},
color::Color,
math::{aabb::AxisAlignedBoundingBox, Rect, TriangleDefinition},
pool::Handle,
reflect::prelude::*,
type_traits::prelude::*,
uuid::{uuid, Uuid},
variable::InheritableVariable,
visitor::{Visit, VisitResult, Visitor},
},
material,
material::{Material, MaterialResource},
renderer::{self, bundle::RenderContext},
scene::{
base::{Base, BaseBuilder},
graph::Graph,
mesh::{
buffer::{
VertexAttributeDataType, VertexAttributeDescriptor, VertexAttributeUsage,
VertexTrait,
},
RenderPath,
},
node::{Node, NodeTrait},
},
};
use bytemuck::{Pod, Zeroable};
use fyrox_core::value_as_u8_slice;
use fyrox_graph::constructor::ConstructorProvider;
use fyrox_graph::BaseSceneGraph;
use std::ops::{Deref, DerefMut};
#[derive(Copy, Clone, Debug, Default, Pod, Zeroable)]
#[repr(C)] pub struct SpriteVertex {
pub position: Vector3<f32>,
pub tex_coord: Vector2<f32>,
pub params: Vector2<f32>,
pub color: Color,
}
impl VertexTrait for SpriteVertex {
fn layout() -> &'static [VertexAttributeDescriptor] {
&[
VertexAttributeDescriptor {
usage: VertexAttributeUsage::Position,
data_type: VertexAttributeDataType::F32,
size: 3,
divisor: 0,
shader_location: 0,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::TexCoord0,
data_type: VertexAttributeDataType::F32,
size: 2,
divisor: 0,
shader_location: 1,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::Custom0,
data_type: VertexAttributeDataType::F32,
size: 2,
divisor: 0,
shader_location: 2,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::Color,
data_type: VertexAttributeDataType::U8,
size: 4,
divisor: 0,
shader_location: 3,
normalized: true,
},
]
}
}
#[derive(Debug, Reflect, Clone, ComponentProvider)]
pub struct Sprite {
base: Base,
#[reflect(setter = "set_uv_rect")]
uv_rect: InheritableVariable<Rect<f32>>,
material: InheritableVariable<MaterialResource>,
#[reflect(setter = "set_color")]
color: InheritableVariable<Color>,
#[reflect(min_value = 0.0, step = 0.1)]
#[reflect(setter = "set_size")]
size: InheritableVariable<f32>,
#[reflect(setter = "set_rotation")]
rotation: InheritableVariable<f32>,
}
impl Visit for Sprite {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
let mut region = visitor.enter_region(name)?;
if region.is_reading() {
if let Some(material) =
material::visit_old_texture_as_material(&mut region, Material::standard_sprite)
{
self.material = material.into();
} else {
self.material.visit("Material", &mut region)?;
}
} else {
self.material.visit("Material", &mut region)?;
}
self.base.visit("Base", &mut region)?;
self.color.visit("Color", &mut region)?;
self.size.visit("Size", &mut region)?;
self.rotation.visit("Rotation", &mut region)?;
let _ = self.uv_rect.visit("UvRect", &mut region);
Ok(())
}
}
impl Deref for Sprite {
type Target = Base;
fn deref(&self) -> &Self::Target {
&self.base
}
}
impl DerefMut for Sprite {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.base
}
}
impl Default for Sprite {
fn default() -> Self {
SpriteBuilder::new(BaseBuilder::new()).build_sprite()
}
}
impl TypeUuidProvider for Sprite {
fn type_uuid() -> Uuid {
uuid!("60fd7e34-46c1-4ae9-8803-1f5f4c341518")
}
}
impl Sprite {
pub fn set_size(&mut self, size: f32) -> f32 {
self.size.set_value_and_mark_modified(size)
}
pub fn size(&self) -> f32 {
*self.size
}
pub fn set_color(&mut self, color: Color) -> Color {
self.color.set_value_and_mark_modified(color)
}
pub fn color(&self) -> Color {
*self.color
}
pub fn set_rotation(&mut self, rotation: f32) -> f32 {
self.rotation.set_value_and_mark_modified(rotation)
}
pub fn rotation(&self) -> f32 {
*self.rotation
}
pub fn material(&self) -> &InheritableVariable<MaterialResource> {
&self.material
}
pub fn material_mut(&mut self) -> &mut InheritableVariable<MaterialResource> {
&mut self.material
}
pub fn uv_rect(&self) -> Rect<f32> {
*self.uv_rect
}
pub fn set_uv_rect(&mut self, uv_rect: Rect<f32>) -> Rect<f32> {
self.uv_rect.set_value_and_mark_modified(uv_rect)
}
}
impl ConstructorProvider<Node, Graph> for Sprite {
fn constructor() -> NodeConstructor {
NodeConstructor::new::<Self>().with_variant("Sprite (3D)", |_| {
SpriteBuilder::new(BaseBuilder::new().with_name("Sprite"))
.build_node()
.into()
})
}
}
impl NodeTrait for Sprite {
fn local_bounding_box(&self) -> AxisAlignedBoundingBox {
AxisAlignedBoundingBox::from_radius(*self.size)
}
fn world_bounding_box(&self) -> AxisAlignedBoundingBox {
self.base.world_bounding_box()
}
fn id(&self) -> Uuid {
Self::type_uuid()
}
fn collect_render_data(&self, ctx: &mut RenderContext) -> RdcControlFlow {
if !self.should_be_rendered(ctx.frustum) {
return RdcControlFlow::Continue;
}
if renderer::is_shadow_pass(ctx.render_pass_name) || !self.cast_shadows() {
return RdcControlFlow::Continue;
}
let position = self.global_position();
let params = Vector2::new(*self.size, *self.rotation);
type Vertex = SpriteVertex;
let vertices = [
Vertex {
position,
tex_coord: self.uv_rect.right_top_corner(),
params,
color: *self.color,
},
Vertex {
position,
tex_coord: self.uv_rect.left_top_corner(),
params,
color: *self.color,
},
Vertex {
position,
tex_coord: self.uv_rect.left_bottom_corner(),
params,
color: *self.color,
},
Vertex {
position,
tex_coord: self.uv_rect.right_bottom_corner(),
params,
color: *self.color,
},
];
let triangles = [TriangleDefinition([0, 1, 2]), TriangleDefinition([2, 3, 0])];
let sort_index = ctx.calculate_sorting_index(self.global_position());
ctx.storage.push_triangles(
Vertex::layout(),
&self.material,
RenderPath::Forward,
sort_index,
self.handle(),
&mut move |mut vertex_buffer, mut triangle_buffer| {
let start_vertex_index = vertex_buffer.vertex_count();
for vertex in vertices.iter() {
vertex_buffer
.push_vertex_raw(value_as_u8_slice(vertex))
.unwrap();
}
triangle_buffer
.push_triangles_iter_with_offset(start_vertex_index, triangles.into_iter());
},
);
RdcControlFlow::Continue
}
}
pub struct SpriteBuilder {
base_builder: BaseBuilder,
uv_rect: Rect<f32>,
material: MaterialResource,
color: Color,
size: f32,
rotation: f32,
}
impl SpriteBuilder {
pub fn new(base_builder: BaseBuilder) -> Self {
Self {
base_builder,
material: MaterialResource::new_ok(Default::default(), Material::standard_sprite()),
uv_rect: Rect::new(0.0, 0.0, 1.0, 1.0),
color: Color::WHITE,
size: 0.2,
rotation: 0.0,
}
}
pub fn with_uv_rect(mut self, uv_rect: Rect<f32>) -> Self {
self.uv_rect = uv_rect;
self
}
pub fn with_material(mut self, material: MaterialResource) -> Self {
self.material = material;
self
}
pub fn with_color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn with_size(mut self, size: f32) -> Self {
self.size = size;
self
}
pub fn with_rotation(mut self, rotation: f32) -> Self {
self.rotation = rotation;
self
}
fn build_sprite(self) -> Sprite {
Sprite {
base: self.base_builder.build_base(),
material: self.material.into(),
uv_rect: self.uv_rect.into(),
color: self.color.into(),
size: self.size.into(),
rotation: self.rotation.into(),
}
}
pub fn build_node(self) -> Node {
Node::new(self.build_sprite())
}
pub fn build(self, graph: &mut Graph) -> Handle<Node> {
graph.add_node(self.build_node())
}
}