fiber.belt.threejs.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import * as THREE from 'three';
  2. import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
  3. import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
  4. import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
  5. import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass.js';
  6. import { GammaCorrectionShader } from 'three/examples/jsm/shaders/GammaCorrectionShader.js';
  7. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  8. // import * as dat from 'dat.gui';
  9. // const gui = new dat.GUI();
  10. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  11. class beltFiber {
  12. model;
  13. modelName = 'belt';
  14. group: THREE.Object3D = new THREE.Object3D();
  15. bloomComposer: EffectComposer | null = null;
  16. finalComposer: EffectComposer | null = null;
  17. positions: THREE.Vector3[] = [];
  18. msgPositions: THREE.Vector3[] = [];
  19. bloomLayer = new THREE.Layers();
  20. darkMaterial = new THREE.MeshBasicMaterial({ color: 'black', transparent: true, side: THREE.DoubleSide });
  21. materials = {};
  22. glob = {
  23. ENTIRE_SCENE: 0,
  24. BLOOM_SCENE: 1,
  25. N: 11,
  26. };
  27. locationMaterial: THREE.Material | null = null;
  28. warningMaterial: THREE.Material | null = null;
  29. errorMaterial: THREE.Material | null = null;
  30. playerStartClickTime1 = new Date().getTime();
  31. playerStartClickTime2 = new Date().getTime();
  32. constructor(model) {
  33. this.model = model;
  34. this.group.name = this.modelName;
  35. this.initMaterial();
  36. }
  37. addLight() {
  38. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.2);
  39. directionalLight.position.set(-0.8, 23, 3.9);
  40. this.group.add(directionalLight);
  41. directionalLight.target = this.group;
  42. // gui.add(directionalLight.position, 'x', -10, 20).onChange(function (value) {
  43. // directionalLight.position.x = Number(value);
  44. // _this.render();
  45. // });
  46. // gui.add(directionalLight.position, 'y', -50, 50).onChange(function (value) {
  47. // directionalLight.position.y = Number(value);
  48. // _this.render();
  49. // });
  50. // gui.add(directionalLight.position, 'z', -20, 20).onChange(function (value) {
  51. // directionalLight.position.z = Number(value);
  52. // _this.render();
  53. // });
  54. // const pointLight5 = new THREE.PointLight(0xffffff, 0.8, 120);
  55. // pointLight5.position.set(-54, 30, 23.8);
  56. // pointLight5.shadow.bias = 0.05;
  57. // this.group.add(pointLight5);
  58. // const pointLight7 = new THREE.PointLight(0xffffff, 1, 1000);
  59. // pointLight7.position.set(45, 51, -4.1);
  60. // pointLight7.shadow.bias = 0.05;
  61. // this.model.scene.add(pointLight7);
  62. const spotLight = new THREE.SpotLight();
  63. spotLight.angle = Math.PI / 2;
  64. spotLight.penumbra = 0;
  65. spotLight.castShadow = true;
  66. spotLight.intensity = 1;
  67. spotLight.shadow.camera.near = 0.5; // default
  68. spotLight.shadow.focus = 1.2;
  69. spotLight.shadow.bias = -0.000002;
  70. spotLight.position.set(-7.19, 199, -68.1);
  71. // this.group.add(spotLight);
  72. // gui.add(directionalLight.position, 'x', -10, 20).onChange(function (value) {
  73. // directionalLight.position.x = Number(value);
  74. // _this.render();
  75. // });
  76. // gui.add(directionalLight.position, 'y', -50, 50).onChange(function (value) {
  77. // directionalLight.position.y = Number(value);
  78. // _this.render();
  79. // });
  80. // gui.add(directionalLight.position, 'z', -20, 20).onChange(function (value) {
  81. // directionalLight.position.z = Number(value);
  82. // _this.render();
  83. // });
  84. // gui.add(spotLight.position, 'x', -600, 600).onChange(function (value) {
  85. // spotLight.position.x = Number(value);
  86. // _this.render();
  87. // });
  88. // gui.add(spotLight.position, 'y', -600, 800).onChange(function (value) {
  89. // spotLight.position.y = Number(value);
  90. // _this.render();
  91. // });
  92. // gui.add(spotLight.position, 'z', -500, 1000).onChange(function (value) {
  93. // spotLight.position.z = Number(value);
  94. // _this.render();
  95. // });
  96. }
  97. // 设置模型位置
  98. setModalPosition() {
  99. this.group?.scale.set(22, 22, 22);
  100. this.group?.position.set(-15, 25, 15);
  101. }
  102. initMaterial() {
  103. const loader = new THREE.TextureLoader();
  104. const locationTexture = loader.load('/model/img/location.png');
  105. const warningLocationTexture = loader.load('/model/img/warning-location.png');
  106. const errorLocationTexture = loader.load('/model/img/error-location.png');
  107. locationTexture.colorSpace = warningLocationTexture.colorSpace = warningLocationTexture.colorSpace = THREE.SRGBColorSpace;
  108. this.locationMaterial = new THREE.MeshPhongMaterial({
  109. emissive: 0x16ecf4,
  110. map: locationTexture,
  111. emissiveMap: locationTexture,
  112. transparent: true,
  113. side: THREE.DoubleSide,
  114. });
  115. this.warningMaterial = new THREE.MeshPhongMaterial({
  116. emissive: 0xf99b21,
  117. map: warningLocationTexture,
  118. emissiveMap: warningLocationTexture,
  119. transparent: true,
  120. side: THREE.DoubleSide,
  121. });
  122. this.errorMaterial = new THREE.MeshPhongMaterial({
  123. emissive: 0xf83b24,
  124. map: errorLocationTexture,
  125. emissiveMap: errorLocationTexture,
  126. transparent: true,
  127. side: THREE.DoubleSide,
  128. });
  129. }
  130. monitorData(warningNum = [3, 6], errorNum = [9]) {
  131. if (this.group) {
  132. if (warningNum.length > 0)
  133. warningNum.forEach((i) => {
  134. const warningMesh = this.group.getObjectByName('location' + i);
  135. if (warningMesh) {
  136. warningMesh.material = this.warningMaterial;
  137. }
  138. });
  139. if (errorNum.length > 0)
  140. errorNum.forEach((i) => {
  141. const errorMesh = this.group.getObjectByName('location' + i);
  142. if (errorMesh) {
  143. errorMesh.material = this.errorMaterial;
  144. }
  145. });
  146. }
  147. }
  148. addBeltText(selectData) {
  149. if (!this.group) {
  150. return;
  151. }
  152. const locationX = [-8.406, -6.597, -4.787, -2.967, -1.118, 0.692, 2.501, 4.411, 6.211, 8.0];
  153. for (let i = 1; i <= this.glob.N; i++) {
  154. const position = this.msgPositions[i];
  155. if (!this.group.getObjectByName('fiberMeg1' + i)) {
  156. const element = document.getElementById('fiberMeg' + i) as HTMLElement;
  157. if (element) {
  158. const mainCSS3D = new CSS3DObject(element);
  159. mainCSS3D.name = 'fiberMeg' + i;
  160. mainCSS3D.scale.set(0.008, 0.008, 0.008);
  161. mainCSS3D.position.set(position.x, -97.8, position.z);
  162. const lookAtPosition = this.model.camera.position;
  163. mainCSS3D.lookAt(lookAtPosition.x, lookAtPosition.y, lookAtPosition.z);
  164. this.group.add(mainCSS3D);
  165. }
  166. }
  167. // if (!this.group.getObjectByName('location' + i)) {
  168. // const geometry = new THREE.PlaneGeometry(1, 0.8);
  169. // const location = new THREE.Mesh(geometry, this.locationMaterial as THREE.Material);
  170. // location.scale.set(0.3, 0.3, 0.3);
  171. // location.name = 'location' + i;
  172. // location.position.set(locationX[i - 1], 1.384, position.z);
  173. // location.layers.enable(this.glob.ENTIRE_SCENE);
  174. // this.group.add(location);
  175. // }
  176. }
  177. }
  178. render() {
  179. const _this = this;
  180. if (this.model && this.model.scene.getObjectByName(this.modelName)) {
  181. this.group?.traverse((obj) => {
  182. _this.darkenNonBloomed(obj);
  183. });
  184. this.bloomComposer?.render();
  185. this.group?.traverse((obj) => {
  186. _this.restoreMaterial(obj);
  187. });
  188. this.finalComposer?.render();
  189. this.model.css3dRender?.render(this.model.scene as THREE.Scene, this.model.camera as THREE.PerspectiveCamera);
  190. }
  191. }
  192. setRenderPass = () => {
  193. this.bloomLayer.set(this.glob.BLOOM_SCENE);
  194. const params = {
  195. bloomStrength: 2,
  196. bloomThreshold: 0,
  197. bloomRadius: 0,
  198. };
  199. const renderScene = new RenderPass(this.model.scene, this.model.camera);
  200. const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85);
  201. bloomPass.strength = params.bloomStrength;
  202. bloomPass.radius = params.bloomRadius;
  203. bloomPass.threshold = params.bloomThreshold;
  204. this.bloomComposer = new EffectComposer(this.model.renderer);
  205. this.bloomComposer.renderToScreen = false;
  206. this.bloomComposer.addPass(renderScene);
  207. this.bloomComposer.addPass(bloomPass);
  208. const finalPass = new ShaderPass(
  209. new THREE.ShaderMaterial({
  210. uniforms: {
  211. baseTexture: { value: null },
  212. bloomTexture: { value: this.bloomComposer.renderTarget2.texture },
  213. },
  214. vertexShader: `
  215. varying vec2 vUv;
  216. void main() {
  217. vUv = uv;
  218. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  219. }`,
  220. fragmentShader: `uniform sampler2D baseTexture;
  221. uniform sampler2D bloomTexture;
  222. varying vec2 vUv;
  223. void main() {
  224. gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0, 1.0, 1.0, 0.0 ) * texture2D( bloomTexture, vUv ) );
  225. }`,
  226. defines: {},
  227. }),
  228. 'baseTexture'
  229. );
  230. const gammaCorrection = new ShaderPass(GammaCorrectionShader);
  231. finalPass.needsSwap = true;
  232. this.finalComposer = new EffectComposer(this.model.renderer);
  233. this.finalComposer.addPass(renderScene);
  234. this.finalComposer.addPass(gammaCorrection);
  235. this.finalComposer.addPass(finalPass);
  236. };
  237. getPositions(num = 40) {
  238. const curve1 = new THREE.LineCurve3(new THREE.Vector3(-9.676, 0.814, 0.019), new THREE.Vector3(9.68, 0.814, 0.019));
  239. // const curve2 = new THREE.LineCurve3(new THREE.Vector3(-7.758, -0.462, -0.275), new THREE.Vector3(20.758, 2.037, -0.275));
  240. // const len1 = curve1.getLength();
  241. // const len2 = curve2.getLength();
  242. // const unit = len1 + len2 / num;
  243. // const num1 = Math.floor(unit * len1);
  244. // const num2 = Math.floor(unit * len2);
  245. const points1 = curve1.getPoints(num);
  246. this.msgPositions = curve1.getPoints(this.glob.N);
  247. // const points2 = curve2.getPoints(num2);
  248. this.positions = [...points1];
  249. }
  250. drawSpheres = () => {
  251. const _this = this;
  252. return new Promise((resolve) => {
  253. new THREE.TextureLoader().load('/model/img/texture-smoke.png', (texture) => {
  254. texture.colorSpace = THREE.SRGBColorSpace;
  255. const geometry = new THREE.BufferGeometry();
  256. geometry.setFromPoints(_this.positions);
  257. const material = new THREE.PointsMaterial({
  258. color: '#FFFFAF',
  259. size: 0.13,
  260. map: texture,
  261. transparent: true, // 开启透明度
  262. });
  263. const points = new THREE.Points(geometry, material);
  264. points.renderOrder = 0;
  265. _this.group.add(points);
  266. points.layers.enable(_this.glob.BLOOM_SCENE);
  267. resolve(null);
  268. texture.dispose();
  269. });
  270. });
  271. };
  272. darkenNonBloomed(obj) {
  273. if (obj.isMesh && this.bloomLayer.test(obj.layers) === false) {
  274. const opacity = obj.material.opacity;
  275. this.materials[obj.uuid] = obj.material;
  276. obj.material = this.darkMaterial.clone();
  277. obj.material.opacity = opacity;
  278. }
  279. }
  280. restoreMaterial(obj) {
  281. if (this.materials[obj.uuid]) {
  282. obj.material = this.materials[obj.uuid];
  283. delete this.materials[obj.uuid];
  284. }
  285. }
  286. /* 点击 */
  287. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  288. if (intersects.length > 0) {
  289. // 判断是否点击到视频
  290. intersects.find((intersect) => {
  291. const mesh = intersect.object;
  292. if (mesh.name === 'player1') {
  293. return true;
  294. }
  295. return false;
  296. });
  297. }
  298. }
  299. mouseUpModel() {}
  300. mountedThree() {
  301. return new Promise(async (resolve) => {
  302. this.model.renderer.sortObjects = true;
  303. this.model.camera.position.set(0, 3.1, 500);
  304. this.setRenderPass();
  305. this.model.orbitControls.update();
  306. // this.model.orbitControls.addEventListener('change', _this.render.bind(_this));
  307. this.model.setGLTFModel(['laneway', 'laneway-device'], this.group).then(async () => {
  308. this.group.position.set(0, 0.7, 0);
  309. (this.group as THREE.Group).remove(this.group.getObjectByName('mesh001'));
  310. const optical = this.group.getObjectByName('optical_fiber_');
  311. if (optical) {
  312. optical.renderOrder = 9;
  313. optical.material = new THREE.MeshBasicMaterial({
  314. color: 0x555555,
  315. side: THREE.DoubleSide,
  316. transparent: true,
  317. opacity: 0.15,
  318. });
  319. }
  320. this.getPositions();
  321. this.addLight();
  322. this.drawSpheres().then(() => {
  323. // this.render();
  324. });
  325. // this.addBeltText();
  326. resolve(null);
  327. });
  328. });
  329. }
  330. destroy() {
  331. this.model.clearGroup(this.group);
  332. this.model = null;
  333. this.group = null;
  334. }
  335. }
  336. export default beltFiber;