Skip to content

Loading GLB models with phy.load() fails with 'Unexpected token '<'' error in Vue+Vite #71

@Fxalll

Description

@Fxalll

I tried React+Vite using the official example "Init modules", but it failed with a console error "Loading error on Physx".
I then tried Vue+Vite, and this time the official example ran smoothly. However, whenever I try to load a glb model with

phy.load("/assets/models/minmac.glb", () => {...}) 

I get an error "Uncaught SyntaxError: Unexpected token '<'".

Image

Upon closer inspection, the error occurs in the source code:

this.decoderPending = Promise.all(librariesPending).then((libraries) => { 
  const jsContent = libraries[0]; 

  const fn = DRACOWorker.toString(); 

  const body = [ 
    "/* draco decoder */", 
    jsContent, 
    "", 
    "/* worker */", 
    fn.substring(fn.indexOf("{") + 1, fn.lastIndexOf("}")), 
  ].join("\n"); 

  this.workerSourceURL = URL.createObjectURL(new Blob([body])); 
});

I'm very confused now. I tried putting draco in the public directory and using phy.setDracoPath('..'); but nothing changed.
I also tried not using phy, and just using the threejs demo which can load this model normally.

Here is the complete Vue code:

<script setup>
import * as THREE from 'three';
import { phy, math } from 'phy-engine';

const w = window.innerWidth;
const h = window.innerHeight;

// init three.js
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, w / h, 0.1, 1000);
camera.position.set(0, 1, 5);
const renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0x07142c, 1);
renderer.setSize(w, h);
document.body.appendChild(renderer.domElement);

// add some light
const light = new THREE.PointLight(0xffffaa, 10, 100, 2);
light.position.set(0, 4, 2);
scene.add(light);
const hemiLight = new THREE.HemisphereLight(0x2076d5, 0x502c09, 1);
scene.add(hemiLight);

// start three render loop
function animate () {
  renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);

// init phy-engine
phy.init({
  type: 'PHYSX',
  worker: true,
  compact: true,
  scene: scene,
  renderer: renderer,
  callback: physicsReady,
})

function physicsReady () {
  phy.set({ substep: 1, gravity: [0, -9.81, 0], fps: 60 });
  phy.add({ type: 'plane', size: [300, 1, 300], visible: true });
  phy.add({ type: 'box', size: [1, 1, 1], pos: [0, 4, 0], mass: 1 });
  phy.add({ type: 'sphere', size: [0.5], pos: [0, 6, 0], mass: 1 });

  phy.load("/assets/models/minmac.glb", addModel);

  function addModel () {
    let scale = 10;

    const model = phy.getMesh("minmac");

    model.mac.geometry.scale(scale, scale, scale);

    const option = {
      type: "box",
      size: [
        (2.5 * scale) / 10,
        (3.4 * scale) / 10,
        (2.8 * scale) / 10,
      ],
      mass: 7.26,
      friction: 0.4,
      restitution: 0.2,
    };

    phy.add({
      ...option,
      pos: [0, 1.7 + 3.4 * 1, 0],
      mesh: model.mac,
      breakable: true,
      breakOption: [50, 3, 4, 2, true],
    });
  }
}
</script>

<template>
  <div ref="container"></div>
</template>

<style scoped>
div {
  margin: 0;
  padding: 0;
  overflow: hidden;
}
</style>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions