简体   繁体   English

如何访问由OBJMTLLoader加载的OBJ的不同部分?

[英]How can I access different parts of an OBJ loaded with OBJMTLLoader?

I made a model of a building with 3DS Max where each room is a modified cube, I used OBJMTLLoader to load the OBJ file with his own MTL. 我用3DS Max制作了一个建筑物模型,其中每个房间都是一个修改过的立方体,我使用OBJMTLLoader用他自己的MTL加载OBJ文件。 The problem is that I need to emphasize certain rooms according to the user requirements. 问题是我需要根据用户要求强调某些房间。

I use dat.gui to display a menu where the user can check/uncheck rooms to remark them (I think I will change the room size or marterial in order to emphasize it). 我使用dat.gui来显示菜单,用户可以在其中检查/取消选中房间以对其进行标记(我想我会更改房间大小或材料以强调它)。

this is the code of my loader 这是我的装载机的代码

var loader = new THREE.OBJMTLLoader();
        loader.load( 'models/ed.obj', 'models/textures/ed.mtl', 

        //onLoad()
        function ( object ){
            contenido = object;
            contenido.position.set(0,0,0);
            contenido.position.y = -80;
            contenido.name = 'edificio';

            scene.add(contenido);

            return contenido;
        } //and other onLoad, and onError stuff

I can play with the whole model, I have a transparency function that adjust transparency to a dat.gui bar (the one called opciones.Opacidad) 我可以使用整个模型,我有一个透明度功能,可以将透明度调整为dat.gui栏(一种称为opciones.Opacidad)

contenido.traverse( function( object ) {

                    if( object.material ) {

                        object.material.opacity = opciones.Opacidad;
                        object.material.transparent = true;
                    }

And it works correctly, but the probblem come when I try to access internal cubes of the geometry to remark them in wireframe (for example) I use: 它可以正常工作,但是当我尝试访问几何的内部多维数据集以在线框中对其进行标记时(例如),问题就来了:

contenido.getObjectByName("RoomNameIn3DSMax").material.wireframe=true;

In order to find and show it in wireframe, because the OBJ file names every module with its 3DS Max name. 为了在线框中查找并显示它,因为OBJ文件使用3DS Max名称来命名每个模块。 But it does not work, It looks like the item is found because I don't get any error, but it does not appear in wireframe, also when I use another room names, sometimes the program show in wireframe lots of things that can include the requested one or not, another ones it finds "undefined" I think it has something to be with the Groups created by the OBJMTLLoader. 但是它不起作用,看起来好像找到了该项目是因为我没有收到任何错误,但是它没有出现在线框中,而且当我使用其他房间名称时,有时程序会在线框中显示很多可以包括的内容请求的一个或多个,发现的另一个“未定义”,我认为这与OBJMTLLoader创建的组有关。

I've also tried 我也尝试过

        contenido.traverse( function ( child ) {
            if ( child.name == "NameInOBJor3DSMax" ) {
                child.material.wireframe = true;
            }
        })

But the result is the same. 但是结果是一样的。

So my question is, How can I access internal modules of my model loaded with OBJMTLLoader? 所以我的问题是, 如何访问加载了OBJMTLLoader的模型的内部模块 As you can see I tried to use the "Object3D" methods to access the internal cubes of the whole model, am I doing it correctly? 如您所见,我尝试使用“ Object3D”方法访问整个模型的内部多维数据集,我是否正确地做到了? I can provide all the code and files if needed, you can see an approach of my problem here: 我可以根据需要提供所有代码和文件,您可以在此处查看解决我的问题的方法:

3DBuilding link You can also see all code there ;) 3DBuilding链接您还可以在那里查看所有代码;)

Thank you for your attention, I hope someone can help me. 谢谢您的关注,希望有人能帮助我。

EDIT: When I do: 编辑:当我这样做时:

 scene.getObjectByName("nameOfModule").material.color.set(0xff0000); 

To remark it on red or just: .material.wireframe=true; 用红色标记或仅用以下标记:.material.wireframe = true; it shows remarked a lot of things that are not realted. 它说明了很多未实现的内容。 It does the same when I use scene.getObjectById(ObjectId, true); 当我使用scene.getObjectById(ObjectId,true)时,它的作用相同。 But it does not show the name of the object in the DOM Tree. 但是它没有在DOM树中显示对象的名称。

I am trying to use another exporting tools to see if the problem is just the OBJ Exporter. 我正在尝试使用其他导出工具来查看问题是否仅是OBJ导出器。 It is something really annoying. 这真的很烦人。

Here are some screens of the problem. 这是问题的一些屏幕。

What I try to remark: 我想说明一下: 我尝试着色的东西

as you can see, it's only a edited box 如您所见,它只是一个编辑框

what is remarked: 备注: 什么是有色的

If we go to the OBJ file we can see something like this: 如果我们转到OBJ文件,我们可以看到如下所示:

#
  # object SDHAE001
#

v  106.4733 84.7697 -94.9228
... (more vertices)
v  106.3747 76.8453 -96.3784
# 16 vertices

vn 0.0000 1.0000 -0.0000
...(more normals)
vn -0.9991 0.0046 0.0425
# 14 vertex normals

#and here the group of faces fot that object ( Using triangles )
g SDHAE001
usemtl wire_115115115
f 10663//5549 10664//5549 10665//5549 
...(more triangle faces)
f 10676//5559 10663//5561 10678//5562 
# 22 faces

As you can see, 3DS Max is creating Groups of polygons for my geometry, and as far as I know, OBJMTL ignores "g" groups, so when I try to remark something, it remarks A LOT of things... Trying with other exporters... Parsing manually the OBJ file would be so annoying... 如您所见,3DS Max正在为我的几何图形创建多边形组,据我所知,OBJMTL会忽略“ g”组,因此当我尝试标记某些内容时,它会标记很多东西...出口商...手动分析OBJ文件会很烦人...

EDITED AGAIN 再次编辑

I've been exploring the options, and OBJ is generated ok by 3DS MAX, BUT when I explore de DOM Tree, I can see the THREE.Mesh as an array where only some of the the pair indexes have the expected object with his own name, and odd indexes have some sort of geometry I can't control without any name assigned, and for some strange reason, there are some groups of "vertices" created in geometry instance with no sense, I upload some screens and the OBJ/MTL files to make my problem clearer: 我一直在探索选项,并且3DS MAX可以很好地生成OBJ,但是当我探索de DOM Tree时,我可以看到THREE.Mesh是一个数组,其中只有一对对索引中有自己的预期对象名称和奇数索引具有某种没有分配名称就无法控制的几何,并且由于某些奇怪的原因,在几何实例中创建了一些毫无意义的“顶点”组,我上传了一些屏幕和OBJ / MTL文件使我的问题更清楚:

Look at this, I will try to remark now a module called "Conserjeria". 看看这个,我现在尝试标记一个名为“ Conserjeria”的模块。 and this is the module located in the DOM Tree: 这是位于DOM树中的模块:

网格位于

Bigger image 图片更大

Here, on the first column we can see, first problem , my OBJ (all the geometry) file only has about 298 Objects, so why are there 1056 Mesh Objects? 在第一列上,我们可以看到, 第一个问题 ,我的OBJ(所有几何图形)文件只有298个对象,那么为什么会有1056个网格对象? It is not very relevant because most of them are with empty names. 这不是很相关,因为其中大多数名称都是空的。

Second problem , on the second column you can see geometry property for "Conserjeria" room, I think here is where the problem is generated... On the third column, I show you that strange "vertices" array for my "Conserjeria" object. 第二个问题 ,在第二列上,您可以看到“ Conserjeria”房间的几何属性,我认为这是问题所在。在第三列上,我为您的“ Conserjeria”对象显示了奇怪的“顶点”数组。 I say strange, because it is composed by 6272 indexes... NOTHING in my OBJ file has 6272 vertices... You can see the OBJ here , and you can find Conserjeria if you want to see its structure (52v, 11vn, 80f) . 我说奇怪,因为它是由6272项指标组成的... 没有在我的OBJ文件中有6272个顶点......你可以看到OBJ 在这里 ,你可以找到Conserjeria如果你想看到它的结构(52V,11vn,80F ) Might this be the reason why when I try to remark only certain objects it remarks a lot of them? 可能这就是为什么当我尝试仅标记某些对象时会标记很多对象的原因吗?

I've also tried with a simplified model of 3 boxes , 2 of them are correctly remarked, but the first one is not, have a look (use the checkboxes to remark them) I can't imagina why is this happening. 我还尝试了3个框简化模型 ,其中2 个框已正确标记,但第一个框没有,请看一眼(使用复选框对其进行标记),我无法想象为什么会这样。 (there are also 7 THREE.MESH index in the array when there are only 3 cubes in the scene...) (当场景中只有3个多维数据集时,数组中还有7个THREE.MESH索引...)

TestingCubes TestingCubes

Any help to remark only my rooms? 对仅评论我的房间有帮助吗? I can share everything I have if needed(Code, OBJ, MTL, 3ds...) 如果需要,我可以共享我拥有的所有内容(代码,OBJ,MTL,3ds ...)

Thank you to everyone who is trying to help me. 感谢所有试图帮助我的人。

Looks like you got a problem with the mesh names. 看起来您的网格名称有问题。

When I execute the following script, it's working just fine: 当我执行以下脚本时,它可以正常工作:

scene.children[2].traverse(function(c) {
  if(c.material && c.name.indexOf("INVESTIGACION_1_") >= 0) {
    c.material.color.set(0xff0000) 
  }
})

That tints part of the model red, as expected. 不出所料,这会使模型的一部分变成红色。

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

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