简体   繁体   English

使用three.js渲染.stl文件时自动缩放

[英]Auto-scale when rendering an .stl file with three.js

I'm trying to create a server/webpage that works as follows: 我正在尝试创建一个服务器/网页,其工作方式如下:

  1. Client uploads an .stl file that they want 3D-printed 客户端上传他们要进行3D打印的.stl文件
  2. Server runs a mesh repair script on the .stl file 服务器在.stl文件上运行网格修复脚本
  3. The newly repaired .stl file is rendered in the client's browser so they can confirm that the script didn't alter the .stl file in a bad way 新修复的.stl文件在客户端的浏览器中呈现,因此他们可以确认脚本没有以不良方式更改.stl文件。

The problem I'm having is with step 3. I'm trying to use this example from the three.js repo to render the .stl file: 我遇到的问题与步骤3有关。我正在尝试使用three.js存储库中的以下示例来呈现.stl文件:

https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_stl.html https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_stl.html

The problem is, if the model in the .stl file is too large you can't see the entire thing. 问题是,如果.stl文件中的模型太大,您将看不到整个内容。 I can fix this so that the entire model is in view of the camera by playing around with the three parameters in the mesh.scale.set(x, y, z) function in this code block: 我可以通过在此代码块中使用mesh.scale.set(x,y,z)函数中的三个参数来解决这个问题,以便整个模型都在摄像机的视野内:

var loader = new THREE.STLLoader();
loader.addEventListener( 'load', function ( event ) {

    var geometry = event.content;
    var mesh = new THREE.Mesh( geometry, material );

    mesh.position.set( 0, - 0.37, 0 );
    mesh.rotation.set( - Math.PI / 2, 0, 0 );
    mesh.scale.set( 2, 2, 2 );

    mesh.castShadow = true;
    mesh.receiveShadow = true;

    scene.add( mesh );

} );
loader.load( './models/stl/binary/pr2_head_tilt.stl' );

However, I would like to know how to automatically recognize the size of the model in the .stl file, and scale it accordingly. 但是,我想知道如何自动识别.stl文件中模型的大小,并据此进行缩放。

I know it is possible, as GitHub has achieved this with their STL file viewer. 我知道这是可能的,因为GitHub已通过其STL文件查看器实现了这一目标。 You can see how it works by navigating here and clicking on one of the .stl files: 您可以通过在此处导航并单击.stl文件之一来查看其工作方式:

https://github.com/mrdoob/three.js/tree/master/examples/models/stl/binary https://github.com/mrdoob/three.js/tree/master/examples/models/stl/binary

Basically the GitHub STL file viewer is exactly what I want to emulate, since it loads any .stl file cleanly without requiring the user to zoom-in/out to properly view the model. 基本上,GitHub STL文件查看器正是我要模拟的东西,因为它可以干净地加载任何.stl文件,而无需用户放大/缩小以正确查看模型。

How much of a model is visible in your viewport does depend on your camera parameters and how you have set up your scene. 在视口中可见的模型的多少确实取决于相机参数以及场景的设置方式。 You can use the .boundingBox or .boundingSphere of the Geometry to figure out how big the model is and then use some heuristic to scale it down or up appropriately to what you would like to achieve. 您可以使用Geometry的.boundingBox或.boundingSphere找出模型的大小,然后使用启发式方法将其适当缩小或放大至您想要实现的大小。 There is also a BoundingBoxHelper that can help you visualize the extends of your model. 还有一个BoundingBoxHelper可以帮助您可视化模型的扩展。

In addition you can look at: How to Fit Camera to Object and Adjusting camera for visible Three.js shape 此外,您还可以查看: 如何将摄像头适配到对象以及如何 调整摄像头的可见Three.js形状

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM