ContentBlock.vue 9.4 KB

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