ContentBlock.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <template>
  2. <div>
  3. <!-- <object3d ref="block3d" name="Content" :position="position"> -->
  4. <!-- <mesh ref="block3d" name="ContentBlock" :position="position">
  5. <geometry type="Box" :args="[size.x, size.y, size.z]" />
  6. <material type="MeshLambert" :color="color" :options="opts" />
  7. </mesh> -->
  8. <!-- <mesh
  9. ref="label3d"
  10. name="Content"
  11. :position="position"
  12. :rotation="{
  13. x:deg2rad(rotation.x),
  14. y:deg2rad(rotation.y),
  15. z:deg2rad(rotation.z)
  16. }"
  17. >
  18. <geometry type="Plane" :args="[size.x, size.y]" />
  19. <material type="MeshLambert" :options="label_opts">
  20. <texture :options="label_texture_opts" />
  21. </material>
  22. </mesh> -->
  23. <mesh
  24. v-if="img_canvas"
  25. ref="img3d"
  26. name="Content"
  27. :position="img_position"
  28. :rotation="{
  29. x:deg2rad(rotation.x),
  30. y:deg2rad(rotation.y),
  31. z:deg2rad(rotation.z)
  32. }"
  33. >
  34. <geometry type="Plane" :args="[img_size.x, img_size.y]" />
  35. <material type="MeshLambert" :options="img_opts">
  36. <texture :options="img_texture_opts" />
  37. </material>
  38. </mesh>
  39. <!-- </object3d> -->
  40. </div>
  41. </template>
  42. <script>
  43. import { mapInstanceState, mapInstanceMutations } from '@urbn/vuex-helpers'
  44. // import { mapState, mapGetters } from 'vuex'
  45. // import { VIMEO } from 'api/vimeo-axios'
  46. // import qs from 'querystring'
  47. import * as THREE from 'three'
  48. import mixins from 'components/mixins'
  49. // We'll determine our module based on the moduleName prop on this component
  50. // https://www.npmjs.com/package/@urbn/vuex-helpers
  51. const getModuleName = cmp => `project:${cmp.prtId}`
  52. export default {
  53. name: 'ContentBlock',
  54. mixins: [mixins],
  55. props: { prtId: Number, type: String, data: Object },
  56. data: () => ({
  57. // block3d: null,
  58. project: null,
  59. size: { x: 0, y: 0 },
  60. position: { x: 0, y: 0, z: 0 },
  61. rotation: { x: 0, y: 0, z: 0 },
  62. // opts: {
  63. // side: THREE.DoubleSide
  64. // // wireframe: false,
  65. // // transparent: false,
  66. // // opacity: 0.6
  67. // },
  68. isOpened: false,
  69. // label3d: null,
  70. // label_opts: {
  71. // side: THREE.DoubleSide,
  72. // // wireframe: false,
  73. // transparent: true
  74. // // opacity: 0.6
  75. // // renderOrder: 0
  76. // },
  77. img_opts: {
  78. // side: THREE.DoubleSide,
  79. // wireframe: false,
  80. // transparent: true
  81. // opacity: 0.6
  82. // renderOrder: 0
  83. },
  84. // label_position: { x: 5, y: 5, z: 6 },
  85. // label_canvas: null,
  86. // label_size: null,
  87. preview_img_url: null,
  88. img_canvas: null,
  89. img_size: { x: 0, y: 0 },
  90. img_position: { x: 0, y: 0, z: 0 },
  91. img_padding: 0.25,
  92. face: null
  93. }),
  94. computed: {
  95. ...mapInstanceState(getModuleName, {
  96. projSize: state => state.size,
  97. projPos: state => state.position,
  98. projWall: state => state.wall,
  99. gridsContentPlaced: state => state.gridsContentPlaced,
  100. contents_size_factor: state => state.contents_size_factor,
  101. faces_opaques: state => state.Faces_opaques
  102. }),
  103. color () {
  104. let color = 0x000000
  105. switch (this.type) {
  106. case 'visible':
  107. color = 0x0000ff
  108. break
  109. case 'context':
  110. color = 0x00ffff
  111. break
  112. case 'process':
  113. color = 0xff00ff
  114. break
  115. default:
  116. color = 0xdddddd
  117. }
  118. return color
  119. },
  120. // label_texture_opts () {
  121. // return {
  122. // canvas: this.label_canvas,
  123. // minFilter: THREE.LinearFilter,
  124. // wrapS: THREE.ClampToEdgeWrapping,
  125. // wrapT: THREE.ClampToEdgeWrapping
  126. // }
  127. // },
  128. img_texture_opts () {
  129. return {
  130. canvas: this.img_canvas,
  131. minFilter: THREE.LinearFilter,
  132. wrapS: THREE.ClampToEdgeWrapping,
  133. wrapT: THREE.ClampToEdgeWrapping
  134. }
  135. },
  136. id () {
  137. return this.data.id
  138. }
  139. },
  140. created () {
  141. if (this.data.Vimeo) {
  142. this.preview_img_url = this.data.Vimeo.thumbnail_url_with_play_button
  143. } else if (this.data.Media && this.data.Media.length) {
  144. this.preview_img_url = `https://api.anarchive-muntadas.figli.io${this.data.Media[0].url}`
  145. }
  146. // for now display only items that have img to preview
  147. // TODO: what to do with items without images ?
  148. if (this.preview_img_url) {
  149. // this.size.x = this.projWall.winW * this.contents_size_factor
  150. // this.size.y = this.projWall.winH * this.contents_size_factor
  151. // get the right size depending on screen
  152. let u
  153. if (window.innerWidth > window.innerHeight) {
  154. u = window.innerHeight / window.innerWidth
  155. } else {
  156. u = window.innerWidth / window.innerHeight
  157. }
  158. this.size.x = this.projWall.winW * this.contents_size_factor
  159. this.size.y = this.projWall.winH * u * this.contents_size_factor
  160. // console.log(`windowW :${window.innerWidth}, windowH :${window.innerHeight}, winW : ${this.projWall.winW}, winH : ${this.projWall.winH}, size.x :${this.size.x}, size.y :${this.size.y}`)
  161. if (typeof this.data.country.id !== 'undefined') {
  162. console.log('this.data.country.id', this.data.country.id)
  163. switch (parseInt(this.data.country.id)) {
  164. case 1:
  165. this.face = 'left'
  166. break
  167. case 2:
  168. this.face = 'back'
  169. break
  170. case 3:
  171. this.face = 'right'
  172. break
  173. }
  174. } else {
  175. let face = Math.random()
  176. switch (this.faces_opaques) {
  177. case 1:
  178. this.face = 'back'
  179. break
  180. case 2:
  181. // RANDOMLY USE 2 FACES
  182. if (face < 0.5) {
  183. this.face = 'left'
  184. } else {
  185. this.face = 'right'
  186. }
  187. break
  188. case 3:
  189. // RANDOMLY USE 3 FACES
  190. if (face < 0.33) {
  191. this.face = 'left'
  192. } else if (face >= 0.33 && face < 0.66) {
  193. this.face = 'back'
  194. } else {
  195. this.face = 'right'
  196. }
  197. break
  198. case 4:
  199. // RANDOMLY USE 4 FACES
  200. if (face < 0.25) {
  201. this.face = 'left'
  202. } else if (face >= 0.25 && face < 0.5) {
  203. this.face = 'back'
  204. } else if (face >= 0.5 && face < 0.75) {
  205. this.face = 'right'
  206. } else {
  207. this.face = 'front'
  208. }
  209. break
  210. }
  211. }
  212. console.log('face', this.face)
  213. // define side
  214. let side
  215. switch (this.face) {
  216. case 'back':
  217. case 'front':
  218. side = 'x'
  219. break
  220. case 'left':
  221. case 'right':
  222. side = 'z'
  223. break
  224. }
  225. // Get GRID's CEL
  226. if (typeof this.gridsContentPlaced[side] === 'undefined' ||
  227. typeof this.gridsContentPlaced[side][this.face] === 'undefined' ||
  228. typeof this.gridsContentPlaced[side][this.face][this.type] === 'undefined' ||
  229. typeof this.gridsContentPlaced[side][this.face][this.type][this.id] === 'undefined') {
  230. // First shift grid place (will transfer it from free places to occupied places)
  231. this.shiftGrid({ face: this.face, type: this.type, id: this.id })
  232. }
  233. // rotation and position
  234. let gridPos = this.gridsContentPlaced[side][this.face][this.type][this.id]
  235. console.log('gridPos', gridPos)
  236. switch (side) {
  237. case 'x':
  238. this.position.x = this.projPos.x - this.projSize.x / 2 + gridPos.x
  239. this.position.y = this.projPos.y - this.projSize.y / 2 + gridPos.y
  240. switch (this.face) {
  241. case 'front':
  242. this.position.z = this.projPos.z + this.projSize.z / 2 - 0.1
  243. this.rotation.y = 180
  244. break
  245. case 'back':
  246. this.position.z = this.projPos.z - this.projSize.z / 2 + 0.1
  247. this.rotation.y = 0
  248. break
  249. }
  250. break
  251. case 'z':
  252. this.position.z = this.projPos.z - this.projSize.z / 2 + gridPos.z
  253. this.position.y = this.projPos.y - this.projSize.y / 2 + gridPos.y
  254. switch (this.face) {
  255. case 'left':
  256. this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  257. this.rotation.y = 90
  258. break
  259. case 'right':
  260. this.position.x = this.projPos.x + this.projSize.x / 2 - 0.1
  261. this.rotation.y = -90
  262. break
  263. }
  264. break
  265. }
  266. // console.log(this.data.Media[0].url)
  267. // create image object with a promise (async img loading)
  268. this.createImgCanvas(this.preview_img_url)
  269. .then(({ img, canvas }) => {
  270. // console.log('THEN img loaded ok', this, img, canvas)
  271. // canvas is way bigger than displayable for good resolution
  272. // reduce it keeping good resolution
  273. // and fit the width and height to the screen
  274. let f
  275. if (canvas.width >= canvas.height) {
  276. f = this.size.x / canvas.width
  277. this.img_size.x = canvas.width * f - this.img_padding * 2
  278. this.img_size.y = this.size.y = canvas.height * f - this.img_padding * 2
  279. } else {
  280. f = this.size.y / canvas.height
  281. this.img_size.y = canvas.height * f - this.img_padding * 2
  282. this.img_size.x = this.size.x = canvas.width * f - this.img_padding * 2
  283. }
  284. console.log(`size.x :${this.size.x}, size.y :${this.size.y}, canvas.width: ${canvas.width}, canvas.height: ${canvas.height}, f: ${f}, img_size.x: ${this.img_size.x}, img_size.y: ${this.img_size.y}`)
  285. this.img_position.y = this.position.y - this.size.y / 2 - this.img_size.y / 2
  286. switch (this.face) {
  287. case 'left':
  288. this.img_position.z = this.position.z + this.size.x / 2 - this.img_size.x / 2
  289. this.img_position.x = this.position.x
  290. break
  291. case 'back':
  292. this.img_position.x = this.position.x - this.size.x / 2 + this.img_size.x / 2
  293. this.img_position.z = this.position.z
  294. break
  295. case 'right':
  296. this.img_position.z = this.position.z - this.size.x / 2 + this.img_size.x / 2
  297. this.img_position.x = this.position.x
  298. break
  299. case 'front':
  300. this.img_position.x = this.position.x + this.size.x / 2 - this.img_size.x / 2
  301. this.img_position.z = this.position.z
  302. break
  303. }
  304. this.img_canvas = canvas
  305. this.curObj.needsUpdate()
  306. // this.imgObj = this.$refs.img3d.curObj
  307. // // record self references
  308. // this.imgObj.userData = {
  309. // vnode: this
  310. // }
  311. })
  312. .catch(function () {
  313. console.warn('CATCH img load ERROR')
  314. })
  315. }
  316. // this.img_position = { ...this.position }
  317. // this.img_position.y -= this.size.y
  318. },
  319. mounted () {
  320. // this.block3d = this.$refs.block3d.curObj
  321. // this.block3d.castShadow = true
  322. // this.block3d.receiveShadow = true
  323. },
  324. updated () {
  325. if (!this.curObj && this.$refs.img3d) {
  326. this.curObj = this.$refs.img3d.curObj
  327. // record self references
  328. this.curObj.userData = {
  329. vnode: this
  330. }
  331. }
  332. },
  333. methods: {
  334. ...mapInstanceMutations(getModuleName, {
  335. shiftGrid: 'shiftGrid'
  336. }),
  337. open () {
  338. this.isOpened = true
  339. // this.loadContents()
  340. }
  341. }
  342. }
  343. </script>