三.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坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)