Hi,
I have input file Json with contains List of Cube locations with positions(x,y,z) and which displaying as expected in 3D.
I need to convert and display in 2D, is there any approach available in Three.js
e.g.
Input Data
“locations”: [
{
“Id”: “1”,
“Name”: “1”,
“Status”: null,
“Velzon”: null,
“LocTypeCat”: null,
“LoadLevel”: null,
“Trvseq”: null,
“Pos”: {
“x”: 20.447,
“y”: 7.946,
“z”: -134.056
},
“Size”: {
“x”: 2.438,
“y”: 1.524,
“z”: 1.016
},
“CurQvl”: null,
“MaxQvl”: null,
“Loccod”: null,
“Contains”: [],
“Level”: null
},
{
“Id”: “2”,
“Name”: “2”,
“Status”: null,
“Velzon”: null,
“LocTypeCat”: null,
“LoadLevel”: null,
“Trvseq”: null,
“Pos”: {
“x”: 20.447,
“y”: 7.946,
“z”: -135.274
},
“Size”: {
“x”: 2.438,
“y”: 1.524,
“z”: 1.016
},
“CurQvl”: null,
“MaxQvl”: null,
“Loccod”: null,
“Contains”: [],
“Level”: null
}
]
Code :
scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
scene.fog = new THREE.Fog(0xffffff, 0, 6000);
//Camera
//const camera = new THREE.OrthographicCamera(canvas.clientWidth / - 2, canvas.clientWidth / 2, canvas.clientHeight / 2, canvas.clientHeight / - 2, 1, 1000);
const fov = 100;
const aspect = 2; // the canvas default
const near = 1;
const far = 20000;
camera = new THREE.PerspectiveCamera(fov, window.innerWidth / window.innerHeight, near, far);
camera.position.set(0, 100, 0);
camera.zoom = 1;
//Renderer
if (Detector.webgl) {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
preserveDrawingBuffer: true
});
} else {
renderer = new THREE.CanvasRenderer();
}
//Canvas
const canvas = document.getElementById('canvas');
canvas.appendChild(renderer.domElement);
controls = new OrbitControls(camera, renderer.domElement);
// to disable rotation
controls.enableRotate = false;
function drawLocations(locations) {
var positionX;
var positionY;
var positionZ;
var boxColor = "#ad8762";
locations.forEach(function (location) {
positionX = location?.pos?.x;
positionY = location?.pos?.y;
positionZ = location?.pos?.z;
if (location != null && location != undefined) {
var boxWidth = location?.size?.x;
var boxDepth = location?.size?.y;
var boxHeight = location?.size?.z;
createCube(boxColor, boxWidth, boxDepth, boxHeight, positionX, positionY, positionZ, location);
}
})
requestAnimationFrame(animate);
renderer.render(scene, camera);
controls.update();
window.addEventListener("resize", onWindowResize, false);
}
function createCube(colorCube, width, depth, height, x, y, z, location) {
const cubeSize = 1;
const cubeGeo = new THREE.PlaneGeometry(cubeSize, cubeSize, cubeSize);
var material = new THREE.MeshBasicMaterial({ color: colorCube, side: THREE.DoubleSide });
var cubeLocation = new THREE.Mesh(cubeGeo, material);
cubeLocation.position.set(x, y, z)
cubeLocation.rotation.x = THREE.Math.degToRad(-90);
scene.add(cubeLocation);
camera.position.y = 100;
}