ContentBlock.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.width, img_size.height]" />
  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. gridCel: state => state.gridCel
  103. }),
  104. color () {
  105. let color = 0x000000
  106. switch (this.type) {
  107. case 'visible':
  108. color = 0x0000ff
  109. break
  110. case 'context':
  111. color = 0x00ffff
  112. break
  113. case 'process':
  114. color = 0xff00ff
  115. break
  116. default:
  117. color = 0xdddddd
  118. }
  119. return color
  120. },
  121. // label_texture_opts () {
  122. // return {
  123. // canvas: this.label_canvas,
  124. // minFilter: THREE.LinearFilter,
  125. // wrapS: THREE.ClampToEdgeWrapping,
  126. // wrapT: THREE.ClampToEdgeWrapping
  127. // }
  128. // },
  129. img_texture_opts () {
  130. return {
  131. canvas: this.img_canvas,
  132. minFilter: THREE.LinearFilter,
  133. wrapS: THREE.ClampToEdgeWrapping,
  134. wrapT: THREE.ClampToEdgeWrapping
  135. }
  136. },
  137. id () {
  138. return this.data.id
  139. }
  140. },
  141. created () {
  142. if (this.data.Vimeo) {
  143. this.preview_img_url = this.data.Vimeo.thumbnail_url_with_play_button
  144. } else if (this.data.Media && this.data.Media.length) {
  145. this.preview_img_url = `https://api.anarchive-muntadas.figli.io${this.data.Media[0].url}`
  146. } else if (this.data.Images && this.data.Images.length) {
  147. this.preview_img_url = `https://api.anarchive-muntadas.figli.io${this.data.Images[0].url}`
  148. }
  149. // for now display only items that have img to preview
  150. // TODO: what to do with items without images ?
  151. if (this.preview_img_url) {
  152. // this.size.width = this.projWall.winW * this.contents_size_factor
  153. // this.size.height = this.projWall.winH * this.contents_size_factor
  154. // // get the right size depending on screen
  155. // let u
  156. // if (window.innerWidth > window.innerHeight) {
  157. // u = window.innerHeight / window.innerWidth
  158. // } else {
  159. // u = window.innerWidth / window.innerHeight
  160. // }
  161. // this.size.width = this.projWall.winW * this.contents_size_factor
  162. // this.size.height = this.projWall.winH * u * this.contents_size_factor
  163. this.size.width = this.gridCel.width
  164. this.size.height = this.gridCel.height
  165. // console.log(`windowW :${window.innerWidth}, windowH :${window.innerHeight}, winW : ${this.projWall.winW}, winH : ${this.projWall.winH}, size.x :${this.size.width}, size.y :${this.size.height}`)
  166. if (typeof this.data.country !== 'undefined' &&
  167. typeof this.data.country.id !== 'undefined') {
  168. // console.log('this.data.country.id', parseInt(this.data.country.id))
  169. switch (parseInt(this.data.country.id)) {
  170. case 1:
  171. this.face = 'right'
  172. break
  173. case 2:
  174. this.face = 'back'
  175. break
  176. case 3:
  177. this.face = 'left'
  178. break
  179. }
  180. } else {
  181. let face = Math.random()
  182. switch (this.faces_opaques) {
  183. case 1:
  184. this.face = 'back'
  185. break
  186. case 2:
  187. // RANDOMLY USE 2 FACES
  188. if (face < 0.5) {
  189. this.face = 'left'
  190. } else {
  191. this.face = 'right'
  192. }
  193. break
  194. case 3:
  195. // RANDOMLY USE 3 FACES
  196. if (face < 0.33) {
  197. this.face = 'left'
  198. } else if (face >= 0.33 && face < 0.66) {
  199. this.face = 'back'
  200. } else {
  201. this.face = 'right'
  202. }
  203. break
  204. case 4:
  205. // RANDOMLY USE 4 FACES
  206. if (face < 0.25) {
  207. this.face = 'left'
  208. } else if (face >= 0.25 && face < 0.5) {
  209. this.face = 'back'
  210. } else if (face >= 0.5 && face < 0.75) {
  211. this.face = 'right'
  212. } else {
  213. this.face = 'front'
  214. }
  215. break
  216. }
  217. }
  218. // this.face = 'left'
  219. // console.log('face', this.face)
  220. // define side
  221. let side
  222. switch (this.face) {
  223. case 'back':
  224. case 'front':
  225. side = 'x'
  226. break
  227. case 'left':
  228. case 'right':
  229. side = 'z'
  230. break
  231. }
  232. // Get GRID's CEL
  233. if (typeof this.gridsContentPlaced[side] === 'undefined' ||
  234. typeof this.gridsContentPlaced[side][this.face] === 'undefined' ||
  235. typeof this.gridsContentPlaced[side][this.face][this.type] === 'undefined' ||
  236. typeof this.gridsContentPlaced[side][this.face][this.type][this.id] === 'undefined') {
  237. // First shift grid place (will transfer it from free places to occupied places)
  238. this.shiftGrid({ face: this.face, type: this.type, id: this.id })
  239. }
  240. // rotation and position
  241. let gridPos = this.gridsContentPlaced[side][this.face][this.type][this.id]
  242. // console.log('gridPos', gridPos)
  243. this.position.x = gridPos.x
  244. this.position.z = gridPos.z
  245. this.position.y = gridPos.y
  246. this.rotation.y = gridPos.ry
  247. // console.log(this.data.Media[0].url)
  248. // create image object with a promise (async img loading)
  249. this.createImgCanvas(this.preview_img_url)
  250. .then(({ img, canvas }) => {
  251. // console.log('THEN img loaded ok', this, img, canvas)
  252. // canvas is way bigger than displayable for good resolution
  253. // reduce it keeping good resolution
  254. // and fit the width and height to the screen
  255. let f
  256. if (canvas.width >= canvas.height) {
  257. f = this.size.width / canvas.width
  258. this.img_size.width = canvas.width * f - this.img_padding * 2
  259. this.img_size.height = canvas.height * f - this.img_padding * 2
  260. } else {
  261. f = this.size.height / canvas.height
  262. this.img_size.height = canvas.height * f - this.img_padding * 2
  263. this.img_size.width = canvas.width * f - this.img_padding * 2
  264. }
  265. // console.log(`size.x :${this.size.width}, size.y :${this.size.height}, canvas.width: ${canvas.width}, canvas.height: ${canvas.height}, f: ${f}, img_size.width: ${this.img_size.width}, img_size.height: ${this.img_size.height}`)
  266. this.img_position.y = this.position.y
  267. switch (side) {
  268. case 'z':
  269. this.img_position.z = this.position.z + this.size.width / 2
  270. this.img_position.x = this.position.x
  271. break
  272. case 'x':
  273. this.img_position.x = this.position.x + this.size.width / 2
  274. this.img_position.z = this.position.z
  275. break
  276. }
  277. this.img_canvas = canvas
  278. this.curObj.needsUpdate()
  279. })
  280. .catch(function () {
  281. console.warn('CATCH img load ERROR')
  282. })
  283. }
  284. // this.img_position = { ...this.position }
  285. // this.img_position.y -= this.size.height
  286. },
  287. mounted () {
  288. // this.block3d = this.$refs.block3d.curObj
  289. // this.block3d.castShadow = true
  290. // this.block3d.receiveShadow = true
  291. },
  292. updated () {
  293. if (!this.curObj && this.$refs.img3d) {
  294. this.curObj = this.$refs.img3d.curObj
  295. // record self references
  296. this.curObj.userData = {
  297. vnode: this
  298. }
  299. }
  300. },
  301. methods: {
  302. ...mapInstanceMutations(getModuleName, {
  303. shiftGrid: 'shiftGrid'
  304. }),
  305. open () {
  306. this.isOpened = true
  307. // this.loadContents()
  308. }
  309. }
  310. }
  311. </script>