Сохраняйте вид на текстуре Three. js - PullRequest
1 голос
/ 26 февраля 2020

Я пытаюсь держать текстуру по центру, когда она отображается в окне другого размера.

Я видел этот ответ

Три. js: сделать текстуру изображения подходящей для объекта без искажения или повторения

Но это не совсем так это для меня.

  this.texture = new THREE.Texture(this.image)
  const vec = new THREE.Vector3()
  new THREE.Box3().setFromObject( this.rounded ).getSize(vec)
  const imageAspect = this.image.width/this.image.height
  const boxAspect = vec.x/vec.y

  this.texture.wrapT = THREE.RepeatWrapping;
  this.texture.offset.y =  0.5 * ( 1 - boxAspect/imageAspect )

  //texture.wrapT = THREE.RepeatWrapping; texture.repeat.x = geometryAspectRatio / imageAspectRatio; texture.offset.x = 0.5 * ( 1 - texture.repeat.x )

  this.texture.needsUpdate = true 
  this.rounded.material = new THREE.MeshPhongMaterial( { map: this.texture, side: THREE.DoubleSide } ) 

small

В этом аспекте значения

Image: {width:399  height:275}
Texture: {width:1, height: 0.75}

enter image description here

В этом аспекте значения

Image: {width:399  height:275}
Texture: {width:2, height: 1}

Как это исправить, чтобы график c всегда был центральным, сохранял аспект и не искажался?

1 Ответ

2 голосов
/ 26 февраля 2020

Надеюсь, я вас правильно понял, вот вариант того, как вы можете отцентрировать его:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 1, 1000);
camera.position.set(0, 0, 10);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);

var planeGeom = new THREE.PlaneBufferGeometry(16, 9);
var planeMat = new THREE.MeshBasicMaterial({
  map: new THREE.TextureLoader().load("https://threejs.org/examples/textures/758px-Canestra_di_frutta_(Caravaggio).jpg", tex => {
    //console.log(tex);
    //console.log(tex.image.width, tex.image.height);
    let imgRatio = tex.image.width / tex.image.height;
    let planeRatio = planeGeom.parameters.width / planeGeom.parameters.height;
    //console.log(imgRatio, planeRatio);
    tex.wrapS = THREE.RepeatWrapping; // THREE.ClampToEdgeWrapping;
    tex.repeat.x = planeRatio / imgRatio;
    tex.offset.x = -0.5 * ((planeRatio / imgRatio) - 1);
  })
});
var plane = new THREE.Mesh(planeGeom, planeMat);
scene.add(plane);

renderer.setAnimationLoop(() => {
  renderer.render(scene, camera);
})
body {
  overflow: hidden;
  margin: 0;
}
<script src="https://threejs.org/build/three.min.js"></script>

Дополнение: как пересчитать UV для ShapeBufferGeometry

var box = new THREE.Box3().setFromObject(mesh); // mesh with ShapeBufferGeometry
var size = new THREE.Vector3();
box.getSize(size);
var vec3 = new THREE.Vector3(); // temp vector
var attPos = mesh.geometry.attributes.position;
var attUv = mesh.geometry.attributes.uv;
for (let i = 0; i < attPos.count; i++){
    vec3.fromBufferAttribute(attPos, i);
    attUv.setXY(i,
    (vec3.x - box.min.x) / size.x,
    (vec3.y - box.min.y) / size.y
  );
}
attUv.needsUpdate = true; // just in case
...