91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

three.js實現炫酷的全景3D重力感應

發布時間:2020-09-16 18:43:00 來源:腳本之家 閱讀:481 作者:Aimee芊 欄目:web開發

本文實例為大家分享了three.js 全景重力感應的具體代碼,供大家參考,具體內容如下

實現three.js 全景圖 demo

使用three.js 寫了球體和圓柱體版本的3D重力感應全景圖,支持手指觸摸和陀螺儀感應,也支持PC端的鼠標。給大家介紹一下基于移動端H5球體的實現方法,圓柱體類似。

設置容器和展示的樣式
設置容器的寬高為全屏展示,清除body的margin,引用three.min.js(3D渲染框架) 和orienter.js (陀螺儀經緯度計算)

three.js實現炫酷的全景3D重力感應

<div id="CanvasBody"></div>

<script src="js/three.min.js"></script>
<!--重力感應-->
<script src="js/orienter.js"></script>
<!--動畫效果-->
<script src="js/tween.js"></script>
<!-- 代碼 -->
body {margin: 0;}
html, body, #CanvasBody {width: 100vw;height: 100vh;overflow: hidden;}
#CanvasBody {position: relative;}

設置html的data-dpr 屬性,設置html 的fontSize

設置html的fontSize,重新計算body的實際可展示尺寸,這樣可以使渲染出來的畫面更清晰,分辨率最完美。

(function(_window) {
 var navigatorUserAgent = navigator.userAgent;
 var iPhone = navigatorUserAgent.indexOf("iPhone");
 if (iPhone > -1) {
  var dpr = Number(window.devicePixelRatio),
    one_dpr = 1 / dpr
 } else {
  var dpr = 1,
   one_dpr = 1
 }
 var writeText = "<meta name=\"viewport\" content=\"width=device-width,initial-scale=" + one_dpr + ",maximum-scale=" + one_dpr + ",minimum-scale=" + one_dpr + ",user-scalable=no\">\n  <meta name=\"'flexible\" content=\"initial-dpr=" + dpr + "\">";
 document.write(writeText);
 var html = document.getElementsByTagName("html");
 var F0 = 75;
 html[0].setAttribute("data-dpr", dpr);
 var getFontSize = function getFontSize() {
  var windowWidth = window.innerWidth;
  html[0].style.fontSize = F0 * windowWidth / 750 + "px"
 };
 getFontSize();
 _window.addEventListener("resize", getFontSize, false)
})(window);

定義相關變量

var camera,//攝像機
 scene,//舞臺
 renderer,//渲染器
 isUserInteracting = false,//用戶是否正在操作
 onMouseDownMouseX = 0, onMouseDownMouseY = 0,//鼠標點擊的x和Y坐標
 lon = 0, onMouseDownLon = 0, onPointerDownLon= 0.0,onPointerDownPointerX = 0,//經度
 lat = 0, onMouseDownLat = 0, onPointerDownLat= 0.0,onPointerDownPointerY = 0,//緯度
 phi = 0, theta = 0,//計算相機位置的重要參數
 o = new Orienter(),//陀螺儀方法對象
 new_longitude=0,last_longitude=0,move_longitude=0,//改變的經度的計算
 new_latitude=0,last_latitude=0,move_latitude=0,//改變的緯度的計算
 is_touch=false,is_start=false,isPlay=true,isMusicPlay=true,tsa=100.1,ppl='';
 var raycaster = new THREE.Raycaster();//拾取場景里面的物體,可判斷點擊或交互事件對應的元素
 var mouse = new THREE.Vector2();//二維向量的對象,鼠標計算

初始化舞臺的元素和內容

圖片的長寬控制在4096px以內,部分機型性能不夠,渲染不了超大的圖片

 function init() {/**初始化**/
    var container, mesh;//容器和素材
    container = document.getElementById( 'CanvasBody' );//容器
    camera = new THREE.PerspectiveCamera( 72, window.innerWidth / window.innerHeight, 0.01, 1100 );//相機
    camera.target = new THREE.Vector3( 0, 0, 0 );//相機位置

    scene = new THREE.Scene();//舞臺
    scene.updateMatrixWorld(true);
    var geometry = new THREE.SphereGeometry(1, 32, 16);//球體

    geometry.scale( 1, 1, -1 );
    //設置球體的背景貼圖
    var textureBg = new THREE.TextureLoader().load("img/bg.jpg");
    textureBg.generateMipmaps = true;
    textureBg.magFilter = THREE.LinearFilter;//設置貼紙素材的質量
    textureBg.minFilter = THREE.LinearFilter;
    var material = new THREE.MeshBasicMaterial( {
      map: textureBg,//圓柱體貼圖,全景圖
      //color:0xFF0000,
      //transparent: true
    } );
    mesh = new THREE.Mesh( geometry, material );
    //這里可以設置對應的動畫效果
//    new TWEEN.Tween( mesh).to( {transform:"rotate(90deg)"}, 800 ).repeat( false ).delay( 300 ).yoyo( true ).easing( TWEEN.Easing.Cubic.InOut ).start();
    scene.add( mesh );

    // 旋轉預設 攝影機看到的角度 Start//
    scene.rotation.set(0,0,0); //首頁

    //初始化渲染器,追加到容器
    renderer = new THREE.WebGLRenderer({precision: 'highp' ,mipmap: 'highp',antialias:false});//加上precision 和 mipmap參數,調整畫面清晰度
    renderer.setPixelRatio( window.devicePixelRatio );//設置像素比
    renderer.setSize( window.innerWidth, window.innerHeight );//設置渲染窗口的大小
    container.appendChild( renderer.domElement );//追加到容器中去

    //鼠標、手機touch的各個事件
    document.addEventListener( 'mousedown', onDocumentMouseDown, false );
    document.addEventListener( 'mousemove', onDocumentMouseMove, false );
    document.addEventListener( 'mouseup', onDocumentMouseUp, false );

    document.addEventListener( 'touchstart', onDocumentTouchDown, false );
    document.addEventListener( 'touchmove', onDocumentTouchMove, false );
    document.addEventListener( 'touchend', onDocumentTouchUp, 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 );
  }

監聽的各事件和方法

//監聽橫豎屏重新設置尺寸
  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;

    // Click action
    mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
    raycaster.setFromCamera( mouse, camera );
    var intersects = raycaster.intersectObjects( scene.children );//第一個是最上面一層的元素
    console.log("點擊的元素",intersects);
    if ( intersects.length > 0 ) {//如果點到小圓點 就執行回調函數回調函數為goto_p
      try {
        intersects[0].object.callback();
      }
      catch(err) {}
    }
  }
  function onDocumentMouseMove( event ) {
    if ( isUserInteracting === true ) {
      lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
      lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
    }
  }
  function onDocumentMouseUp( event ) {
    isUserInteracting = false;
  }

  // touch event start
  function onDocumentTouchDown( event ) {
    is_touch=true;

    event.preventDefault();
    isUserInteracting = true;
    onPointerDownPointerX = event.touches[ 0 ].clientX;
    onPointerDownPointerY = event.touches[ 0 ].clientY;
    if(is_start){
      onPointerDownLon = lon;
      onPointerDownLat = lat;
    }
    // For Click action

    mouse.x = ( onPointerDownPointerX / renderer.domElement.clientWidth ) * 2 - 1;
    mouse.y = - ( onPointerDownPointerY / renderer.domElement.clientHeight ) * 2 + 1;

    raycaster.setFromCamera( mouse, camera );
    var intersects = raycaster.intersectObjects( scene.children );
    console.log('touchDown',lon,lat);
    if ( intersects.length > 0 ) {
      try {
        intersects[0].object.callback();
      }
      catch(err) {}
    }
  }
  function onDocumentTouchMove( event ) {
    if(is_start){
      if ( isUserInteracting === true ) {

        lon = ( onPointerDownPointerX - event.touches[ 0 ].clientX ) * 0.1 + onPointerDownLon;
        lat = ( event.touches[ 0 ].clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
      }
    }
  }
  function onDocumentTouchUp( event ) {
    is_touch=false;
  }
  // touch event end

  // set
  function onDocumentTouchDown2( event ) {
    tsa = event.touches[0].clientY;
    console.log( '@:'+event.touches[0].clientY );
    event.preventDefault();

  }
  function onDocumentMouseWheel( event ) {
    camera.fov += event.deltaY * 0.05;
    camera.updateProjectionMatrix();
  }

動畫播放和陀螺儀

function animate() {//播放動畫
    if(isPlay){
      TWEEN.update();
      update();
      requestAnimationFrame( animate );
    }

  }
  o.onOrient = function (obj) {//重力感應計算角度
    if(is_start){
      //最新經度
      new_longitude = obj.lon;
      move_longitude=new_longitude-last_longitude;

      //最新緯度
      new_latitude = obj.lat;
      move_latitude = new_latitude-last_latitude;

      //判斷經緯度
      if(move_longitude>=300){
        move_longitude=move_longitude-361;
      }else if(move_longitude<=-300){
        move_longitude=move_longitude+359;
      }


      if(move_latitude>=300){
        move_latitude=move_latitude-361;
      }else if(move_latitude<=-300){
        move_latitude=move_latitude+359;
      }

      if( is_touch ){
        move_longitude=0;
        move_latitude=0;
      }else{
        move_longitude=move_longitude*0.6;
        move_latitude=move_latitude*0.6;
      }
      //計算得出重力感應的經緯度
      lon=lon-move_longitude;
      last_longitude = obj.lon;
      lat = lat-move_latitude;
      last_latitude = obj.lat;
    }

  };

  function update() {//更新攝像機位置,旋轉平移
    //lat = Math.max( -6, Math.min( 6, lat ) );//設置lat緯度的范圍,只在一個范圍內旋轉
    phi = THREE.Math.degToRad( 90 - lat );
    theta = THREE.Math.degToRad( lon );
    camera.target.x = 500 * Math.sin( phi ) * Math.cos( theta );//X軸的坐標
    camera.target.y = 500 * Math.cos( phi );//y軸的坐標
    camera.target.z = 500 * Math.sin( phi ) * Math.sin( theta ) ;//z軸的坐標
    camera.lookAt( camera.target );
    renderer.render( scene, camera );//重新渲染
  }

執行所有

//執行所有
  is_start=true;
  init();
  o.init();
  animate();

綜上,炫酷的3D重力感應H5就出來啦!

源碼 GitHub

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

张家川| 临夏县| 红桥区| 东山县| 宣城市| 建德市| 精河县| 泸州市| 黄骅市| 阳新县| 锦州市| 山东省| 利津县| 雅安市| 石河子市| 玉环县| 定南县| 贡嘎县| 莎车县| 伊宁市| 哈巴河县| 宜君县| 双流县| 昌黎县| 峨山| 铁岭县| 黔南| 眉山市| 谷城县| 安新县| 犍为县| 东宁县| 民勤县| 兴安县| 伊金霍洛旗| 南岸区| 侯马市| 连州市| 巴林右旗| 云浮市| 剑川县|