ContentBlock.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 * as THREE from 'three'
  46. import mixins from 'components/mixins'
  47. // We'll determine our module based on the moduleName prop on this component
  48. // https://www.npmjs.com/package/@urbn/vuex-helpers
  49. const getModuleName = cmp => `project:${cmp.prtId}`
  50. export default {
  51. name: 'ContentBlock',
  52. mixins: [mixins],
  53. props: { prtId: Number, type: String, data: Object },
  54. data: () => ({
  55. // block3d: null,
  56. project: null,
  57. size: { x: 0, y: 0 },
  58. position: { x: 0, y: 0, z: 0 },
  59. rotation: { x: 0, y: 0, z: 0 },
  60. // opts: {
  61. // side: THREE.DoubleSide
  62. // // wireframe: false,
  63. // // transparent: false,
  64. // // opacity: 0.6
  65. // },
  66. isOpened: false,
  67. // label3d: null,
  68. // label_opts: {
  69. // side: THREE.DoubleSide,
  70. // // wireframe: false,
  71. // transparent: true
  72. // // opacity: 0.6
  73. // // renderOrder: 0
  74. // },
  75. img_opts: {
  76. // side: THREE.DoubleSide,
  77. // wireframe: false,
  78. // transparent: true
  79. // opacity: 0.6
  80. // renderOrder: 0
  81. },
  82. // label_position: { x: 5, y: 5, z: 6 },
  83. // label_canvas: null,
  84. // label_size: null,
  85. img_canvas: null,
  86. img_size: { x: 0, y: 0 },
  87. img_position: { x: 0, y: 0, z: 0 },
  88. face: null
  89. }),
  90. computed: {
  91. ...mapInstanceState(getModuleName, {
  92. projSize: state => state.size,
  93. projPos: state => state.position,
  94. projWall: state => state.wall,
  95. gridContentPlaced: state => state.gridContentPlaced
  96. }),
  97. color () {
  98. let color = 0x000000
  99. switch (this.type) {
  100. case 'visible':
  101. color = 0x0000ff
  102. break
  103. case 'context':
  104. color = 0x00ffff
  105. break
  106. case 'process':
  107. color = 0xff00ff
  108. break
  109. default:
  110. color = 0xdddddd
  111. }
  112. return color
  113. },
  114. // label_texture_opts () {
  115. // return {
  116. // canvas: this.label_canvas,
  117. // minFilter: THREE.LinearFilter,
  118. // wrapS: THREE.ClampToEdgeWrapping,
  119. // wrapT: THREE.ClampToEdgeWrapping
  120. // }
  121. // },
  122. img_texture_opts () {
  123. return {
  124. canvas: this.img_canvas,
  125. minFilter: THREE.LinearFilter,
  126. wrapS: THREE.ClampToEdgeWrapping,
  127. wrapT: THREE.ClampToEdgeWrapping
  128. }
  129. },
  130. id () {
  131. return this.data.id
  132. }
  133. },
  134. created () {
  135. this.size.x = this.projWall.winW / 2
  136. this.size.y = this.projWall.winY / 2
  137. // console.log('ContentBlock created', this.data)
  138. // let txtcolor = '#000000'
  139. // switch (this.type) {
  140. // case 'visible':
  141. // txtcolor = '#0000ff'
  142. // break
  143. // case 'context':
  144. // txtcolor = '#ff0000'
  145. // break
  146. // case 'process':
  147. // txtcolor = '#00ff00'
  148. // break
  149. // default:
  150. // }
  151. // this.label_canvas = this.createLabelCanvas(this.data.Name.replace(/ /g, '\n').toUpperCase(), 60, 150, txtcolor)
  152. //
  153. // this.size.x = this.label_size.x + 0.2
  154. // this.size.y = this.label_size.y + 0.2
  155. // let y = 0
  156. // let top = this.projPos.y + this.projSize.y / 2
  157. // let floor = this.projPos.y - this.projSize.y / 2
  158. // switch (this.type) {
  159. // case 'visible':
  160. // y = top * Math.random()
  161. // break
  162. // case 'context':
  163. // y = floor / 3 * Math.random()
  164. // break
  165. // case 'process':
  166. // y = floor / 3 + floor / 3 * Math.random()
  167. // break
  168. // case 'concept':
  169. // y = (floor / 3) * 2 + floor / 3 * Math.random()
  170. // break
  171. // }
  172. // this.position.y = y
  173. let face = Math.random()
  174. // // USES 4 FACES
  175. // if (face < 0.25) {
  176. // // gauche
  177. // this.face = 'left'
  178. // this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  179. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  180. // this.rotation.y = 90
  181. // } else if (face >= 0.25 && face < 0.5) {
  182. // // fond
  183. // this.face = 'back'
  184. // this.position.z = this.projPos.z - this.projSize.z / 2 + 0.1
  185. // this.position.x = this.projPos.x - this.projSize.x / 2 + this.size.x / 2 + Math.random() * (this.projSize.x - this.size.x / 2)
  186. // } else if (face >= 0.5 && face < 0.75) {
  187. // // droite
  188. // this.face = 'right'
  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 {
  193. // // face
  194. // this.face = 'front'
  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. // this.rotation.y = 180
  198. // }
  199. // // USES 2 FACES
  200. if (face < 0.5) {
  201. // gauche
  202. this.face = 'left'
  203. this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  204. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  205. this.rotation.y = 90
  206. } else {
  207. // droite
  208. this.face = 'right'
  209. this.position.x = this.projPos.x + this.projSize.x / 2 - 0.1
  210. // this.position.z = this.projPos.z - this.projSize.z / 2 + this.size.x / 2 + Math.random() * (this.projSize.z - this.size.x / 2)
  211. this.rotation.y = -90
  212. }
  213. // With GRID
  214. // this.face = 'left'
  215. // this.position.x = this.projPos.x - this.projSize.x / 2 + 0.1
  216. // this.rotation.y = 90
  217. // First shift grid place (will transfer it from free places to occupied places)
  218. this.shiftGrid({ type: this.type, id: this.id })
  219. // Then get the right place for the current content
  220. let pos = this.gridContentPlaced[this.type][this.id]
  221. console.log('pos', pos)
  222. this.position.z = this.projPos.z - this.projSize.z / 2 + pos.z
  223. this.position.y = this.projPos.y - this.projSize.y / 2 + pos.y
  224. // this.label_position.x = this.position.x - this.size.x / 2 + this.label_size.x / 2 + 0.1
  225. // this.label_position.y = this.position.y + this.size.y / 2 - this.label_size.y / 2 - 0.1
  226. // this.label_position.z = this.position.z + this.size.z / 2 + 0.01
  227. // console.log()
  228. if (this.data.Media && this.data.Media.length) {
  229. // console.log(this.data.Media[0].url)
  230. // create image object with a promise (async img loading)
  231. this.createImgCanvas(`https://api.anarchive-muntadas.figli.io${this.data.Media[0].url}`)
  232. .then(({ img, canvas }) => {
  233. // console.log('THEN img loaded ok', this, img, canvas)
  234. let f = this.size.x / canvas.width
  235. this.img_size.x = canvas.width * f
  236. this.img_size.y = this.size.y = canvas.height * f
  237. 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}`)
  238. this.img_position.y = this.position.y - this.size.y / 2 - this.img_size.y / 2
  239. switch (this.face) {
  240. case 'left':
  241. this.img_position.z = this.position.z + this.size.x / 2 - this.img_size.x / 2
  242. this.img_position.x = this.position.x
  243. break
  244. case 'back':
  245. this.img_position.x = this.position.x - this.size.x / 2 + this.img_size.x / 2
  246. this.img_position.z = this.position.z
  247. break
  248. case 'right':
  249. this.img_position.z = this.position.z - this.size.x / 2 + this.img_size.x / 2
  250. this.img_position.x = this.position.x
  251. break
  252. case 'front':
  253. this.img_position.x = this.position.x + this.size.x / 2 - this.img_size.x / 2
  254. this.img_position.z = this.position.z
  255. break
  256. }
  257. this.img_canvas = canvas
  258. this.curObj.needsUpdate()
  259. // this.imgObj = this.$refs.img3d.curObj
  260. // // record self references
  261. // this.imgObj.userData = {
  262. // vnode: this
  263. // }
  264. })
  265. .catch(function () {
  266. console.warn('CATCH img load ERROR')
  267. })
  268. }
  269. // this.img_position = { ...this.position }
  270. // this.img_position.y -= this.size.y
  271. },
  272. mounted () {
  273. // this.block3d = this.$refs.block3d.curObj
  274. // this.block3d.castShadow = true
  275. // this.block3d.receiveShadow = true
  276. },
  277. updated () {
  278. if (!this.curObj && this.$refs.img3d) {
  279. this.curObj = this.$refs.img3d.curObj
  280. // record self references
  281. this.curObj.userData = {
  282. vnode: this
  283. }
  284. }
  285. },
  286. methods: {
  287. ...mapInstanceMutations(getModuleName, {
  288. shiftGrid: 'shiftGrid'
  289. })
  290. }
  291. }
  292. </script>