0.0.23 • Published 2 years ago

@edwmurph/threejsr v0.0.23

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

ThreeJSR

UNDER DEVELOPMENT

React based library for three.js projects.

Features

  • render three.js scene in dimensions set by parent element
  • three.js scene and render loop logic is defined with javascript to ensure consistency with the three.js docs
  • add post operation passes like the GlitchPass and UnrealBloomPass
  • threejs environment requirement error handling with customizable error boundary

See example usage: https://github.com/edwmurph/threejs

Installation

npm i @edwmurph/threejsr

Also install required peer dependencies:

npm i three@^0 react@^16

Getting started

  1. Extend ThreeJSR to build your own threejs scene:
// src/threejs/sphere.js
import * as THREE from 'three';
import { ThreeJSR } from '@edwmurph/threejsr';

import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass.js';

class Sphere extends ThreeJSR {
  constructor (ref, newFrameHook) {
    const bloomPass = new UnrealBloomPass();
    super(ref, newFrameHook, { passes: [bloomPass] });
  }

  renderNextFrame({ spin, timestamp }) {
    if (spin) {
      this.mesh.rotation.x += 0.001;
      this.mesh.rotation.y += 0.001;
    }

    return super.renderNextFrame(timestamp);
  }

  createThreeScene() {
    this.scene = new THREE.Scene();

    this.camera = new THREE.PerspectiveCamera(75, 0, 0.1, 1000);
    this.camera.position.z = 100;

    const geometry = new THREE.SphereGeometry(40, 50, 30);
    const material = new THREE.MeshLambertMaterial({ color: 0xffffff, wireframe: true });

    this.mesh = new THREE.Mesh(geometry, material);
    this.scene.add(this.mesh);

    const spotLight = new THREE.SpotLight(0xffffff);
    spotLight.position.set(100, 10, 100);

    this.scene.add(spotLight);
  }
}

export default new Sphere();
  1. Add ThreeJSRComponent to one of your components:
// src/components/app.js
import React from 'react';
import Sphere from '../threejs/sphere';
import { ThreeJSRComponent } from '@edwmurph/threejsr';

const renderLoopData = {
  spin: true
};

const Component = () => {
  return (
    <ThreeJSRComponent
      ThreeJSR={Sphere}
      renderLoopData={renderLoopData}
      style={{ border: '5px solid green' }}
    />
  );
};

export default Component;
0.0.23

2 years ago

0.0.22

3 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.12

4 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago