Make PC building simulator in 3D using babylon.js, user should connect everything…
- User
Make PC building simulator in 3D using babylon.js, user should connect everything dragging components, provide all in one file
- Assistant
Make PC building simulator in 3D using babylon.js, user should connect everything dragging components, provide all in one file
Certainly! Below is an HTML file that creates a simple 3D PC building simulator using Babylon.js. In this simulator, users can drag and connect basic components like a motherboard, CPU, GPU, and RAM. All the code is provided in one file. Just save it with a .html extension and open it in a web browser.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>3D PC Building Simulator</title>
<!-- Include Babylon.js from CDN -->
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<!-- Include Babylon.js GUI for user interactions -->
<script src="https://cdn.babylonjs.com/gui/babylon.gui.min.js"></script>
<style>
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
</head>
<body>
<canvas id="renderCanvas"></canvas> <!-- Canvas for Babylon.js -->
<script>
// Get the canvas element
const canvas = document.getElementById("renderCanvas");
// Generate the Babylon.js 3D engine
const engine = new BABYLON.Engine(canvas, true);
// Create the scene
const createScene = function () {
const scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3(0.8, 0.8, 0.8);
// Add a camera to the scene and attach it to the canvas
const camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 4, 10, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, true);
// Add lights to the scene
const light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
// Create ground
const ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 10, height: 10}, scene);
// Function to create components
const createComponent = function(name, color, position) {
// Create a box to represent the component
const box = BABYLON.MeshBuilder.CreateBox(name, {height: 0.2, width: 1, depth: 1}, scene);
box.position = position;
// Add color material
const mat = new BABYLON.StandardMaterial(name + "Mat", scene);
mat.diffuseColor = color;
box.material = mat;
// Enable dragging
box.actionManager = new BABYLON.ActionManager(scene);
box.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickDownTrigger, function (evt) {
box.isPicked = true;
}));
box.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickUpTrigger, function (evt) {
box.isPicked = false;
}));
// Handle dragging
scene.onPointerObservable.add(function (evt) {
if (box.isPicked && evt.pickInfo.hit && evt.pickInfo.pickedPoint) {
box.position.x = evt.pickInfo.pickedPoint.x;
box.position.z = evt.pickInfo.pickedPoint.z;
}
}, BABYLON.PointerEventTypes.POINTERMOVE);
return box;
};
// Create components
const motherboard = createComponent("Motherboard", new BABYLON.Color3(0, 0.5, 0), new BABYLON.Vector3(0, 0.1, 0));
const cpu = createComponent("CPU", new BABYLON.Color3(0.5, 0.5, 0.5), new BABYLON.Vector3(-2, 0.1, 0));
const gpu = createComponent("GPU", new BABYLON.Color3(0.5, 0, 0), new BABYLON.Vector3(2, 0.1, 0));
const ram = createComponent("RAM", new BABYLON.Color3(0, 0, 0.5), new BABYLON.Vector3(0, 0.1, -2));
// Function to check if components are connected
const checkConnection = function(component1, component2, distance) {
const dx = component1.position.x - component2.position.x;
const dz = component1.position.z - component2.position.z;
return Math.sqrt(dx * dx + dz * dz) < distance;
};
// Display connection status
const ui = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
const textBlock = new BABYLON.GUI.TextBlock();
textBlock.text = "Connect the components by dragging them onto the motherboard.";
textBlock.color = "white";
textBlock.fontSize = 24;
textBlock.top = "-45%";
ui.addControl(textBlock);
scene.registerBeforeRender(function() {
if (checkConnection(cpu, motherboard, 0.6)) {
textBlock.text = "CPU connected!";
} else if (checkConnection(gpu, motherboard, 0.6)) {
textBlock.text = "GPU connected!";
} else if (checkConnection(ram, motherboard, 0.6)) {
textBlock.text = "RAM connected!";
} else {
textBlock.text = "Connect the components by dragging them onto the motherboard.";
}
});
return scene;
};
const scene = createScene();
// Run the render loop
engine.runRenderLoop(function () {
scene.render();
});
// Resize the engine when the window is resized
window.addEventListener("resize", function () {
engine.resize();
});
</script>
</body>
</html>
Instructions:
Save the File: Copy the code above and paste it into a text editor. Save the file with a .html extension, for example, pc_builder.html.
Open in Browser: Open the saved HTML file in a modern web browser like Chrome, Firefox, or Edge.
Interact with the Simulator:
Features:
Notes:
Customization:
createComponent with different parameters.BABYLON.Color3 values to change component colors.BABYLON.MeshBuilder.CreateBox to change component sizes.Dependencies:
Browser Compatibility:
Enjoy building your virtual PC!
Not yet included in a published release.