ThreeJS r71 - PlaneBufferGeometry顶点UV

时间:2015-04-11 21:40:26

标签: javascript three.js

我有一个创建简单THREE.PlaneGeometry的函数,并使用图像纹理材质将一部分图像映射到平面上。我想弄清楚的是如何将此逻辑从使用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 }
    });

1 个答案:

答案 0 :(得分:0)

您可以像这样直接设置它们:

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 ) );