gues99999

Untitled

Aug 9th, 2017
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main:
  2.     const config = {
  3.       mesh: '/models/feetv2.babylon',
  4.       height: 100,
  5.       width: 100,
  6.       workers: ['left', 'right'],
  7.     };
  8.     const newEngine = await createEngine(config);
  9.     this.getParent().appendChild(newEngine.canvas);
  10.     newEngine.run();
  11.  
  12. createEngine:
  13.  
  14. import { Engine } from 'babylonjs';
  15. import createScene from './createScene';
  16. import { importScene } from 'helpers/material';
  17.  
  18. const defaultConfig = {
  19.   // color: [33, 43, 51],
  20.   color: [255, 255, 255]
  21. };
  22.  
  23. export default async function createEngine(base, element = undefined) {
  24.   const canvas = element || document.createElement('canvas');
  25.   canvas.width = 200;
  26.   canvas.height = 200;
  27.  
  28.   // Todo check for Antialiasing
  29.   const engine = new Engine(canvas);
  30.  
  31.   const config = Object.assign(
  32.     {}, defaultConfig, base, { engine, canvas }
  33.   );
  34.   const scene = await createScene(config);
  35.  
  36.   function dispose() {
  37.     scene.workers
  38.       .map(worker => worker.terminate());
  39.     engine.dispose();
  40.     console.log('disposed');
  41.   }
  42.  
  43.   async function run() {
  44.     await importScene(config.mesh, scene.internal);
  45.     engine.runRenderLoop(() => scene.render());
  46.     // engine.runRenderLoop(() => console.log('test'));
  47.   }
  48.  
  49.   function resize() {
  50.     engine.resize();
  51.   }
  52.  
  53.   return {
  54.     update: scene.update,
  55.     dispose,
  56.     resize,
  57.     run,
  58.     canvas,
  59.     dev: scene,
  60.   };
  61. }
  62.  
  63. createScene:
  64.  
  65. import { Color4, Scene, ArcRotateCamera, Vector3, FreeCamera, Mesh } from 'babylonjs';
  66. import { importScene } from 'helpers/material';
  67.  
  68. // $FlowIgnore
  69. const Worker = require('worker-loader!../helpers/worker');// eslint-disable-line
  70.  
  71. // TODO implement camera from blender
  72. function createCamera(scene) {
  73.   return new ArcRotateCamera(
  74.     'Camera',
  75.     1.5 * Math.PI,
  76.     Math.PI / 8,
  77.     50,
  78.     Vector3.Zero(),
  79.     scene
  80.   );
  81. }
  82.  
  83. async function createScene(config) {
  84.   const { textureModifier } = config;
  85.   const { height, width, color } = config;
  86.  
  87.   const scene = new Scene(config.engine);
  88.   // await importScene(config.mesh, scene);
  89.   // scene = await importScene(scene, config.mesh);
  90.   scene.clearColor = new Color4(color[0] / 255, color[1] / 255, color[2] / 255, 1);
  91.  
  92.   const camera = createCamera(scene);
  93.   camera.setTarget(Vector3.Zero());
  94.   camera.attachControl(config.canvas, false);
  95.   camera.wheelPrecision = 50;
  96.  
  97.   // Sets up the workers for later usage
  98.   const workers = {};
  99.   config.workers.map((key) => {
  100.     workers[key] = new Worker();
  101.   });
  102.  
  103.   // const body = await textureModifier(scene, width, height);
  104.   // const textures = body.textures;
  105.   // scene = body.scene;
  106.  
  107.   // function update(points, key) {
  108.   //   return new Promise((resolve) => {
  109.   //     workers[key].onmessage = (e) => {
  110.   //       const newTexture = new Uint8Array(e.data);
  111.   //       textures[key].update(newTexture);
  112.   //       return resolve();
  113.   //     };
  114.  
  115.   //     workers[key].postMessage({
  116.   //       points, height, width,
  117.   //     });
  118.   //   });
  119.   // }
  120.  
  121.     // This creates and positions a free camera (non-mesh)
  122.     // var camera = new FreeCamera("camera1", new Vector3(0, 5, -10), scene);
  123.  
  124.     // // This targets the camera to scene origin
  125.     // camera.setTarget(Vector3.Zero());
  126.  
  127.     // // This attaches the camera to the canvas
  128.     // camera.attachControl(config.canvas, true);
  129.  
  130.     // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
  131.     // var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
  132.  
  133.     // // Default intensity is 1. Let's dim the light a small amount
  134.     // light.intensity = 0.7;
  135.  
  136.     // Our built-in 'sphere' shape. Params: name, subdivs, size, scene
  137.     // var sphere = Mesh.CreateSphere("sphere1", 16, 2, scene);
  138.  
  139.     // // Move the sphere upward 1/2 its height
  140.     // sphere.position.y = 1;
  141.  
  142.     // // Our built-in 'ground' shape. Params: name, width, depth, subdivs, scene
  143.     // var ground = Mesh.CreateGround("ground1", 6, 6, 2, scene);
  144.  
  145.   return {
  146.     render: scene.render.bind(scene),
  147.     workers: Object.values(workers),
  148.     internal: scene,
  149.   };
  150. }
  151.  
  152. export default createScene;
  153.  
  154. importScene:
  155.  
  156. export async function importScene(name, scene) {
  157.   const filePath = path.join('./dist/', name);
  158.   let model;
  159.   try {
  160.     model = await readFile(filePath, 'utf8');
  161.     const loader = await SceneLoader.GetPluginForExtension('babylon');
  162.     await loader.load(scene, model);
  163.   } catch (e) {
  164.     console.error(
  165.       `Error: ${e.toString()}
  166.        When Trying to load: ${filePath}`
  167.     );
  168.   }
  169.   return scene;
  170. }
RAW Paste Data