我使用three.js和CSS3D渲染器构建了一个全景图,现在我想使用WebGL渲染器执行相同操作。
在CSS3D中,以下代码为我提供了一个完全齐声且不破坏的全景:
var sides = [
{
url: '/assets/posx.jpg',
position: [ -644, 0, 0 ],
rotation: [ 0, Math.PI / 2, 0 ]
},
{
url: '/assets/negx.jpg',
position: [ 644, 0, 0 ],
rotation: [ 0, -Math.PI / 2, 0 ]
},
{
url: '/assets/posy.jpg',
position: [ 0, 644, 0 ],
rotation: [ Math.PI / 2, 0, Math.PI ]
},
{
url: '/assets/negy.jpg',
position: [ 0, -644, 0 ],
rotation: [ -Math.PI / 2, 0, Math.PI ]
},
{
url: '/assets/posz.jpg',
position: [ 0, 0, 644 ],
rotation: [ 0, Math.PI, 0 ]
},
{
url: '/assets/negz.jpg',
position: [ 0, 0, -644 ],
rotation: [ 0, 0, 0 ]
}
];
for (var i = 0; i < sides.length; i++) {
var side = sides[ i ];
var element = document.createElement('img');
element.width = 1300;
element.height = 1300;
element.src = side.url;
var object = new THREE.CSS3DObject(element);
object.position.fromArray(side.position);
object.rotation.fromArray(side.rotation);
scene.add(object);
}
[旁边的问题:除了逐行扫描和打空间4次之外,还有一种更好的格式化粘贴代码的方法,对吗?]
这是我用于渲染图像的WebGL的等效代码,但它完全没有干净,而且图像重叠没有明显的中断:
var sides = [
{
url: '/assets/posx.jpg'
},
{
url: '/assets/negx.jpg'
},
{
url: '/assets/posy.jpg'
},
{
url: '/assets/negy.jpg'
},
{
url: '/assets/posz.jpg'
},
{
url: '/assets/negz.jpg'
}
];
//i chose 8 for this because other numbers i tried rendered interesting
//distortions into the images
var k = 8
for (var i = 0; i < sides.length; i++) {
var side = sides[ i ];
var geometry = new THREE.SphereGeometry(10, k, k);
k += 8;
geometry.applyMatrix(new THREE.Matrix4().makeScale(-1, 1, 1));
var material = new THREE.MeshBasicMaterial({
map: THREE.ImageUtils.loadTexture(side.url)
});
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
有没有使用Three.js和WebGl渲染引擎将六幅图像投影到全景图中的标准方法?
答案 0 :(得分:1)
你在寻找什么名字&#34; skybox&#34;。
有一个最简单的例子: https://stemkoski.github.io/Three.js/Skybox.html
在这部分中插入图像:
var imagePrefix = "images/dawnmountain-";
var directions = ["xpos", "xneg", "ypos", "yneg", "zpos", "zneg"];
var imageSuffix = ".png";
var skyGeometry = new THREE.CubeGeometry( 5000, 5000, 5000 );
var materialArray = [];
for (var i = 0; i < 6; i++)
materialArray.push( new THREE.MeshBasicMaterial({
map: THREE.ImageUtils.loadTexture( imagePrefix + directions[i] + imageSuffix ),
side: THREE.BackSide
}));
var skyMaterial = new THREE.MeshFaceMaterial( materialArray );
var skyBox = new THREE.Mesh( skyGeometry, skyMaterial );
scene.add( skyBox );