Dynamically Morph A Mesh Takes No Effects by using class attribute instances

I want to update a LinesMesh by the following code:

import * as BABYLON from "@babylonjs/core";
import { Vector3 } from "@babylonjs/core";

export class Box {

    private scene: BABYLON.Scene;
    
    // @ts-ignore
    private xhi: number;
    private yhi: number;
    private zhi: number;
    private xlo: number;
    private ylo: number;
    private zlo: number;
    private xy: number;
    private xz: number;
    private yz: number;
    private Lx: number;
    private Ly: number;
    private Lz: number;

    private box_mesh: BABYLON.LinesMesh;

    constructor(scene: BABYLON.Scene, xhi: number, yhi: number, zhi: number, xlo: number, ylo: number, zlo: number, xy: number, xz: number, yz: number) {
        
        this.scene = scene;
        const points1 = this.computeBoxVertries(xhi, yhi, zhi, xlo, ylo, zlo, xy, xz, yz);

        // create box mesh
        this.box_mesh = BABYLON.MeshBuilder.CreateLines("box", {points: points1}, scene);

    }

    private computeBoxVertries(xhi: number, yhi: number, zhi: number, xlo: number, ylo: number, zlo: number, xy: number, xz: number, yz: number) {

        this.xhi = xhi;
        this.yhi = yhi;
        this.zhi = zhi;
        this.xlo = xlo;
        this.ylo = ylo;
        this.zlo = zlo;
        this.xy = xy;
        this.xz = xz;
        this.yz = yz;
        this.Lx = xhi - xlo;
        this.Ly = yhi - ylo;
        this.Lz = zhi - zlo;

        return [
            new Vector3(xlo, ylo, zlo), // (0, 0, 0)
            new Vector3(xhi, ylo, zlo), // (1, 0, 0)
            new Vector3(xhi+xy, yhi, zlo), // (1, 1, 0)
            new Vector3(xlo+xy, yhi, zlo), // (0, 1, 0)
            new Vector3(xlo, ylo, zlo), // (0, 0, 0)

            new Vector3(xlo+xz, ylo+yz, zhi), // (0, 0, 1)
            new Vector3(xhi+xz, ylo+yz, zhi), // (1, 0, 1)
            new Vector3(xhi+xz+xy, yhi+yz, zhi), // (1, 1, 1)
            new Vector3(xlo+xz+xy, yhi+yz, zhi), // (0, 1, 1)
            new Vector3(xlo+xz, ylo+yz, zhi), // (0, 0, 1)

            new Vector3(xhi+xz, ylo+yz, zhi), // (1, 0, 1)
            new Vector3(xhi, ylo, zlo),  // (1, 0, 0)
            new Vector3(xhi+xy, yhi, zlo), // (1, 1, 0)
            new Vector3(xhi+xz+xy, yhi+yz, zhi), // (1, 1, 1)
            new Vector3(xlo+xz+xy, yhi+yz, zhi), // (0, 1, 1)
            new Vector3(xlo+xy, yhi, zlo), // (0, 1, 0)
        ];
    }

    public update(xhi: number, yhi: number, zhi: number, xlo: number, ylo: number, zlo: number, xy: number, xz: number, yz: number) {
        // change the vertices positions of an existing mesh.
        // Indices remain unchanged
        let points = this.computeBoxVertries(xhi, yhi, zhi, xlo, ylo, zlo, xy, xz, yz);
        
        this.box_mesh = BABYLON.MeshBuilder.CreateLines("box", {points: points, instance: this.box_mesh});

}

}

I find that if I use this.box_mesh, it takes no effects, but I use let box_mesh and instance is box_mesh (not class member), it is ok? Can someone tell me what’s the difference?

Hello and welcome to the Babylon community! You have to mark your mesh as updatable, see: Babylon.js Playground (babylonjs.com)

3 Likes

Thank you! That solves the problem! But why? Why local variable is ok but class member not ok when updatable is unset (undefined)?