ContentBlock.vue 9.7 KB

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