javascript - 如何使用 babylonjs 或 threejs 预览 3D 中的 2D 平面对象?
问题描述
我正在使用fabricJS,并且在平面画布大小的矩形对象(基础)上几乎没有随机图形(例如圆形、正方形、多边形)。
我想以 3D 形式预览所有这些刻在主矩形对象上的图纸。
他们有任何可用的图书馆吗?我用谷歌搜索了一下,发现了 three.js 和 Babylon.js,但其中没有与我的要求类似的示例,即使我不知道这些库是否可行。
现在想在浏览器中使用 JS 库将其转换为 3D 预览。
请指导我。
解决方案
我认为您应该使用 Three.JS 而不是 BabylonJs,它更易于处理并且尺寸也更小。
这是一个示例代码,也许您看起来与此类似。
您所要做的就是从您的 FabricJS 中提取 SVG 并将其放入 ThreeJS。
// --- Init threejs scene
// ----------------------
const scene = new THREE.Scene();
const ratio = window.innerWidth / window.innerHeight;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
const camera = new THREE.PerspectiveCamera(100, ratio, 0.01, 1000);
camera.position.z = 300;
document.querySelector("body").appendChild(renderer.domElement);
// Resize and update camera
window.addEventListener('resize', function(e) {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Axes helper
const axesHelper = new THREE.AxesHelper(500);
scene.add(axesHelper);
// --- Main part, load and parse SVG
// ---------------------------------
const svgMarkup = document.querySelector('svg').outerHTML;
// SVG Loader is not a part of the main three.js bundle
// We need to load it separately, it is included in this pen's Settings > JavaScript
// https://threejs.org/docs/#examples/en/loaders/SVGLoader
const loader = new THREE.SVGLoader();
const svgData = loader.parse(svgMarkup);
// Group we'll use for all SVG paths
const svgGroup = new THREE.Group();
// When importing SVGs paths are inverted on Y axis
// it happens in the process of mapping from 2d to 3d coordinate system
svgGroup.scale.y *= -1;
const material = new THREE.MeshNormalMaterial();
// Loop through all of the parsed paths
svgData.paths.forEach((path, i) => {
const shapes = path.toShapes(true);
// Each path has array of shapes
shapes.forEach((shape, j) => {
// Finally we can take each shape and extrude it
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 20,
bevelEnabled: false
});
// Create a mesh and add it to the group
const mesh = new THREE.Mesh(geometry, material);
svgGroup.add(mesh);
});
});
// Meshes we got are all relative to themselves
// meaning they have position set to (0, 0, 0)
// which makes centering them in the group easy
// Get group's size
const box = new THREE.Box3().setFromObject(svgGroup);
const size = new THREE.Vector3();
box.getSize(size);
const yOffset = size.y / -2;
const xOffset = size.x / -2;
// Offset all of group's elements, to center them
svgGroup.children.forEach(item => {
item.position.x = xOffset;
item.position.y = yOffset;
});
// Finally we add svg group to the scene
scene.add(svgGroup);
// --- Animation loop
// ------------------
function animate() {
renderer.render(scene, camera);
// Rotate out group
svgGroup.rotation.y += 0.005;
requestAnimationFrame(animate);
}
animate();
svg {
display: none;
}
canvas {
display: block;
}
a {
font-family: Helvetica, Arial, sans-serif;
font-size: 15px;
padding: 10px 16px;
display: block;
position: fixed;
bottom: 0;
right: 0;
color: #ddd;
transition: all 0.25s;
}
a:hover {
color: #3498db;
}
<script src="https://unpkg.com/three@0.111.0/build/three.js"></script>
<script src="https://unpkg.com/three@0.111.0/examples/js/loaders/SVGLoader.js"></script>
<svg width="202px" height="202px" viewBox="0 0 202 202" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<path fill="none" d="M201,1 L201,201 L1,201 L1,1 L201,1 Z M53.27053,71 L37.6666667,71 L37.6666667,134.333333 L53.27053,134.333333 L53.27053,86.6038647 C59.2367133,86.879227 66.1207733,91.1014493 66.1207733,99.9130433 L66.1207733,134.333333 L80.4396133,134.333333 L80.4396133,99.9130433 C80.4396133,91.1014493 87.32367,86.879227 93.2898567,86.6038647 L93.2898567,134.333333 L110.63768,134.333333 L110.63768,86.6038647 C116.603863,86.879227 123.487923,91.1014493 123.487923,99.9130433 L123.487923,134.333333 L137.806763,134.333333 L137.806763,99.9130433 C137.806763,91.1014493 144.69082,86.879227 150.657003,86.6038647 L150.657003,134.333333 L166.26087,134.333333 L166.26087,71 L150.657003,71 C142.120773,71 133.859903,75.589372 130.647343,80.178744 C127.434783,75.589372 119.173913,71 110.63768,71 L93.2898567,71 C84.7536233,71 76.4927533,75.589372 73.2801933,80.178744 C70.0676333,75.589372 61.8067633,71 53.27053,71 Z" stroke="#979797"></path></svg>
推荐阅读
- bootstrap-4 - 有没有boostrap会影响对齐?
- sharepoint-online - 如何在线获取 SharePoint 的所有主题并设置特定主题
- ruby - 如何使用 capybara 查看本地存储和会话存储
- c# - 线程池 - 创建了 10 个线程?
- angular - 无法从 EJ2 多选下拉列表中获取所选记录
- c# - 在共享主机上发布使用 .net core 2.2 构建的网站
- android - Gradle 查看错误的 Maven 存储库
- vba - 类型不匹配尝试在集合中的对象中设置数据
- amazon-web-services - Amazon EC2 预留实例优化检查已禁用
- c# - LUIS 分数在调度刷新时重新对齐