9

我有一个纹理应用于网格我可以改变偏移量

mesh.material.map.offset.set

我可以用

mesh.material.repeat.set

所以我的问题是,如何在平面内旋转纹理?

例子:

由此:

在此处输入图像描述

对此

在此处输入图像描述

谢谢。

4

5 回答 5

16

使用 2D 画布作为纹理

演示:

https://dl.dropboxusercontent.com/u/1236764/temp/stackoverflow_20130525/index.html

示例代码

var camera, scene, renderer, mesh;

var width = window.innerWidth;
var height = window.innerHeight;

scene = new THREE.Scene();

camera = new THREE.PerspectiveCamera( 30, width / height, 1, 1000 );
camera.position.z = 100;

renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

var img = new Image();
img.onload = createMeshThenRender;
img.src = 'img.jpg';

function createMeshThenRender () {
    var imgWidth = imgHeight = 256;
    var mapCanvas = document.createElement( 'canvas' );
    mapCanvas.width = mapCanvas.height = 256;

    // document.body.appendChild( mapCanvas );
    var ctx = mapCanvas.getContext( '2d' );
    ctx.translate( imgWidth / 2, imgHeight / 2 );
    ctx.rotate( Math.PI / 4 );
    ctx.translate( -imgWidth / 2, -imgHeight / 2 );
    ctx.drawImage( img, 0, 0, imgWidth, imgHeight );

    var texture = new THREE.Texture( mapCanvas );
    texture.needsUpdate = true;

    mesh = new THREE.Mesh(
        new THREE.PlaneGeometry( 50, 50, 1, 1 ),
        new THREE.MeshBasicMaterial( {
            map : texture
        } )
    );
    scene.add( mesh );
    renderer.render( scene, camera );
}
于 2013-05-25T06:57:34.030 回答
3

three.js 没有 UV 编辑实用程序,因此您要么必须geometry.faceVertexUvs手动编辑,要么在图像编辑程序中旋转图像。我建议后者。

三.js r.58

于 2013-05-24T06:14:02.023 回答
2

三.js r121

在较新版本的three.js中,可以直接设置纹理的旋转和旋转中心。

var texture = new THREE.Texture( ... );
texture.rotation = Math.PI/4;
texture.center = new Vector2d(0.5, 0.5); // center of texture.
于 2020-10-12T16:23:00.683 回答
2

三.js r85

对于那些希望使用 ShapeBufferGeometry 或 PlaneBufferGeometry 在位于 XY 平面(默认平面)的平面上实际“旋转 UV ”的人。

var planeGeo = new THREE.PlaneBufferGeometry(24,24);
var planeMesh = new THREE.Mesh(planeGeo, new THREE.MeshBasicMaterial({map: yourTexture}));
scene.add(planeMesh);
rotateUVonPlanarBufferGeometry(45, planeMesh);

function rotateUVonPlanarBufferGeometry(rotateInDeg, mesh){
  if(rotateInDeg != undefined && mesh){

    var degreeInRad = THREE.Math.degToRad(rotateInDeg);
    var tempGeo = mesh.geometry.clone();
    var geo;

    if(tempGeo instanceof THREE.BufferGeometry){
        geo = new THREE.Geometry().fromBufferGeometry(tempGeo);
    }else{
        console.log('regular geometry currently not supported in this method, but can be if code is modified, so use a buffer geometry');
        return;
    }

    // rotate the geo on Z-axis
    // which will rotate the vertices accordingly
    geo.applyMatrix(new THREE.Matrix4().makeRotationZ(degreeInRad));

    // loop through the vertices which should now have been rotated
    // change the values of UVs based on the new rotated vertices
    var index = 0;
    geo.vertices.forEach(function(v){
      mesh.geometry.attributes.uv.setXY( index, v.x, v.y );
      index++;
    });

    mesh.geometry.attributes.uv.needsUpdate = true;

  }

}
于 2017-06-01T23:19:12.990 回答
0

上面的解决方案对我不好,有点老了。这是对我有用的简单解决方案(Three.js 125)

imgData = canvasCtx.getImageData(0, 0, canvasElement.width, canvasElement.height);
texture = new THREE.DataTexture( imgData.data, canvasElement.width, canvasElement.height, THREE.SRGB );
texture.rotation = Math.PI;
texture.center = new THREE.Vector2(0.5, 0.5); // center of texture.
mymaterial = new THREE.MeshBasicMaterial({
                                map: texture,
                                alphaTest: 0.5,
                                transparent: true,
                                side: THREE.DoubleSide,
                            });
于 2021-08-30T12:33:39.670 回答