main.js 17 KB

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