split into two crates
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
//! Tape generation for [primitive shapes](bevy_shape).
|
||||
//!
|
||||
//! Primitives that support taping implement the [`Tapeable`] trait.
|
||||
//! Calling [`tape`](Tapeable::tape) will return either a [`Tape`] or a builder
|
||||
//! that can be used to specify shape-specific configuration for creating the
|
||||
//! [`Tape`].
|
||||
//!
|
||||
//! ```
|
||||
//! # use bevy_asset::Assets;
|
||||
//! # use bevy_ecs::prelude::ResMut;
|
||||
//! # use bevy_shape::Circle;
|
||||
//! # use bevy_tape_load::*;
|
||||
//! #
|
||||
//! # fn setup(mut tapes: ResMut<Assets<Tape>>) {
|
||||
//! // Create circle tape with default configuration
|
||||
//! let circle = tapes.add(Circle { radius: 25.0 });
|
||||
//!
|
||||
//! // Specify number of vertices
|
||||
//! let circle = tapes.add(Circle { radius: 25.0 }.tape().resolution(64));
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
pub mod sphere;
|
||||
|
||||
use super::Tape;
|
||||
|
||||
/// A trait for shapes that can be turned into a [`Tape`].
|
||||
pub trait Tapeable {
|
||||
/// The output of [`Self::tape`]. This will be a [`TapeBuilder`] used for
|
||||
/// creating a [`Tape`].
|
||||
type Output: TapeBuilder;
|
||||
|
||||
/// Creates a [`Tape`] for a shape.
|
||||
fn tape(&self) -> Self::Output;
|
||||
}
|
||||
|
||||
/// A trait used to build [`Tape`]s from a configuration
|
||||
pub trait TapeBuilder {
|
||||
/// Builds a [`Tape`] based on the configuration in `self`.
|
||||
fn build(&self) -> Tape;
|
||||
}
|
||||
|
||||
impl<T: TapeBuilder> From<T> for Tape {
|
||||
fn from(builder: T) -> Self {
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
use bevy_reflect::prelude::*;
|
||||
use bevy_shape::Sphere;
|
||||
|
||||
use crate::{
|
||||
Tape,
|
||||
primitives::{TapeBuilder, Tapeable},
|
||||
ssa::{SSAInput, SSAOpcode, SSAOpcodeSized, SSATape},
|
||||
};
|
||||
|
||||
/// A builder used for creating a [`Tape`] with an [`Sphere`] shape.
|
||||
#[derive(Clone, Copy, Debug, Default, Reflect)]
|
||||
#[reflect(Default, Debug, Clone)]
|
||||
pub struct SphereTapeBuilder {
|
||||
/// The [`Sphere`] shape.
|
||||
pub sphere: Sphere,
|
||||
}
|
||||
|
||||
impl SphereTapeBuilder {
|
||||
/// Creates a new [`SphereTapeBuilder`] from a radius.
|
||||
#[inline]
|
||||
pub const fn new(radius: f32) -> Self {
|
||||
Self {
|
||||
sphere: Sphere { radius },
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a sphere [`Tape`] with the given radius.
|
||||
/// TODO: make it so that this is composable.
|
||||
pub fn build_with_radius(radius: f32) -> Tape {
|
||||
let mut tape = SSATape::default();
|
||||
let pos = tape.push_instruction(
|
||||
SSAOpcodeSized {
|
||||
opcode: SSAOpcode::SSAPosition,
|
||||
size: 1,
|
||||
},
|
||||
vec![],
|
||||
);
|
||||
let sphere = tape.push_instruction(
|
||||
SSAOpcodeSized {
|
||||
opcode: SSAOpcode::SSASDFSphere,
|
||||
size: 1,
|
||||
},
|
||||
vec![pos[0], pos[1], pos[2], SSAInput::Constant(radius)],
|
||||
);
|
||||
tape.push_instruction(
|
||||
SSAOpcodeSized {
|
||||
opcode: SSAOpcode::SSAReturn,
|
||||
size: 1,
|
||||
},
|
||||
sphere,
|
||||
);
|
||||
Tape {
|
||||
parts: tape,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TapeBuilder for SphereTapeBuilder {
|
||||
/// Builds a [`Tape`] according to the configuration in `self`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the sphere is a [`SphereKind::Ico`] with a subdivision count
|
||||
/// that is greater than or equal to `80` because there will be too many
|
||||
/// vertices.
|
||||
fn build(&self) -> Tape {
|
||||
Self::build_with_radius(self.sphere.radius)
|
||||
}
|
||||
}
|
||||
|
||||
impl Tapeable for Sphere {
|
||||
type Output = SphereTapeBuilder;
|
||||
|
||||
fn tape(&self) -> Self::Output {
|
||||
SphereTapeBuilder {
|
||||
sphere: *self,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Sphere> for Tape {
|
||||
fn from(sphere: Sphere) -> Self {
|
||||
sphere.tape().build()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user