ContentBlock.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div>
  3. <!-- <mesh ref="block3d" name="ContentBlock" :position="position">
  4. <geometry type="Box" :args="[size.x, size.y, size.z]" />
  5. <material type="MeshLambert" :color="color" :options="opts" />
  6. </mesh> -->
  7. <mesh
  8. ref="label3d"
  9. name="Content"
  10. :position="position"
  11. :rotation="{
  12. x:deg2rad(rotation.x),
  13. y:deg2rad(rotation.y),
  14. z:deg2rad(rotation.z)
  15. }"
  16. >
  17. <geometry type="Plane" :args="[size.x, size.y]" />
  18. <material type="MeshLambert" :options="label_opts">
  19. <texture :options="label_texture_opts" />
  20. </material>
  21. </mesh>
  22. <mesh
  23. v-if="img_canvas"
  24. ref="img3d"
  25. name="Content"
  26. :position="img_position"
  27. :rotation="{
  28. x:deg2rad(rotation.x),
  29. y:deg2rad(rotation.y),
  30. z:deg2rad(rotation.z)
  31. }"
  32. >
  33. <geometry type="Plane" :args="[img_size.x, img_size.y]" />
  34. <material type="MeshLambert" :options="img_opts">
  35. <texture :options="img_texture_opts" />
  36. </material>
  37. </mesh>
  38. </div>
  39. </template>
  40. <script>
  41. import * as THREE from 'three'
  42. import mixins from 'components/mixins'
  43. export default {
  44. name: 'ContentBlock',
  45. mixins: [mixins],
  46. props: { prtPosition: Object, prtSize: Object, type: String, data: Object },
  47. data: () => ({
  48. // block3d: null,
  49. size: { x: 2, y: 3, z: 0.1 },
  50. position: { x: 0, y: 0, z: 0 },
  51. rotation: { x: 0, y: 0, z: 0 },
  52. // opts: {
  53. // side: THREE.DoubleSide
  54. // // wireframe: false,
  55. // // transparent: false,
  56. // // opacity: 0.6
  57. // },
  58. isOpened: false,
  59. label3d: null,
  60. label_opts: {
  61. side: THREE.DoubleSide,
  62. // wireframe: false,
  63. transparent: true
  64. // opacity: 0.6
  65. // renderOrder: 0
  66. },
  67. img_opts: {
  68. // side: THREE.DoubleSide,
  69. // wireframe: false,
  70. // transparent: true
  71. // opacity: 0.6
  72. // renderOrder: 0
  73. },
  74. // label_position: { x: 5, y: 5, z: 6 },
  75. label_canvas: null,
  76. label_size: null,
  77. img_canvas: null,
  78. img_size: { x: 2, y: 3, z: 0.1 },
  79. img_position: { x: 0, y: 0, z: 0 },
  80. face: null
  81. }),
  82. computed: {
  83. color () {
  84. let color = 0x000000
  85. switch (this.type) {
  86. case 'visible':
  87. color = 0x0000ff
  88. break
  89. case 'context':
  90. color = 0x00ffff
  91. break
  92. case 'process':
  93. color = 0xff00ff
  94. break
  95. default:
  96. color = 0xdddddd
  97. }
  98. return color
  99. },
  100. label_texture_opts () {
  101. return {
  102. canvas: this.label_canvas,
  103. minFilter: THREE.LinearFilter,
  104. wrapS: THREE.ClampToEdgeWrapping,
  105. wrapT: THREE.ClampToEdgeWrapping
  106. }
  107. },
  108. img_texture_opts () {
  109. return {
  110. canvas: this.img_canvas,
  111. minFilter: THREE.LinearFilter,
  112. wrapS: THREE.ClampToEdgeWrapping,
  113. wrapT: THREE.ClampToEdgeWrapping
  114. }
  115. }
  116. },
  117. created () {
  118. console.log('ContentBlock created', this.data)
  119. let txtcolor = '#000000'
  120. // switch (this.type) {
  121. // case 'visible':
  122. // txtcolor = '#0000ff'
  123. // break
  124. // case 'context':
  125. // txtcolor = '#ff0000'
  126. // break
  127. // case 'process':
  128. // txtcolor = '#00ff00'
  129. // break
  130. // default:
  131. // }
  132. this.label_canvas = this.createLabelCanvas(this.data.Name.replace(/ /g, '\n').toUpperCase(), 60, 150, txtcolor)
  133. this.size.x = this.label_size.x + 0.2
  134. this.size.y = this.label_size.y + 0.2
  135. let y = 0
  136. switch (this.type) {
  137. case 'visible':
  138. y = (this.prtPosition.y + this.prtSize.y / 2) * Math.random()
  139. break
  140. case 'context':
  141. y = (this.prtPosition.y - this.prtSize.y / 2) * Math.random()
  142. break
  143. case 'process':
  144. y = (this.prtPosition.y - this.prtSize.y / 2) * Math.random()
  145. break
  146. default:
  147. }
  148. this.position.y = y
  149. let face = Math.random()
  150. if (face < 0.25) {
  151. // gauche
  152. this.face = 'left'
  153. this.position.x = this.prtPosition.x - this.prtSize.x / 2 + 0.1
  154. this.position.z = this.prtPosition.z - this.prtSize.z / 2 + this.size.x / 2 + Math.random() * (this.prtSize.z - this.size.x / 2)
  155. this.rotation.y = 90
  156. } else if (face >= 0.25 && face < 0.5) {
  157. // fond
  158. this.face = 'back'
  159. this.position.z = this.prtPosition.z - this.prtSize.z / 2 + 0.1
  160. this.position.x = this.prtPosition.x - this.prtSize.x / 2 + this.size.x / 2 + Math.random() * (this.prtSize.x - this.size.x / 2)
  161. } else if (face >= 0.5 && face < 0.75) {
  162. // droite
  163. this.face = 'right'
  164. this.position.x = this.prtPosition.x + this.prtSize.x / 2 - 0.1
  165. this.position.z = this.prtPosition.z - this.prtSize.z / 2 + this.size.x / 2 + Math.random() * (this.prtSize.z - this.size.x / 2)
  166. this.rotation.y = -90
  167. } else {
  168. // face
  169. this.face = 'front'
  170. this.position.z = this.prtPosition.z + this.prtSize.z / 2 - 0.1
  171. this.position.x = this.prtPosition.x - this.prtSize.x / 2 + this.size.x / 2 + Math.random() * (this.prtSize.x - this.size.x / 2)
  172. this.rotation.y = 180
  173. }
  174. // this.label_position.x = this.position.x - this.size.x / 2 + this.label_size.x / 2 + 0.1
  175. // this.label_position.y = this.position.y + this.size.y / 2 - this.label_size.y / 2 - 0.1
  176. // this.label_position.z = this.position.z + this.size.z / 2 + 0.01
  177. // console.log()
  178. if (this.data.Media && this.data.Media.length) {
  179. console.log(this.data.Media[0].url)
  180. this.createImgCanvas(`https://api.anarchive-muntadas.figli.io${this.data.Media[0].url}`)
  181. .then(({ img, canvas }) => {
  182. // console.log('THEN img loaded ok', this, img, canvas)
  183. let f = 3 / canvas.width
  184. this.img_size.x = canvas.width * f
  185. this.img_size.y = canvas.height * f
  186. this.img_position.y = this.position.y - this.size.y / 2 - this.img_size.y / 2
  187. switch (this.face) {
  188. case 'left':
  189. this.img_position.z = this.position.z + this.size.x / 2 - this.img_size.x / 2
  190. this.img_position.x = this.position.x
  191. break
  192. case 'back':
  193. this.img_position.x = this.position.x - this.size.x / 2 + this.img_size.x / 2
  194. this.img_position.z = this.position.z
  195. break
  196. case 'right':
  197. this.img_position.z = this.position.z - this.size.x / 2 + this.img_size.x / 2
  198. this.img_position.x = this.position.x
  199. break
  200. case 'front':
  201. this.img_position.x = this.position.x + this.size.x / 2 - this.img_size.x / 2
  202. this.img_position.z = this.position.z
  203. break
  204. }
  205. this.img_canvas = canvas
  206. })
  207. .catch(function () {
  208. console.warn('CATCH img load ERROR')
  209. })
  210. }
  211. // this.img_position = { ...this.position }
  212. // this.img_position.y -= this.size.y
  213. },
  214. mounted () {
  215. // this.block3d = this.$refs.block3d.curObj
  216. // this.block3d.castShadow = true
  217. // this.block3d.receiveShadow = true
  218. this.label3d = this.$refs.label3d.curObj
  219. // record self references
  220. this.label3d.userData = {
  221. vnode: this
  222. }
  223. }
  224. }
  225. </script>