ThreeJS: How to replace a cube mesh with a car mesh?

I developed a 3D cube that moves on one axis simulating the accelerometer sensor. Currently, I have the .obj and .mtl of a toycar which I aim to add to the scene(), but when I remove the BoxGeometry 3D cube and replace it with the car mesh I got these errors all the time:

error

I also get this error saying that obj is not defined, even if I defined it globally I still have the same issue:

error2

I checked libraries that exist locally and other function but I can't see where is the problem.

Below is how I load the car model:

const scene = new THREE.Scene();

    var loader = new THREE.OBJMTLLoader();
    loader.load('https://jyunming-chen.github.io/tutsplus/models/toycar.obj', 'https://jyunming-chen.github.io/tutsplus/models/toycar.mtl',
      function (vehicle) {
        toycar = vehicle;
        toycar.rotateY(-10.99);
        scene.add(toycar);
      });

and this is my full .HTML code with js implementation:

This is hoq looks like now: 3D cube mesh

and this is what I am aiming to achieve:

final car model

That's my current code:

<html>

<head>
  <meta charset="UTF-8">
  <script src="./three.min.js"></script>
  <script src="./require.js" type="text/javascript"></script>
  <script src="./OrbitControls.js"></script>
    <script src="./KeyboardState.js"></script>
    <script src="./MTLLoader.js"></script>
    <script src="./OBJMTLLoader.js"></script>
  <script type="module"> import * as THREE from "./three.module.js"</script>
</head>

<body>
  <canvas id="canvas" width="1000" height="600" style="border:1px solid #000000;"></canvas>

</body>
<script>

  let sensorValue = 0;
  let sensorAddr = 0;
  var toycar;
  StartRetrieveLiveData();

  function main() {
    const canvas = document.querySelector('#canvas');
    const accelPanel = document.querySelector('#accelPanel');
    const renderer = new THREE.WebGLRenderer({  alpha: true, canvas });
    renderer.setClearColor( 0x626d73, 1 ); 
    var context = canvas.getContext("2d");
    var width = window.innerWidth;
    var height = window.innerHeight;

    const fov = 70;
    const aspect = 2;
    const near = 20;
    const far = 500;
    const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);

    camera.position.set(0, 50, 1.5);
    camera.up.set(0, 0, 1);
    camera.lookAt(0, 0, 0);

    const scene = new THREE.Scene();

    // var loader = new THREE.OBJMTLLoader();
    // loader.load('https://jyunming-chen.github.io/tutsplus/models/toycar.obj', 'https://jyunming-chen.github.io/tutsplus/models/toycar.mtl',
    //   function (vehicle) {
    //     toycar = vehicle;
    //     toycar.rotateY(-10.99);
    //     scene.add(toycar);
    //   });

    // An array of objects who's rotation to update
    const objects = [];
    const radius = 3;
    const widthSegments = 3;
    const heightSegments = 3;
    const sphereGeometry = new THREE.BoxGeometry(radius, widthSegments, heightSegments);
    const sunMaterial = new THREE.MeshBasicMaterial({ color: "green", wireframe: false });
    const object = new THREE.Mesh(sphereGeometry, sunMaterial);

    var cubeAxis = new THREE.AxesHelper(10);
    object.add(cubeAxis);

    object.scale.set(2, 2, 2);
    scene.add(object);
    objects.push(object);

    function resizeRendererToDisplaySize(renderer) {
      const canvas = renderer.domElement;
      const width = canvas.clientWidth;
      const height = canvas.clientHeight;
      const needResize = canvas.width !== width || canvas.height !== height;
      if (needResize) {
        renderer.setSize(width, height, false);
      }
      return needResize;
    }

    function render() {

      if (resizeRendererToDisplaySize(renderer)) {
        const canvas = renderer.domElement;
        camera.aspect = canvas.clientWidth / canvas.clientHeight;
        camera.updateProjectionMatrix();
      }
 
      objects.forEach((obj) => {
        sensorValueIndex = ((sensorValue / 16384) * 10);
        obj.position.z = ((sensorValue / 16384) * 20);
        console.log("AccX: ",sensorValueIndex);

        // // Here I take accelerometerX and pass them to the 3D model
        // if (sensorAddr === 1) {
          
        // }
        
      });

      renderer.render(scene, camera);
      
      requestAnimationFrame(render);
    }
    requestAnimationFrame(render);
  }

  function onMsg(event) {
    // console.log(`[message] Data received from server: ${event.data}`);
    // console.log("event.data = " + JSON.parse(event.data));

    var received_msg = event.data;
    var obj = JSON.parse(JSON.parse(received_msg));

    if (obj !== null) {

      if (
        obj.hasOwnProperty("DataMapChangedObjectsAddressValue") &&
        obj["DataMapChangedObjectsAddressValue"][0]["DataMapAddress"] !==
        undefined
      ) {
        sensorAddr =
          obj["DataMapChangedObjectsAddressValue"][0]["DataMapAddress"];
        sensorValue =
          obj["DataMapChangedObjectsAddressValue"][0]["Value"];

        // if (sensorAddr === 1) {
        //   sensorValueIndex = (sensorValue / 16384) * 500;
        // }
      }
    }
  }

  function onOpen(e) {
    console.log("SSE connected");
  }

  function onError(e) {
    // console.log(`[error] ${error.message}`);
    if (e.eventPhase == EventSource.CLOSED) this.source.close();
    if (e.target.readyState == EventSource.CLOSED) {
      console.log("SSE Disconnected");
    } else if (e.target.readyState == EventSource.CONNECTING) {
      console.log("SSE Connecting ...");
    }
  }

  function StartRetrieveLiveData() {
    if (!!window.EventSource) {
      this.source = new EventSource("/sse");
    } else {
      console.log("Your browser doesn't support SSE");
    }

    this.source.addEventListener("message", e => this.onMsg(e));

    this.source.addEventListener("open", e => this.onOpen(e), false);

    this.source.addEventListener("error", e => this.onError(e), false);

    // Add here (only mozilla)
    main();
    // Add here

  }

</script>

</html>

Note that when I used a public server the whole thing works just fine, but I used the remote server (the actual server) I get these error and the whole thing doesn't work as expected.

Would appreciate a solution for this.



from Recent Questions - Stack Overflow https://ift.tt/2PfvOnQ
https://ift.tt/3cvbMin

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations