ContentBlock.vue 9.9 KB

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