fiber.ds.threejs.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 GrottoFiber {
  12. model;
  13. modelName = 'grotto';
  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. locationTexture: THREE.Texture | null = null;
  28. warningLocationTexture: THREE.Texture | null = null;
  29. errorLocationTexture: THREE.Texture | 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. }
  36. addLight() {
  37. const _this = this;
  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. monitorData(warningNum = [3, 6], errorNum = [9]) {
  103. if (this.group) {
  104. if (warningNum.length > 0)
  105. warningNum.forEach((i) => {
  106. const warningMesh = this.group.getObjectByName('location' + i);
  107. if (warningMesh) {
  108. const material = (warningMesh as THREE.Mesh).material as THREE.MeshStandardMaterial;
  109. material.map = this.warningLocationTexture;
  110. material.emissiveMap = this.warningLocationTexture;
  111. }
  112. });
  113. if (errorNum.length > 0)
  114. errorNum.forEach((i) => {
  115. const warningMesh = this.group.getObjectByName('location' + i);
  116. if (warningMesh) {
  117. const material = (warningMesh as THREE.Mesh).material as THREE.MeshStandardMaterial;
  118. material.map = this.errorLocationTexture;
  119. material.emissiveMap = this.errorLocationTexture;
  120. }
  121. });
  122. }
  123. }
  124. addBeltText() {
  125. if (!this.group) {
  126. return;
  127. }
  128. const locationMaterial = new THREE.MeshStandardMaterial({
  129. map: this.locationTexture,
  130. emissiveMap: this.locationTexture,
  131. transparent: true,
  132. side: THREE.DoubleSide,
  133. });
  134. const locationX = [-8.406, -6.597, -4.787, -2.967, -1.118, 0.692, 2.501, 4.411, 6.211, 8.0];
  135. for (let i = 1; i <= this.glob.N; i++) {
  136. if (!this.group.getObjectByName('fiberMeg' + i)) {
  137. const position = this.msgPositions[i];
  138. const element = document.getElementById('fiberMeg' + i) as HTMLElement;
  139. if (element) {
  140. const mainCSS3D = new CSS3DObject(element);
  141. mainCSS3D.name = 'fiberMeg' + i;
  142. mainCSS3D.scale.set(0.008, 0.008, 0.008);
  143. mainCSS3D.position.set(position.x, -97.8, position.z);
  144. const lookAtPosition = this.model.camera.position;
  145. mainCSS3D.lookAt(lookAtPosition.x, lookAtPosition.y, lookAtPosition.z);
  146. this.group.add(mainCSS3D);
  147. }
  148. const geometry = new THREE.PlaneGeometry(1, 0.9);
  149. const location = new THREE.Mesh(geometry, locationMaterial);
  150. location.scale.set(0.3, 0.3, 0.3);
  151. location.name = 'location' + i;
  152. location.position.set(locationX[i - 1], 1.384, position.z);
  153. location.layers.enable(this.glob.ENTIRE_SCENE);
  154. this.group.add(location);
  155. }
  156. }
  157. }
  158. render() {
  159. const _this = this;
  160. if (this.model && this.model.scene.getObjectByName(this.modelName)) {
  161. this.group?.traverse((obj) => {
  162. _this.darkenNonBloomed(obj);
  163. });
  164. this.bloomComposer?.render();
  165. this.group?.traverse((obj) => {
  166. _this.restoreMaterial(obj);
  167. });
  168. this.finalComposer?.render();
  169. this.model.css3dRender?.render(this.model.scene as THREE.Scene, this.model.camera as THREE.PerspectiveCamera);
  170. }
  171. }
  172. setRenderPass = () => {
  173. this.bloomLayer.set(this.glob.BLOOM_SCENE);
  174. const params = {
  175. bloomStrength: 2,
  176. bloomThreshold: 0,
  177. bloomRadius: 0,
  178. };
  179. const renderScene = new RenderPass(this.model.scene, this.model.camera);
  180. const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85);
  181. bloomPass.strength = params.bloomStrength;
  182. bloomPass.radius = params.bloomRadius;
  183. bloomPass.threshold = params.bloomThreshold;
  184. this.bloomComposer = new EffectComposer(this.model.renderer);
  185. this.bloomComposer.renderToScreen = false;
  186. this.bloomComposer.addPass(renderScene);
  187. this.bloomComposer.addPass(bloomPass);
  188. const finalPass = new ShaderPass(
  189. new THREE.ShaderMaterial({
  190. uniforms: {
  191. baseTexture: { value: null },
  192. bloomTexture: { value: this.bloomComposer.renderTarget2.texture },
  193. },
  194. vertexShader: `
  195. varying vec2 vUv;
  196. void main() {
  197. vUv = uv;
  198. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  199. }`,
  200. fragmentShader: `uniform sampler2D baseTexture;
  201. uniform sampler2D bloomTexture;
  202. varying vec2 vUv;
  203. void main() {
  204. gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0, 1.0, 1.0, 0.0 ) * texture2D( bloomTexture, vUv ) );
  205. }`,
  206. defines: {},
  207. }),
  208. 'baseTexture'
  209. );
  210. const gammaCorrection = new ShaderPass(GammaCorrectionShader);
  211. finalPass.needsSwap = true;
  212. this.finalComposer = new EffectComposer(this.model.renderer);
  213. this.finalComposer.addPass(renderScene);
  214. this.finalComposer.addPass(gammaCorrection);
  215. this.finalComposer.addPass(finalPass);
  216. };
  217. getPositions(num = 40) {
  218. const curve1 = new THREE.LineCurve3(new THREE.Vector3(-9.676, 0.814, 0.019), new THREE.Vector3(9.68, 0.814, 0.019));
  219. // const curve2 = new THREE.LineCurve3(new THREE.Vector3(-7.758, -0.462, -0.275), new THREE.Vector3(20.758, 2.037, -0.275));
  220. // const len1 = curve1.getLength();
  221. // const len2 = curve2.getLength();
  222. // const unit = len1 + len2 / num;
  223. // const num1 = Math.floor(unit * len1);
  224. // const num2 = Math.floor(unit * len2);
  225. const points1 = curve1.getPoints(num);
  226. this.msgPositions = curve1.getPoints(this.glob.N);
  227. // const points2 = curve2.getPoints(num2);
  228. this.positions = [...points1];
  229. }
  230. drawSpheres = () => {
  231. const _this = this;
  232. return new Promise((resolve) => {
  233. new THREE.TextureLoader().load('/model/img/texture-smoke.png', (texture) => {
  234. texture.encoding = THREE.sRGBEncoding;
  235. const geometry = new THREE.BufferGeometry();
  236. geometry.setFromPoints(_this.positions);
  237. const material = new THREE.PointsMaterial({
  238. color: '#FFFFAF',
  239. size: 0.13,
  240. map: texture,
  241. transparent: true, // 开启透明度
  242. });
  243. const points = new THREE.Points(geometry, material);
  244. points.renderOrder = 0;
  245. _this.group.add(points);
  246. points.layers.enable(_this.glob.BLOOM_SCENE);
  247. resolve(null);
  248. });
  249. });
  250. };
  251. darkenNonBloomed(obj) {
  252. if (obj.isMesh && this.bloomLayer.test(obj.layers) === false) {
  253. const opacity = obj.material.opacity;
  254. this.materials[obj.uuid] = obj.material;
  255. obj.material = this.darkMaterial.clone();
  256. obj.material.opacity = opacity;
  257. }
  258. }
  259. restoreMaterial(obj) {
  260. if (this.materials[obj.uuid]) {
  261. obj.material = this.materials[obj.uuid];
  262. delete this.materials[obj.uuid];
  263. }
  264. }
  265. /* 点击 */
  266. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  267. // 判断是否点击到视频
  268. intersects.find((intersect) => {
  269. const mesh = intersect.object;
  270. if (mesh.name === 'player1') {
  271. return true;
  272. }
  273. return false;
  274. });
  275. }
  276. mountedThree() {
  277. this.locationTexture = new THREE.TextureLoader().load('/model/img/location.png');
  278. this.warningLocationTexture = new THREE.TextureLoader().load('/model/img/location.png');
  279. this.errorLocationTexture = new THREE.TextureLoader().load('/model/img/location.png');
  280. this.locationTexture.encoding = this.warningLocationTexture.encoding = this.warningLocationTexture.encoding = THREE.sRGBEncoding;
  281. return new Promise(async (resolve) => {
  282. this.model.renderer.sortObjects = true;
  283. this.model.camera.position.set(0, 3.1, 500);
  284. this.setRenderPass();
  285. this.model.orbitControls.update();
  286. // this.model.orbitControls.addEventListener('change', _this.render.bind(_this));
  287. this.model.setModel(['laneway']).then(async (gltf) => {
  288. this.group = gltf[0];
  289. this.group.position.set(0, 0.7, 0);
  290. (this.group as THREE.Group).remove(this.group.getObjectByName('mesh001'));
  291. const optical = this.group.getObjectByName('optical_fiber_');
  292. if (optical) {
  293. optical.renderOrder = 9;
  294. optical.material = new THREE.MeshBasicMaterial({
  295. color: 0x555555,
  296. side: THREE.DoubleSide,
  297. transparent: true,
  298. opacity: 0.15,
  299. });
  300. }
  301. this.getPositions();
  302. this.addLight();
  303. this.drawSpheres().then(() => {
  304. // this.render();
  305. });
  306. this.addBeltText();
  307. resolve(null);
  308. });
  309. // window.onresize = function () {
  310. // const width = window.innerWidth;
  311. // const height = window.innerHeight;
  312. // model.camera.aspect = width / height;
  313. // model.camera.updateProjectionMatrix();
  314. // model.renderer.setSize(width, height);
  315. // bloomComposer.setSize(width, height);
  316. // finalComposer.setSize(width, height);
  317. // render();
  318. // };
  319. });
  320. }
  321. destroy() {
  322. this.model.clearGroup(this.group);
  323. this.model = null;
  324. this.group = null;
  325. }
  326. }
  327. export default GrottoFiber;