App.vue 11 KB

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