three.js - 天空盒的地板在 THREE.js 中旋转 180 度
问题描述
我正在用 three.js 开发一个游戏(但这是我的第一个),我正在制作一个天空盒,所有的图像都是完美的,除了底部的那个被翻转了 180 度。
我尝试在 mac os 预览中旋转图像,但这并没有改变任何东西,而且我在网上找不到任何帮助。
这是我的代码
//cube2 表示天空盒 //这是我的天空盒的所有代码(使用three.js)
var 立方体材料 = [
new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader( ).load( 'Images/skybox/PositiveX.png' ), side: THREE.DoubleSide } ), // Right side
new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader( ).load( 'Images/skybox/NegativeX.png' ), side: THREE.DoubleSide } ), // Left side
new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader( ).load( 'Images/skybox/NegativeY.png' ), side: THREE.DoubleSide } ), // Top side
new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader( ).load( 'Images/skybox/PositiveY.png' ), side: THREE.DoubleSide} ), // Bottom side
new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader( ).load( 'Images/skybox/PositiveZ.png' ), side: THREE.DoubleSide } ), // Front side
new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader( ).load( 'Images/skybox/NegativeZ.png' ), side: THREE.DoubleSide } ) // Back side
];
// Create a MeshFaceMaterial, which allows the cube to have different materials on each face
var geometry2 = new THREE.BoxGeometry( 100000, 100000, 100000 );
var cube2 = new THREE.Mesh( geometry2, cubeMaterials );
scene.add( cube2 );
解决方案
在 three.js 中使用天空盒的方法要简单得多
使用CubeTextureLoader
并将结果分配给scene.background
这样的
const loader = new THREE.CubeTextureLoader();
const texture = loader.load([
'path/to/pos-x.jpg',
'path/to/neg-x.jpg',
'path/to/pos-y.jpg',
'path/to/neg-y.jpg',
'path/to/pos-z.jpg',
'path/to/neg-z.jpg',
]);
scene.background = texture;
例子
'use strict';
/* global THREE */
function main() {
const canvas = document.querySelector('#c');
const renderer = new THREE.WebGLRenderer({
canvas
});
const fov = 75;
const aspect = 2; // the canvas default
const near = 0.1;
const far = 100;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.z = 2;
const controls = new THREE.OrbitControls(camera, canvas);
controls.target.set(0, 0, 0);
controls.update();
const scene = new THREE.Scene();
{
const loader = new THREE.CubeTextureLoader();
const texture = loader.load([
'https://threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/pos-x.jpg',
'https://threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/neg-x.jpg',
'https://threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/pos-y.jpg',
'https://threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/neg-y.jpg',
'https://threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/pos-z.jpg',
'https://threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/neg-z.jpg',
]);
scene.background = texture;
}
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render(time) {
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
renderer.render(scene, camera);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
main();
body {
margin: 0;
}
#c {
width: 100vw;
height: 100vh;
display: block;
}
<canvas tabstop="1" id="c"></canvas>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r103/three.min.js"></script>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r103/js/controls/OrbitControls.js"></script>
从这里
推荐阅读
- ios - NSAttributedString 在文本和背景之前和之后有填充
- java - 如何在java中将长度为22的字符串转换为整数
- java - 类的构造函数有问题
- javascript - 在 JS 搜索中删除区分大小写
- c - 使用 c 通过 mqtt 将 sqlite 数据库读取到 mqtt 代理
- swiftui - 如何禁用 NavigationView 推送和弹出动画
- visual-studio-code - VS Code 上 Edge 模板引擎的自动关闭标签?
- email - DNN 7.2 中新用户注册电子邮件的发件人电子邮件地址
- python - 两个永无止境的python脚本之间的交互
- python-3.x - 如何使用 pandas 生成间隔为 1 秒的一个月的时间序列数据?