ContentBlock.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. }),
  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. // RANDOMLY USE 2 FACES
  207. if (face < 0.5) {
  208. this.face = 'left'
  209. } else {
  210. this.face = 'right'
  211. }
  212. // define side
  213. let side
  214. switch (this.face) {
  215. case 'back':
  216. case 'front':
  217. side = 'x'
  218. break
  219. case 'left':
  220. case 'right':
  221. side = 'z'
  222. break
  223. }
  224. // Get GRID's CEL
  225. if (typeof this.gridsContentPlaced[side] === 'undefined' ||
  226. typeof this.gridsContentPlaced[side][this.face] === 'undefined' ||
  227. typeof this.gridsContentPlaced[side][this.face][this.type] === 'undefined' ||
  228. typeof this.gridsContentPlaced[side][this.face][this.type][this.id] === 'undefined') {
  229. // First shift grid place (will transfer it from free places to occupied places)
  230. this.shiftGrid({ face: this.face, type: this.type, id: this.id })
  231. }
  232. // rotation and position
  233. let gridPos = this.gridsContentPlaced[side][this.face][this.type][this.id]
  234. console.log('gridPos', gridPos)
  235. switch (side) {
  236. case 'x':
  237. this.position.x = this.projPos.x - this.projSize.x / 2 + gridPos.x
  238. this.position.y = this.projPos.y - this.projSize.y / 2 + gridPos.y
  239. switch (this.face) {
  240. case 'face':
  241. this.position.z = this.projPos.z + this.projSize.z / 2 - 0.1
  242. this.rotation.y = 180
  243. break
  244. case 'back':
  245. this.position.z = this.projPos.z - this.projSize.z / 2 + 0.1
  246. this.rotation.y = 0
  247. break
  248. }
  249. break
  250. case 'z':
  251. this.position.z = this.projPos.z - this.projSize.z / 2 + gridPos.z
  252. this.position.y = this.projPos.y - this.projSize.y / 2 + gridPos.y
  253. switch (this.face) {
  254. case 'left':
  255. this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  256. this.rotation.y = 90
  257. break
  258. case 'right':
  259. this.position.x = this.projPos.x + this.projSize.x / 2 - 0.1
  260. this.rotation.y = -90
  261. break
  262. }
  263. break
  264. }
  265. // console.log(this.data.Media[0].url)
  266. // create image object with a promise (async img loading)
  267. this.createImgCanvas(this.preview_img_url)
  268. .then(({ img, canvas }) => {
  269. // console.log('THEN img loaded ok', this, img, canvas)
  270. // canvas is way bigger than displayable for good resolution
  271. // reduce it keeping good resolution
  272. // and fit the width and height to the screen
  273. let f
  274. if (canvas.width >= canvas.height) {
  275. f = this.size.x / canvas.width
  276. this.img_size.x = canvas.width * f - this.img_padding * 2
  277. this.img_size.y = this.size.y = canvas.height * f - this.img_padding * 2
  278. } else {
  279. f = this.size.y / canvas.height
  280. this.img_size.y = canvas.height * f - this.img_padding * 2
  281. this.img_size.x = this.size.x = canvas.width * f - this.img_padding * 2
  282. }
  283. 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}`)
  284. this.img_position.y = this.position.y - this.size.y / 2 - this.img_size.y / 2
  285. switch (this.face) {
  286. case 'left':
  287. this.img_position.z = this.position.z + this.size.x / 2 - this.img_size.x / 2
  288. this.img_position.x = this.position.x
  289. break
  290. case 'back':
  291. this.img_position.x = this.position.x - this.size.x / 2 + this.img_size.x / 2
  292. this.img_position.z = this.position.z
  293. break
  294. case 'right':
  295. this.img_position.z = this.position.z - this.size.x / 2 + this.img_size.x / 2
  296. this.img_position.x = this.position.x
  297. break
  298. case 'front':
  299. this.img_position.x = this.position.x + this.size.x / 2 - this.img_size.x / 2
  300. this.img_position.z = this.position.z
  301. break
  302. }
  303. this.img_canvas = canvas
  304. this.curObj.needsUpdate()
  305. // this.imgObj = this.$refs.img3d.curObj
  306. // // record self references
  307. // this.imgObj.userData = {
  308. // vnode: this
  309. // }
  310. })
  311. .catch(function () {
  312. console.warn('CATCH img load ERROR')
  313. })
  314. }
  315. // this.img_position = { ...this.position }
  316. // this.img_position.y -= this.size.y
  317. },
  318. mounted () {
  319. // this.block3d = this.$refs.block3d.curObj
  320. // this.block3d.castShadow = true
  321. // this.block3d.receiveShadow = true
  322. },
  323. updated () {
  324. if (!this.curObj && this.$refs.img3d) {
  325. this.curObj = this.$refs.img3d.curObj
  326. // record self references
  327. this.curObj.userData = {
  328. vnode: this
  329. }
  330. }
  331. },
  332. methods: {
  333. ...mapInstanceMutations(getModuleName, {
  334. shiftGrid: 'shiftGrid'
  335. }),
  336. open () {
  337. this.isOpened = true
  338. // this.loadContents()
  339. }
  340. }
  341. }
  342. </script>