StxModel.js 4.17 KB
import { BufferGeometry, BufferAttribute, Float32BufferAttribute } from 'three';
import SupportMesh from '@/utility/jsm/mesh/SupportMesh';
import { mergeGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils.js';

const transferToGeometry = (meshstruct) => {
    const { vertex, index } = meshstruct;
    const geometry = new BufferGeometry();
    if (index.length > 0) {
        geometry.setIndex(new BufferAttribute(new Uint32Array(index), 1));
    }
    if (vertex.length > 0) {
        geometry.setAttribute('position', new BufferAttribute(new Float32Array(vertex), 3));
    }
    geometry.setAttribute('uv', new Float32BufferAttribute([], 2));
    geometry.computeVertexNormals();
    return geometry;
};

class MeshInfo {
    constructor({ name = '', numberOfVertex = 0, numberOfIndex = 0, vertex = [], index = [], support = null }) {
        this.name = name;
        this.numberOfVertex = numberOfVertex;
        this.numberOfIndex = numberOfIndex;
        this.vertex = vertex;
        this.index = index;
        this.support = support;
        this.geometry = null;
    }
}

class StxModel {
    constructor({
        headerName = '',
        stxVersion = 0,
        luxflowVersion = 0,
        imageBase64 = '',
        imageWidth = 0,
        imageHeight = 0,
        imageContent = '',
        numberOfMesh = 0,
        meshs = []
    }) {
        this.headerName = headerName;
        this.stxVersion = stxVersion;
        this.luxflowVersion = luxflowVersion;
        this.imageBase64 = imageBase64;
        this.imageWidth = imageWidth;
        this.imageHeight = imageHeight;
        this.imageContent = imageContent;
        this.numberOfMesh = numberOfMesh;
        this.mesh = [];
        this.setMesh(meshs);
        this.type = 'StxModel';
    }

    setMesh = (meshs) => {
        meshs.forEach((el) => {
            const { name, numberOfVertex, numberOfIndex, vertex, index, support, floors } = el;
            // support
            const supportMeshInfo = new MeshInfo({
                name: support.name,
                numberOfVertex: support.numberOfVertex,
                numberOfIndex: support.numberOfIndex,
                vertex: support.vertex,
                index: support.index,
                mesh: null
            });
            const supportGeometry = transferToGeometry({ vertex: support.vertex, index: support.index });
            supportGeometry.name = support.name;
            supportMeshInfo.geometry = supportGeometry;
            const meshinfo = new MeshInfo({
                name,
                numberOfVertex,
                numberOfIndex,
                vertex,
                index,
                support: support.numberOfVertex > 0 ? supportMeshInfo : null
            });
            const geometry = transferToGeometry({ vertex, index });
            geometry.name = name;
            meshinfo.geometry = geometry;

            const geoms = [];
            floors.forEach((ele) => {
                const supportFloorGeometry = transferToGeometry({ vertex: ele.vertex, index: ele.index });
                if (supportFloorGeometry) geoms.push(supportFloorGeometry);
            });

            const { generalSupports, parameters } = el;
            const supportMeshs = new SupportMesh();
            supportMeshs.setSupports(generalSupports);
            supportMeshs.setParameters(parameters);

            generalSupports.forEach((ele) => {
                const geom = supportMeshs.updateGeometry({ ele });
                if (geom) geoms.push(geom);
            });

            const supprtGeometry = geoms.length > 0 ? mergeGeometries(geoms, true) : null;
            meshinfo.supprtGeometry = supprtGeometry;

            this.mesh.push(meshinfo);
        });
        // this.showConsole();
    };

    showConsole = () => {
        console.log('文件名稱', this.headerName);
        console.log('stx版本', this.stxVersion);
        console.log('版本號', this.luxflowVersion);
        console.log('圖片號', this.imageBase64);
        console.log('圖片寬', this.imageWidth);
        console.log('圖片高', this.imageHeight);
        console.log('模型個數', this.numberOfMesh);
        console.log('模型', this.mesh);
    };
}
export default StxModel;