Script TS TypeScript Free

Physics Cruiser

Physics Cruiser picture
Author
matas
Created
Nov 11, 2023, 9:07:42 AM
Last edit
Apr 29, 2024, 7:42:19 AM

Edit scriptPreview

Project

Alien Nebula Cruiser

This is a conceptual design of advanced spacecraft that shows off some of the new algorithms of the platform, such as splitting the wire into multiple pieces, selecting geometry by using boolean pattern matching, and adopting advanced lofting techniques.

View full project
const scene = bitbybit.babylon.scene.getScene();

scene.activeCamera.position = new BABYLON.Vector3(-40, 10, -40)

const start = async () => {

    const files = [
        "alien-nebula-cruiser-1.glb",
        "alien-nebula-collision-body-1.glb",
    ]

    const gltfFiles = await Promise.all(files.map(fileName => bitbybit.asset.getFile({ fileName })));
    const meshes = await Promise.all(gltfFiles.map(assetFile => bitbybit.babylon.io.loadAssetIntoScene({ assetFile, hidden: true })));
    const meshSpaceship = meshes[0];
    meshSpaceship.position = new BABYLON.Vector3(0, 50, 0);

    const meshSpadceshipCollisionBody = meshes[1];
    const collisionBody = meshSpadceshipCollisionBody.getChildMeshes().find((m) => m.name.includes("surface")) as BABYLON.Mesh;
    collisionBody.position = new BABYLON.Vector3(0, 50, 0);
    collisionBody.isVisible = false;
    const groundMesh = await createBox();
    const groundCollisionMesh = groundMesh.getChildMeshes().find((m) => m.name.includes("surface")) as BABYLON.Mesh;

    bitbybit.babylon.scene.enablePhysics({ vector: [0, -9.81, 0] });

    const size = 20;
    const step = 20;
    for (let x = -size; x <= size; x += step) {
        for (let y = -size; y < size * 4; y += step) {
            for (let z = -size; z <= size; z += step) {
                const parentMesh = new BABYLON.Mesh(`${x}-${y}-${z}`, scene);
                meshSpaceship.getChildMeshes().forEach((mesh: BABYLON.Mesh) => {
                    if (mesh.name !== "__root__") {
                        const inst = mesh.createInstance(`${x}-${y}-${z}-instance`);
                        inst.parent = parentMesh;
                    }
                });

                parentMesh.position = new BABYLON.Vector3(x, y + (size * 2), z);
                const collisionBodyMesh = collisionBody.clone(`${x}-${y}-${z}-collision`);
                new BABYLON.PhysicsAggregate(parentMesh, BABYLON.PhysicsShapeType.MESH, { mesh: collisionBodyMesh, mass: 3.75, restitution: 0.75 }, scene);
            }
        }
    }

    new BABYLON.PhysicsAggregate(groundMesh, BABYLON.PhysicsShapeType.MESH, { mesh: groundCollisionMesh, mass: 0, restitution: 0.75 }, scene);
    meshSpaceship.getChildMeshes().forEach(m => m.isVisible = false);
    const lightOpt = new Bit.Inputs.BabylonScene.DirectionalLightDto();
    lightOpt.direction = [-100, -500, -100];
    lightOpt.shadowGeneratorMapSize = 2056;
    bitbybit.babylon.scene.drawDirectionalLight(lightOpt)
}

start();


async function createBox() {

    const box = await bitbybit.occt.shapes.solid.createBox({
        width: 100,
        length: 100,
        height: 30,
        center: [0, 13, 0],
    })

    const boxCutout = await bitbybit.occt.shapes.solid.createBox({
        width: 90,
        length: 90,
        height: 30,
        center: [0, 15, 0],
    })

    const diff = await bitbybit.occt.booleans.difference({
        shape: box,
        shapes: [boxCutout],
        keepEdges: false
    })

    const boxDrawOptions = new Bit.Inputs.Draw.DrawOcctShapeOptions();
    boxDrawOptions.precision = 0.1;
    boxDrawOptions.faceColour = "#ff00ff";
    const boxMesh = await bitbybit.draw.drawAnyAsync({
        entity: diff,
        options: boxDrawOptions
    })

    return boxMesh

}

Plans and pricing

Choose your plan

Editor plans for 3D development, API keys for server-side CAD algorithms

EnterpriseB2BCustompricing

Custom software development, dedicated servers and CAD automation at scale.

Learn moreContact us

CAD automation and software
  • Custom software development
  • Cloud CAD automation pipelines
  • 3D configurators (STEP & GLTF)
  • Batch export jobs
  • Custom algorithms & deployment
Infrastructure and support
  • Custom compute allocation
  • Dedicated / VPS server tenants
  • Long-running computation jobs
  • Custom upload limits & overage
  • SLA & premium support

All prices exclude applicable taxesView all plan details

Contact

Talk to us

Tell us what you are building or what is blocking you, through whichever channel suits you.