簡體   English   中英

ThreeJS r71 - PlaneBufferGeometry 頂點 UV

[英]ThreeJS r71 - PlaneBufferGeometry Vertex UV

我有一個函數可以創建一個簡單的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 }
    });

您可以像這樣直接設置它們:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM