main.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. import '../scss/main.scss'
  2. import '../scss/iphonex1.scss'
  3. import '../scss/swiper-4.1.6.min.css'
  4. import * as THREE from 'three';
  5. import 'three/examples/js/loaders/GLTFLoader';
  6. import Swiper from './lib/swiper-4.1.6.min';
  7. import UTIL from './util';
  8. import Video from './video';
  9. import 'three/examples/js/controls/OrbitControls';
  10. // import textureBox from '../texture/box-location.png'
  11. // 全景图
  12. import negx from '../texture/wzrybg/negx.jpg'
  13. import negy from '../texture/wzrybg/negy.jpg'
  14. import negz from '../texture/wzrybg/negz.jpg'
  15. import posx from '../texture/wzrybg/posx.jpg'
  16. import posy from '../texture/wzrybg/posy.jpg'
  17. import posz from '../texture/wzrybg/posz.jpg'
  18. var container, controls;
  19. var camera, scene, renderer;
  20. var projectiveObj;//定义上次投射到的对象
  21. var raycaster = new THREE.Raycaster();//光线投射器
  22. var mouse = new THREE.Vector2();//二维向量
  23. document.addEventListener('mousemove', function(){
  24. event.preventDefault();
  25. mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  26. mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  27. }, false);
  28. $(document).on('click', function(){
  29. $('.modal').fadeOut()
  30. controls ? controls.autoRotate = true : ''
  31. })
  32. /**
  33. * 添加鼠标点击事件,捕获点击时当前选中的物体
  34. */
  35. window.addEventListener( 'click', function(){
  36. if(projectiveObj){
  37. $('.entry-tips').hide()
  38. controls.autoRotate = false
  39. var Util = new UTIL(camera)
  40. var dataInfo = projectiveObj.dataInfo
  41. $('.modal-left-img').fadeOut()
  42. $('.modal-right').fadeOut()
  43. $('.modal-hide').fadeOut()
  44. if(dataInfo == 'screen') {
  45. Util.CameraTo(0, 0, 150, function() {
  46. $('#modal1').fadeIn()
  47. $('.modal-group1').addClass('fadeInLeft').show()
  48. })
  49. }
  50. if(dataInfo == 'faceId') {
  51. Util.CameraTo(-20, 40, 100, function() {
  52. $('#modal2').fadeIn()
  53. // $('.modal-group2').fadeIn()
  54. $('.modal-group2').addClass('fadeInLeft').show()
  55. })
  56. }
  57. if(dataInfo == 'camera') {
  58. Util.CameraTo(20, 20, 180, function() {
  59. $('#modal3').fadeIn()
  60. $('.modal-group3').addClass('fadeInRight').show()
  61. })
  62. }
  63. if(dataInfo == 'back-camera') {
  64. Util.CameraTo(40, 50, -160, function() {
  65. $('#modal4').fadeIn()
  66. $('.modal-group4').addClass('fadeInRight').show()
  67. })
  68. }
  69. if(dataInfo == 'A11') {
  70. Util.CameraTo(0, 0, -150, function() {
  71. $('#modal5').fadeIn()
  72. $('.modal-group5').addClass('fadeInRight').show()
  73. })
  74. }
  75. }
  76. }, false );
  77. function init() {
  78. container = document.createElement( 'div' );
  79. document.body.appendChild( container );
  80. // 视角:90度
  81. // 镜头宽高比: window.innerWidth / window.innerHeight
  82. // 近平面距离: 0.1
  83. // 远平面距离:1000
  84. camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 0.1, 1000 );
  85. camera.position.z = 200
  86. camera.position.y = 0
  87. camera.position.x = 0
  88. controls = new THREE.OrbitControls( camera );
  89. controls.autoRotate = true
  90. controls.autoRotateSpeed = 0.5
  91. controls.enablePan = false
  92. // controls.enableZoom = false
  93. controls.maxDistance = 300
  94. controls.minDistance = 200
  95. scene = new THREE.Scene();
  96. scene.add( new THREE.HemisphereLight(0xffffff, 0xffffff, 1) );
  97. // envmap
  98. var path = 'iphoneX/bg/';
  99. var format = '.jpg';
  100. var envMap = new THREE.CubeTextureLoader().load( [
  101. posx, negx,
  102. posy, negy,
  103. posz, negz
  104. ] );
  105. scene.background = envMap
  106. // 添加辅助线
  107. // var helper = new THREE.GridHelper( 500, 100 );
  108. // helper.position.y = -50;
  109. // helper.material.opacity = 0.25;
  110. // helper.material.transparent = true;
  111. // scene.add( helper );
  112. //参数设置了三条轴线的长度
  113. // var axes = new THREE.AxisHelper(800);
  114. // scene.add(axes);
  115. // 创建互动点
  116. var geometry = new THREE.SphereGeometry(2,32,32);//盒子模型
  117. var tranGeometry = new THREE.SphereGeometry(6,96,96);//盒子模型
  118. var material = new THREE.MeshLambertMaterial({color: 0xffc000, transparent: true, opacity: 0.8})
  119. var tranMaterial = new THREE.MeshLambertMaterial({color: 0x0000ff, transparent: true, opacity: 0});
  120. var point1 = new THREE.Mesh( geometry, material );
  121. point1.position.set(0,0,8)
  122. scene.add( point1 );
  123. var tranPoint1 = new THREE.Mesh( tranGeometry, tranMaterial );
  124. tranPoint1.position.set(0,0,8)
  125. tranPoint1.dataInfo = 'screen'
  126. scene.add(tranPoint1)
  127. var point2 = new THREE.Mesh( geometry, material );
  128. point2.position.set(-12,119,10)
  129. scene.add( point2 );
  130. var tranPoint2 = new THREE.Mesh( tranGeometry, tranMaterial );
  131. tranPoint2.position.set(-12,119,10)
  132. tranPoint2.dataInfo = 'faceId'
  133. scene.add(tranPoint2)
  134. var point3 = new THREE.Mesh( geometry, material );
  135. point3.position.set(17,119,10)
  136. scene.add( point3 );
  137. var tranPoint3 = new THREE.Mesh( tranGeometry, tranMaterial );
  138. tranPoint3.position.set(17,119,10)
  139. tranPoint3.dataInfo = 'camera'
  140. scene.add(tranPoint3)
  141. var point4 = new THREE.Mesh( geometry, material );
  142. point4.position.set(45,98,-10)
  143. scene.add( point4 );
  144. var tranPoint4 = new THREE.Mesh( tranGeometry, tranMaterial );
  145. tranPoint4.position.set(45,98,-10)
  146. tranPoint4.dataInfo = 'back-camera'
  147. scene.add(tranPoint4)
  148. var point5 = new THREE.Mesh( geometry, material );
  149. point5.position.set(-26,55,-12)
  150. scene.add( point5 );
  151. var tranPoint5 = new THREE.Mesh( tranGeometry, tranMaterial );
  152. tranPoint5.position.set(-26,55,-12)
  153. tranPoint5.dataInfo = 'A11'
  154. scene.add(tranPoint5)
  155. // 加载3D模型
  156. var loader = new THREE.GLTFLoader();
  157. // var modelPath = 's/3dmodel/iphoneX/scene.gltf'
  158. var localPath = 'iphonex1/scene.gltf'
  159. loader.load( localPath, function ( object ) {
  160. object.scene.traverse( function ( child ) {
  161. if ( child.isMesh ) {
  162. child.material.envMap = envMap;
  163. console.log(child)
  164. }
  165. } );
  166. scene.add( object.scene );
  167. });
  168. renderer = new THREE.WebGLRenderer({
  169. antialias: true,
  170. alpha: true
  171. });
  172. renderer.setClearColor(0x000000, 0);
  173. renderer.setPixelRatio( window.devicePixelRatio );
  174. renderer.setSize( window.innerWidth, window.innerHeight );
  175. container.appendChild( renderer.domElement );
  176. window.addEventListener( 'resize', resize, false );
  177. }
  178. function resize() {
  179. camera.aspect = window.innerWidth / window.innerHeight;
  180. camera.updateProjectionMatrix();
  181. renderer.setSize( window.innerWidth, window.innerHeight );
  182. }
  183. function animate() {
  184. if(controls) {
  185. controls.update();
  186. }
  187. renderRaycasterObj(raycaster,scene,camera,mouse);//渲染光投射器投射到的对象
  188. renderer.render( scene, camera );
  189. //重新渲染标签位置
  190. var Util = new UTIL(camera)
  191. Util.windowVector(0, 0, 8, 'modal1');
  192. Util.windowVector(-12,119,10, 'modal2');
  193. Util.windowVector(17,119,10, 'modal3');
  194. Util.windowVector(45,98,-10, 'modal4');
  195. Util.windowVector(-26,55,-12, 'modal5');
  196. requestAnimationFrame( animate );
  197. }
  198. /**
  199. * 根据光投射器判断鼠标所在向量方向是否穿过物体
  200. */
  201. function renderRaycasterObj(raycaster,scene,camera,mouse) {
  202. raycaster.setFromCamera(mouse, camera);
  203. var intersects = raycaster.intersectObjects(scene.children);
  204. if (intersects.length > 0) {
  205. var currentProjectiveObjT = intersects[0].object;
  206. if (projectiveObj != currentProjectiveObjT) {
  207. if((currentProjectiveObjT instanceof THREE.AxisHelper) || (currentProjectiveObjT instanceof THREE.GridHelper)){
  208. //穿过的是坐标轴线和网格线
  209. return;
  210. }
  211. projectiveObj = intersects[0].object;
  212. }
  213. } else {
  214. projectiveObj = null;
  215. }
  216. }
  217. // 弹窗根随球体
  218. // function modalAnimate() {
  219. // var Util = new UTIL(camera)
  220. // //重新渲染标签位置
  221. // Util.windowVector(0, 0, 8, 'modal1');
  222. // Util.windowVector(-12,119,10, 'modal2');
  223. // Util.windowVector(17,119,10, 'modal3');
  224. // Util.windowVector(45,98,-10, 'modal4');
  225. // Util.windowVector(-26,55,-12, 'modal5');
  226. // //每帧渲染
  227. // // renderer.render(scene, camera);
  228. // requestAnimationFrame(modalAnimate);
  229. // }
  230. init();
  231. animate();
  232. // modalAnimate()
  233. // ---- 弹窗交互相关 ------
  234. var frontCameraSwiper = null
  235. var screenSwiper = null
  236. var planeSwiper = null
  237. var faceSwiper = null
  238. var backCameraSwiper = null
  239. var View = {
  240. init() {
  241. this.setVideoSrc()
  242. this.handleModal()
  243. },
  244. setVideoSrc() {
  245. var vids = [8904623, 8904999, 8908143, 8908435, 8908441,8908437,8908443,
  246. 8909221, 8909229,8925097]
  247. var videoEls = {
  248. '8904623': 'face-video1',
  249. '8904999': 'face-video2',
  250. '8908143': 'back-camera-video',
  251. '8908435': 'front-camera-video',
  252. '8908441': 'front-camera-video2',
  253. '8908437': 'front-camera-video3',
  254. '8908443': 'front-camera-video4',
  255. '8909221': 'screen-video1',
  256. '8909229': 'screen-video2',
  257. '8925097': 'suggest-video',
  258. }
  259. Video.getVideoSource(vids, videoEls)
  260. },
  261. phoneChange() {
  262. var flag = true
  263. var $phone = $('.iphone-pic')
  264. var $phoneDown = $('.down-iphone-pic')
  265. timer = setInterval(() => {
  266. if(flag) {
  267. flag = false
  268. $phone.fadeOut('slow')
  269. $phoneDown.fadeIn('slow')
  270. } else {
  271. flag = true
  272. $phone.fadeIn('slow')
  273. $phoneDown.fadeOut('slow')
  274. }
  275. }, 6000);
  276. },
  277. videoPause() {
  278. var videoList = document.getElementsByTagName('video')
  279. for(var i = 0; i < videoList.length; i++) {
  280. videoList[i].pause()
  281. }
  282. },
  283. handleModal() {
  284. var $videoCon = $('.full-video-wrap')
  285. $('.close-full-video').click(function() {
  286. $(this).parent().hide()
  287. $videoCon.find('video').hide()
  288. var videoList = document.getElementsByTagName('video')
  289. for(var i = 0; i < videoList.length; i++) {
  290. videoList[i].pause()
  291. }
  292. })
  293. // 屏幕
  294. $('#modal1-poster1').click(function() {
  295. $('#screen-video1').show()
  296. $videoCon.show()
  297. document.getElementById('screen-video1').play()
  298. })
  299. $('#screen-know-more').click(function() {
  300. $('.screen-swiper').removeClass('fadeOutDown').addClass('fadeInUp').show()
  301. if(!screenSwiper) {
  302. View.swiperScreenCamera()
  303. }
  304. document.getElementById('screen-video2').currentTime = 0
  305. document.getElementById('screen-video2').play()
  306. })
  307. $('#close-screen-swiper').click(function() {
  308. View.videoPause()
  309. $('.screen-swiper').removeClass('fadeInUp').addClass('fadeOutDown')
  310. screenSwiper.slideTo(0, 0, false)
  311. })
  312. $('#reset-screen-video2').click(function() {
  313. document.getElementById('screen-video2').currentTime = 0
  314. document.getElementById('screen-video2').play()
  315. })
  316. // faceid
  317. $('#modal2-poster1').click(function() {
  318. $('#face-video1').show()
  319. $videoCon.show()
  320. document.getElementById('face-video1').play()
  321. })
  322. $('#modal2-poster2').click(function() {
  323. $('#face-video2').show()
  324. $videoCon.show()
  325. document.getElementById('face-video2').play()
  326. })
  327. $('#face-know-more').click(function() {
  328. $('.face-swiper').removeClass('fadeOutDown').addClass('fadeInUp').show()
  329. if(!faceSwiper) {
  330. View.swiperFace()
  331. }
  332. })
  333. $('#close-face-swiper').click(function() {
  334. $('.face-swiper').removeClass('fadeInUp').addClass('fadeOutDown')
  335. faceSwiper.slideTo(0, 0, false)
  336. })
  337. // 前置摄像头
  338. $('#modal3-poster').click(function() {
  339. $('#front-camera-video').show()
  340. $videoCon.show()
  341. document.getElementById('front-camera-video').play()
  342. })
  343. $('#modal3-poster1').click(function() {
  344. $('#front-camera-video3').show()
  345. $videoCon.show()
  346. document.getElementById('front-camera-video3').play()
  347. })
  348. $('#camera-know-more').click(function() {
  349. $('.camera-swiper').removeClass('fadeOutDown').addClass('fadeInUp').show()
  350. if(!frontCameraSwiper) {
  351. View.swiperFrontCamera()
  352. }
  353. })
  354. $('#close-camera-siwper').click(function() {
  355. View.videoPause()
  356. $('.camera-swiper').removeClass('fadeInUp').addClass('fadeOutDown')
  357. frontCameraSwiper.slideTo(0, 0, false)
  358. })
  359. // 后置摄像头
  360. $('#modal4-poster').click(function() {
  361. $('#back-camera-video').show()
  362. $videoCon.show()
  363. document.getElementById('back-camera-video').play()
  364. })
  365. $('#backcamera-know-more').click(function() {
  366. $('.back-camera-swiper').removeClass('fadeOutDown').addClass('fadeInUp').show()
  367. if(!backCameraSwiper) {
  368. View.swiperBackCamera()
  369. }
  370. })
  371. $('#close-backcamera-swiper').click(function() {
  372. $('.back-camera-swiper').removeClass('fadeInUp').addClass('fadeOutDown')
  373. backCameraSwiper.slideTo(0, 0, false)
  374. })
  375. // 玻璃后盖
  376. $('#plane-know-more').click(function() {
  377. $('.plane-swiper').removeClass('fadeOutDown').addClass('fadeInUp').show()
  378. if(!planeSwiper) {
  379. View.swiperPlane()
  380. }
  381. })
  382. $('#close-plane-swiper').click(function() {
  383. $('.plane-swiper').removeClass('fadeInUp').addClass('fadeOutDown')
  384. planeSwiper.slideTo(0, 0, false)
  385. })
  386. // 全屏视频结束自动退出
  387. var videoBox = document.getElementsByClassName('full-video-wrap')[0]
  388. var videos = videoBox.getElementsByTagName('video')
  389. for(var i = 0; i < videos.length; i++) {
  390. videos[i].addEventListener('ended', function() {
  391. $videoCon.hide()
  392. $videoCon.find('video').hide()
  393. })
  394. }
  395. // 评测视频
  396. $('#suggest-watch').click(function() {
  397. $('#suggest-video').show()
  398. $videoCon.show()
  399. document.getElementById('suggest-video').play()
  400. })
  401. // 进入扩展页,停止转动和缩放
  402. $('.bottom-more-btn').find('button').click(function(e) {
  403. controls.enableZoom = false
  404. return false
  405. })
  406. $('.close-swiper-btn').click(function() {
  407. controls.enableZoom = true
  408. return false
  409. })
  410. },
  411. swiperFrontCamera() {
  412. var ipxVideo = document.getElementById('front-camera-video2')
  413. var ipxVideo4 = document.getElementById('front-camera-video4')
  414. // 前置
  415. frontCameraSwiper = new Swiper('.swiper-front-camera',{
  416. direction : 'vertical',
  417. speed:800,
  418. mousewheel: true,
  419. pagination: {
  420. el: '.swiper-pagination',
  421. clickable: true,
  422. },
  423. on:{
  424. slideChangeTransitionStart: function(){
  425. // alert(this.activeIndex);
  426. if(this.activeIndex == 1) {
  427. ipxVideo.play()
  428. } else {
  429. ipxVideo.pause()
  430. }
  431. if(this.activeIndex == 2) {
  432. ipxVideo4.play()
  433. } else {
  434. ipxVideo4.pause()
  435. }
  436. }
  437. }
  438. });
  439. // 重播
  440. $('#reset-front-video2').click(function() {
  441. ipxVideo.currentTime = 0
  442. ipxVideo.play()
  443. })
  444. $('#reset-front-video4').click(function() {
  445. ipxVideo4.currentTime = 0
  446. ipxVideo4.play()
  447. })
  448. },
  449. swiperScreenCamera() {
  450. var ipxVideo = document.getElementById('screen-video2')
  451. screenSwiper = new Swiper('#screen-swiper',{
  452. direction : 'vertical',
  453. speed:800,
  454. mousewheel: true,
  455. pagination: {
  456. el: '.swiper-pagination',
  457. clickable: true,
  458. },
  459. on:{
  460. slideChangeTransitionStart: function(){
  461. if(this.activeIndex == 0) {
  462. setTimeout(() => {
  463. ipxVideo.play()
  464. }, 500);
  465. } else {
  466. ipxVideo.pause()
  467. }
  468. }
  469. }
  470. });
  471. },
  472. swiperPlane() {
  473. planeSwiper = new Swiper('.swiper-plane',{
  474. direction : 'vertical',
  475. speed:800,
  476. mousewheel: true,
  477. pagination: {
  478. el: '.swiper-pagination',
  479. clickable: true,
  480. },
  481. on:{
  482. }
  483. });
  484. },
  485. swiperFace() {
  486. faceSwiper = new Swiper('.swiper-face',{
  487. direction : 'vertical',
  488. speed:800,
  489. mousewheel: true,
  490. pagination: {
  491. el: '.swiper-pagination',
  492. clickable: true,
  493. },
  494. on:{
  495. }
  496. });
  497. },
  498. swiperBackCamera() {
  499. backCameraSwiper = new Swiper('.swiper-back-camera',{
  500. direction : 'vertical',
  501. speed:800,
  502. mousewheel: true,
  503. pagination: {
  504. el: '.swiper-pagination',
  505. clickable: true,
  506. },
  507. on:{
  508. }
  509. });
  510. }
  511. }
  512. View.init()