Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental props #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/BabylonSvelte/BabylonScene.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
let sceneStore = writable(scene);
$: $sceneStore = scene; // eslint-disable-line @typescript-eslint/no-unused-vars

export const elapsedTime = writable(0);

setContext('BabylonScene', {
getScene: () => sceneStore
});
Expand All @@ -17,6 +19,7 @@

function renderFunction() {
scene.render();
$elapsedTime += scene.deltaTime;
}

let BABYLON: typeof import('babylonjs');
Expand Down
18 changes: 13 additions & 5 deletions src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
BabylonGround
} from '$lib/BabylonSvelte';

let BABYLON: typeof import('babylonjs'); // Init Babylon

// Sphere falling
const fall = {
duration: 1000,
Expand All @@ -28,7 +30,7 @@

const t = tweened(4);

// Chaining a Svelte Tweens to loop infinitely (changing the tween options)
// Chaining a Svelte Tween to loop infinitely (changing the tween options)
(function loop() {
t.set(1, fall).then(() => {
t.set(4, rise).then(loop);
Expand All @@ -38,10 +40,16 @@
let spherePosition: Vector3;
$: if (spherePosition) spherePosition.y = $t; // Reactively changing the Babylon sphere's y-position with our Svelte Tween

let sphereColor: Color3;
$: if (sphereColor) sphereColor = new BABYLON.Color3($t / 4, 0, 1); // Also reactively changing the Babylon sphere's diffuseColor with our Svelte Tween (lazy example)
let elapsedTime; // Read-only timer from the scene

let refColor: Color3;
$: if (BABYLON) refColor = new BABYLON.Color3();

let BABYLON: typeof import('babylonjs');
$: if (elapsedTime && refColor)
BABYLON.Color3.HSVtoRGBToRef(($elapsedTime / 20) % 360, 1, 1, refColor); // Generate a refColor based on the scene's frame count

let sphereColor: Color3;
$: if (refColor && sphereColor) sphereColor = refColor; // Also reactively changing the Babylon sphere's diffuseColor to our refColor

onMount(async () => {
const babylonjs = await import('babylonjs');
Expand All @@ -52,7 +60,7 @@
<div class:BABYLON>
{#if BABYLON}
<BabylonEngine>
<BabylonScene>
<BabylonScene bind:elapsedTime>
<BabylonCamera position={new BABYLON.Vector3(0, 5, -10)} target={BABYLON.Vector3.Zero()} />
<BabylonHemisphericLight direction={new BABYLON.Vector3(0, 1, 0)} intensity={0.7} />
<!-- Below is two-way binding between spherePosition and position -->
Expand Down