THREE.js OBJLoader - 加载到几何体,操作,然后保存到BufferGeometry [英] THREE.js OBJLoader - load to Geometry, manipulate, then save to BufferGeometry

查看:294
本文介绍了THREE.js OBJLoader - 加载到几何体,操作,然后保存到BufferGeometry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定为什么我无法平滑用OBJLoader加载的阴影几何体。

I'm trying establish why I can't smooth shade geometry loaded with OBJLoader.

var loader = new THREE.OBJLoader(manager);
loader.load('/manmodel/js/man.obj', function (object, materials) {
    console.log(object);
    console.log(materials);
    man = object;
    man.traverse(function (child) {
        if (child instanceof THREE.Mesh) {
            child.geometry.computeFaceNormals();
            child.geometry.computeVertexNormals( true );
            child.material = new THREE.MeshPhongMaterial({
                color: 'white',
                shading: THREE.SmoothShading // <-----------  THIS IS THE PROBLEM
            });
        }
    });
    man.position.y = -10;
    scene.add(man);
});

结果如下:

< img src =https://i.stack.imgur.com/OtnQK.pngalt =显然是平面阴影>

如果我删除了行computeFaceNormals( )模型渲染相同。
如果我删除computeVertexNormals(true),对象渲染时没有光照(黑色) - 所以我知道这样做了。

If I remove the line computeFaceNormals() the model renders the same. If I remove computeVertexNormals( true ) the object renders with no lighting (black) - so I know this is doing something.

如果我更改颜色属性在这段代码的MeshPhongMaterial中,颜色发生了变化,所以我也知道这是有效的。

If I change the color attribute of the MeshPhongMaterial in this code, the color changes, so I also know that this is working.

我试过用顶点和普通助手来确定问题所在是,但它们失败,因为使用BufferGeometry时,面和顶点不会存储为数组。

I have tried to use the vertex and normal helpers to establish what the problem is, but they fail because with BufferGeometry the faces and verticices are not stored as arrays.

我还尝试修改man.obj文件,将's'值从'off'更改为1.这没有做任何事。

I have also tried modifying the man.obj file to change the 's' values from 'off' to 1. This did nothing.

由于我将为Blender中生成的不同人物加载几个.obj文件,目前每个大约2MB,我更喜欢在浏览器上进行着色而不是将其烘焙到文件,如果这会增加文件大小。

As I will be loading several .obj files for different human figures, generated in Blender, and each is currently around 2MB, I prefer to do the shading on the browser than to 'bake' it into the file if this would increase file size.

问题:我在这里遗漏了什么吗?或者,
有没有办法将.obj文件作为标准几何体加载,计算法线,应用着色,然后保存为BufferGeometry?

THE QUESTION(s): Am I missing something here? OR, Is there a way to load the .obj file as standard Geometry, compute the normals, apply the shading, and then save as BufferGeometry?

ps 。我可能还需要光线追踪的法线进一步向下。

ps. I may also need the normals for ray tracing further down the line.

推荐答案

最新版本的ObjLoader将.obj解析为出于性能原因的BufferGeometry。如果你回顾一下GitHub上的历史,你可以找到解析为Geometry的先前版本:

The most recent version of ObjLoader parses the .obj to a BufferGeometry for performance reasons. If you look back through the history on GitHub you can find the previous version which parses to Geometry:

https://github.com/mrdoob/three.js/blob/a321ba05f02ae3a1586a4060a53f5ad63b90729b/examples/js/loaders/OBJLoader.js

使用此加载.obj,然后您可以根据需要操作几何体,然后创建一个新的BufferGeometry并使用BufferGeometry将Geometry加载到其中.fromGeometry(geometry)以获得性能。我的工作很好。

Load your .obj using this and you can then manipulate the Geometry until you have it as you need it, then create a new BufferGeometry and load the Geometry into it using BufferGeometry.fromGeometry(geometry) in order to get the performance. I have this working nicely.

这篇关于THREE.js OBJLoader - 加载到几何体,操作,然后保存到BufferGeometry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆