Script TS TypeScript Free

The Orb

The Orb picture
Author
matas
Created
Nov 7, 2023, 2:58:30 PM
Last edit
Dec 5, 2023, 2:07:59 PM

Edit scriptPreview

Project

Creative Coding Floating 3D Orb

This project contains example of how to use some of our CAD algorithms in real time render loops

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

const pipeline = scene.postProcessRenderPipelineManager.supportedPipelines.find(s => s.name === "default");
if (!pipeline) {
    var defaultPipeline = new BABYLON.DefaultRenderingPipeline("default", true, scene, [scene.activeCamera]);
    defaultPipeline.bloomEnabled = true;
    defaultPipeline.fxaaEnabled = true;
    defaultPipeline.bloomWeight = 1;
}


const adjOpt = new Bit.Inputs.BabylonScene.CameraConfigurationDto();
adjOpt.position = [6, 0, -6];
adjOpt.lookAt = [0, 0, 0];
adjOpt.panningSensibility = 400;
adjOpt.wheelPrecision = 3;
bitbybit.babylon.scene.adjustActiveArcRotateCamera(adjOpt)

type Point3 = Bit.Inputs.Base.Point3;

const s1: Point3 = [1, 0, 0];
const s2: Point3 = [1, 0, 1];
const s3: Point3 = [-1, 0, 1];
const s4: Point3 = [-1, 0, -1];

const d1: Point3 = [2, 0.5, 0];
const d2: Point3 = [1.5, 0.5, 2];
const d3: Point3 = [-1, 0.6, 1.2];
const d4: Point3 = [-1.5, 0.3, -1.3];

const c1: Point3 = [3, 0, 0];
const c2: Point3 = [3.5, 0, 3];
const c3: Point3 = [-2, 0, 2.2];
const c4: Point3 = [-2.5, 0, -2.3];

let resolved = true;
let lastWireMesh: BABYLON.Mesh[] = [];
let lastInstances: BABYLON.InstancedMesh[] = [];

let step1 = 0.0004;
let step2 = -0.0008;
let step3 = -0.0012;
let step4 = -0.0016;

let val1 = -1;
let val2 = -3;
let val3 = -2;
let val4 = 1;


const rotationVec = new BABYLON.Vector3(0, 1, 0);
const rotationPoint = new BABYLON.Vector3(0, 0, 0);
let rotation = 0.01;

const optWireDraw = new Bit.Inputs.Draw.DrawOcctShapeOptions();
optWireDraw.precision = 0.04;
optWireDraw.edgeWidth = 10;
optWireDraw.edgeOpacity = 0.3;
optWireDraw.edgeColour = "#ff44ff";
optWireDraw.drawFaces = false;

const instanceEdgesColor = new BABYLON.Color4(1, 1, 1, 1)

const nrDivisions = 70;

const divideToPointsOptions = new Bit.Inputs.OCCT.DivideShapesDto<Bit.Inputs.OCCT.TopoDSWirePointer>(undefined, nrDivisions, true, false);

let indexCounter = 0;

bitbybit.time.registerRenderFunction(() => {
    if (indexCounter % 1000 === 0) {
        step1 *= -1
    }

    if (indexCounter % 2500 === 0) {
        step2 *= -1
    }

    if (indexCounter % 500 === 0) {
        step4 *= -1
    }

    if (indexCounter % 1500 === 0) {
        step3 *= -1
    }

    val1 += step1;
    val2 += step2;
    val3 += step3;
    val4 += step4;

    s1[1] = val1;
    s2[1] = val1;
    s3[1] = val1;
    s4[1] = val1;

    d1[1] = val4;
    d2[1] = val2;
    d3[1] = val4;
    d4[1] = val2;

    c1[1] = val3;
    c2[1] = -val1;
    c3[1] = val2;
    c4[1] = -val3;

    if (resolved) {
        start();
    }

    indexCounter++;
    rotation += 0.001;
});

async function start() {

    resolved = false;

    await bitbybit.occt.cleanAllCache();


    const intDto: Bit.Inputs.OCCT.InterpolateWiresDto = {
        interpolations: [
            {
                points: [s1, s2, s3, s4],
                periodic: true,
                tolerance: 0.1
            },
            {
                points: [d1, d2, d3, d4],
                periodic: true,
                tolerance: 0.1
            },
            {
                points: [c1, c2, c3, c4],
                periodic: true,
                tolerance: 0.1
            }
        ],
        returnCompound: false
    }

    const wrs = await bitbybit.occt.shapes.wire.interpolateWires(intDto) as Bit.Inputs.OCCT.TopoDSWirePointer[];

    divideToPointsOptions.shapes = wrs;
    const ptsOnWires = await bitbybit.occt.shapes.wire.divideWiresByParamsToPoints(divideToPointsOptions)

    const interpDto: Bit.Inputs.OCCT.InterpolateWiresDto = {
        interpolations: [],
        returnCompound: true
    };

    const flipped = bitbybit.lists.flipLists({
        list: ptsOnWires
    });
    flipped.forEach((pw) => {
        interpDto.interpolations.push({
            points: [...pw],
            periodic: true,
            tolerance: 0.1
        });

    });

    const wires = await bitbybit.occt.shapes.wire.interpolateWires(interpDto) as Bit.Inputs.OCCT.TopoDSCompoundPointer;

    const mesh = await bitbybit.draw.drawAnyAsync({
        entity: wires,
        options: optWireDraw
    });

    if (mesh) {
        mesh.rotateAround(rotationPoint, rotationVec, rotation);

        lastInstances.forEach(i => {
            i.dispose();
        });

        const children = mesh.getChildMeshes() as BABYLON.LinesMesh[];

        const instance = children[0].createInstance("cool");
        instance.enableEdgesRendering();
        instance.edgesColor = instanceEdgesColor;
        instance.edgesWidth = 2;
        instance.rotateAround(rotationPoint, rotationVec, rotation);
        lastInstances = [instance];

        if (lastWireMesh && lastWireMesh.length > 0) {
            lastWireMesh.forEach(s => {
                s.dispose();
            })
        }

        lastWireMesh.push(mesh);
    }

    await bitbybit.occt.deleteShapes({ shapes: [...wrs, wires] })
    resolved = true;


}

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.