Skip to content

Instantly share code, notes, and snippets.

@Trass3r
Created April 5, 2023 15:26
Show Gist options
  • Save Trass3r/d461ca4fab81c2ab9c9580c19e81fe2c to your computer and use it in GitHub Desktop.
Save Trass3r/d461ca4fab81c2ab9c9580c19e81fe2c to your computer and use it in GitHub Desktop.
var createScene = function() {
var scene = new BABYLON.Scene(engine);
var camera = new BABYLON.ArcRotateCamera("Camera", - Math.PI / 2, Math.PI / 4, 10, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, true);
var light1 = new BABYLON.DirectionalLight("DirectionalLight", new BABYLON.Vector3(0, -1, 1), scene);
var light2 = new BABYLON.HemisphericLight("HemiLight", new BABYLON.Vector3(0, 1, 0), scene);
light1.intensity =0.75;
light2.intensity =0.5;
//var box = BABYLON.MeshBuilder.CreateBox("box", {}, scene);
// Create a beveled cube
var size = 2;
var bevelSize = 0.1;
var corners = [
new BABYLON.Vector3(-size/2 + bevelSize, -size/2, -size/2),
new BABYLON.Vector3(size/2 - bevelSize, -size/2, -size/2),
new BABYLON.Vector3(-size/2, -size/2 + bevelSize, -size/2),
new BABYLON.Vector3(-size/2, size/2 - bevelSize, -size/2),
new BABYLON.Vector3(-size/2 + bevelSize, size/2, -size/2),
new BABYLON.Vector3(size/2 - bevelSize, size/2, -size/2),
new BABYLON.Vector3(size/2, size/2 - bevelSize, -size/2),
new BABYLON.Vector3(size/2, -size/2 + bevelSize, -size/2),
new BABYLON.Vector3(-size/2 + bevelSize, -size/2, size/2),
new BABYLON.Vector3(size/2 - bevelSize, -size/2, size/2),
new BABYLON.Vector3(-size/2, -size/2 + bevelSize, size/2),
new BABYLON.Vector3(-size/2, size/2 - bevelSize, size/2),
new BABYLON.Vector3(-size/2 + bevelSize, size/2, size/2),
new BABYLON.Vector3(size/2 - bevelSize, size/2, size/2),
new BABYLON.Vector3(size/2, size/2 - bevelSize, size/2),
new BABYLON.Vector3(size/2, -size/2 + bevelSize, size/2)
];
var indices = [
0, 2, 1,
1, 2, 3,
2, 4, 3,
3, 4, 5,
4, 6, 5,
5, 6, 7,
6, 8, 7,
7, 8, 9,
8, 10, 9,
9, 10, 11,
10, 12, 11,
11, 12, 13,
12, 14, 13,
13, 14, 15,
14, 0, 15,
15, 0, 1,
1, 3, 15,
3, 5, 15,
5, 7, 15,
7, 9, 15,
9, 11, 15,
11, 13, 15,
13, 15, 1
];
var normals = [];
const colors = [];
for (var i = 0; i < indices.length; i += 3) {
var a = corners[indices[i]];
var b = corners[indices[i + 1]];
var c = corners[indices[i + 2]];
var normal = BABYLON.Vector3.Cross(b.subtract(a), c.subtract(a)).normalize();
normals.push(normal, normal, normal);
colors.push(new BABYLON.Color4(1, 0, 0, 1), new BABYLON.Color4(0, 1, 0, 1), new BABYLON.Color4(0, 0, 1, 1));
}
if (false) {
var box = new BABYLON.Mesh("beveledCube", scene);
box.setVerticesData(BABYLON.VertexBuffer.PositionKind, corners);
box.setIndices(indices);
box.setVerticesData(BABYLON.VertexBuffer.ColorKind, colors);
box.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
} else {
var vertexData = new BABYLON.VertexData();
vertexData.positions = corners;
vertexData.indices = indices;
vertexData.normals = normals;
vertexData.colors = colors;
var box = new BABYLON.Mesh("beveledCubeMesh", scene);
vertexData.applyToMesh(box);
}
const material = new BABYLON.StandardMaterial("boxMat", scene);
material.diffuseColor = new BABYLON.Color4(1, 0, 0, 1);
material.wireframe = true;
box.material = material;
return scene;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment