Skip to content

Commit

Permalink
Getting Closer
Browse files Browse the repository at this point in the history
  • Loading branch information
polyhobbyist committed Sep 22, 2023
1 parent 41767e8 commit 200800c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 37 deletions.
16 changes: 10 additions & 6 deletions src/GeometryBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Box implements IGeometry {
public depth : number = 0;


public mesh: BABYLON.Mesh | undefined = undefined;
public meshes: BABYLON.Mesh[] = [];
public transform : BABYLON.TransformNode | undefined;

constructor(x : number, y: number, z: number) {
Expand All @@ -26,21 +26,25 @@ export class Box implements IGeometry {
public create(scene: BABYLON.Scene, mat : Material | undefined) : void {
this.transform = new BABYLON.TransformNode("mesh_box", scene);

this.mesh = BABYLON.MeshBuilder.CreateBox("box",
this.meshes.push(BABYLON.MeshBuilder.CreateBox("box",
{
width: this.width,
height: this.height,
depth: this.depth,
}, scene);
}, scene));

this.mesh.parent = this.transform;
this.meshes[0].parent = this.transform;
if (mat != undefined && mat.material != undefined) {
this.mesh.material = mat.material;
this.meshes[0].material = mat.material;
}
}

public dispose() : void {
this.mesh?.dispose();
if (this.meshes != undefined) {
this.meshes.forEach(m => {
m.dispose();
});
}
this.transform?.dispose();
}

Expand Down
18 changes: 11 additions & 7 deletions src/GeometryCylinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Cylinder implements IGeometry {
public radius : number = 0;


public mesh: BABYLON.AbstractMesh | undefined = undefined;
public meshes: BABYLON.AbstractMesh[] = [];
public transform : BABYLON.TransformNode | undefined;

constructor(l : number, r: number) {
Expand All @@ -18,20 +18,24 @@ export class Cylinder implements IGeometry {
public create(scene: BABYLON.Scene, mat : Material | undefined) : void {
this.transform = new BABYLON.TransformNode("mesh_cylinder", scene);

this.mesh = BABYLON.MeshBuilder.CreateCylinder("cylinder",
this.meshes.push(BABYLON.MeshBuilder.CreateCylinder("cylinder",
{
diameter: this.radius * 2.0,
height: this.length
}, scene);
}, scene));

this.mesh.parent = this.transform;
this.mesh.addRotation(Math.PI / 2.0, 0, 0);
this.meshes[0].parent = this.transform;
this.meshes[0].addRotation(Math.PI / 2.0, 0, 0);
if (mat != undefined && mat.material != undefined) {
this.mesh.material = mat.material;
this.meshes[0].material = mat.material;
}
}
public dispose() : void {
this.mesh?.dispose();
if (this.meshes != undefined) {
this.meshes.forEach(m => {
m.dispose();
});
}
this.transform?.dispose();
}
}
47 changes: 33 additions & 14 deletions src/GeometryMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class Mesh implements IGeometry {
public uri: string = "";
public scale: BABYLON.Vector3 = new BABYLON.Vector3(1.0, 1.0, 1.0);

public mesh: BABYLON.AbstractMesh | undefined = undefined;
public meshes: BABYLON.AbstractMesh[] | undefined = undefined;
public transform : BABYLON.TransformNode | undefined = undefined;
public material : Material | undefined = undefined;

Expand All @@ -18,18 +18,33 @@ export class Mesh implements IGeometry {
this.scale = scale;
}

private meshCallback(mesh : BABYLON.AbstractMesh[]) {
private meshCallback(scene: BABYLON.Scene, meshes : BABYLON.AbstractMesh[], particleSystems : BABYLON.IParticleSystem[] | undefined, skeletons : BABYLON.Skeleton[] | undefined) {
// Get a pointer to the mesh
if (mesh.length > 0) {
this.mesh = mesh[0];
if (this.mesh != null &&
this.transform != undefined) {
this.mesh.parent = this.transform;
this.mesh.scaling = this.scale;
this.mesh.addRotation(0, 0, Math.PI).addRotation(Math.PI/2, 0, 0);
if (this.material != undefined && this.material.material != undefined) {
this.mesh.material = this.material.material;
if (meshes.length > 0 && this.transform != undefined) {
this.meshes = meshes;

// find the top level bone in skeletons
if (skeletons != undefined && skeletons.length > 0) {
let rootBone = skeletons[0].bones.find(b => b.getParent() == undefined);
if (rootBone != undefined) {
let t = rootBone.getTransformNode();
if (t != undefined) {
t.parent = this.transform;
//t.addRotation(0, 0, Math.PI).addRotation(Math.PI/2, 0, 0);
}
}
} else {

this.meshes.forEach(m => {
if (this.transform != undefined) {
m.addRotation(0, 0, Math.PI).addRotation(Math.PI/2, 0, 0);
m.parent = this.transform;
m.scaling = this.scale;
if (this.material != undefined && this.material.material != undefined) {
m.material = this.material.material;
}
}
});
}
}
}
Expand All @@ -50,18 +65,22 @@ export class Mesh implements IGeometry {
var meshdata = readFileSync(filePath).toString('base64');

// Force the file to be read as base64 encoded data blob
BABYLON.SceneLoader.ImportMesh(null, "", "data:;base64," + meshdata, scene, (mesh) => {this.meshCallback(mesh)}, null, null, fileExtension);
BABYLON.SceneLoader.ImportMesh(null, "", "data:;base64," + meshdata, scene, (mesh, ps, sk) => {this.meshCallback(scene, mesh, ps, sk)}, null, null, fileExtension);
} else {
let filename = this.uri.substring(this.uri.lastIndexOf('/') + 1);
if (filename) {
let base = this.uri.substring(0, this.uri.lastIndexOf('/') + 1);
BABYLON.SceneLoader.ImportMesh(null, base, filename, scene, (mesh) => {this.meshCallback(mesh)});
BABYLON.SceneLoader.ImportMesh(null, base, filename, scene, (mesh, ps, sk) => {this.meshCallback(scene, mesh, ps, sk)});
}
}
}

public dispose(): void {
this.mesh?.dispose();
if (this.meshes != undefined) {
this.meshes.forEach(m => {
m.dispose();
});
}
this.transform?.dispose();
}
}
16 changes: 10 additions & 6 deletions src/GeometrySphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Sphere implements IGeometry {
public radius : number = 0;


public mesh: BABYLON.AbstractMesh | undefined = undefined;
public meshes: BABYLON.AbstractMesh[] = [];
public transform : BABYLON.TransformNode | undefined;

constructor(r: number) {
Expand All @@ -17,19 +17,23 @@ export class Sphere implements IGeometry {
public create(scene: BABYLON.Scene, mat : Material | undefined) : void {
this.transform = new BABYLON.TransformNode("mesh_sphere", scene);

this.mesh = BABYLON.MeshBuilder.CreateSphere("sphere",
this.meshes.push(BABYLON.MeshBuilder.CreateSphere("sphere",
{
diameter: this.radius * 2.0,
}, scene);
}, scene));

this.mesh.parent = this.transform;
this.meshes[0].parent = this.transform;
if (mat != undefined && mat.material != undefined) {
this.mesh.material = mat.material;
this.meshes[0].material = mat.material;
}
}

public dispose() : void {
this.mesh?.dispose();
if (this.meshes != undefined) {
this.meshes.forEach(m => {
m.dispose();
});
}
this.transform?.dispose();
}
}
2 changes: 1 addition & 1 deletion src/IGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as BABYLON from 'babylonjs';
import { Material } from './Material';

export interface IGeometry {
mesh : BABYLON.AbstractMesh | undefined;
meshes : BABYLON.AbstractMesh[] | undefined;
transform : BABYLON.TransformNode | undefined;

create(scene: BABYLON.Scene, mat : Material | undefined) : void;
Expand Down
6 changes: 3 additions & 3 deletions src/Render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var createScene = async function (engine : BABYLON.Engine, canvas : HTMLCanvasEl
scene.useRightHandedSystem = true;
scene.clearColor = new BABYLON.Color4(0.4, 0.4, 0.4, 1.0);// TODO (polyhobbyist) Make this configurable

var radius = 30; // TODO (polyhobbyist): make this configurable
var radius = 10; // TODO (polyhobbyist): make this configurable

// This creates and positions a free camera (non-mesh)
var camera = new BABYLON.ArcRotateCamera("camera1", - Math.PI / 3, 5 * Math.PI / 12, radius, new BABYLON.Vector3(0, 0, 0), scene);
Expand Down Expand Up @@ -217,7 +217,7 @@ export async function RenderMain() {
<link name="base_link">
<visual>
<geometry>
<mesh filename="https://raw.githubusercontent.com/polyhobbyist/babylon-collada-loader/main/test/testdata/leo.dae" scale="0.001 0.001 0.001" />
<mesh filename="https://raw.githubusercontent.com/polyhobbyist/babylon-collada-loader/main/test/testdata/Chassis.dae" scale="1 1 1" />
</geometry>
</visual>
</link>
Expand All @@ -227,7 +227,7 @@ export async function RenderMain() {
const canvas = document.getElementById("renderCanvas") as HTMLCanvasElement; // Get the canvas element
const engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
let scene = await createScene(engine, canvas);
//scene.debugLayer.show();
scene.debugLayer.show();

let robot = await applyURDF(scene, u);

Expand Down

0 comments on commit 200800c

Please sign in to comment.