babylonjs note 05 build a village (08) Combining Meshes

116 阅读1分钟

Combining(结合) Meshes Using(使用) Merge(合并) Meshes

const combined = BABYLON.Mesh.MergeMeshes(Array_of_Meshes_to_Combine)
const house = BABYLON.Mesh.MergeMeshes([box, roof])

const house = BABYLON.Mesh.MergeMeshes([box, roof], true, false, null, false, true);
import { ArcRotateCamera, Color3, HemisphericLight, MeshBuilder, Scene, Texture, StandardMaterial, Vector3, Vector4, Mesh } from "@babylonjs/core";
import { createApp } from "../utils";

/**
 * Build Functions
 */
const buildGround = () => {
    // color
    const groundMat = new StandardMaterial('groundMat');
    groundMat.diffuseColor = new Color3(0, 1, 0);

    const ground = MeshBuilder.CreateGround('ground', { width: 10, height: 10, });
    ground.material = groundMat;

    return ground;
}

const buildBox = () => {
    // texture
    const boxMat = new StandardMaterial('boxMat');
    boxMat.diffuseTexture = new Texture('/assets/texture/ex01/cubehouse.png')

    //options parameter to set different images on each side
    const faceUV = [];
    faceUV[0] = new Vector4(0.5, 0.0, 0.75, 1.0); //rear face
    faceUV[1] = new Vector4(0.0, 0.0, 0.25, 1.0); //front face
    faceUV[2] = new Vector4(0.25, 0, 0.5, 1.0); //right side
    faceUV[3] = new Vector4(0.75, 0, 1.0, 1.0); //left side
    // top 4 and bottom 5 not seen so not set

     /**** World Objects *****/
     const box = MeshBuilder.CreateBox('box', {faceUV, wrap: true});
     box.material = boxMat;
     box.position.y = 0.5;

     return box;
}

const buildRoof = ()  => {
    const roofMaat = new StandardMaterial('roofMat');
    roofMaat.diffuseTexture = new Texture('/assets/texture/ex01/roof.jpg')


    const roof = MeshBuilder.CreateCylinder('roof', {diameter: 1.3, height: 1.2, tessellation: 3});
    roof.material = roofMaat;
    roof.scaling.x = 0.75;
    roof.rotation.z = Math.PI / 2;
    roof.position.y = 1.22;

    return roof;
}


const app = createApp((canvas, engine) => {
    const scene = new Scene(engine);

    /**** Set camera and light *****/
    const camera = new ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 15, Vector3.Zero(), scene);
    camera.attachControl(canvas, true);

    const light = new HemisphericLight('light', new Vector3(1, 1, 0), scene);

    // customize your scene here

    const ground = buildGround();
    const box = buildBox();
    const roof = buildRoof();

    const house = Mesh.MergeMeshes([box, roof], true, false, null, false, true);

    return scene;
})

app.run();

image.png