Code Monkey home page Code Monkey logo

elixr's Introduction

elixr

npm version language npm download license

Elixr is a lightweight and flexible framework for building WebXR experiences. Built on top of the popular three.js library and integrated with the highly performant Rapier physics engine, Elixr aims to provide an easy-to-use and customizable solution for creating XR experiences on the web.

Table of contents

Key features | Installation | Usage | API | License

Key features

  • ๐Ÿš€ Easy-to-use WebXR scene setup: Intuitive APIs for setting up WebXR scenes.
  • ๐ŸŽฎ Powerful ECS architecture: Efficient game logic with a flexible entity-component system.
  • ๐Ÿ—๏ธ Rapier physics integration: Realistic physics simulations with the highly performant Rapier physics engine.
  • ๐Ÿ•น๏ธ Pre-built, customizable interaction systems: Easily add pre-built interaction systems like snap-turn and teleportation.
  • ๐ŸŒ Compatibility with three.js plugins: Build on top of an established three.js ecosystem with compatibility for plugins.

Installation

To install and set up the library, run:

$ npm install elixr

Or if you prefer using Yarn:

$ yarn add elixr

Usage

To import elixr and set up your new world with a cube:

import { Core, GameObject, PrimitiveType, THREE, VRButton } from 'elixr';

// create a Core objects automatically sets up the base scene and the render loop
Core.init(document.getElementById('scene-container')).then((core) => {
	const cubeObject = new GameObject();
	cubeObject.addComponent(MeshRenderer, {
		meshRef: new THREE.Mesh(
			new THREE.BoxGeometry(1, 1, 1),
			new THREE.MeshStandardMaterial({ color: 0xff0000 }),
		),
	});
	cubeObject.position.set(0, 1, -2);

	// add some lighting
	core.scene.add(new THREE.AmbientLight(0xffffff, 0.5));
	core.scene.add(new THREE.DirectionalLight(0xffffff, 1));

	// convert a button to the Enter VR button
	const vrButton = document.getElementById('vr-button');
	VRButton.convertToVRButton(vrButton, core.renderer);
});

Use ECS to add a spin behavior to that cube:

import { GameComponent, GameSystem } from 'elixr';

class Spin extends GameComponent {}

class SpinSystem extends GameSystem {
	// update(delta, time) is run every frame, define the game loop behavior here
	update(delta) {
		// query game objects as defined in SpinSystem.queries
		this.queryGameObjects('cubes').forEach((cubeObject) => {
			// GameObjects extends THREE.Object3D
			cubeObject.rotateY(Math.PI * delta);
		});
	}
}

SpinSystem.queries = {
	cubes: { components: [Spin] },
};

core.registerGameComponent(Spin);
core.registerGameSystem(SpinSystem);

// GameObjects also function as entities that can be queried in systems
cubeObject.addComponent(Spin);

Make the world and the cube have physics:

import { Core, GameObject, PrimitiveType, THREE, VRButton } from 'elixr';

// set gravity
core.physics.gravity.set(0, -9.8, 0);

// primitive objects come with rigidbodies and colliders
const cubeObject = GameObject.createPrimitive(PrimitiveType.Cube);
cubeObject.position.set(0, 10, -2);
const floor = GameObject.createPrimitive(PrimitiveType.Plane);
floor.position.set(0, 0, 0);

To implement naive joystick movement:

import { AXES, Vector3, XRGameSystem } from 'elixr';

const MAX_MOVEMENT_SPEED = 1;

export class JoystickMovementSystem extends XRGameSystem {
	update(delta, _time) {
		// "left" and "right" controllers are stored in core.controllers
		// they are only available after entering XR
		if (!this.core.controllers['left']) return;
		const gamepad = this.core.controllers['left'].gamepad;
		const xValue = gamepad.getAxis(AXES.XR_STANDARD.THUMBSTICK_X);
		const zValue = gamepad.getAxis(AXES.XR_STANDARD.THUMBSTICK_Y);
		// core.playerSpace is a THREE.Group that contains the camera and both controllers
		this.core.playerSpace.position.x += xValue * delta * MAX_MOVEMENT_SPEED;
		this.core.playerSpace.position.z += zValue * delta * MAX_MOVEMENT_SPEED;
	}
}

API

Please refer to elixrjs.io for full API documentation.

License

Apache-2.0 License ยฉ 2023 Felix Zhang

elixr's People

Contributors

felixtrz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

elixr's Issues

iOS devices

Hello, does this lib works on ios devices?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.