You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThreeAnime.vue 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <div>
  3. <div id="console"></div>
  4. <div id="container"></div>
  5. </div>
  6. </template>
  7. <script>
  8. import {
  9. AbsoluteOrientationSensor
  10. // RelativeOrientationSensor
  11. } from 'motion-sensors-polyfill'
  12. import * as THREE from 'three'
  13. // import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader';
  14. // import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader';
  15. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
  16. const params = new URLSearchParams(new URL(window.location.href).search.slice(1))
  17. // const relative = !!Number(params.get("relative"));
  18. const coordinateSystem = params.get('coord')
  19. // let container, camera, scene, renderer, model;
  20. let container, camera, scene, renderer, cube, sensor
  21. export default {
  22. name: 'HelloWorld',
  23. props: {
  24. msg: String
  25. },
  26. data: function () {
  27. return {
  28. }
  29. },
  30. mounted: function () {
  31. this.initScene()
  32. if (navigator.permissions) {
  33. // https://w3c.github.io/orientation-sensor/#model
  34. Promise.all([navigator.permissions.query({ name: 'accelerometer' }),
  35. navigator.permissions.query({ name: 'magnetometer' }),
  36. navigator.permissions.query({ name: 'gyroscope' })])
  37. .then(results => {
  38. if (results.every(result => result.state === 'granted')) {
  39. this.initSensor()
  40. } else {
  41. console.log('Permission to use sensor was denied.')
  42. }
  43. }).catch(err => {
  44. console.log('Integration with Permissions API is not enabled, still try to start app.' + err)
  45. this.initSensor()
  46. })
  47. } else {
  48. console.log('No Permissions API, still try to start app.')
  49. this.initSensor()
  50. }
  51. // this.renderScene();
  52. this.animate()
  53. const log = console.log
  54. console.log = (message, ...rest) => {
  55. const div = document.querySelector('#console')
  56. div.innerText = message
  57. log.call(console, message, ...rest)
  58. }
  59. console.log('mounted')
  60. },
  61. methods: {
  62. calcPosFromLatLonRad(lat,lon,radius){
  63. var phi = (90-lat)*(Math.PI/180);
  64. var theta = (lon+180)*(Math.PI/180);
  65. var x, y,z
  66. x = -((radius) * Math.sin(phi)*Math.cos(theta));
  67. z = ((radius) * Math.sin(phi)*Math.sin(theta));
  68. y = ((radius) * Math.cos(phi));
  69. console.log([x,y,z]);
  70. return [x,y,z];
  71. },
  72. createRandomPoints() {
  73. var meshes=[];
  74. for(var i = 0 ; i < 10 ; i++){
  75. var geometry = new THREE.SphereGeometry(0.025, 20, 20)
  76. var material = new THREE.MeshBasicMaterial({
  77. color: new THREE.Color('white')
  78. })
  79. var mesh = new THREE.Mesh(geometry, material)
  80. meshes.push(mesh);
  81. }
  82. return meshes
  83. },
  84. initScene () {
  85. container = document.getElementById('container')
  86. // document.body.appendChild(container);
  87. camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 100)
  88. camera.position.z = 10
  89. scene = new THREE.Scene()
  90. scene.background = new THREE.Color(0x00f0ff)
  91. // var ambientLight = new THREE.AmbientLight(0x404040, 10);
  92. // scene.add(ambientLight);
  93. // camera.position.set( 0, 250, 1000 );
  94. scene.add(camera)
  95. scene.add(new THREE.AmbientLight(0xf0f0f0))
  96. var light = new THREE.SpotLight(0xffffff, 1.5)
  97. light.position.set(0, 10, 0)
  98. light.angle = Math.PI * 0.2
  99. light.castShadow = true
  100. light.shadow.camera.near = 20
  101. light.shadow.camera.far = 50
  102. light.shadow.bias = -0.222
  103. light.shadow.mapSize.width = 10
  104. light.shadow.mapSize.height = 10
  105. scene.add(light)
  106. var helper = new THREE.GridHelper(2000, 1000)
  107. helper.position.y = -20
  108. helper.material.opacity = 0.7
  109. helper.material.transparent = false
  110. // helper.material.color = 0x0f00f0
  111. scene.add(helper)
  112. helper = new THREE.GridHelper(2000, 500)
  113. helper.position.y = 20
  114. helper.material.opacity = 1
  115. helper.material.transparent = true
  116. scene.add(helper)
  117. renderer = new THREE.WebGLRenderer({ antialias: true })
  118. renderer.setPixelRatio(window.devicePixelRatio)
  119. renderer.setSize(window.innerWidth * 0.8, window.innerHeight * 0.8)
  120. renderer.shadowMap.enabled = true
  121. var controls = new OrbitControls(camera, renderer.domElement)
  122. controls.target.set(0, 0, 0)
  123. controls.update()
  124. container.appendChild(renderer.domElement)
  125. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  126. var material = new THREE.MeshPhongMaterial({
  127. map : THREE.ImageUtils.loadTexture('./img/map/night.jpg'),
  128. bumpMap: new THREE.ImageUtils.loadTexture('./img/map/bump.jpg' ),
  129. specularMap: new THREE.ImageUtils.loadTexture( './img/map/spekular.jpg' ),
  130. })
  131. var mesh = new THREE.Mesh(geometry, material)
  132. scene.add(mesh)
  133. var latlons = [[40.7142700,-74.0059700], [52.5243700,13.4105300]];
  134. var meshes= this.createRandomPoints()
  135. for(var i = 0; i< meshes.length; i++ ){
  136. scene.add(meshes[i])
  137. var latlon=latlons[Math.floor(Math.random()*latlons.length)];
  138. var latlonpoint = this.calcPosFromLatLonRad(latlon[0],latlon[1], 0.5);
  139. meshes[i].position.set(latlonpoint[0],latlonpoint[1],latlonpoint[2]);
  140. }
  141. },
  142. initSensor () {
  143. const options = { frequency: 60, coordinateSystem }
  144. console.log(JSON.stringify(options))
  145. sensor = new AbsoluteOrientationSensor(options)
  146. sensor.onreading = () => cube.quaternion.fromArray(sensor.quaternion).inverse()
  147. sensor.onerror = (event) => {
  148. if (event.error.name === 'NotReadableError') {
  149. console.log('Sensor is not available.')
  150. }
  151. }
  152. sensor.start()
  153. console.log(sensor)
  154. },
  155. // renderScene() {
  156. // requestAnimationFrame(this.renderScene);
  157. // camera.lookAt(scene.position);
  158. // renderer.render(scene, camera);
  159. // }
  160. animate () {
  161. requestAnimationFrame(this.animate)
  162. // Rotate cube (Change values to change speed)
  163. // cube.rotation.x += 0.01
  164. // cube.position.z -= 0.001
  165. renderer.render(scene, camera)
  166. }
  167. }
  168. }
  169. </script>
  170. <!-- Add "scoped" attribute to limit CSS to this component only -->
  171. <style type="text/css">
  172. /* .garden {
  173. position: relative;
  174. width : 200px;
  175. height: 200px;
  176. border: 5px solid #CCC;
  177. border-radius: 10px;
  178. }
  179. .ball {
  180. position: absolute;
  181. top : 90px;
  182. left : 90px;
  183. width : 20px;
  184. height: 20px;
  185. background: green;
  186. border-radius: 100%;
  187. } */
  188. </style>