1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
use super::Buffer;
use crate::core::*;
///
/// A buffer containing per instance data.
/// To send this data to a shader, use the [Program::use_instance_attribute] method.
///
pub struct InstanceBuffer {
buffer: Buffer,
}
impl InstanceBuffer {
///
/// Creates a new empty instance buffer.
///
pub fn new(context: &Context) -> ThreeDResult<Self> {
Ok(Self {
buffer: Buffer::new(context)?,
})
}
///
/// Creates a new instance buffer and fills it with the given data. The data should be in the same format as specified in the shader.
/// As an example, if specified as `vec3` in the shader it needs to be specified as an array of `Vector3<T>` where `T` is a primitive type that implements [BufferDataType], for example can be f16 or f32.
///
pub fn new_with_data<T: BufferDataType>(context: &Context, data: &[T]) -> ThreeDResult<Self> {
Ok(Self {
buffer: Buffer::new_with_data(context, data)?,
})
}
///
/// Fills the instance buffer with the given data. The data should be in the same format as specified in the shader.
/// As an example, if specified as `vec3` in the shader it needs to be specified as an array of `Vector3<T>` where `T` is a primitive type that implements [BufferDataType], for example can be f16 or f32.
///
pub fn fill<T: BufferDataType>(&mut self, data: &[T]) -> ThreeDResult<()> {
self.buffer.fill(data)
}
///
/// The number of values in the buffer.
///
pub fn count(&self) -> u32 {
self.buffer.attribute_count() * self.buffer.data_size
}
///
/// The number of instance attributes in the buffer.
///
pub fn instance_count(&self) -> u32 {
self.buffer.attribute_count()
}
pub(crate) fn bind(&self) {
self.buffer.bind();
}
pub(crate) fn data_type(&self) -> u32 {
self.buffer.data_type
}
pub(crate) fn data_size(&self) -> u32 {
self.buffer.data_size
}
///
/// Creates a new instance buffer and fills it with the given data which must contain between 1 and 4 contiguous values for each vertex.
/// Use this method instead of [new_with_dynamic](InstanceBuffer::new_with_dynamic)
/// when you do not expect the data to change often.
///
#[deprecated = "use new_with_data() and specify the data in the same format as in the shader (for example an array of Vec3 instead of f32)"]
pub fn new_with_static<T: BufferDataType>(context: &Context, data: &[T]) -> ThreeDResult<Self> {
Self::new_with_data(context, data)
}
///
/// Creates a new instance buffer and fills it with the given data which must contain between 1 and 4 contiguous values for each vertex.
/// Use this method instead of [new_with_static](InstanceBuffer::new_with_static)
/// when you expect the data to change often.
///
#[deprecated = "use new_with_data() and specify the data in the same format as in the shader (for example an array of Vec3 instead of f32)"]
pub fn new_with_dynamic<T: BufferDataType>(
context: &Context,
data: &[T],
) -> ThreeDResult<Self> {
Self::new_with_data(context, data)
}
///
/// Fills the instance buffer with the given data which must contain between 1 and 4 contiguous values for each vertex.
/// Use this method instead of [fill_with_dynamic](InstanceBuffer::fill_with_dynamic)
/// when you do not expect the data to change often.
///
#[deprecated = "use fill() and specify the data in the same format as in the shader (for example an array of Vec3 instead of f32)"]
pub fn fill_with_static<T: BufferDataType>(&mut self, data: &[T]) {
self.fill(data).unwrap();
}
///
/// Fills the instance buffer with the given data which must contain between 1 and 4 contiguous values for each vertex.
/// Use this method instead of [fill_with_static](InstanceBuffer::fill_with_static)
/// when you expect the data to change often.
///
#[deprecated = "use fill() and specify the data in the same format as in the shader (for example an array of Vec3 instead of f32)"]
pub fn fill_with_dynamic<T: BufferDataType>(&mut self, data: &[T]) {
self.fill(data).unwrap();
}
}