getMatrixAt returns undefined

Hi i am trying to getMatrixAt but it keeps returning undefined what am I missing?


import * as THREE from './three/build/three.module.js';
import {entity} from './entity.js';
import {GLTFLoader} from './three/examples/jsm/loaders/GLTFLoader.js';


export const scenery_controller = (() => {
let mesh;
const count = 600;
const dummy = new THREE.Object3D();

class SceneryController extends entity.Component {
   constructor(params) {
     super();
     this.params_ = params;
     this.center_ = null;
     this.crap_ = [];
   }

   InitEntity() {
     this.SpawnShit_();
   }

   SpawnShit_() {
     const loader = new GLTFLoader();
     loader.load("./resources/nature2/GLTF/Cloud1.glb", function (geometry) {
     console.log(geometry);
      let geometry_ = geometry.scene;
       geometry_.traverse(function (child) {
         if (child.isMesh) {
           let material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
           child.material = material;
           mesh = new THREE.InstancedMesh(child.geometry, child.material, count);
              mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
             scene.add(mesh);
           for (let i = 0; i < count; i++) {
             dummy.position.y = 0;
             dummy.position.set(0 +
               (Math.random() * 2.0 - 1.0) * 15,
               0,
               0 + (Math.random() * 2.0 - 1.0) * 16
             );
            
             dummy.rotation.y = 0 + Math.random();
             dummy.rotation.z = dummy.rotation.y * 2;
     
             dummy.updateMatrix();
     
             mesh.setMatrixAt(i++, dummy.matrix);
           }
         }
       });
      });
   }

 SpawnCrap_() {
     // anytime the player moves
     // SpawnCrap_() will be called.

     const player = this.FindEntity('player');
     if (!player) {
       return;
     }
     const pos = player._position;
     const center = new THREE.Vector3().copy(player.Position);
     center.x = Math.round(center.x / 2);
     center.y = 0.0;
     center.z = Math.round(center.z / 2);

     if (this.center_ && this.center_.equals(center)) {
       return;
     }
    this.center_ = center;
    let _P = new THREE.Vector3();
     const _V = new THREE.Vector3();
     const terrain = this.FindEntity('terrain').GetComponent('TerrainChunkManager');
     
     if (mesh) {
     for (let i = 0; i < count; i++) {
       
         const x = (Math.random() * 2.0 - 1.0) * 85;
         const y = (Math.random() * 2.0 - 1.0) * 85;
         _P.set(x, 0.0, y);
         _P.add(pos);
         _P.y = terrain.GetHeight(_P)[0];
    const scaler = (Math.random() * (0.720 - 2.200) + 2.200).toFixed(3);
    
    dummy.scale.set( 
    scaler, scaler, scaler
     );
   
     dummy.position.set( 
        _P.x,
        _P.y,
        _P.z
      );

  // dummy.scale.set(0,0,0)
   dummy.updateMatrix();
   mesh.instanceMatrix.needsUpdate = true;
   dummy.rotation.y = 0 + Math.random();
   dummy.rotation.z = dummy.rotation.y * 2;
  
   dummy.updateMatrix();

   mesh.setMatrixAt(i++, dummy.matrix);

   // gm returns undefined 
   // i suspect dummy.matrix isnt the right thing to go here..
   let gm =  mesh.getMatrixAt( i++, dummy.matrix );
   console.log(gm, 'get instanced mesh');
  
     }
     }

   }
 Update(_) {
     this.SpawnCrap_();
   }
 };

 return {
     SceneryController: SceneryController,
 };
})();

thank you for any help

When in doubt, read the docs . Retuning undefined is the expected behaviour, the matrix you pass as an argument to that method will be set to the corresponding ,atrix.

Also, it may be worth installing types for three. I believe they even without typescript your IDE could be of help here. Could save you some time browsing the docs.

1 Like