Skip to content

Commit

Permalink
Initial group API
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-xr committed Jun 10, 2024
1 parent e84a54c commit 3e39c12
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 4 additions & 1 deletion examples/Pong/Pong.fs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ open Fable.Core.Rust
let init (_args: array<string>) =
game
|> GameBuilder.draw3d (fun model frameTime ->
Graphics.Scene3D.sphere()
Graphics.Scene3D.group([|
Graphics.Scene3D.cylinder() |> Graphics.Scene3D.Transform.translateY -1.0f;
Graphics.Scene3D.cube()
|])
|> Graphics.Scene3D.Transform.translateX ((sin (frameTime.tts * 5.0f)) * 1.0f)
)
|> GameBuilder.tick tick
Expand Down
2 changes: 2 additions & 0 deletions runtime/functor-runtime-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ glow = "0.13.1"
image = "0.25.1"
once_cell = "1.19.0"
serde = { version = "1.0", features = ["derive"] }
fable_library_rust = { path = "./../../fable_modules/fable-library-rust" }


# Conditionally include dependencies for the WebAssembly target
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
24 changes: 22 additions & 2 deletions runtime/functor-runtime-common/src/scene3d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use cgmath::{vec3, Matrix4, SquareMatrix};
use serde::{Deserialize, Serialize};

use fable_library_rust::{List_, NativeArray_::Array};

use crate::{
geometry::{self, Geometry},
material::BasicMaterial,
Expand All @@ -17,6 +19,7 @@ pub enum Shape {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SceneObject {
Geometry(Shape),
Group(Vec<Scene3D>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -51,6 +54,13 @@ impl Scene3D {
}
}

pub fn group(items: Array<Scene3D>) -> Self {
Scene3D {
obj: SceneObject::Group(items.to_vec()),
xform: Matrix4::identity(),
}
}

pub fn transform(self, xform: Matrix4<f32>) -> Self {
Scene3D {
xform: self.xform * xform,
Expand All @@ -70,7 +80,6 @@ impl Scene3D {
self.transform(Matrix4::from_translation(vec3(0.0, 0.0, z)))
}


pub fn render(
&self,
context: &RenderContext,
Expand All @@ -83,7 +92,18 @@ impl Scene3D {
basic_material.initialize(&context);

let skinning_data = vec![];
match self.obj {
match &self.obj {
SceneObject::Group(items) => {
let new_world_matrix = self.xform * world_matrix;
for item in items.into_iter() {
item.render(
&context,
&new_world_matrix,
&projection_matrix,
&view_matrix,
)
}
}
SceneObject::Geometry(Shape::Cube) => {
let xform = self.xform * world_matrix;
basic_material.draw_opaque(
Expand Down
3 changes: 3 additions & 0 deletions src/Functor.Game/Scene3D.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module Scene3D =
[<Emit("functor_runtime_common::Scene3D::cylinder()")>]
let cylinder(): Scene3D = nativeOnly

[<Emit("functor_runtime_common::Scene3D::group($0)")>]
let group (items: Scene3D[]): Scene3D = nativeOnly

module Transform =
[<Emit("functor_runtime_common::Scene3D::translate_x($1, $0)")>]
let translateX (x: float32) (scene: Scene3D): Scene3D = nativeOnly
Expand Down

0 comments on commit 3e39c12

Please sign in to comment.