css - 查看图像,如倒转转盘或 VR/球形
问题描述
我正在尝试构建这样的东西:
但不同的是,我有一个 4096x1024 的背景图像,我希望它看起来像某种 VR 外观,中心离屏幕有点远,边缘有点拉伸和靠近
我设法使用框架并将我的图像设置为<a-sky>
标签的背景,因此它将我的图像扭曲成一个球体,而视图或相机则位于球体的中心。我得到了我想要的正确类型的视图,问题是我认为有更轻松的解决方案。我还想放文字,我希望它以与图像相同的方式运行。
这就是我用 aframes 归档的内容:Imgur 上的 Gif
我不想在场景/屏幕周围移动(使用 AWDS),只是为了水平移动屏幕,因为我将拥有用户需要移动屏幕才能看到它们的元素,就像在 gif 和普通 carrossel 上一样。
解决方案
定义“打火机”。A-Frame
是建立在WebVR
and上的three.js
,所以如果你不想A-Frame
依赖,也许你可以自己使用three.js
. 它实际上已经有一个如何做到这一点的例子:
https://threejs.org/examples/webgl_panorama_equirectangular.html
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/92/three.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - equirectangular panorama</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
color: #ffffff;
padding: 5px;
font-family:Monospace;
font-size:13px;
font-weight: bold;
text-align:center;
}
a {
color: #ffffff;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js webgl</a> - equirectangular panorama demo. photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank" rel="noopener">Jón Ragnarsson</a>.<br />
drag equirectangular texture into the page.
</div>
<script>
var camera, scene, renderer;
var isUserInteracting = false,
onMouseDownMouseX = 0, onMouseDownMouseY = 0,
lon = 0, onMouseDownLon = 0,
lat = 0, onMouseDownLat = 0,
phi = 0, theta = 0;
init();
animate();
function init() {
var container, mesh;
container = document.getElementById( 'container' );
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
camera.target = new THREE.Vector3( 0, 0, 0 );
scene = new THREE.Scene();
var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 );
// invert the geometry on the x-axis so that all of the faces point inward
geometry.scale( - 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( {
map: new THREE.TextureLoader().load( 'https://farm4.staticflickr.com/3289/2294472375_24a3b8ef46_o.jpg' )
} );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'wheel', onDocumentMouseWheel, false );
//
document.addEventListener( 'dragover', function ( event ) {
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
}, false );
document.addEventListener( 'dragenter', function ( event ) {
document.body.style.opacity = 0.5;
}, false );
document.addEventListener( 'dragleave', function ( event ) {
document.body.style.opacity = 1;
}, false );
document.addEventListener( 'drop', function ( event ) {
event.preventDefault();
var reader = new FileReader();
reader.addEventListener( 'load', function ( event ) {
material.map.image.src = event.target.result;
material.map.needsUpdate = true;
}, false );
reader.readAsDataURL( event.dataTransfer.files[ 0 ] );
document.body.style.opacity = 1;
}, false );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseDown( event ) {
event.preventDefault();
isUserInteracting = true;
onMouseDownMouseX = event.clientX;
onMouseDownMouseY = event.clientY;
onMouseDownLon = lon;
onMouseDownLat = lat;
}
function onDocumentMouseMove( event ) {
if ( isUserInteracting === true ) {
lon = ( onMouseDownMouseX - event.clientX ) * 0.1 + onMouseDownLon;
lat = ( event.clientY - onMouseDownMouseY ) * 0.1 + onMouseDownLat;
}
}
function onDocumentMouseUp( event ) {
isUserInteracting = false;
}
function onDocumentMouseWheel( event ) {
var fov = camera.fov + event.deltaY * 0.05;
camera.fov = THREE.Math.clamp( fov, 10, 75 );
camera.updateProjectionMatrix();
}
function animate() {
requestAnimationFrame( animate );
update();
}
function update() {
if ( isUserInteracting === false ) {
lon += 0.1;
}
lat = Math.max( - 85, Math.min( 85, lat ) );
phi = THREE.Math.degToRad( 90 - lat );
theta = THREE.Math.degToRad( lon );
camera.target.x = 500 * Math.sin( phi ) * Math.cos( theta );
//camera.target.y = 500 * Math.cos( phi ); // comment this to disable Y axis
camera.target.z = 500 * Math.sin( phi ) * Math.sin( theta );
camera.lookAt( camera.target );
/*
// distortion
camera.position.copy( camera.target ).negate();
*/
renderer.render( scene, camera );
}
</script>
</body>
</html>
推荐阅读
- c# - 实时图表 WPF 自定义控件:无法将轴标题绑定到依赖属性
- ionic3 - 未能在您的 'PATH' 中找到 'android' 命令。尝试更新您的“PATH”以包含有效 SDK 目录的路径
- pytorch - PyTorch:FasterRCNN/MaskRCNN 在不同 cuda 设备上的不同输出
- r - 基于 POSIXct 值组合两个数据集(数据集的长度不相等!)
- python - 在 Keras 中构建数据时如何使用 repeat() 函数?
- java - 冒泡排序和选择排序
- yarnpkg - 删除 yarn.lock 并通过运行 yarn install 再次生成它是个好主意吗?
- jquery - 如何在 JQGrid 列中添加自定义掩码
- java - 加工编程修改
- postman - 邮递员从服务器接收 callabck 消息