ContentBlock.vue 8.5 KB

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