简体   繁体   English

如何在Three.js中为.obj模型添加颜色?

[英]How can I add colour to a .obj model in Three.js?

I am creating a WebGL scene with Three.js and I have the next problem: I am trying to load a .obj model with Three.js, and I want to add a color (blue, for example), to that .obj model, instead of a mtl texture. 我正在用Three.js创建一个WebGL场景,我遇到了下一个问题:我正在尝试使用Three.js加载.obj模型,并且我想为该.obj模型添加颜色(例如蓝色)。 ,而不是mtl纹理。 I try to do this way: 我尝试这样做:

// instantiate a loader
var loader = new THREE.OBJLoader();

// load a resource
loader.load(
    // resource URL
    'model_path.obj',
    // Function when resource is loaded
    function ( object ) {
        scene.add( object );
    }
);

But I have the next error that says: 但是下一个错误是:

WebGL: INVALID_VALUE: bufferData: no data
Uncaught TypeError: Cannot read property 'length' of undefined
[.CommandBufferContext]GL ERROR :GL_INVALID_VALUE : glVertexAttribPointer: size GL_INVALID_VALUE

How could I correct the error, to load an obj model with blue color? 如何加载蓝色的obj模型以更正错误?

Thank you so much! 非常感谢!

Hey the error seems the vertex points are not there with the file. 嘿,错误似乎文件中没有顶点。 OBJ file is the simple raw file with OBJ文件是具有以下内容的简单原始文件

v :::: vertex v ::::顶点

vt :::: texture coordinate vt ::::纹理坐标

vn :::: normal vn ::::正常

usemtl ::: define the texture,ka(ambient),ks(specular) and kd(diffused) usemtl :::定义纹理,ka(环境),ks(镜面)和kd(扩散)

try to import your obj into the blender and then export with the triangulated face on like this: 尝试将您的obj导入到搅拌器中,然后像这样用三角形的面导出: 在此处输入图片说明

actually if you don't do this the indices appear to you in face4 format like: 实际上,如果您不这样做,索引将以face4格式显示给您,例如:

0/1/2/3 3/2/4/0 .... so on after triangulation they appear like: 0/1/2/3 3/2/4/0 ....等等,在三角剖分之后它们看起来像:

0/1/2 3/2/0 ... 0/1/2 3/2/0 ...

So when you correct your *.obj you can load the file in three parser and add the material to the children. 因此,当您更正* .obj时,可以将文件加载到三个解析器中,并将材质添加到子级中。

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

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