ContentBlock.vue 11 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. 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. gridContentPlaced: state => state.gridContentPlaced,
  100. contents_size_factor: state => state.contents_size_factor
  101. }),
  102. color () {
  103. let color = 0x000000
  104. switch (this.type) {
  105. case 'visible':
  106. color = 0x0000ff
  107. break
  108. case 'context':
  109. color = 0x00ffff
  110. break
  111. case 'process':
  112. color = 0xff00ff
  113. break
  114. default:
  115. color = 0xdddddd
  116. }
  117. return color
  118. },
  119. // label_texture_opts () {
  120. // return {
  121. // canvas: this.label_canvas,
  122. // minFilter: THREE.LinearFilter,
  123. // wrapS: THREE.ClampToEdgeWrapping,
  124. // wrapT: THREE.ClampToEdgeWrapping
  125. // }
  126. // },
  127. img_texture_opts () {
  128. return {
  129. canvas: this.img_canvas,
  130. minFilter: THREE.LinearFilter,
  131. wrapS: THREE.ClampToEdgeWrapping,
  132. wrapT: THREE.ClampToEdgeWrapping
  133. }
  134. },
  135. id () {
  136. return this.data.id
  137. }
  138. },
  139. created () {
  140. if (this.data.Vimeo) {
  141. this.preview_img_url = this.data.Vimeo.thumbnail_url_with_play_button
  142. } else if (this.data.Media && this.data.Media.length) {
  143. this.preview_img_url = `https://api.anarchive-muntadas.figli.io${this.data.Media[0].url}`
  144. }
  145. if (this.preview_img_url) {
  146. this.size.x = this.projWall.winW * this.contents_size_factor
  147. this.size.y = this.projWall.winY * this.contents_size_factor
  148. // console.log('ContentBlock created', this.data)
  149. // let txtcolor = '#000000'
  150. // switch (this.type) {
  151. // case 'visible':
  152. // txtcolor = '#0000ff'
  153. // break
  154. // case 'context':
  155. // txtcolor = '#ff0000'
  156. // break
  157. // case 'process':
  158. // txtcolor = '#00ff00'
  159. // break
  160. // default:
  161. // }
  162. // this.label_canvas = this.createLabelCanvas(this.data.Name.replace(/ /g, '\n').toUpperCase(), 60, 150, txtcolor)
  163. //
  164. // this.size.x = this.label_size.x + 0.2
  165. // this.size.y = this.label_size.y + 0.2
  166. // let y = 0
  167. // let top = this.projPos.y + this.projSize.y / 2
  168. // let floor = this.projPos.y - this.projSize.y / 2
  169. // switch (this.type) {
  170. // case 'visible':
  171. // y = top * Math.random()
  172. // break
  173. // case 'context':
  174. // y = floor / 3 * Math.random()
  175. // break
  176. // case 'process':
  177. // y = floor / 3 + floor / 3 * Math.random()
  178. // break
  179. // case 'concept':
  180. // y = (floor / 3) * 2 + floor / 3 * Math.random()
  181. // break
  182. // }
  183. // this.position.y = y
  184. let face = Math.random()
  185. // // USES 4 FACES
  186. // if (face < 0.25) {
  187. // // gauche
  188. // this.face = 'left'
  189. // this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  190. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  191. // this.rotation.y = 90
  192. // } else if (face >= 0.25 && face < 0.5) {
  193. // // fond
  194. // this.face = 'back'
  195. // this.position.z = this.projPos.z - this.projSize.z / 2 + 0.1
  196. // this.position.x = this.projPos.x - this.projSize.x / 2 + this.size.x / 2 + Math.random() * (this.projSize.x - this.size.x / 2)
  197. // } else if (face >= 0.5 && face < 0.75) {
  198. // // droite
  199. // this.face = 'right'
  200. // this.position.x = this.projPos.x + this.projSize.x / 2 - 0.1
  201. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  202. // this.rotation.y = -90
  203. // } else {
  204. // // face
  205. // this.face = 'front'
  206. // this.position.z = this.projPos.z + this.projSize.z / 2 - 0.1
  207. // this.position.x = this.projPos.x - this.projSize.x / 2 + this.size.x / 2 + Math.random() * (this.projSize.x - this.size.x / 2)
  208. // this.rotation.y = 180
  209. // }
  210. // // USES 2 FACES
  211. if (face < 0.5) {
  212. // gauche
  213. this.face = 'left'
  214. this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  215. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  216. this.rotation.y = 90
  217. } else {
  218. // droite
  219. this.face = 'right'
  220. this.position.x = this.projPos.x + this.projSize.x / 2 - 0.1
  221. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  222. this.rotation.y = -90
  223. }
  224. // With GRID
  225. // this.face = 'left'
  226. // this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  227. // this.rotation.y = 90
  228. if (typeof this.gridContentPlaced[this.type] === 'undefined' ||
  229. typeof this.gridContentPlaced[this.type][this.id] === 'undefined') {
  230. // First shift grid place (will transfer it from free places to occupied places)
  231. this.shiftGrid({ type: this.type, id: this.id })
  232. }
  233. // Then get the right place for the current content
  234. let pos = this.gridContentPlaced[this.type][this.id]
  235. console.log('pos', pos)
  236. this.position.z = this.projPos.z - this.projSize.z / 2 + pos.z
  237. this.position.y = this.projPos.y - this.projSize.y / 2 + pos.y
  238. // this.label_position.x = this.position.x - this.size.x / 2 + this.label_size.x / 2 + 0.1
  239. // this.label_position.y = this.position.y + this.size.y / 2 - this.label_size.y / 2 - 0.1
  240. // this.label_position.z = this.position.z + this.size.z / 2 + 0.01
  241. // console.log(this.data.Media[0].url)
  242. // create image object with a promise (async img loading)
  243. this.createImgCanvas(this.preview_img_url)
  244. .then(({ img, canvas }) => {
  245. // console.log('THEN img loaded ok', this, img, canvas)
  246. let f = this.size.x / canvas.width
  247. this.img_size.x = canvas.width * f - this.img_padding * 2
  248. this.img_size.y = this.size.y = canvas.height * f - this.img_padding * 2
  249. console.log(`size.x :${this.size.x}, canvas.width: ${canvas.width}, canvas.height: ${canvas.height}, f: ${f}, img_size.x: ${this.img_size.x}, img_size.y: ${this.img_size.y}`)
  250. this.img_position.y = this.position.y - this.size.y / 2 - this.img_size.y / 2
  251. switch (this.face) {
  252. case 'left':
  253. this.img_position.z = this.position.z + this.size.x / 2 - this.img_size.x / 2
  254. this.img_position.x = this.position.x
  255. break
  256. case 'back':
  257. this.img_position.x = this.position.x - this.size.x / 2 + this.img_size.x / 2
  258. this.img_position.z = this.position.z
  259. break
  260. case 'right':
  261. this.img_position.z = this.position.z - this.size.x / 2 + this.img_size.x / 2
  262. this.img_position.x = this.position.x
  263. break
  264. case 'front':
  265. this.img_position.x = this.position.x + this.size.x / 2 - this.img_size.x / 2
  266. this.img_position.z = this.position.z
  267. break
  268. }
  269. this.img_canvas = canvas
  270. this.curObj.needsUpdate()
  271. // this.imgObj = this.$refs.img3d.curObj
  272. // // record self references
  273. // this.imgObj.userData = {
  274. // vnode: this
  275. // }
  276. })
  277. .catch(function () {
  278. console.warn('CATCH img load ERROR')
  279. })
  280. }
  281. // this.img_position = { ...this.position }
  282. // this.img_position.y -= this.size.y
  283. },
  284. mounted () {
  285. // this.block3d = this.$refs.block3d.curObj
  286. // this.block3d.castShadow = true
  287. // this.block3d.receiveShadow = true
  288. },
  289. updated () {
  290. if (!this.curObj && this.$refs.img3d) {
  291. this.curObj = this.$refs.img3d.curObj
  292. // record self references
  293. this.curObj.userData = {
  294. vnode: this
  295. }
  296. }
  297. },
  298. methods: {
  299. ...mapInstanceMutations(getModuleName, {
  300. shiftGrid: 'shiftGrid'
  301. }),
  302. open () {
  303. this.isOpened = true
  304. // this.loadContents()
  305. }
  306. }
  307. }
  308. </script>