feat: new stereokit
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user