简体   繁体   English

如何使用three.js查看STL文件的固有颜色?

[英]How to view Intrinsic color of an STL file using three.js?

I have a binary STL colored file.我有一个二进制 STL 彩色文件。 I can see the color using the online mesh viewer http://www.viewstl.com/ .我可以使用在线网格查看器http://www.viewstl.com/查看颜色。

I am using the below standard approach to load and visualize the stl file, and it works well.我正在使用以下标准方法来加载和可视化 stl 文件,并且效果很好。 However, the intrisic colors do not appear correctly and are too sensivite to light changes.但是,内在颜色无法正确显示,并且对光线变化过于敏感。

Detector.addGetWebGLMessage();
scene = new THREE.Scene();
var aspect = window.innerWidth / window.innerHeight;
var d = 100;
camera = new THREE.OrthographicCamera( - d * aspect, d, d * aspect,- d, 1, 1000 );
camera.position.set( 0, 0, 200 );
camera.lookAt( scene.position ); 
scene.add( camera );
var light = new THREE.AmbientLight( 0x404040 ); // soft white light
scene.add( light );
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.x = 0; 
directionalLight.position.y = 0; 
directionalLight.position.z = 1; 
directionalLight.position.normalize();
scene.add( directionalLight );
var loader = new THREE.STLLoader();
var material = new THREE.MeshPhongMaterial( { color: 0xAAAAAA, specular: 0x111111, shininess: 0 } );
// Colored binary STL

var stlfile = "myBinarySTLColoredFile.stl"
loader.load( stlfile, function ( geometry ) {
    var meshMaterial = material;
    if (geometry.hasColors) {
        meshMaterial = new THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors });
    }
mesh = new THREE.Mesh( geometry, meshMaterial );
scene.add( 
mesh.position.set(-100, -100, 0);
});

renderer = new THREE.WebGLRenderer({ alpha: true,  antialias: true } ); 
renderer.setSize(....);

var container = document.getElementById('`enter code here`flex2');

Question: How to visualize the color enclosed in the STL file ?问题:如何可视化包含在 STL 文件中的颜色?

Thanks谢谢

I finally succeed to render correctly the colored object.我终于成功地正确渲染了彩色对象。

I use PLYLoader instead of STLLoader.我使用 PLYLoader 而不是 STLLoader。

Apparently, three.js STL api do not manage the color.显然,three.js STL api 不管理颜色。

There is no right answer.没有正确的答案。 The color will depend on the light.颜色将取决于光线。 See the clear thread Realistic lighting (sunlight) with Three.js? 使用 Three.js看到清晰的线程逼真的照明(阳光)?

To get the original colors and no directional lighting, remove the directional light and crank the ambient light up to full brightness:要获得原始颜色且没有定向照明,请移除定向光并将环境光调到最高亮度:

...
scene.add( camera );

var light = new THREE.AmbientLight( 0xFFFFFF ); // Full-bright white light
scene.add( light );

var loader = new THREE.STLLoader();
...

If that doesn't give you the desired result, please add screenshots of the actual and desired visual look to your question.如果这没有给您想要的结果,请在您的问题中添加实际和所需视觉外观的屏幕截图。

This is an old question, but for anyone else who comes across it, I don't think the accepted answer about using the PLYLoader instead of the STLLloader is correct, the STLLoader appears to support embedded color in binary STL s just fine in the example .这是一个老问题,但对于其他人谁越过它来了,我不认为有关使用公认的答案PLYLoader代替STLLloader是正确的, STLLoader似乎支持二进制嵌入颜色STL很好,没有问题的例子.

One possible cause of the OP's issue using THREE.VertexColors in THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors }) instead of true as in the docs/example . OP 问题的一个可能原因是在THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors })使用THREE.VertexColors而不是docs/example 中true

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

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