三.js 生成UV坐标

THREE.js generate UV coordinate(三.js 生成UV坐标)
本文介绍了三.js 生成UV坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 THREE.js OBJ 加载器将模型导入场景.

I am working on importing a model into a scene using the THREE.js OBJ loader.

我知道我可以很好地导入几何图形,因为当我为它分配一个 MeshNormalMaterial 时,它显示得很好.但是,如果我使用任何需要 UV 坐标的东西,它会给我错误:

I know that I am able to import the geometry fine, because when I assign a MeshNormalMaterial to it, it shows up great. However, if I use anything that requires UV coordinates, It gives me the error:

[.WebGLRenderingContext]GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 1 

我知道这是因为加载的 OBJ 没有 UV 坐标,但我想知道是否有任何方法可以生成所需的纹理坐标.我试过了

I know this is because the loaded OBJ has no UV coordinates, but I was wondering if there was any way to generate the needed texture coordinates. I have tried

material.needsUpdate = true;
geometry.uvsNeedUpdate = true;
geometry.buffersNeedUpdate = true;

...但无济于事.

有没有办法使用three.js自动生成UV纹理,还是我必须自己分配坐标?

Is there any way to automagically generate UV textures using three.js, or do I have to assign the coordinates myself?

推荐答案

据我所知,没有自动计算 UV 的方法.

To my knowledge there is no automatic way to calculate UV.

你必须自己计算.计算平面的 UV 非常简单,这个网站解释了如何:计算纹理坐标

You must calculate yourself. Calculate a UV for a plane is quite easy, this site explains how: calculating texture coordinates

对于一个复杂的形状,我不知道怎么做.也许你可以检测平面.

For a complex shape, I don't know how. Maybe you could detect planar surface.

编辑

这是一个平面的示例代码(x, y, z) where z = 0:

Here is a sample code for a planar surface (x, y, z) where z = 0:

geometry.computeBoundingBox();

var max = geometry.boundingBox.max,
    min = geometry.boundingBox.min;
var offset = new THREE.Vector2(0 - min.x, 0 - min.y);
var range = new THREE.Vector2(max.x - min.x, max.y - min.y);
var faces = geometry.faces;

geometry.faceVertexUvs[0] = [];

for (var i = 0; i < faces.length ; i++) {

    var v1 = geometry.vertices[faces[i].a], 
        v2 = geometry.vertices[faces[i].b], 
        v3 = geometry.vertices[faces[i].c];

    geometry.faceVertexUvs[0].push([
        new THREE.Vector2((v1.x + offset.x)/range.x ,(v1.y + offset.y)/range.y),
        new THREE.Vector2((v2.x + offset.x)/range.x ,(v2.y + offset.y)/range.y),
        new THREE.Vector2((v3.x + offset.x)/range.x ,(v3.y + offset.y)/range.y)
    ]);
}
geometry.uvsNeedUpdate = true;

这篇关于三.js 生成UV坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)