threeJS如何修改已经生成的Geometry的长宽高?

我写了以下函数,目的是想创建面片,把图片作为面片的贴图放进场景里,现在想让面片能在图片加载成功后自动更新长宽,但是怎么都修改不了。我又不想异步创建面片...



function createPeace(img) {

    var mesh,

        geometry = new THREE.PlaneGeometry( 1, 1 );


    var texture = new THREE.TextureLoader().load( "models/20nian.png" ,function (texture) {

        //贴图加载成功后想修改之前创建的 geometry 的宽高,而不想修改 

        var i = texture.image;

        geometry.parameters.width = i.naturalWidth/400;

        geometry.parameters.height = 3;

        console.log(geometry);

    });

    var material = new THREE.MeshBasicMaterial( {

        color: 0xffff00,

        side: THREE.DoubleSide,

        map:texture

    });

    mesh = new THREE.Mesh( geometry, material );

    mesh.lookAt(new THREE.Vector3(0,0,0));

    return mesh;

}


浮云间
浏览 5400回答 1
1回答

忽然笑

不能修改geometry的尺寸,应该修改mesh的尺寸,mesh才是实体。function createPeace(img) {    var mesh,        geometry = new THREE.PlaneGeometry( 1, 1 );    var texture = new THREE.TextureLoader().load( "models/20nian.png" ,function (texture) {        //贴图加载成功后想修改之前创建的 geometry 的宽高,而不想修改         var i = texture.image;       // geometry.parameters.width = i.naturalWidth/400;       // geometry.parameters.height = 3;       // console.log(geometry);       // geometry只是模型,mesh才是实体       mesh.scale.set(1 || your need, 1 || your need, 1 || your need); //mesh.scale.set(x轴方向的缩放倍数, y轴方向的缩放倍数, z轴方向的缩放倍数);    });    var material = new THREE.MeshBasicMaterial( {        color: 0xffff00,        side: THREE.DoubleSide,        map:texture    });    mesh = new THREE.Mesh( geometry, material );    mesh.lookAt(new THREE.Vector3(0,0,0));    return mesh;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript