Project.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <div>
  3. <object3d ref="block3d" name="Project" :obj="building" :position="building_position"/>
  4. <object3d ref="top" :obj="top_mesh" :position="top_position"/>
  5. <object3d ref="floor" :obj="floor_mesh" :position="floor_position"/>
  6. <!-- <light :obj="light" :position="building_position" /> -->
  7. <mesh ref="label3d" name="Label" :position="label_position">
  8. <geometry type="Plane" :args="[label_size.x, label_size.y]" />
  9. <material type="MeshLambert" :options="label_opts">
  10. <texture :options="label_texture_opts" />
  11. </material>
  12. </mesh>
  13. <div v-if="isOpened">
  14. <ContentBlock
  15. v-for="item in data.visibles"
  16. :key="item.id"
  17. :prtPosition="position"
  18. :prtSize="size"
  19. :prtIndex="index"
  20. type="visible"
  21. :data="item"
  22. />
  23. <ContentBlock
  24. v-for="item in data.contexts"
  25. :key="item.id"
  26. :prtPosition="position"
  27. :prtSize="size"
  28. :prtIndex="index"
  29. type="context"
  30. :data="item"
  31. />
  32. <ContentBlock
  33. v-for="item in data.processes"
  34. :key="item.id"
  35. :prtPosition="position"
  36. :prtSize="size"
  37. :prtIndex="index"
  38. type="process"
  39. :data="item"
  40. />
  41. <ContentBlock
  42. v-for="item in data.concepts"
  43. :key="item.id"
  44. :prtPosition="position"
  45. :prtSize="size"
  46. :prtIndex="index"
  47. type="concept"
  48. :data="item"
  49. />
  50. </div>
  51. </div>
  52. </template>
  53. <script>
  54. import * as THREE from 'three'
  55. // import { toCSG, fromCSG } from 'three-csg'
  56. import { ThreeBSP } from 'three-js-csg-es6'
  57. import mixins from 'components/mixins'
  58. import ContentBlock from 'components/objects/ContentBlock'
  59. // import BgVertex from 'assets/glsl/BgVertex'
  60. // import BuildingFragment from 'assets/glsl/BuildingFragment'
  61. export default {
  62. name: 'Project',
  63. components: {
  64. ContentBlock
  65. },
  66. mixins: [mixins],
  67. // props: { size: Object, texture: String, position: Object, color: Number },
  68. props: { data: Object, len: Number, index: Number },
  69. data () {
  70. console.log('Project data() : data', this.data)
  71. // get size and positions from project store
  72. let size = { ...this.data.size }
  73. let position = { ...this.data.position }
  74. let wall = { ...this.data.wall }
  75. // http://learningthreejs.com/blog/2011/12/10/constructive-solid-geometry-with-csg-js/
  76. // console.log('ThreeBSP', ThreeBSP)
  77. let backGeom = new THREE.BoxGeometry(size.x, size.y, wall.wallW)
  78. let backMesh = new THREE.Mesh(backGeom)
  79. backMesh.position.z = -0.5 * size.z
  80. let backBSP = new ThreeBSP(backMesh)
  81. let winGeom = new THREE.BoxGeometry(wall.winW, wall.winH, wall.wallW)
  82. let winMesh = new THREE.Mesh(winGeom)
  83. let winBSP
  84. for (var i = 0; i < wall.nbrWinX; i++) {
  85. for (var j = 0; j < wall.nbrWinY; j++) {
  86. winMesh.position.z = -0.5 * size.z
  87. winMesh.position.x = -0.5 * size.x + wall.margin + wall.winW * 0.5 + i * (wall.winW + wall.paddingX)
  88. winMesh.position.y = 0.5 * size.y - wall.margin - wall.winH * 0.5 - j * (wall.winH + wall.paddingY)
  89. winBSP = new ThreeBSP(winMesh)
  90. backBSP = backBSP.subtract(winBSP)
  91. }
  92. }
  93. // let abovewWaterH = position.y + size.y / 2
  94. // let underWaterHThd = (-1 * position.y - size.y / 2) / 3
  95. // let levelH = 4
  96. // let levelGeom = new THREE.BoxGeometry(size.x, levelH, wallW)
  97. // let levelMesh, levelBSP
  98. // for (var l = 0; l < 2; l++) {
  99. // levelMesh = new THREE.Mesh(levelGeom)
  100. // levelMesh.position.z = -0.5 * size.z
  101. // levelMesh.position.y = 0.5 * size.y - abovewWaterH - underWaterHThd - underWaterHThd * l + levelH * 0.5
  102. // levelBSP = new ThreeBSP(levelMesh)
  103. // backBSP = backBSP.union(levelBSP)
  104. // }
  105. backMesh = backBSP.toMesh()
  106. let faceMesh = backMesh.clone()
  107. faceMesh.position.z = 0.5 * size.z
  108. let faceBSP = new ThreeBSP(faceMesh)
  109. let rightGeom = new THREE.BoxGeometry(wall.wallW, size.y, size.z)
  110. let rightMesh = new THREE.Mesh(rightGeom)
  111. rightMesh.position.x = 0.5 * size.x
  112. // rightMesh.position.z = 0.5 * size.z
  113. let rightBSP = new ThreeBSP(rightMesh)
  114. // WE MAY NO NEED TO DIG WINDOWS ON SIDE WALLS
  115. // // dig windows on right
  116. // let nbrWinZ = Math.floor((size.z - 2 * margin) / winW)
  117. // let paddingZ = (size.z - 2 * margin - winW * nbrWinZ) / (nbrWinX - 1)
  118. // winGeom = new THREE.BoxGeometry(wallW, winH, winW)
  119. // for (i = 0; i < nbrWinZ; i++) {
  120. // for (j = 0; j < nbrWinY; j++) {
  121. // // right
  122. // winMesh = new THREE.Mesh(winGeom)
  123. // winMesh.position.x = 0.5 * size.x
  124. // winMesh.position.z = -0.5 * size.z + margin + winW * 0.5 + i * (winW + paddingZ)
  125. // winMesh.position.y = 0.5 * size.y - margin - winH * 0.5 - j * (winH + paddingY)
  126. // winBSP = new ThreeBSP(winMesh)
  127. // rightBSP = rightBSP.subtract(winBSP)
  128. // }
  129. // }
  130. // rightMesh = rightBSP.toMesh()
  131. let leftMesh = rightMesh.clone()
  132. leftMesh.position.x = -0.5 * size.x
  133. // leftMesh.position.z = 0.5 * size.z
  134. let leftBSP = new ThreeBSP(leftMesh)
  135. //
  136. // let topGeom = new THREE.BoxGeometry(size.x, wallW, size.z)
  137. // let topMesh = new THREE.Mesh(topGeom)
  138. // // topMesh.position.y = -0.5 * size.y
  139. // // let topBSP = new ThreeBSP(topMesh)
  140. //
  141. // let floorGeom = new THREE.BoxGeometry(size.x, wallW, size.z)
  142. // let floorMesh = new THREE.Mesh(floorGeom)
  143. // // floorMesh.position.y = 0.5 * size.y
  144. // // let floorBSP = new ThreeBSP(floorMesh)
  145. let buildingBSP = backBSP.union(rightBSP)
  146. buildingBSP = buildingBSP.union(faceBSP)
  147. buildingBSP = buildingBSP.union(leftBSP)
  148. // buildingBSP = buildingBSP.union(topBSP)
  149. // buildingBSP = buildingBSP.union(floorBSP)
  150. // convert back to three.js mesh
  151. let building = buildingBSP.toMesh()
  152. console.log('building.faces', building)
  153. // create a custom shaderMaterial to had gradian colors
  154. // var uniforms = THREE.UniformsUtils.merge([
  155. // THREE.UniformsLib['lights'],
  156. // {
  157. // 'topColor': { value: new THREE.Color(0xffffff) },
  158. // 'groundColor': { value: new THREE.Color(0xbcd6e4) },
  159. // 'bottomColor': { value: new THREE.Color(0x07223b) },
  160. // // 'bottomColor': { value: new THREE.Color(0x000000) },
  161. // // 'groundColor': { value: new THREE.Color(0xff0000) },
  162. // // 'bottomColor': { value: new THREE.Color(0x00ff00) },
  163. // lightIntensity: { type: 'f', value: 1.0 }
  164. // }
  165. // ])
  166. // var buildingMat = new THREE.ShaderMaterial({
  167. // uniforms: uniforms,
  168. // vertexShader: BgVertex,
  169. // fragmentShader: BuildingFragment,
  170. // side: THREE.DoubleSide,
  171. // lights: true
  172. // })
  173. // building.material = buildingMat
  174. // create a classical material for building
  175. // let topColor = `hsla(201, 100%, 95%, 1)`
  176. let hTop = Math.round(195 + Math.random() * 10)
  177. let sTop = Math.round(100)
  178. let lTop = Math.round(95)
  179. let hFloor = Math.round(205 + Math.random() * 10)
  180. let sFloor = Math.round(40 + Math.random() * 20)
  181. let lFloor = Math.round(5 + Math.random() * 15)
  182. let topColor = `hsla(${hTop}, ${sTop}%, ${lTop}%, 1)`
  183. let bottomColor = `hsla(${hFloor}, ${sFloor}%, ${lFloor}%, 1)`
  184. let gradientTexture = new THREE.CanvasTexture(this.createGradientCanvas(topColor, bottomColor))
  185. let materialOpts = {
  186. color: 0xffffff,
  187. side: THREE.DoubleSide,
  188. shininess: 30,
  189. map: gradientTexture
  190. // wireframe: true,
  191. // transparent: true,
  192. // opacity: 0.6
  193. // renderOrder: 0
  194. }
  195. building.material = new THREE.MeshPhongMaterial(materialOpts)
  196. // building.castShadow = true
  197. // building.receiveShadow = true
  198. // let buildingGeom = fromCSG(boxCSG)
  199. // buildingGeom.computeVertexNormals()
  200. // result
  201. // let subtractCSG = boxMeshCSG.subtract(holeMeshCSG)
  202. // let building = fromCSG(boxMeshCSG) // converting CSG back into ThreeJS object
  203. //
  204. let buildingPos = { ...position }
  205. buildingPos.z -= 0.5 * size.z
  206. // let light = new THREE.PointLight(0xfcfcf9, 1)
  207. // var sun = new THREE.DirectionalLight(0xfcfcf9, 2)
  208. // sun.position.set(-150, 200, 110)
  209. let topGeom = new THREE.BoxGeometry(size.x, wall.wallW, size.z)
  210. let topOpts = {
  211. color: new THREE.Color(`hsl(${hTop}, ${sTop}%, ${lTop}%)`),
  212. // side: THREE.DoubleSide,
  213. shininess: 30
  214. }
  215. let topMat = new THREE.MeshPhongMaterial(topOpts)
  216. let topMesh = new THREE.Mesh(topGeom, topMat)
  217. let topPosition = { ...position }
  218. topPosition.y += 0.5 * size.y
  219. let floorGeom = new THREE.BoxGeometry(size.x, wall.wallW, size.z)
  220. let floorOpts = {
  221. color: new THREE.Color(`hsl(${hFloor}, ${sFloor}%, ${lFloor}%)`),
  222. // side: THREE.DoubleSide,
  223. shininess: 10
  224. }
  225. let floorMat = new THREE.MeshPhongMaterial(floorOpts)
  226. let floorMesh = new THREE.Mesh(floorGeom, floorMat)
  227. let floorPosition = { ...position }
  228. floorPosition.y -= 0.5 * size.y
  229. return {
  230. block3d: null,
  231. // block_opts: materialOpts,
  232. label3d: null,
  233. label_opts: {
  234. side: THREE.DoubleSide,
  235. // wireframe: false,
  236. transparent: true
  237. // opacity: 0.6
  238. // renderOrder: 0
  239. },
  240. // size and positions are defined in store project
  241. size: size,
  242. position: position,
  243. // geometry: geometry,
  244. // light: light,
  245. building: building,
  246. building_position: buildingPos,
  247. top_mesh: topMesh,
  248. top_position: topPosition,
  249. floor_mesh: floorMesh,
  250. floor_position: floorPosition,
  251. label_position: { x: 0, y: 0, z: 0 },
  252. color: 0xffffff,
  253. label_canvas: null,
  254. label_size: null,
  255. isOpened: false
  256. }
  257. },
  258. computed: {
  259. label_texture_opts () {
  260. return {
  261. canvas: this.label_canvas,
  262. minFilter: THREE.LinearFilter,
  263. wrapS: THREE.ClampToEdgeWrapping,
  264. wrapT: THREE.ClampToEdgeWrapping
  265. }
  266. }
  267. },
  268. created () {
  269. console.log('Project created: data', this, this.data)
  270. this.label_canvas = this.createLabelCanvas(this.data.Titre.replace(/ /g, '\n').toUpperCase(), 48, 21, '#000000', '#ffffff')
  271. this.label_position.x = this.position.x - this.size.x / 2 + this.label_size.x / 2 + 1
  272. this.label_position.y = this.position.y + this.size.y / 2 - this.label_size.y / 2 - 1
  273. this.label_position.z = this.position.z + this.size.z / 2 + 0.5
  274. // console.log('this.label_canvas', this.label_canvas)
  275. },
  276. mounted () {
  277. console.log('project mounted', this)
  278. // record self references
  279. this.block3d = this.$refs.block3d.curObj
  280. // this.block3d.castShadow = true
  281. // this.block3d.receiveShadow = true
  282. this.block3d.userData = {
  283. vnode: this
  284. }
  285. this.label3d = this.$refs.label3d.curObj
  286. // light
  287. // console.log('project mounted', this.$env3d)
  288. // var light = new THREE.AmbientLight(0xbf1a1a) // soft white light
  289. // this.$env3d.scene.add(light)
  290. }
  291. // ,
  292. // methods: {
  293. //
  294. // }
  295. }
  296. </script>