App.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <div id="root">
  3. <!-- <header role="banner">
  4. <div class="wrapper">
  5. <h1
  6. class="site-title"
  7. tabindex="0"
  8. >
  9. </h1>
  10. </div>
  11. </header> -->
  12. <section role="main-content">
  13. <div class="wrapper">
  14. <renderer :obj="myRenderer" :size="size">
  15. <scene :obj="myScene" @update:obj="handleScene">
  16. <!-- <SkyBox :size="{x:500,y:500,z:500}" :position="{x:0,y:0,z:0}" :color="0x992222" /> -->
  17. <!-- <orbit-controls ref="cam_controls" :position="init_cam_pos" :rotation="{ x: 0, y: 0, z: 0 }">
  18. <camera />
  19. </orbit-controls> -->
  20. <camera ref="camera" :position="init_cam_pos" :rotation="{x:0, y:0, z:0}" />
  21. <light :hex="0xffffff" :intensity="5" :position="{x:-100,y:150,z:50}" />
  22. <light :hex="0xffffff" :intensity="2" :position="{x:100,y:-150,z:-50}" :options="light_opts" />
  23. <animation :fn="animate" :speed="3" />
  24. <template v-if="debug">
  25. <cube :size="{x:30,y:1,z:1}" :position="{x:15,y:0,z:0}" :color="0x992222" />
  26. <cube :size="{x:1,y:30,z:1}" :position="{x:0,y:15,z:0}" :color="0x00BBFF" />
  27. <cube :size="{x:1,y:1,z:30}" :position="{x:0,y:0,z:15}" :color="0x17d100" />
  28. </template>
  29. <ground :size="{w:5000,h:5000}" :position="{x:0,y:0,z:0}" :color="0x000011" :rotation="{x:90,y:0,z:0}" />
  30. <template v-if="projects.length">
  31. <project
  32. v-for="(project, index) in projects"
  33. :key="project.id"
  34. :data="project"
  35. :len="projects.length"
  36. :index="index"
  37. />
  38. </template>
  39. </scene>
  40. </renderer>
  41. </div>
  42. </section>
  43. <footer />
  44. </div>
  45. </template>
  46. <script>
  47. import { mapState, mapActions } from 'vuex'
  48. import mixins from 'components/mixins'
  49. import * as THREE from 'three'
  50. import TWEEN from '@tweenjs/tween.js'
  51. import BgVertex from 'assets/glsl/BgVertex'
  52. import SkyFragment from 'assets/glsl/SkyFragment'
  53. import WaterFragment from 'assets/glsl/WaterFragment'
  54. import Ground from './components/objects/Ground'
  55. import Cube from './components/objects/Cube'
  56. import Project from './components/objects/Project'
  57. // const TWEEN = require('@tweenjs/tween.js')
  58. const _debug = false
  59. export default {
  60. metaInfo: {
  61. // if no subcomponents specify a metaInfo.title, this title will be used
  62. title: 'Home',
  63. // all titles will be injected into this template
  64. titleTemplate: '%s | Muntadas'
  65. },
  66. components: {
  67. Cube,
  68. Project,
  69. Ground
  70. },
  71. mixins: [mixins],
  72. data () {
  73. // const envcolor = 0xffffff
  74. let renderer = new THREE.WebGLRenderer({ alpha: false, antialias: true })
  75. renderer.setClearColor(0x000000, 0)
  76. renderer.shadowMap.enabled = true
  77. // scene obj is well overwrited but background color not visible
  78. let scene = new THREE.Scene()
  79. scene.background = new THREE.Color(0x1a1a1a)
  80. // scene.fog = new THREE.Fog(new THREE.Color(0xffffff), 50, 200)
  81. // console.log('myScene', scene)
  82. // SKYDOME
  83. // https://github.com/mrdoob/three.js/blob/master/examples/webgl_lights_hemisphere.html
  84. // https://gamedevelopment.tutsplus.com/tutorials/a-beginners-guide-to-coding-graphics-shaders--cms-23313
  85. var uniforms = {
  86. 'topColor': { value: new THREE.Color(0x0077ff) },
  87. 'horizontColor': { value: new THREE.Color(0xffffff) },
  88. 'waterColor': { value: new THREE.Color(0x0000aa) },
  89. 'bottomColor': { value: new THREE.Color(0x000000) },
  90. 'offset': { value: 33 },
  91. 'wateroffset': { value: 33 },
  92. 'exponent': { value: 0.6 },
  93. 'waterexponent': { value: 0.6 }
  94. }
  95. // SphereBufferGeometry(radius : Float, widthSegments : Integer, heightSegments : Integer, phiStart : Float, phiLength : Float, thetaStart : Float, thetaLength : Float)
  96. var bgGeo = new THREE.SphereBufferGeometry(900, 32, 15, 0, 2 * Math.PI, 0, 0.5 * Math.PI)
  97. var skyMat = new THREE.ShaderMaterial({
  98. uniforms: uniforms,
  99. vertexShader: BgVertex,
  100. fragmentShader: SkyFragment,
  101. side: THREE.BackSide
  102. })
  103. var sky = new THREE.Mesh(bgGeo, skyMat)
  104. scene.add(sky)
  105. var waterMat = new THREE.ShaderMaterial({
  106. uniforms: uniforms,
  107. vertexShader: BgVertex,
  108. fragmentShader: WaterFragment,
  109. side: THREE.BackSide
  110. })
  111. var ground = new THREE.Mesh(bgGeo, waterMat)
  112. ground.rotateZ(this.deg2rad(180))
  113. scene.add(ground)
  114. return {
  115. debug: _debug,
  116. myRenderer: renderer,
  117. myScene: scene,
  118. // mouse_start: new THREE.Vector2(),
  119. // mouse: new THREE.Vector2(),
  120. camera: null,
  121. mouse: new THREE.Vector2(),
  122. controls: {
  123. user_interact: false,
  124. mouse: new THREE.Vector2(),
  125. mouse_start: new THREE.Vector2(),
  126. lon: 0,
  127. lat: 0,
  128. lon_start: 0,
  129. lat_start: 0,
  130. phi: 0,
  131. theta: 0,
  132. cam_target: new THREE.Vector3(0, 0, 0)
  133. },
  134. // cam_controls: null,
  135. init_cam_pos: { x: 0, y: 10, z: 100 },
  136. raycaster: new THREE.Raycaster(),
  137. interactive_objects: [],
  138. light_opts: {
  139. castShadow: true
  140. }
  141. }
  142. },
  143. computed: {
  144. ...mapState({
  145. projects: state => state.Projects.projects
  146. }),
  147. size () {
  148. return {
  149. w: window.innerWidth,
  150. h: window.innerHeight
  151. }
  152. }
  153. },
  154. created () {
  155. if (!this.projects.length) {
  156. this.getProjects()
  157. }
  158. },
  159. mounted () {
  160. console.log('mounted', this)
  161. this.camera = this.$refs.camera.curObj
  162. // this.cam_controls = this.$refs.cam_controls.controls
  163. // console.log('cam_controls', this.cam_controls)
  164. // this.cam_controls = new THREE.PointerLockControls(this.camera)
  165. // console.log('this.cam_controls', this.cam_controls)
  166. this.updatedInteractiveObjects()
  167. document.addEventListener('mousedown', this.onDocMouseDown, false)
  168. document.addEventListener('mousemove', this.onDocMouseMove, false)
  169. document.addEventListener('mouseup', this.onDocMouseup, false)
  170. },
  171. updated () {
  172. this.updatedInteractiveObjects()
  173. },
  174. methods: {
  175. ...mapActions({
  176. getProjects: 'Projects/getProjects'
  177. }),
  178. handleScene (scene) {
  179. console.log('handlescene', scene)
  180. },
  181. updatedInteractiveObjects () {
  182. this.interactive_objects = []
  183. for (var i = 0; i < this.myScene.children.length; i++) {
  184. if (this.myScene.children[i].name === 'Project') {
  185. this.interactive_objects.push(this.myScene.children[i])
  186. }
  187. }
  188. // console.log('this.interactive_objects', this.interactive_objects)
  189. },
  190. onDocMouseDown (e) {
  191. // controls
  192. this.controls.mouse_start.x = e.clientX
  193. this.controls.mouse_start.y = e.clientY
  194. this.controls.lon_start = this.controls.lon
  195. this.controls.lat_start = this.controls.lat
  196. this.controls.user_interact = true
  197. },
  198. onDocMouseMove (e) {
  199. // RAY CASTING : update the mouse variable
  200. this.mouse.x = (e.clientX / window.innerWidth) * 2 - 1
  201. this.mouse.y = -(e.clientY / window.innerHeight) * 2 + 1
  202. // CONTROLS
  203. if (this.controls.user_interact) {
  204. // lon = ( onMouseDownMouseX - clientX ) * 0.1 + onMouseDownLon;
  205. this.controls.lon = (this.controls.mouse_start.x - e.clientX) * 0.1 + this.controls.lon_start
  206. // lat = ( clientY - onMouseDownMouseY ) * 0.1 + onMouseDownLat;
  207. this.controls.lat = (e.clientY - this.controls.mouse_start.y) * 0.1 + this.controls.lat_start
  208. }
  209. },
  210. onDocMouseup (e) {
  211. // console.log('onDocumentMouseup', e)
  212. // CONTROLS
  213. this.controls.user_interact = false
  214. // INTERACTIONS
  215. // TODO: check if draging
  216. // update the picking ray with the camera and mouse position
  217. this.raycaster.setFromCamera(this.mouse, this.camera)
  218. // calculate objects intersecting the picking ray
  219. var intersects = this.raycaster.intersectObjects(this.interactive_objects)
  220. // console.log('intersects', intersects)
  221. let object, vnode//, toPos
  222. // let cam = this.camera
  223. // let camPos = { ...this.camera.position }
  224. if (intersects.length) {
  225. for (var i = 0; i < intersects.length; i++) {
  226. object = intersects[i].object
  227. vnode = object.userData.vnode
  228. vnode.isOpened = true
  229. // new TWEEN.Tween(this.controls.cam_target)
  230. // .to(object.position, 400)
  231. // .start()
  232. //
  233. // toPos = { ...object.position }
  234. // toPos.z += 20
  235. // toPos.y = 5
  236. // new TWEEN.Tween(camPos)
  237. // .easing(TWEEN.Easing.Quadratic.InOut)
  238. // .to(toPos, 3000)
  239. // .onUpdate(function () {
  240. // // console.log('tween update', this._object)
  241. // cam.position.set(this._object.x, this._object.y, this._object.z)
  242. // // camControls.update()
  243. // })
  244. // .start()
  245. // console.log('tween', tween)
  246. break
  247. }
  248. } else {
  249. // new TWEEN.Tween(this.camera.position)
  250. // .to(this.init_cam_pos, 1000)
  251. // .start()
  252. }
  253. },
  254. animate (tt) {
  255. this.controls.lat = Math.max(-85, Math.min(85, this.controls.lat))
  256. this.controls.phi = this.deg2rad(90 - this.controls.lat)
  257. this.controls.theta = this.deg2rad(this.controls.lon)
  258. // if (this.controls.user_interact === false) {
  259. // this.controls.lon += 0.1
  260. // }
  261. this.controls.cam_target.x = 500 * Math.sin(this.controls.phi) * Math.cos(this.controls.theta)
  262. this.controls.cam_target.y = 500 * Math.cos(this.controls.phi)
  263. this.controls.cam_target.z = 500 * Math.sin(this.controls.phi) * Math.sin(this.controls.theta)
  264. if (this.camera && this.controls.user_interact) {
  265. this.camera.lookAt(this.controls.cam_target)
  266. }
  267. TWEEN.update()
  268. }
  269. }
  270. }
  271. </script>
  272. <style lang="scss" scoped>
  273. .container{
  274. // font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  275. max-width: 1200px;
  276. }
  277. </style>