Threes.js入門篇之9 - 全景圖
全景圖是一個當下非常流行的展示方式,在 VR | AR領域有廣泛應用。
在三維內的 全景圖 通常是根據 滑鼠事件調整攝像機位置(在包圍球內旋轉),其基本思路一致,包圍球可以通過 球面、立方體來實現,在幾何上面貼上全景圖紋理,一個典型的全景紋理圖可能是下面這幾種:
或者也有可能是多副圖的形式,組成一個Cube(根據實際的方式會有所不同)。
概念就不解釋了,我們先來看下 多魚眼全景的展示效果(webgl_panorama_dualfisheye.html):
參考程式碼:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - dual fisheye 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: #000000;
- padding: 5px;
- font-family:Monospace;
- font-size:13px;
- font-weight: bold;
- text-align:center;
- }
- a {
- color: #0000ff;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="http://threejs.org" target="_blank">three.js webgl</a> - dualfisheye panorama.
- </div>
- <script src="../build/three.js"></script>
- <script>
- var camera, scene, renderer;
- var isUserInteracting = false,
- onMouseDownMouseX = 0, onMouseDownMouseY = 0,
- lon = 0, onMouseDownLon = 0,
- lat = 0, onMouseDownLat = 0,
- phi = 0, theta = 0,
- distance = 500;
- init();
- animate();
- function init() {
- var container, mesh;
- container = document.getElementById( 'container' );
- camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
- scene = new THREE.Scene();
- var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 ).toNonIndexed();
- geometry.scale( - 1, 1, 1 );
- // Remap UVs
- var normals = geometry.attributes.normal.array;
- var uvs = geometry.attributes.uv.array;
- for ( var i = 0, l = normals.length / 3; i < l; i ++ ) {
- var x = normals[ i * 3 + 0 ];
- var y = normals[ i * 3 + 1 ];
- var z = normals[ i * 3 + 2 ];
- if ( i < l / 2 ) {
- var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
- uvs[ i * 2 + 0 ] = x * ( 404 / 1920 ) * correction + ( 447 / 1920 );
- uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
- } else {
- var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( - y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
- uvs[ i * 2 + 0 ] = - x * ( 404 / 1920 ) * correction + ( 1460 / 1920 );
- uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
- }
- }
- geometry.rotateZ( - Math.PI / 2 );
- //
- var texture = new THREE.TextureLoader().load( 'textures/ricoh_theta_s.jpg' );
- texture.format = THREE.RGBFormat;
- var material = new THREE.MeshBasicMaterial( { map: texture } );
- 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 );
- //
- 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;
- onPointerDownPointerX = event.clientX;
- onPointerDownPointerY = event.clientY;
- onPointerDownLon = lon;
- onPointerDownLat = lat;
- }
- function onDocumentMouseMove( event ) {
- if ( isUserInteracting === true ) {
- lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
- lat = ( onPointerDownPointerY - event.clientY ) * 0.1 + onPointerDownLat;
- }
- }
- function onDocumentMouseUp( event ) {
- isUserInteracting = false;
- }
- function onDocumentMouseWheel( event ) {
- distance += event.deltaY * 0.05;
- }
- 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 - 180 );
- camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
- camera.position.y = distance * Math.cos( phi );
- camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
- camera.lookAt( scene.position );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
相關文章
- Threes.js入門篇之2 - Hello WorldJS
- Threes.js入門篇之7 - 場景光照JS
- Threes.js入門篇之4 - World View ProjectionJSViewProject
- Threes.js入門篇之6 - 場景漫遊JS
- Threes.js入門篇之8 - 材質與紋理JS
- Threes.js入門篇之3 - 場景與相機JS
- Threes.js入門篇之5 - 場景操縱器TrackballJS
- node之tcp篇入門理解TCP
- 效能測試之入門篇
- Android 自定義 View 之入門篇AndroidView
- Flink 入門篇之 寫個WordCount
- WindowsServerVersion1709管理之入門篇WindowsServer
- 一、Ansible基礎之入門篇
- Springboot快速入門篇,圖文並茂Spring Boot
- 深入理解Java SPI之入門篇Java
- 入門設計模式之彙總篇設計模式
- 踩坑指南:入門OpenTenBase之部署篇
- 入門篇-其之七-Java運算子Java
- 前端入門9-JavaScript語法之運算子前端JavaScript
- 如何建立價值流圖(VSM)?--入門篇
- openresty前端開發入門三之JSON篇REST前端JSON
- openresty前端開發入門四之Redis篇REST前端Redis
- openresty前端開發入門五之Mysql篇REST前端MySql
- Three.js入門篇之1 - WebGL on HTMLJSWebHTML
- 踩坑指南:入門OpenTenBase之監控篇
- WebSocket系列之基礎知識入門篇Web
- Spring Boot -01- 快速入門篇(圖文教程)Spring Boot
- openlayers入門開發系列之地圖工具欄篇地圖
- 第45篇 ARM架構的全景圖瞭解架構
- llvm入門篇LVM
- Redis 入門篇Redis
- Spark入門篇Spark
- plan9 assembly 入門
- 爬蟲不得不學之 JavaScript 入門篇爬蟲JavaScript
- 入門篇-其之十-流程控制之迴圈結構
- 基於 HTML5 網路拓撲圖的快速開發之入門篇(一)HTML
- 基於 HTML5 網路拓撲圖的快速開發之入門篇(二)HTML
- Flutter入門篇(一)Flutter