App.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. Muntadas
  10. </h1>
  11. </div>
  12. </header>
  13. <section role="main-content">
  14. <div class="wrapper">
  15. <renderer :obj="myRenderer" :size="size">
  16. <scene :obj="myScene" @update:obj="handleScene">
  17. <!-- <SkyBox :size="{x:500,y:500,z:500}" :position="{x:0,y:0,z:0}" :color="0x992222" /> -->
  18. <!-- <orbit-controls ref="cam_controls" :position="init_cam_pos" :rotation="{ x: 0, y: 0, z: 0 }">
  19. <camera />
  20. </orbit-controls> -->
  21. <camera ref="camera" :position="init_cam_pos" :rotation="{x:0, y:0, z:0}" />
  22. <!-- <light :hex="0xffffff" :intensity="5" :position="{x:-100,y:150,z:50}" />
  23. <light :hex="0xffffff" :intensity="2" :position="{x:100,y:-150,z:-50}" :options="light_opts" /> -->
  24. <animation :fn="animate" :speed="3" />
  25. <template v-if="debug">
  26. <cube :size="{x:30,y:1,z:1}" :position="{x:15,y:0,z:0}" :color="0x992222" />
  27. <cube :size="{x:1,y:30,z:1}" :position="{x:0,y:15,z:0}" :color="0x00BBFF" />
  28. <cube :size="{x:1,y:1,z:30}" :position="{x:0,y:0,z:15}" :color="0x17d100" />
  29. </template>
  30. <ground :size="{w:5000,h:5000}" :position="{x:0,y:0,z:0}" :color="0x133f52" :rotation="{x:90,y:0,z:0}" />
  31. <template v-if="projects.length">
  32. <project
  33. v-for="(project, index) in projects"
  34. :key="project.id"
  35. :data="project"
  36. :len="projects.length"
  37. :index="index"
  38. />
  39. </template>
  40. </scene>
  41. </renderer>
  42. <Content v-if="content_data" :data="content_data" @onClose="onCloseContent" />
  43. </div>
  44. </section>
  45. <footer />
  46. </div>
  47. </template>
  48. <script>
  49. import { mapState, mapActions } from 'vuex'
  50. import mixins from 'components/mixins'
  51. import * as THREE from 'three'
  52. import TWEEN from '@tweenjs/tween.js'
  53. import BgVertex from 'assets/glsl/BgVertex'
  54. import SkyFragment from 'assets/glsl/SkyFragment'
  55. import WaterFragment from 'assets/glsl/WaterFragment'
  56. import Ground from './components/objects/Ground'
  57. import Cube from './components/objects/Cube'
  58. import Project from './components/objects/Project'
  59. import Content from './components/Content'
  60. // const TWEEN = require('@tweenjs/tween.js')
  61. const _debug = false
  62. export default {
  63. metaInfo: {
  64. // if no subcomponents specify a metaInfo.title, this title will be used
  65. title: 'Home',
  66. // all titles will be injected into this template
  67. titleTemplate: '%s | Muntadas'
  68. },
  69. components: {
  70. Cube,
  71. Project,
  72. Ground,
  73. Content
  74. },
  75. mixins: [mixins],
  76. data () {
  77. // const envcolor = 0xffffff
  78. let renderer = new THREE.WebGLRenderer({ alpha: false, antialias: true })
  79. renderer.setClearColor(0x000000, 0)
  80. renderer.shadowMap.enabled = true
  81. // scene obj is well overwrited but background color not visible
  82. let scene = new THREE.Scene()
  83. scene.background = new THREE.Color(0x1a1a1a)
  84. // scene.fog = new THREE.Fog(new THREE.Color(0xffffff), 50, 200)
  85. // console.log('myScene', scene)
  86. // SKYDOME
  87. // https://github.com/mrdoob/three.js/blob/master/examples/webgl_lights_hemisphere.html
  88. // https://gamedevelopment.tutsplus.com/tutorials/a-beginners-guide-to-coding-graphics-shaders--cms-23313
  89. var uniforms = {
  90. 'topColor': { value: new THREE.Color(0x0077ff) },
  91. 'horizontColor': { value: new THREE.Color(0xffffff) },
  92. 'waterColor': { value: new THREE.Color(0x13719a) },
  93. 'bottomColor': { value: new THREE.Color(0x000000) },
  94. 'offset': { value: 33 },
  95. 'wateroffset': { value: 33 },
  96. 'exponent': { value: 0.6 },
  97. 'waterexponent': { value: 0.6 }
  98. }
  99. // SphereBufferGeometry(radius : Float, widthSegments : Integer, heightSegments : Integer, phiStart : Float, phiLength : Float, thetaStart : Float, thetaLength : Float)
  100. var bgGeo = new THREE.SphereBufferGeometry(900, 32, 15, 0, 2 * Math.PI, 0, 0.5 * Math.PI)
  101. var skyMat = new THREE.ShaderMaterial({
  102. uniforms: uniforms,
  103. vertexShader: BgVertex,
  104. fragmentShader: SkyFragment,
  105. side: THREE.BackSide
  106. })
  107. var sky = new THREE.Mesh(bgGeo, skyMat)
  108. scene.add(sky)
  109. var waterMat = new THREE.ShaderMaterial({
  110. uniforms: uniforms,
  111. vertexShader: BgVertex,
  112. fragmentShader: WaterFragment,
  113. side: THREE.BackSide
  114. })
  115. var water = new THREE.Mesh(bgGeo, waterMat)
  116. water.rotateZ(this.deg2rad(180))
  117. scene.add(water)
  118. // SEA
  119. // lights
  120. var sun = new THREE.PointLight(0xfcfcf9, 2)
  121. sun.position.set(-150, 150, 110)
  122. sun.castShadows = true
  123. // sun.target.position.set(0, 0, 0)
  124. scene.add(sun)
  125. // scene.add(sun.target)
  126. var sun2 = new THREE.PointLight(0xfcfcf9, 0.6)
  127. sun2.position.set(150, 150, 110)
  128. sun2.castShadows = true
  129. // sun2.target.position.set(0, 0, 0)
  130. scene.add(sun2)
  131. // scene.add(sun2.target)
  132. var sun3 = new THREE.PointLight(0xfcfcf9, 0.6)
  133. sun3.position.set(0, 150, -110)
  134. sun3.castShadows = true
  135. // sun3.target.position.set(0, 0, 0)
  136. scene.add(sun3)
  137. // scene.add(sun2.target)
  138. var sun4 = new THREE.PointLight(0xfcfcf9, 0.6)
  139. sun4.position.set(0, -150, -110)
  140. sun4.castShadows = true
  141. // sun4.target.position.set(0, 0, 0)
  142. scene.add(sun4)
  143. // scene.add(sun2.target)
  144. // RETURN DATA
  145. return {
  146. debug: _debug,
  147. myRenderer: renderer,
  148. myScene: scene,
  149. // mouse_start: new THREE.Vector2(),
  150. // mouse: new THREE.Vector2(),
  151. camera: null,
  152. mouse: new THREE.Vector2(),
  153. controls: {
  154. user_interact: false,
  155. is_dragging: false,
  156. mouse: new THREE.Vector2(),
  157. mouse_start: new THREE.Vector2(),
  158. lon: -91,
  159. lat: 0,
  160. lon_start: 0,
  161. lat_start: 0,
  162. phi: 0,
  163. theta: 0,
  164. cam_target: new THREE.Vector3(0, 0, 0)
  165. },
  166. // cam_controls: null,
  167. init_cam_pos: { x: 0, y: 10, z: 100 },
  168. raycaster: new THREE.Raycaster(),
  169. interactive_objects_names: ['Project', 'Content'],
  170. interactive_objects: [],
  171. light_opts: {
  172. castShadow: true
  173. },
  174. opened_vnode: null,
  175. content_data: null
  176. }
  177. },
  178. computed: {
  179. ...mapState({
  180. projects: state => state.Projects.projects
  181. }),
  182. size () {
  183. return {
  184. w: window.innerWidth,
  185. h: window.innerHeight
  186. }
  187. }
  188. },
  189. created () {
  190. if (!this.projects.length) {
  191. this.getProjects()
  192. }
  193. this.$env3d.scene = this.myScene
  194. this.$env3d.renderer = this.myRenderer
  195. },
  196. mounted () {
  197. console.log('App mounted', this)
  198. console.log('App mounted $env3d', this.$env3d)
  199. this.camera = this.$env3d.camera = this.$refs.camera.curObj
  200. // this.cam_controls = this.$refs.cam_controls.controls
  201. // console.log('cam_controls', this.cam_controls)
  202. // this.cam_controls = new THREE.PointerLockControls(this.camera)
  203. // console.log('this.cam_controls', this.cam_controls)
  204. this.updatedInteractiveObjects()
  205. // CONTROLS
  206. // https://blogs.perficient.com/2020/05/21/3d-camera-movement-in-three-js-i-learned-the-hard-way-so-you-dont-have-to/
  207. document.addEventListener('mousedown', this.onDocMouseDown, false)
  208. document.addEventListener('mousemove', this.onDocMouseMove, false)
  209. document.addEventListener('mouseup', this.onDocMouseup, false)
  210. },
  211. updated () {
  212. this.updatedInteractiveObjects()
  213. },
  214. methods: {
  215. ...mapActions({
  216. getProjects: 'Projects/getProjects'
  217. }),
  218. handleScene (scene) {
  219. console.log('handlescene', scene)
  220. },
  221. updatedInteractiveObjects () {
  222. console.log('updatedInteractiveObjects', this.myScene.children)
  223. this.interactive_objects = []
  224. for (var i = 0; i < this.myScene.children.length; i++) {
  225. if (this.interactive_objects_names.indexOf(this.myScene.children[i].name) !== -1) {
  226. this.interactive_objects.push(this.myScene.children[i])
  227. }
  228. }
  229. // console.log('this.interactive_objects', this.interactive_objects)
  230. },
  231. onDocMouseDown (e) {
  232. // CONTROLS
  233. this.controls.mouse_start.x = e.clientX
  234. this.controls.mouse_start.y = e.clientY
  235. this.controls.lon_start = this.controls.lon
  236. this.controls.lat_start = this.controls.lat
  237. this.controls.user_interact = true
  238. },
  239. onDocMouseMove (e) {
  240. // RAY CASTING : update the mouse variable
  241. this.mouse.x = (e.clientX / window.innerWidth) * 2 - 1
  242. this.mouse.y = -(e.clientY / window.innerHeight) * 2 + 1
  243. // CONTROLS
  244. if (this.controls.user_interact) {
  245. this.controls.lon = (this.controls.mouse_start.x - e.clientX) * 0.1 + this.controls.lon_start
  246. this.controls.lat = (e.clientY - this.controls.mouse_start.y) * 0.1 + this.controls.lat_start
  247. }
  248. },
  249. onDocMouseup (e) {
  250. console.log('onDocumentMouseup', e)
  251. // CONTROLS
  252. this.controls.user_interact = false
  253. // check if event is not a classic html link
  254. if (e.target.className === 'close-btn') {
  255. // console.log('close-btn: vnode', this.opened_vnode)
  256. // this.opened_vnode.isOpened = false
  257. return
  258. }
  259. // INTERACTIONS
  260. this.updatedInteractiveObjects()
  261. // check if draging
  262. if (Math.hypot(e.clientX - this.controls.mouse_start.x, e.clientY - this.controls.mouse_start.y) < 5) {
  263. // update the picking ray with the camera and mouse position
  264. this.raycaster.setFromCamera(this.mouse, this.camera)
  265. // calculate objects intersecting the picking ray
  266. var intersects = this.raycaster.intersectObjects(this.interactive_objects)
  267. // console.log('intersects', intersects)
  268. let object, vnode, toPos
  269. let cam = this.camera
  270. let camPos = { ...this.camera.position }
  271. if (intersects.length) {
  272. // console.log('onDocMouseUp intersects', intersects)
  273. // for (var i = 0; i < intersects.length; i++) {
  274. object = intersects[0].object
  275. vnode = object.userData.vnode
  276. if (object.name === 'Content' && vnode.isOpened) {
  277. this.content_data = vnode.data
  278. }
  279. // close precedent vnode
  280. if (this.opened_vnode && this.opened_vnode.$options._componentTag === 'ContentBlock') {
  281. // console.log('opened_vnode', this.opened_vnode)
  282. this.opened_vnode.isOpened = false
  283. }
  284. // opene current vnode
  285. vnode.isOpened = true
  286. // recorde vnode as precedent vnode
  287. this.opened_vnode = vnode
  288. toPos = { ...object.position }
  289. if (object.name === 'Content') {
  290. // toPos.z += 1
  291. switch (vnode.face) {
  292. case 'left':
  293. toPos.x += 4
  294. break
  295. case 'back':
  296. toPos.z += 4
  297. break
  298. case 'right':
  299. toPos.x -= 4
  300. break
  301. case 'front':
  302. toPos.z -= 4
  303. break
  304. }
  305. // TODO: we need to update lon and lat accordingly when chaging camera orientation
  306. // new TWEEN.Tween(this.controls.cam_target)
  307. // .to(object.position, 2000)
  308. // .start()
  309. }
  310. // toPos.y = 5
  311. new TWEEN.Tween(camPos)
  312. .easing(TWEEN.Easing.Quadratic.InOut)
  313. .to(toPos, 3000)
  314. .onUpdate(function () {
  315. // console.log('tween update', this._object)
  316. cam.position.set(this._object.x, this._object.y, this._object.z)
  317. })
  318. .start()
  319. // console.log('tween', tween)
  320. // break
  321. // }
  322. } else {
  323. // new TWEEN.Tween(this.camera.position)
  324. // .to(this.init_cam_pos, 1000)
  325. // .start()
  326. }
  327. } // end if dragging
  328. },
  329. animate (tt) {
  330. // CONTROLS
  331. this.controls.lat = Math.max(-85, Math.min(85, this.controls.lat))
  332. this.controls.phi = this.deg2rad(90 - this.controls.lat)
  333. this.controls.theta = this.deg2rad(this.controls.lon)
  334. // if (this.controls.user_interact === false) {
  335. // this.controls.lon += 0.1
  336. // }
  337. // if (this.controls.user_interact) {
  338. this.controls.cam_target.x = 500 * Math.sin(this.controls.phi) * Math.cos(this.controls.theta)
  339. this.controls.cam_target.y = 500 * Math.cos(this.controls.phi)
  340. this.controls.cam_target.z = 500 * Math.sin(this.controls.phi) * Math.sin(this.controls.theta)
  341. // }
  342. if (this.camera) {
  343. this.camera.lookAt(this.controls.cam_target)
  344. }
  345. // TWEENS
  346. TWEEN.update()
  347. },
  348. onCloseContent () {
  349. this.content_data = null
  350. }
  351. }
  352. }
  353. </script>
  354. <style lang="scss" scoped>
  355. .container{
  356. // font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  357. max-width: 1200px;
  358. }
  359. </style>