feat: new stereokit

This commit is contained in:
Nova
2023-04-30 13:25:13 -04:00
parent 35f42559ac
commit aefa6dc62f
17 changed files with 278 additions and 483 deletions

View File

@@ -5,7 +5,7 @@ use crate::core::registry::Registry;
use crate::core::resource::ResourceID;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use color_eyre::eyre::{ensure, eyre, Result};
use glam::{vec3a, Vec4Swizzles};
use glam::{vec3, Vec4Swizzles};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use send_wrapper::SendWrapper;
@@ -13,19 +13,20 @@ use serde::Deserialize;
use stardust_xr::schemas::flex::deserialize;
use stardust_xr::values::Transform;
use std::ops::DerefMut;
use std::{ffi::OsStr, fmt::Error, path::PathBuf, sync::Arc};
use stereokit::sound::SoundInstance;
use stereokit::sound::{Sound as SKSound, SoundT};
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
use stereokit::{Sound as SkSound, SoundInstance, StereoKitDraw};
static SOUND_REGISTRY: Registry<Sound> = Registry::new();
pub struct Sound {
space: Arc<Spatial>,
resource_id: ResourceID,
pending_audio_path: OnceCell<PathBuf>,
instance: Mutex<Option<SoundInstance>>,
volume: f32,
sk_sound: OnceCell<SendWrapper<SKSound>>,
pending_audio_path: PathBuf,
sk_sound: OnceCell<SendWrapper<SkSound>>,
instance: Mutex<Option<SoundInstance>>,
stop: Mutex<Option<()>>,
play: Mutex<Option<()>>,
}
impl Sound {
@@ -34,78 +35,70 @@ impl Sound {
node.spatial.get().is_some(),
"Internal: Node does not have a spatial attached!"
);
let pending_audio_path = resource_id
.get_file(
&node
.get_client()
.ok_or_else(|| eyre!("Client not found"))?
.base_resource_prefixes
.lock()
.clone(),
&[OsStr::new("wav"), OsStr::new("mp3")],
)
.ok_or_else(|| eyre!("Resource not found"))?;
let sound = Sound {
space: node.spatial.get().unwrap().clone(),
resource_id,
volume: 1.0,
instance: Mutex::new(None),
pending_audio_path: OnceCell::new(),
pending_audio_path,
sk_sound: OnceCell::new(),
instance: Mutex::new(None),
stop: Mutex::new(None),
play: Mutex::new(None),
};
let sound_arc = SOUND_REGISTRY.add(sound);
node.add_local_signal("play", Sound::play_flex);
node.add_local_signal("stop", Sound::stop_flex);
let _ = sound_arc.pending_audio_path.set(
sound_arc
.resource_id
.get_file(
&node
.get_client()
.ok_or_else(|| eyre!("Client not found"))?
.base_resource_prefixes
.lock()
.clone(),
&[OsStr::new("wav"), OsStr::new("mp3")],
)
.ok_or_else(|| eyre!("Resource not found"))?,
);
let _ = node.sound.set(sound_arc.clone());
Ok(sound_arc)
}
fn update(&self) {
fn update(&self, sk: &impl StereoKitDraw) {
let sound = self.sk_sound.get_or_init(|| {
SendWrapper::new(sk.sound_create(self.pending_audio_path.clone()).unwrap())
});
if self.stop.lock().take().is_some() {
if let Some(instance) = self.instance.lock().take() {
sk.sound_inst_stop(instance);
}
}
if self.play.lock().is_some() && self.instance.lock().is_none() {
self.instance.lock().replace(sk.sound_play(
sound.as_ref(),
vec3(0.0, 0.0, 0.0),
self.volume,
));
}
if let Some(instance) = self.instance.lock().deref_mut() {
instance.set_position(self.space.global_transform().w_axis.xyz())
sk.sound_inst_set_pos(*instance, self.space.global_transform().w_axis.xyz());
}
}
fn play_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
let sound = node.sound.get().unwrap();
let sk_sound = sound
.sk_sound
.get_or_try_init(|| -> color_eyre::eyre::Result<SendWrapper<SKSound>> {
let pending_audio_path = sound.pending_audio_path.get().ok_or(Error)?;
let sound = SKSound::from_file(pending_audio_path.as_path()).ok_or(Error)?;
Ok(SendWrapper::new(sound))
})
.ok();
if let Some(sk_sound) = sk_sound {
let position = sound
.space
.global_transform()
.transform_point3a(vec3a(0.0, 0.0, 0.0));
sound
.instance
.lock()
.replace(sk_sound.play_sound(position, sound.volume));
}
sound.play.lock().replace(());
Ok(())
}
pub fn stop_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
let sound = node.sound.get().unwrap();
if let Some(instance) = sound.instance.lock().take() {
instance.stop();
}
sound.stop.lock().replace(());
Ok(())
}
}
pub fn update() {
pub fn update(sk: &impl StereoKitDraw) {
for sound in SOUND_REGISTRY.get_valid_contents() {
sound.update()
sound.update(sk)
}
}

View File

@@ -125,8 +125,8 @@ impl PulseSender {
}
let (_, rotation, position) = Spatial::space_to_space_matrix(
rx_node.spatial.get().map(|s| &**s),
tx_node.spatial.get().map(|s| &**s),
rx_node.spatial.get().map(|s| s.as_ref()),
tx_node.spatial.get().map(|s| s.as_ref()),
)
.to_scale_rotation_translation();

View File

@@ -14,7 +14,7 @@ use prisma::{Flatten, Lerp, Rgba};
use serde::Deserialize;
use stardust_xr::{schemas::flex::deserialize, values::Transform};
use std::{collections::VecDeque, sync::Arc};
use stereokit::{lifecycle::StereoKitDraw, lines::LinePoint as SkLinePoint, values::Color128};
use stereokit::{Color128, LinePoint as SkLinePoint, StereoKitDraw};
use super::Drawable;
@@ -57,14 +57,14 @@ impl Lines {
Ok(lines)
}
fn draw(&self, draw_ctx: &StereoKitDraw) {
fn draw(&self, draw_ctx: &impl StereoKitDraw) {
let transform_mat = self.space.global_transform();
let data = self.data.lock().clone();
let mut points: VecDeque<SkLinePoint> = data
.points
.iter()
.map(|p| SkLinePoint {
point: transform_mat.transform_point3a(Vec3A::from(p.point)).into(),
pt: transform_mat.transform_point3a(Vec3A::from(p.point)).into(),
thickness: p.thickness,
color: p.color.map(|c| (c * 255.0) as u8).into(),
})
@@ -72,20 +72,19 @@ impl Lines {
if data.cyclic && !points.is_empty() {
let first = data.points.first().unwrap();
let last = data.points.last().unwrap();
let color = Rgba::from_slice(&first.color).lerp(&Rgba::from_slice(&last.color), 0.5);
let connect_point = SkLinePoint {
point: transform_mat
pt: transform_mat
.transform_point3a(Vec3A::from(first.point).lerp(Vec3A::from(last.point), 0.5))
.into(),
thickness: (first.thickness + last.thickness) * 0.5,
color: Color128::from(
Rgba::from_slice(&first.color).lerp(&Rgba::from_slice(&last.color), 0.5),
)
.into(),
color: Color128::from([color.red(), color.green(), color.blue(), color.alpha()])
.into(),
};
points.push_front(connect_point.clone());
points.push_back(connect_point);
}
stereokit::lines::line_add_listv(draw_ctx, points.make_contiguous());
draw_ctx.line_add_listv(points.make_contiguous());
}
pub fn set_points_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
@@ -113,7 +112,7 @@ impl Drop for Lines {
}
}
pub fn draw_all(draw_ctx: &StereoKitDraw) {
pub fn draw_all(draw_ctx: &impl StereoKitDraw) {
for lines in LINES_REGISTRY.get_valid_contents() {
if lines.enabled.load(Ordering::Relaxed) {
lines.draw(draw_ctx);

View File

@@ -11,7 +11,7 @@ use parking_lot::Mutex;
use serde::Deserialize;
use stardust_xr::schemas::flex::deserialize;
use std::{path::PathBuf, sync::Arc};
use stereokit::{lifecycle::StereoKitDraw, render::StereoKitRender, texture::Texture};
use stereokit::StereoKitDraw;
use tracing::instrument;
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
@@ -30,31 +30,19 @@ pub enum Drawable {
}
#[instrument(level = "debug", skip(sk))]
pub fn draw(sk: &StereoKitDraw) {
pub fn draw(sk: &impl StereoKitDraw) {
lines::draw_all(sk);
model::draw_all(sk);
text::draw_all(sk);
let new_skytex = QUEUED_SKYTEX.lock().take();
let mut new_skylight = QUEUED_SKYLIGHT.lock().take();
let same_file = new_skytex == new_skylight;
if let Some(skytex) = new_skytex {
if let Some((skytex, skylight)) =
Texture::from_cubemap_equirectangular(sk, &skytex, true, i32::MAX)
{
sk.set_skytex(&skytex);
if same_file {
sk.set_skylight(&skylight);
new_skylight = None;
}
if let Some(skytex) = QUEUED_SKYTEX.lock().take() {
if let Ok((_skylight, skytex)) = sk.tex_create_cubemap_file(&skytex, true, i32::MAX) {
sk.render_set_skytex(&skytex);
}
}
if let Some(skylight) = new_skylight {
if let Some((_, skylight)) =
Texture::from_cubemap_equirectangular(sk, &skylight, true, i32::MAX)
{
sk.set_skylight(&skylight);
if let Some(skylight) = QUEUED_SKYLIGHT.lock().take() {
if let Ok((skylight, _)) = sk.tex_create_cubemap_file(&skylight, true, i32::MAX) {
sk.render_set_skylight(skylight);
}
}
}

View File

@@ -6,6 +6,7 @@ use crate::core::resource::ResourceID;
use crate::nodes::drawable::Drawable;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use color_eyre::eyre::{bail, ensure, eyre, Result};
use glam::Mat4;
use mint::{ColumnMatrix4, Vector2, Vector3, Vector4};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
@@ -19,13 +20,10 @@ use std::ffi::OsStr;
use std::fmt::Error;
use std::path::PathBuf;
use std::sync::Arc;
use stereokit::color_named::WHITE;
use stereokit::lifecycle::{StereoKitContext, StereoKitDraw};
use stereokit::material::Material;
use stereokit::model::Model as SKModel;
use stereokit::render::RenderLayer;
use stereokit::texture::Texture;
use stereokit::values::Color128;
use stereokit::named_colors::WHITE;
use stereokit::{
Color128, Material, Model as SKModel, RenderLayer, Shader, StereoKitDraw, StereoKitMultiThread,
};
static MODEL_REGISTRY: Registry<Model> = Registry::new();
@@ -53,63 +51,63 @@ impl MaterialParameter {
fn apply_to_material(
&self,
client: &Client,
sk: &impl StereoKitContext,
sk: &impl StereoKitMultiThread,
material: &Material,
parameter_name: &str,
) {
match self {
MaterialParameter::Float(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_float(material, parameter_name, *val);
}
MaterialParameter::Vector2(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_vector2(material, parameter_name, *val);
}
MaterialParameter::Vector3(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_vector3(material, parameter_name, *val);
}
MaterialParameter::Vector4(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_vector4(material, parameter_name, *val);
}
MaterialParameter::Color(val) => {
material.set_parameter(sk, parameter_name, &Color128::from(val.clone()));
sk.material_set_color(material, parameter_name, Color128::from(val.clone()));
}
MaterialParameter::Int(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_int(material, parameter_name, *val);
}
MaterialParameter::Int2(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_int2(material, parameter_name, val.x, val.y);
}
MaterialParameter::Int3(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_int3(material, parameter_name, val.x, val.y, val.z);
}
MaterialParameter::Int4(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_int4(material, parameter_name, val.w, val.x, val.y, val.z);
}
MaterialParameter::Bool(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_bool(material, parameter_name, *val);
}
MaterialParameter::UInt(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_uint(material, parameter_name, *val);
}
MaterialParameter::UInt2(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_uint2(material, parameter_name, val.x, val.y);
}
MaterialParameter::UInt3(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_uint3(material, parameter_name, val.x, val.y, val.z);
}
MaterialParameter::UInt4(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_uint4(material, parameter_name, val.w, val.x, val.y, val.z);
}
MaterialParameter::Matrix(val) => {
material.set_parameter(sk, parameter_name, val);
sk.material_set_matrix(material, parameter_name, Mat4::from(*val));
}
MaterialParameter::Texture(resource) => {
let Some(texture_path) = resource.get_file(
&client.base_resource_prefixes.lock().clone(),
&[OsStr::new("png"), OsStr::new("jpg")],
) else {return};
if let Some(tex) = Texture::from_file(sk, texture_path, true, 0) {
material.set_parameter(sk, parameter_name, &tex);
if let Ok(tex) = sk.tex_create_file(texture_path, true, 0) {
sk.material_set_texture(material, parameter_name, &tex);
}
}
}
@@ -188,14 +186,15 @@ impl Model {
Ok(())
}
fn draw(&self, sk: &StereoKitDraw) {
fn draw(&self, sk: &impl StereoKitDraw) {
let sk_model = self
.sk_model
.get_or_try_init(|| -> color_eyre::eyre::Result<SendWrapper<SKModel>> {
let pending_model_path = self.pending_model_path.get().ok_or(Error)?;
let model = SKModel::from_file(sk, pending_model_path.as_path(), None)?;
let model =
sk.model_create_file(pending_model_path.to_str().unwrap(), None::<Shader>)?;
Ok(SendWrapper::new(model.clone()))
Ok(SendWrapper::new(sk.model_copy(model)))
})
.ok();
@@ -203,8 +202,15 @@ impl Model {
{
let mut material_replacements = self.pending_material_replacements.lock();
for (material_idx, replacement_material) in material_replacements.iter() {
if sk_model.get_material(sk, *material_idx as i32).is_some() {
sk_model.set_material(sk, *material_idx as i32, replacement_material);
if sk
.model_get_material(sk_model.as_ref(), *material_idx as i32)
.is_some()
{
sk.model_set_material(
sk_model.as_ref(),
*material_idx as i32,
replacement_material.as_ref().as_ref(),
);
}
}
material_replacements.clear();
@@ -214,20 +220,24 @@ impl Model {
let mut material_parameters = self.pending_material_parameters.lock();
for ((material_idx, parameter_name), parameter_value) in material_parameters.drain()
{
let Some(material) = sk_model.get_material(sk, material_idx) else {continue};
let new_material = material.clone();
let Some(material) = sk.model_get_material(sk_model.as_ref(), material_idx) else {continue};
let new_material = sk.material_copy(material);
parameter_value.apply_to_material(
&client,
sk,
&new_material,
parameter_name.as_str(),
);
sk_model.set_material(sk, material_idx, &new_material);
sk.model_set_material(sk_model.as_ref(), material_idx, &new_material);
}
}
let global_transform = self.space.global_transform().into();
sk_model.draw(sk, global_transform, WHITE, RenderLayer::Layer0);
sk.model_draw(
sk_model.as_ref(),
self.space.global_transform(),
WHITE,
RenderLayer::LAYER0,
);
}
}
}
@@ -240,7 +250,7 @@ impl Drop for Model {
}
}
pub fn draw_all(sk: &StereoKitDraw) {
pub fn draw_all(sk: &impl StereoKitDraw) {
for model in MODEL_REGISTRY.get_valid_contents() {
if model.enabled.load(Ordering::Relaxed) {
model.draw(sk);

View File

@@ -17,13 +17,7 @@ use send_wrapper::SendWrapper;
use serde::Deserialize;
use stardust_xr::{schemas::flex::deserialize, values::Transform};
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
use stereokit::{
color_named::WHITE,
font::Font,
lifecycle::StereoKitDraw,
text::{self, TextAlign, TextFit, TextStyle},
values::Color128,
};
use stereokit::{named_colors::WHITE, Color128, StereoKitDraw, TextAlign, TextFit, TextStyle};
static TEXT_REGISTRY: Registry<Text> = Registry::new();
@@ -96,15 +90,17 @@ impl Text {
Ok(text)
}
fn draw(&self, sk: &StereoKitDraw) {
fn draw(&self, sk: &impl StereoKitDraw) {
let style = self.style.get_or_try_init(
|| -> Result<SendWrapper<TextStyle>, color_eyre::eyre::Error> {
let font = self
.font_path
.as_deref()
.and_then(|path| Font::from_file(sk, path))
.unwrap_or_else(|| Font::default(sk));
Ok(SendWrapper::new(TextStyle::new(sk, font, 1.0, WHITE)))
.and_then(|path| sk.font_create(path).ok())
.unwrap_or_else(|| sk.font_find("default/font").unwrap());
Ok(SendWrapper::new(unsafe {
sk.text_make_style(font, 1.0, WHITE)
}))
},
);
@@ -117,28 +113,36 @@ impl Text {
data.character_height,
));
if let Some(bounds) = data.bounds {
text::draw_in(
sk,
sk.text_add_in(
&data.text,
transform,
bounds / data.character_height,
data.fit,
style,
**style,
data.bounds_align,
data.text_align,
vec3(0.0, 0.0, 0.0),
Color128::from(data.color),
Color128::from([
data.color.red(),
data.color.green(),
data.color.blue(),
data.color.alpha(),
]),
);
} else {
text::draw_at(
sk,
sk.text_add_at(
&data.text,
transform,
style,
**style,
data.bounds_align,
data.text_align,
vec3(0.0, 0.0, 0.0),
data.color,
Color128::from([
data.color.red(),
data.color.green(),
data.color.blue(),
data.color.alpha(),
]),
);
}
}
@@ -171,7 +175,7 @@ impl Drop for Text {
}
}
pub fn draw_all(sk: &StereoKitDraw) {
pub fn draw_all(sk: &impl StereoKitDraw) {
for text in TEXT_REGISTRY.get_valid_contents() {
if text.enabled.load(Ordering::Relaxed) {
text.draw(sk);

View File

@@ -6,7 +6,7 @@ use crate::{
use color_eyre::eyre::Result;
use glam::{vec3, Mat4};
use std::sync::Arc;
use stereokit::input::StereoKitInput;
use stereokit::StereoKitMultiThread;
use tracing::instrument;
lazy_static::lazy_static! {
@@ -21,7 +21,7 @@ fn create() -> Arc<Node> {
}
#[instrument(level = "debug", name = "Update HMD Pose", skip(sk))]
pub fn frame(sk: &impl StereoKitInput) {
pub fn frame(sk: &impl StereoKitMultiThread) {
let spatial = HMD
.spatial
.get()

View File

@@ -186,7 +186,7 @@ impl Deref for ItemType {
match self {
ItemType::Environment(item) => item,
#[cfg(feature = "wayland")]
ItemType::Panel(item) => &**item,
ItemType::Panel(item) => item.as_ref(),
}
}
}