ContentBlock.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.winH * this.contents_size_factor
  148. // get the right size depending on screen
  149. let u
  150. if (window.innerWidth > window.innerHeight) {
  151. u = window.innerHeight / window.innerWidth
  152. } else {
  153. u = window.innerWidth / window.innerHeight
  154. }
  155. this.size.x = this.projWall.winW * this.contents_size_factor
  156. this.size.y = this.projWall.winH * u * this.contents_size_factor
  157. // 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}`)
  158. // this.label_canvas = this.createLabelCanvas(this.data.Name.replace(/ /g, '\n').toUpperCase(), 60, 150, txtcolor)
  159. //
  160. // this.size.x = this.label_size.x + 0.2
  161. // this.size.y = this.label_size.y + 0.2
  162. // let y = 0
  163. // let top = this.projPos.y + this.projSize.y / 2
  164. // let floor = this.projPos.y - this.projSize.y / 2
  165. // switch (this.type) {
  166. // case 'visible':
  167. // y = top * Math.random()
  168. // break
  169. // case 'context':
  170. // y = floor / 3 * Math.random()
  171. // break
  172. // case 'process':
  173. // y = floor / 3 + floor / 3 * Math.random()
  174. // break
  175. // case 'concept':
  176. // y = (floor / 3) * 2 + floor / 3 * Math.random()
  177. // break
  178. // }
  179. // this.position.y = y
  180. let face = Math.random()
  181. // // USES 4 FACES
  182. // if (face < 0.25) {
  183. // // gauche
  184. // this.face = 'left'
  185. // this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  186. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  187. // this.rotation.y = 90
  188. // } else if (face >= 0.25 && face < 0.5) {
  189. // // fond
  190. // this.face = 'back'
  191. // this.position.z = this.projPos.z - this.projSize.z / 2 + 0.1
  192. // this.position.x = this.projPos.x - this.projSize.x / 2 + this.size.x / 2 + Math.random() * (this.projSize.x - this.size.x / 2)
  193. // } else if (face >= 0.5 && face < 0.75) {
  194. // // droite
  195. // this.face = 'right'
  196. // this.position.x = this.projPos.x + this.projSize.x / 2 - 0.1
  197. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  198. // this.rotation.y = -90
  199. // } else {
  200. // // face
  201. // this.face = 'front'
  202. // this.position.z = this.projPos.z + this.projSize.z / 2 - 0.1
  203. // this.position.x = this.projPos.x - this.projSize.x / 2 + this.size.x / 2 + Math.random() * (this.projSize.x - this.size.x / 2)
  204. // this.rotation.y = 180
  205. // }
  206. // // USES 2 FACES
  207. if (face < 0.5) {
  208. // gauche
  209. this.face = 'left'
  210. this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  211. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  212. this.rotation.y = 90
  213. } else {
  214. // droite
  215. this.face = 'right'
  216. this.position.x = this.projPos.x + this.projSize.x / 2 - 0.1
  217. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  218. this.rotation.y = -90
  219. }
  220. // With GRID
  221. // this.face = 'left'
  222. // this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  223. // this.rotation.y = 90
  224. if (typeof this.gridContentPlaced[this.type] === 'undefined' ||
  225. typeof this.gridContentPlaced[this.type][this.id] === 'undefined') {
  226. // First shift grid place (will transfer it from free places to occupied places)
  227. this.shiftGrid({ type: this.type, id: this.id })
  228. }
  229. // Then get the right place for the current content
  230. let pos = this.gridContentPlaced[this.type][this.id]
  231. console.log('pos', pos)
  232. this.position.z = this.projPos.z - this.projSize.z / 2 + pos.z
  233. this.position.y = this.projPos.y - this.projSize.y / 2 + pos.y
  234. // this.label_position.x = this.position.x - this.size.x / 2 + this.label_size.x / 2 + 0.1
  235. // this.label_position.y = this.position.y + this.size.y / 2 - this.label_size.y / 2 - 0.1
  236. // this.label_position.z = this.position.z + this.size.z / 2 + 0.01
  237. // console.log(this.data.Media[0].url)
  238. // create image object with a promise (async img loading)
  239. this.createImgCanvas(this.preview_img_url)
  240. .then(({ img, canvas }) => {
  241. // console.log('THEN img loaded ok', this, img, canvas)
  242. // canvas is way bigger than displayable for good resolution
  243. // reduce it keeping good resolution
  244. // and fit the width and height to the screen
  245. let f
  246. if (canvas.width >= canvas.height) {
  247. f = this.size.x / canvas.width
  248. this.img_size.x = canvas.width * f - this.img_padding * 2
  249. this.img_size.y = this.size.y = canvas.height * f - this.img_padding * 2
  250. } else {
  251. f = this.size.y / canvas.height
  252. this.img_size.y = canvas.height * f - this.img_padding * 2
  253. this.img_size.x = this.size.x = canvas.width * f - this.img_padding * 2
  254. }
  255. 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}`)
  256. this.img_position.y = this.position.y - this.size.y / 2 - this.img_size.y / 2
  257. switch (this.face) {
  258. case 'left':
  259. this.img_position.z = this.position.z + this.size.x / 2 - this.img_size.x / 2
  260. this.img_position.x = this.position.x
  261. break
  262. case 'back':
  263. this.img_position.x = this.position.x - this.size.x / 2 + this.img_size.x / 2
  264. this.img_position.z = this.position.z
  265. break
  266. case 'right':
  267. this.img_position.z = this.position.z - this.size.x / 2 + this.img_size.x / 2
  268. this.img_position.x = this.position.x
  269. break
  270. case 'front':
  271. this.img_position.x = this.position.x + this.size.x / 2 - this.img_size.x / 2
  272. this.img_position.z = this.position.z
  273. break
  274. }
  275. this.img_canvas = canvas
  276. this.curObj.needsUpdate()
  277. // this.imgObj = this.$refs.img3d.curObj
  278. // // record self references
  279. // this.imgObj.userData = {
  280. // vnode: this
  281. // }
  282. })
  283. .catch(function () {
  284. console.warn('CATCH img load ERROR')
  285. })
  286. }
  287. // this.img_position = { ...this.position }
  288. // this.img_position.y -= this.size.y
  289. },
  290. mounted () {
  291. // this.block3d = this.$refs.block3d.curObj
  292. // this.block3d.castShadow = true
  293. // this.block3d.receiveShadow = true
  294. },
  295. updated () {
  296. if (!this.curObj && this.$refs.img3d) {
  297. this.curObj = this.$refs.img3d.curObj
  298. // record self references
  299. this.curObj.userData = {
  300. vnode: this
  301. }
  302. }
  303. },
  304. methods: {
  305. ...mapInstanceMutations(getModuleName, {
  306. shiftGrid: 'shiftGrid'
  307. }),
  308. open () {
  309. this.isOpened = true
  310. // this.loadContents()
  311. }
  312. }
  313. }
  314. </script>