project.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // https://codeburst.io/dynamic-modules-with-vuex-and-vue-b9c481ca792
  2. // https://www.brophy.org/post/instance-aware-vuex-modules-1/
  3. // import qs from 'querystring'
  4. // import { REST } from 'api/rest-axios'
  5. import { GRAPHQL } from 'api/graphql-axios'
  6. import * as THREE from 'three'
  7. import { ThreeBSP } from 'three-js-csg-es6'
  8. export default {
  9. namespaced: true,
  10. // initial state
  11. state: {
  12. obj3d: null,
  13. size: { x: 0, y: 0, z: 0 },
  14. position: { x: 0, y: 0, z: 0 },
  15. walls3dObj: null,
  16. wallsPos: null,
  17. top3dObj: null,
  18. topPos: null,
  19. topColor: null,
  20. floor3dObj: null,
  21. floorPos: null,
  22. florrColor: null,
  23. wall: {
  24. wallW: 0.001,
  25. // dig windows on face and back
  26. winW: 2 + Math.random() * 2,
  27. winH: 4 + Math.random() * 4,
  28. margin: 2,
  29. nbrWinX: 0,
  30. paddingX: 0,
  31. nbrWinY: 0,
  32. paddingY: 0,
  33. nbrWinZ: 0,
  34. paddingZ: 0
  35. },
  36. contents: {},
  37. contents_size_factor: 2, // factor to get the contents (grid) size proportional to windows
  38. contentTypes: ['visible', 'context', 'process', 'concept'],
  39. grid: { visible: [], context: [], process: [], concept: [] },
  40. gridContentPlaced: {}
  41. },
  42. // getters
  43. getters: {
  44. position: (state) => {
  45. return state.position
  46. },
  47. size: (state) => {
  48. return state.size
  49. },
  50. createGradientCanvas: (state) => (c1, c2) => {
  51. var ctx = document.createElement('canvas').getContext('2d')
  52. ctx.canvas.width = 1024
  53. ctx.canvas.height = 1024
  54. var lingrad = ctx.createLinearGradient(0, 0, 0, 1024)
  55. lingrad.addColorStop(0, c1)
  56. lingrad.addColorStop(1, c2)
  57. ctx.fillStyle = lingrad
  58. ctx.fillRect(0, 0, 1024, 1024)
  59. return ctx.canvas
  60. }
  61. },
  62. // mutations
  63. mutations: {
  64. setSize: (state, size) => {
  65. state.size = size
  66. },
  67. setPosition: (state, pos) => {
  68. state.position = pos
  69. },
  70. setWalls3dObj: (state, obj) => { state.walls3dObj = obj },
  71. setWallsPos: (state, pos) => { state.wallsPos = pos },
  72. setTop3dObj: (state, obj) => { state.top3dObj = obj },
  73. setTopPos: (state, pos) => { state.topPos = pos },
  74. setFloor3dObj: (state, obj) => { state.floor3dObj = obj },
  75. setFloorPos: (state, pos) => { state.floorPos = pos },
  76. setTopColor: (state, col) => { state.topColor = col },
  77. setFloorColor: (state, col) => { state.floorColor = col },
  78. setContents: (state, contents) => { state.contents = contents },
  79. setGrid: (state, grid) => { state.grid = grid },
  80. shiftGrid: (state, c) => {
  81. let p = state.grid[c.type].shift()
  82. if (!state.gridContentPlaced[c.type]) {
  83. state.gridContentPlaced[c.type] = {}
  84. }
  85. state.gridContentPlaced[c.type][c.id] = p
  86. }
  87. },
  88. // actions
  89. actions: {
  90. init ({ dispatch, commit, state, rootGetters }) {
  91. console.log('Init Project module', state.id)
  92. dispatch('sizingBuilding')
  93. dispatch('build3dObjs')
  94. },
  95. sizingBuilding ({ dispatch, commit, state, rootGetters }) {
  96. console.log('sizingBuilding')
  97. let totalW = rootGetters['Projects/totalW']
  98. // console.log('totalW', totalW)
  99. let margin = rootGetters['Projects/marginBetweenBuildings']
  100. // console.log('margin', margin)
  101. // positioning buildings on x regarding the widths
  102. // & setting up the window sizing
  103. // & setting up the content grid
  104. // let wall, a
  105. // let grid
  106. // X POS
  107. let x
  108. if (state.index === 0) {
  109. // if it's the first
  110. x = -1 * totalW / 2 + state.size.x / 2
  111. } else {
  112. // else get the precedent pos
  113. let prevProjID = rootGetters['Projects/projectID'](state.index - 1)
  114. // console.log('prevProjID', prevProjID)
  115. let prevProjPos = rootGetters[`project:${prevProjID}/position`]
  116. // console.log(`project:${prevProjID}/position.x`, prevProjPos.x)
  117. let prevProjSize = rootGetters[`project:${prevProjID}/size`]
  118. // console.log(`project:${prevProjID}/size.x`, prevProjSize.x)
  119. // console.log('state.size.x', state.size.x)
  120. // prev X + alf of prev size x + margin + half of current size x
  121. x = prevProjPos.x + prevProjSize.x / 2 + margin + state.size.x / 2
  122. }
  123. // console.log('x', x)
  124. commit('setPosition', {
  125. x: x,
  126. // y: -1 * state.size.y / 2 + 10 + Math.random() * 30, // -10 + Math.random() * this.size.y / 2
  127. y: -state.size.y / 4,
  128. z: -10 + Math.random() * 10
  129. })
  130. // WINDOWS
  131. let a = 0
  132. state.wall.nbrWinX = Math.floor((state.size.x - 2 * state.wall.margin) / state.wall.winW)
  133. // removing windows on X until padding is enough
  134. while (state.wall.paddingX < 0.4) {
  135. state.wall.nbrWinX -= a
  136. state.wall.paddingX = (state.size.x - 2 * state.wall.margin - state.wall.winW * state.wall.nbrWinX) / (state.wall.nbrWinX - 1)
  137. a++
  138. }
  139. a = 0
  140. state.wall.nbrWinY = Math.floor((state.size.y - 2 * state.wall.margin) / state.wall.winH)
  141. // removing windows on Y until padding is enough
  142. while (state.wall.paddingY < 0.4) {
  143. state.wall.nbrWinY -= a
  144. state.wall.paddingY = (state.size.y - 2 * state.wall.margin - state.wall.winH * state.wall.nbrWinY) / (state.wall.nbrWinY - 1)
  145. a++
  146. }
  147. // CONTENTS GRID
  148. a = 0
  149. state.wall.nbrWinZ = Math.floor((state.size.z - 2 * state.wall.margin) / state.wall.winW)
  150. while (state.wall.paddingZ < 0.4) {
  151. state.wall.nbrWinZ -= a
  152. state.wall.paddingZ = (state.size.z - 2 * state.wall.margin - state.wall.winW * state.wall.nbrWinZ) / (state.wall.nbrWinZ - 1)
  153. a++
  154. }
  155. let grid = {}
  156. let rows = state.wall.nbrWinY / state.contents_size_factor
  157. let cols = state.wall.nbrWinZ / state.contents_size_factor
  158. let t
  159. for (var m = 0; m < rows; m++) { // rows
  160. if (m > rows / 4 * 3) {
  161. t = state.contentTypes[0]
  162. } else if (m > rows / 4 * 2) {
  163. t = state.contentTypes[1]
  164. } else if (m > rows / 4) {
  165. t = state.contentTypes[2]
  166. } else {
  167. t = state.contentTypes[3]
  168. }
  169. if (!grid[t]) {
  170. grid[t] = []
  171. }
  172. for (var l = 0; l < cols; l++) { // cols
  173. grid[t].push({
  174. z: margin + state.wall.winW * state.contents_size_factor * l,
  175. y: margin + state.wall.winH * state.contents_size_factor * m
  176. })
  177. }
  178. }
  179. console.log('grid', grid)
  180. // shuffle the grids
  181. for (var i = 0; i < state.contentTypes.length; i++) {
  182. for (let n = grid[state.contentTypes[i]].length - 1; n > 0; n--) {
  183. const o = Math.floor(Math.random() * n)
  184. const temp = grid[state.contentTypes[i]][n]
  185. grid[state.contentTypes[i]][n] = grid[state.contentTypes[i]][o]
  186. grid[state.contentTypes[i]][o] = temp
  187. }
  188. }
  189. console.log('shuffeld grid', grid)
  190. commit('setGrid', grid)
  191. console.log('state.grid', state.grid)
  192. },
  193. build3dObjs ({ dispatch, commit, state, getters, rootGetters }) {
  194. console.log('build3dObjs')
  195. // http://learningthreejs.com/blog/2011/12/10/constructive-solid-geometry-with-csg-js/
  196. let backGeom = new THREE.BoxGeometry(state.size.x, state.size.y, state.wall.wallW)
  197. let backMesh = new THREE.Mesh(backGeom)
  198. backMesh.position.z = -0.5 * state.size.z
  199. let backBSP = new ThreeBSP(backMesh)
  200. // https://medium.com/techtrument/multithreading-javascript-46156179cf9a
  201. let winGeom = new THREE.BoxGeometry(state.wall.winW, state.wall.winH, state.wall.wallW)
  202. let winMesh = new THREE.Mesh(winGeom)
  203. let windowsGeom = new THREE.Geometry()
  204. for (var i = 0; i < state.wall.nbrWinX; i++) {
  205. for (var j = 0; j < state.wall.nbrWinY; j++) {
  206. winMesh.position.z = -0.5 * state.size.z
  207. winMesh.position.x = -0.5 * state.size.x + state.wall.margin + state.wall.winW * 0.5 + i * (state.wall.winW + state.wall.paddingX)
  208. winMesh.position.y = 0.5 * state.size.y - state.wall.margin - state.wall.winH * 0.5 - j * (state.wall.winH + state.wall.paddingY)
  209. // winMesh.updateMatrix()
  210. windowsGeom.mergeMesh(winMesh)
  211. }
  212. }
  213. let windowsBSP = new ThreeBSP(windowsGeom)
  214. backBSP = backBSP.subtract(windowsBSP)
  215. // let winBSP
  216. // for (var i = 0; i < state.wall.nbrWinX; i++) {
  217. // for (var j = 0; j < state.wall.nbrWinY; j++) {
  218. // winMesh.position.z = -0.5 * state.size.z
  219. // winMesh.position.x = -0.5 * state.size.x + state.wall.margin + state.wall.winW * 0.5 + i * (state.wall.winW + state.wall.paddingX)
  220. // winMesh.position.y = 0.5 * state.size.y - state.wall.margin - state.wall.winH * 0.5 - j * (state.wall.winH + state.wall.paddingY)
  221. // winBSP = new ThreeBSP(winMesh)
  222. // backBSP = backBSP.subtract(winBSP)
  223. // }
  224. // }
  225. backMesh = backBSP.toMesh()
  226. let faceMesh = backMesh.clone()
  227. faceMesh.position.z = 0.5 * state.size.z
  228. let faceBSP = new ThreeBSP(faceMesh)
  229. let rightGeom = new THREE.BoxGeometry(state.wall.wallW, state.size.y, state.size.z)
  230. let rightMesh = new THREE.Mesh(rightGeom)
  231. rightMesh.position.x = 0.5 * state.size.x
  232. // rightMesh.position.z = 0.5 * state.size.z
  233. let rightBSP = new ThreeBSP(rightMesh)
  234. let leftMesh = rightMesh.clone()
  235. leftMesh.position.x = -0.5 * state.size.x
  236. // leftMesh.position.z = 0.5 * state.size.z
  237. let leftBSP = new ThreeBSP(leftMesh)
  238. let buildingBSP = backBSP.union(rightBSP)
  239. buildingBSP = buildingBSP.union(faceBSP)
  240. buildingBSP = buildingBSP.union(leftBSP)
  241. // buildingBSP = buildingBSP.union(topBSP)
  242. // buildingBSP = buildingBSP.union(floorBSP)
  243. // convert back to three.js mesh
  244. let building = buildingBSP.toMesh()
  245. // create a classical material for building
  246. // let topColor = `hsla(201, 100%, 95%, 1)`
  247. let hTop = Math.round(195 + Math.random() * 10)
  248. let sTop = Math.round(100)
  249. let lTop = Math.round(95)
  250. let hFloor = Math.round(205 + Math.random() * 10)
  251. let sFloor = Math.round(40 + Math.random() * 20)
  252. let lFloor = Math.round(5 + Math.random() * 15)
  253. let topColor = `hsla(${hTop}, ${sTop}%, ${lTop}%, 1)`
  254. commit('setTopColor', topColor)
  255. let floorColor = `hsla(${hFloor}, ${sFloor}%, ${lFloor}%, 1)`
  256. commit('setFloorColor', floorColor)
  257. let gradientTexture = new THREE.CanvasTexture(getters.createGradientCanvas(topColor, floorColor))
  258. let materialOpts = {
  259. color: 0xffffff,
  260. side: THREE.DoubleSide,
  261. shininess: 30,
  262. map: gradientTexture
  263. }
  264. building.material = new THREE.MeshPhongMaterial(materialOpts)
  265. // commiting walls
  266. commit('setWalls3dObj', building)
  267. let buildingPos = { ...state.position, ...{ z: state.position.z - 0.5 * state.size.z } }
  268. commit('setWallsPos', buildingPos)
  269. // TOP & FLOOR
  270. let topGeom = new THREE.BoxGeometry(state.size.x, state.wall.wallW, state.size.z)
  271. let topOpts = {
  272. color: new THREE.Color(`hsl(${hTop}, ${sTop}%, ${lTop}%)`),
  273. shininess: 30
  274. }
  275. let topMat = new THREE.MeshPhongMaterial(topOpts)
  276. let topMesh = new THREE.Mesh(topGeom, topMat)
  277. commit('setTop3dObj', topMesh)
  278. let topPosition = { ...state.position, ...{ y: state.position.y + 0.5 * state.size.y } }
  279. commit('setTopPos', topPosition)
  280. let floorOpts = {
  281. color: new THREE.Color(`hsl(${hFloor}, ${sFloor}%, ${lFloor}%)`),
  282. shininess: 10
  283. }
  284. let floorMat = new THREE.MeshPhongMaterial(floorOpts)
  285. let floorMesh = new THREE.Mesh(topGeom, floorMat)
  286. commit('setFloor3dObj', floorMesh)
  287. let floorPosition = { ...state.position, ...{ y: state.position.y - 0.5 * state.size.y } }
  288. commit('setFloorPos', floorPosition)
  289. //
  290. // // LEVELS
  291. // let levelGeom = new THREE.BoxGeometry(state.size.x, 1, state.size.z)
  292. // let levelMesh = new THREE.Mesh(levelGeom)
  293. // let levelBSP = new ThreeBSP(levelMesh)
  294. // let levelHoleGeom = new THREE.BoxGeometry(state.size.x - 3, 1, state.size.z - 3)
  295. // let levelHoleMesh = new THREE.Mesh(levelHoleGeom)
  296. // let levelHoleBSP = new ThreeBSP(levelHoleMesh)
  297. // levelBSP = levelBSP.subtract(levelHoleBSP)
  298. // levelMesh = levelBSP.toMesh()
  299. // let levelOpts = {
  300. // color: 0xff0000,
  301. // shininess: 10
  302. // }
  303. // let levelMat = new THREE.MeshPhongMaterial(levelOpts)
  304. // levelMesh.material = levelMat
  305. },
  306. loadContents ({ dispatch, commit, state }) {
  307. console.log('loadContents')
  308. GRAPHQL.post('', { query: `query {
  309. project(id: "${state.id}") {
  310. visibles(where: { Published: "true" }){
  311. id
  312. Name
  313. Media {
  314. url
  315. size
  316. }
  317. Text2
  318. Vimeo
  319. Url
  320. categories
  321. }
  322. contexts(where: { Published: "true" }){
  323. id
  324. Name
  325. Images {
  326. url
  327. size
  328. }
  329. Text2
  330. Vimeo
  331. Url
  332. }
  333. processes(where: { Published: "true" }){
  334. id
  335. Name
  336. Media {
  337. url
  338. size
  339. }
  340. Text2
  341. Vimeo
  342. Url
  343. }
  344. concepts(where: { Published: "true" }){
  345. id
  346. Name
  347. Images {
  348. url
  349. size
  350. }
  351. Text2
  352. Vimeo
  353. }
  354. }
  355. }` })
  356. .then(({ data: { data: { project } = null } }) => {
  357. console.log('graphql contents', project)
  358. commit('setContents', project)
  359. // dispatch('computeProjects', projects)
  360. })
  361. .catch((error) => {
  362. console.warn('Issue with getProjects', error)
  363. Promise.reject(error)
  364. })
  365. }
  366. // getGridPos ({ state, commit }) {
  367. // let p = state.grid[0]
  368. // commit('shiftGrid')
  369. // Promise.resolve(p)
  370. // }
  371. }
  372. }