Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main:
- const config = {
- mesh: '/models/feetv2.babylon',
- height: 100,
- width: 100,
- workers: ['left', 'right'],
- };
- const newEngine = await createEngine(config);
- this.getParent().appendChild(newEngine.canvas);
- newEngine.run();
- createEngine:
- import { Engine } from 'babylonjs';
- import createScene from './createScene';
- import { importScene } from 'helpers/material';
- const defaultConfig = {
- // color: [33, 43, 51],
- color: [255, 255, 255]
- };
- export default async function createEngine(base, element = undefined) {
- const canvas = element || document.createElement('canvas');
- canvas.width = 200;
- canvas.height = 200;
- // Todo check for Antialiasing
- const engine = new Engine(canvas);
- const config = Object.assign(
- {}, defaultConfig, base, { engine, canvas }
- );
- const scene = await createScene(config);
- function dispose() {
- scene.workers
- .map(worker => worker.terminate());
- engine.dispose();
- console.log('disposed');
- }
- async function run() {
- await importScene(config.mesh, scene.internal);
- engine.runRenderLoop(() => scene.render());
- // engine.runRenderLoop(() => console.log('test'));
- }
- function resize() {
- engine.resize();
- }
- return {
- update: scene.update,
- dispose,
- resize,
- run,
- canvas,
- dev: scene,
- };
- }
- createScene:
- import { Color4, Scene, ArcRotateCamera, Vector3, FreeCamera, Mesh } from 'babylonjs';
- import { importScene } from 'helpers/material';
- // $FlowIgnore
- const Worker = require('worker-loader!../helpers/worker');// eslint-disable-line
- // TODO implement camera from blender
- function createCamera(scene) {
- return new ArcRotateCamera(
- 'Camera',
- 1.5 * Math.PI,
- Math.PI / 8,
- 50,
- Vector3.Zero(),
- scene
- );
- }
- async function createScene(config) {
- const { textureModifier } = config;
- const { height, width, color } = config;
- const scene = new Scene(config.engine);
- // await importScene(config.mesh, scene);
- // scene = await importScene(scene, config.mesh);
- scene.clearColor = new Color4(color[0] / 255, color[1] / 255, color[2] / 255, 1);
- const camera = createCamera(scene);
- camera.setTarget(Vector3.Zero());
- camera.attachControl(config.canvas, false);
- camera.wheelPrecision = 50;
- // Sets up the workers for later usage
- const workers = {};
- config.workers.map((key) => {
- workers[key] = new Worker();
- });
- // const body = await textureModifier(scene, width, height);
- // const textures = body.textures;
- // scene = body.scene;
- // function update(points, key) {
- // return new Promise((resolve) => {
- // workers[key].onmessage = (e) => {
- // const newTexture = new Uint8Array(e.data);
- // textures[key].update(newTexture);
- // return resolve();
- // };
- // workers[key].postMessage({
- // points, height, width,
- // });
- // });
- // }
- // This creates and positions a free camera (non-mesh)
- // var camera = new FreeCamera("camera1", new Vector3(0, 5, -10), scene);
- // // This targets the camera to scene origin
- // camera.setTarget(Vector3.Zero());
- // // This attaches the camera to the canvas
- // camera.attachControl(config.canvas, true);
- // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
- // var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
- // // Default intensity is 1. Let's dim the light a small amount
- // light.intensity = 0.7;
- // Our built-in 'sphere' shape. Params: name, subdivs, size, scene
- // var sphere = Mesh.CreateSphere("sphere1", 16, 2, scene);
- // // Move the sphere upward 1/2 its height
- // sphere.position.y = 1;
- // // Our built-in 'ground' shape. Params: name, width, depth, subdivs, scene
- // var ground = Mesh.CreateGround("ground1", 6, 6, 2, scene);
- return {
- render: scene.render.bind(scene),
- workers: Object.values(workers),
- internal: scene,
- };
- }
- export default createScene;
- importScene:
- export async function importScene(name, scene) {
- const filePath = path.join('./dist/', name);
- let model;
- try {
- model = await readFile(filePath, 'utf8');
- const loader = await SceneLoader.GetPluginForExtension('babylon');
- await loader.load(scene, model);
- } catch (e) {
- console.error(
- `Error: ${e.toString()}
- When Trying to load: ${filePath}`
- );
- }
- return scene;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement