App.vue 13 KB

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