Yes, I am using earcut for Polygon mesh.
My point is, It is for weird to me. I have to import “earcut” everytime when I use PolygonMesh or like else.
Is there any other way to skip param for ExtrudePolygon function??
Here is my code. some changes from offical tutorial.
Thanks for your help
import {
CreateCylinder,
MeshBuilder,
Vector3,
Vector4,
Texture,
StandardMaterial,
Animation,
} from "@babylonjs/core";
import { ExtrudePolygon } from "@babylonjs/core";
import earcut from "earcut";
export default function Car(name: string, scene?: any) {
// mashes
const sample = Array(24)
.fill(null)
.map((_, index) => {
if (index === 0) return new Vector3(-0.3, 0, -0.1);
if (index === 1) return new Vector3(0.2, 0, -0.1);
if (index === 22) return new Vector3(0, 0, 0.1);
if (index === 23) return new Vector3(-0.3, 0, 0.1);
const weight = index - 2;
return new Vector3(
0.2 * Math.cos((weight * Math.PI) / 40),
0,
0.2 * Math.sin((weight * Math.PI) / 40) - 0.1
);
});
// materials
const carMat = new StandardMaterial("carMat");
carMat.diffuseTexture = new Texture(
"https://assets.babylonjs.com/environments/car.png"
);
const faceUV = [
new Vector4(0, 0.5, 0.38, 1),
new Vector4(0, 0, 1, 0.5),
new Vector4(0.38, 1, 0, 0.5),
];
/* I want to write this line like
const car = ExtrudePolygon(name,{shape:sample, depth:0.2,wrap:true,faceUV:faceUV},scene)
*/
const car = ExtrudePolygon(
name,
{ shape: sample, depth: 0.2, wrap: true, faceUV: faceUV },
scene,
earcut
);
car.material = carMat;
// wheel
const wheelMat = new StandardMaterial("wheelMat");
wheelMat.diffuseTexture = new Texture(
"https://assets.babylonjs.com/environments/wheel.png"
);
const wheelRB = CreateCylinder(`${name}_wheelRB`, {
diameter: 0.125,
height: 0.05,
faceUV: [
new Vector4(0, 0, 1, 1),
new Vector4(0, 0.5, 0, 0.5),
new Vector4(0, 0, 1, 1),
],
});
wheelRB.material = wheelMat;
wheelRB.parent = car;
wheelRB.position.z = -0.1;
wheelRB.position.x = -0.2;
wheelRB.position.y = 0.035;
//animation
const animWheel = new Animation(
"wheelAnimation",
"rotation.y",
30,
Animation.ANIMATIONTYPE_FLOAT,
Animation.ANIMATIONLOOPMODE_CYCLE
);
animWheel.setKeys([
{ frame: 0, value: 0 },
{ frame: 30, value: 2 * Math.PI },
]);
wheelRB.animations = [animWheel];
const wheelRF = wheelRB.clone(`${name}_wheelRF`);
wheelRF.position.x = 0.1;
const wheelLB = wheelRB.clone(`${name}_wheelLB`);
wheelLB.position.y = -0.2 - 0.035;
const wheelLF = wheelRF.clone(`${name}_wheelLF`);
wheelLF.position.y = -0.2 - 0.035;
return car;
}