gpt4 book ai didi

three.js - 使用Three.js翻转(镜像)任何对象

转载 作者:行者123 更新时间:2023-12-03 14:45:29 25 4
gpt4 key购买 nike

更新2019
由于r89 three.js也将调整面部和法线。要翻转/镜像对象,只需使用:

object3D.applyMatrix(new THREE.Matrix4().makeScale(-1, 1, 1));
不需要原始解决方案中所示的其他调整。链接到票证: Support reflection matrices. #12787
原始问题
我正在尝试创建一个实用程序,该实用程序可以翻转Three.js场景中的任何对象。翻转本身很容易:
object3D.applyMatrix(new THREE.Matrix4().makeScale(-1, 1, 1));
事实证明,困难的是在翻转后固定面部和法线。结果看起来很困惑。源和翻转对象的图像: https://dl.dropboxusercontent.com/u/20925853/Flipped.png
我已经看到许多讨论此问题和类似问题的线程,但没有发现任何可用的线程。有人知道我在那儿想念什么吗? - 谢谢!
Js fiddle 上的示例代码:
https://jsfiddle.net/7dwh084w/
var renderer;
var scene;
var camera;

function render() {

renderer.render(scene, camera);
};

function load(callback) {

new THREE.ColladaLoader().load("https://dl.dropboxusercontent.com/u/20925853/Kitchen.dae", function (result) {

var mesh = result.scene.children[0].children[0].clone();
if (callback) callback(mesh);
});
}

function init() {

scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.z = 1000;

var light = new THREE.DirectionalLight(0xffffff);
light.position.set(2, 1, 1).normalize();
scene.add(light);
var ambient = new THREE.AmbientLight(0x777777);
scene.add(ambient);

var controls = new THREE.OrbitControls(camera);
controls.damping = 0.2;
controls.addEventListener('change', render);

renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

load(function (mesh) {

flipMesh(mesh);

scene.add(mesh);
render();
});

render();
}

function flipMesh(object3D) {

object3D.applyMatrix(new THREE.Matrix4().makeScale(-1, 1, 1));
reverseWindingOrder(object3D);
}

function reverseWindingOrder(object3D) {

// TODO: Something is missing, the objects are flipped alright but the light reflection on them is somehow broken

if (object3D.type === "Mesh") {

var geometry = object3D.geometry;

for (var i = 0, l = geometry.faces.length; i < l; i++) {

var face = geometry.faces[i];
var temp = face.a;
face.a = face.c;
face.c = temp;

}

var faceVertexUvs = geometry.faceVertexUvs[0];
for (i = 0, l = faceVertexUvs.length; i < l; i++) {

var vector2 = faceVertexUvs[i][0];
faceVertexUvs[i][0] = faceVertexUvs[i][2];
faceVertexUvs[i][2] = vector2;
}

geometry.computeFaceNormals();
geometry.computeVertexNormals();
}

if (object3D.children) {

for (var j = 0, jl = object3D.children.length; j < jl; j++) {

reverseWindingOrder(object3D.children[j]);
}
}
}

init();

最佳答案

我在项目中使用以下代码来翻转对象:

    const scale = new THREE.Vector3(1, 1, 1);
if (flipX) {
scale.x *= -1;
}
if (flipY) {
scale.z *= -1;
}
object.scale.multiply(scale);

希望这可以帮助。

关于three.js - 使用Three.js翻转(镜像)任何对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28630097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com