javascript - ThreeJS r71 - PlaneBufferGeometry 顶点 UV

标签 javascript three.js

我有一个函数可以创建一个简单的 THREE.PlaneGeometry并使用图像纹理 Material 将图像的一部分映射到平面上。我想弄清楚的是如何从使用 THREE.PlaneGeometry 转换此逻辑到 THREE.PlaneBufferGeometry ,但我无法弄清楚如何以相同的方式访问顶点和更改。

如果这是一个重复的问题,我深表歉意。我确实搜索过,如果存在,我找不到它。

这是函数,下面是它的示例调用:

/**
 * options:
 *  imageSize: { w: #, h: # } - size of the source image
 *  planeSize: { w: #, h:# } - size of the actual plane to create
 *  position: { x: #, y:#, z: # } - position of the next plane
 *  material: material - the material to apply
 *  clipRect: { x: #, y:#, w: #, h:# } - the part of the image to clip
 *      x and y from the bottom left of the image.
 *      w and h width and height of the image region,
 *   name: name of the mesh
 */
function createPlane( opts ) {

    var i, faces, v, vertexes, point, p,
        plane = new THREE.PlaneGeometry( opts.planeSize.w, opts.planeSize.h, 1, 1 ),
        mesh = new THREE.Mesh(plane, opts.material),
        imgRect = {
            x: opts.clipRect.x / opts.imageSize.w,
            y: opts.clipRect.y / opts.imageSize.h,
            w: opts.clipRect.w / opts.imageSize.w,
            h: opts.clipRect.h / opts.imageSize.h
        };

    if (opts.name !== '') {
        mesh.name = opts.name;
    }

    if (opts.position !== null) {
        mesh.position.set(
            opts.position.x,
            opts.position.y,
            opts.position.z
        );
    }

    for( i = 0; i < plane.faceVertexUvs.length; i++) {
        faces = plane.faceVertexUvs[i];
        for( v = 0; v < faces.length; v++) {
            vertexes = faces[v];
            for( p = 0; p < vertexes.length; p ++ ) {
                point = vertexes[p];
                point.x = imgRect.x + ( point.x * imgRect.w );
                point.y = imgRect.y + ( point.y * imgRect.h );
            }
        }
    }
    return mesh;
}

这是一个示例调用:
var plane = createPlane({
        imageSize: { w: 1024, h: 1024},
        planeSize: { w: 23, h: 31 },
        position: null,
        material: new THREE.MeshBasicMaterial({
            map: myTexture, // defined elsewhere
            transparent: true
        }),
        name: 'myPlane',
        clipRect: { x: 958, y: 226, w: 23, h: 31 }
    });

最佳答案

您可以像这样直接设置它们:object.geometry.attributes.uv.array[0] = 0.1;另一种方式(我猜将来不太可能打破):

var quad_uvs =
[
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
];
var uvs = new Float32Array( quad_uvs);
object.geometry.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );

关于javascript - ThreeJS r71 - PlaneBufferGeometry 顶点 UV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29583387/

相关文章:

javascript - 有没有一种简单的方法可以用简单的英语来理解 JavaScript 的 .bind ?

javascript - 可能的 Javascript 选择器(无框架)使用数组键命名语法获取元素数组

javascript - 从对象复制 THREE.js 矩阵

JavaScript 表单验证在 IE8 中不起作用

javascript - jQuery 向每一行的下一列添加附加值

javascript - 如何同时滚动两个div?

javascript - 使用 D3.js 的 3D 条形图的透视问题

javascript - 适用于 Android 的三.js cordova apk

three.js - 如何在使用 JSONLoader() 加载的模型上应用 THREE.FlatShading?

javascript - 如何使用 three.js 将 Material (.mtl) 添加到对象 (.obj)?