three的版本:
"three": "^0.144.0",
1.相机分为透视相机(PerspectiveCamera)和正交相机(OrthographicCamera)
function createCamera(width, height) {
var k = width / height; //窗口宽高比
var s = 200; //三维场景显示范围控制系数,系数越大,显示的范围越大
// //创建相机对象
// 正交相机
// camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
// 透视相机(此处需要全景和透视)
camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
20000
);
camera.position.set(200, 300, 200); //设置相机位置
camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
}
2.如果要旋转看四周的场景必要要用透视相机
function createBackground() {
var urls = [
"model/textures/posx.jpg",
"model/textures/negx.jpg",
"model/textures/posy.jpg",
"model/textures/negy.jpg",
"model/textures/posz.jpg",
"model/textures/negz.jpg",
];
// 注意要使用透视相机(PerspectiveCamera),而非正交相机(OrthographicCamera)
scene.background = new THREE.CubeTextureLoader().load(urls);
// 方法二
/* scene.background = new THREE.CubeTextureLoader()
.setPath(`${publicPath}model/textures/`)
.load([
"posx.jpg",
"negx.jpg",
"posy.jpg",
"negy.jpg",
"posz.jpg",
"negz.jpg",
]); */
}
3.如果只是设置背景图片(两个相机模式都可以)
function createBackground() {
const loader = new THREE.TextureLoader();
const bgTexture = loader.load("/model/textures/negz.jpg");
scene.background = bgTexture;
}
4.备注
文件放在
public\model\textures