gasAssessmen.threejs.base.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import * as THREE from 'three';
  2. import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
  3. import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass.js';
  4. import { setModalCenter, setTag3D, gradientColors } from '/@/utils/threejs/util';
  5. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  6. import { green, yellow } from '@ant-design/colors';
  7. // import * as dat from 'dat.gui';
  8. // const gui = new dat.GUI();
  9. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  10. type Unit = {
  11. id: string;
  12. ratio: number;
  13. color: THREE.Color;
  14. };
  15. class GasAssessmen {
  16. model;
  17. modelName = 'workFace';
  18. group: THREE.Object3D = new THREE.Object3D();
  19. planeGroup: THREE.Group = new THREE.Group();
  20. bloomComposer: EffectComposer | null = null;
  21. finalComposer: EffectComposer | null = null;
  22. outlinePass: OutlinePass | null = null;
  23. positions: THREE.Vector3[][] = [];
  24. bloomLayer = new THREE.Layers();
  25. darkMaterial = new THREE.MeshBasicMaterial({ color: 'black', transparent: true, side: THREE.DoubleSide });
  26. materials = {};
  27. glob = {
  28. ENTIRE_SCENE: 0,
  29. BLOOM_SCENE: 10,
  30. N: 100,
  31. };
  32. locationTexture: THREE.Texture | null = null;
  33. warningLocationTexture: THREE.Texture | null = null;
  34. errorLocationTexture: THREE.Texture | null = null;
  35. playerStartClickTime1 = new Date().getTime();
  36. playerStartClickTime2 = new Date().getTime();
  37. planeNum = 0;
  38. unitList: Unit[] = [];
  39. constructor(model) {
  40. this.model = model;
  41. this.group.name = this.modelName;
  42. }
  43. addLight() {
  44. // const _this = this;
  45. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  46. directionalLight.position.set(-196, 150, 258);
  47. this.group.add(directionalLight);
  48. directionalLight.target = this.group;
  49. }
  50. render() {
  51. this.model.renderer?.render(this.model.scene as THREE.Scene, this.model.camera as THREE.PerspectiveCamera);
  52. }
  53. setPlanes = (n) => {
  54. // const sizeList = [0.2, 0.3, 0.1, 0.2, 0.2];
  55. const colors = {
  56. c1: new THREE.Color(0x00fe00), // >90
  57. c2: new THREE.Color(0xf9b866), // >75 <90 249,184,102
  58. c3: new THREE.Color(0xfefe00), // 50-75 254,254,0
  59. c4: new THREE.Color(0xfe6600), // 25-50 254,102,0
  60. c5: new THREE.Color(0xb00101), // <25 176,1,1
  61. };
  62. const sizeList = [
  63. {
  64. id: '111',
  65. ratio: 0.5,
  66. color: colors.c1,
  67. },
  68. {
  69. id: '222',
  70. ratio: 0.3,
  71. color: colors.c2,
  72. },
  73. {
  74. id: '333',
  75. ratio: 0.2,
  76. color: colors.c4,
  77. },
  78. // {
  79. // id: '444',
  80. // ratio: 0.2,
  81. // color: colors.c5,
  82. // },
  83. // {
  84. // id: '555',
  85. // ratio: 0.2,
  86. // color: colors.c3,
  87. // },
  88. ];
  89. this.unitList = sizeList;
  90. // width = 7.713 height =3.717
  91. // const sizeList = [
  92. // {
  93. // ratio: 0.4,
  94. // color: colors.c1,
  95. // },
  96. // {
  97. // ratio: 0.5,
  98. // color: colors.c2,
  99. // },
  100. // {
  101. // ratio: 0.1,
  102. // color: colors.c4,
  103. // },
  104. // ];
  105. const geometry = new THREE.PlaneGeometry(7.723, 3.72, 1, 1);
  106. // 初始化累积比例数组和颜色数组
  107. const accumulatedRatios = [];
  108. const colorsArray = new Float32Array(3 * sizeList.length);
  109. // 计算累积比例和颜色数组
  110. function updateShaderData(sizeList) {
  111. let accRatio = 0;
  112. for (let i = 0; i < sizeList.length; i++) {
  113. const item = sizeList[i];
  114. accRatio += item.ratio;
  115. accumulatedRatios.push(accRatio);
  116. colorsArray[i * 3] = item.color.r;
  117. colorsArray[i * 3 + 1] = item.color.g;
  118. colorsArray[i * 3 + 2] = item.color.b;
  119. }
  120. }
  121. updateShaderData(sizeList); // 初始调用
  122. // 定义着色器代码
  123. const vertexShader = `
  124. varying vec2 vUv;
  125. void main() {
  126. vUv = uv;
  127. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  128. }
  129. `;
  130. const fragmentShader = `
  131. varying vec2 vUv;
  132. uniform float ratios[${sizeList.length}];
  133. uniform vec3 colors[${sizeList.length}];
  134. void main() {
  135. for(int i = 0; i < ${sizeList.length}; i++) {
  136. if(vUv.x < ratios[i]) {
  137. gl_FragColor = vec4(colors[i], 1.0);
  138. return;
  139. }
  140. }
  141. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.5);
  142. }
  143. `;
  144. // const fragmentShader = `
  145. // varying vec2 vUv;
  146. // uniform float ratios[${sizeList.length + 1}]; // 加入了起始点 0
  147. // uniform vec3 colors[${sizeList.length}];
  148. // vec3 lerp(vec3 a, vec3 b, float t) {
  149. // return a + t * (b - a);
  150. // }
  151. // void main() {
  152. // int index = 0;
  153. // for(int i = 1; i <= ${sizeList.length}; i++) {
  154. // if(vUv.x >= ratios[i-1] && vUv.x < ratios[i]) {
  155. // index = i - 1;
  156. // break;
  157. // }
  158. // }
  159. // // 如果是最后一个颜色块,直接使用其颜色
  160. // if (index == ${sizeList.length - 1}) {
  161. // gl_FragColor = vec4(colors[index], 1.0);
  162. // } else {
  163. // // 计算该像素在当前颜色块内的相对位置
  164. // float t = (vUv.x - ratios[index]) / (ratios[index + 1] - ratios[index]);
  165. // // 在相邻颜色间进行线性插值
  166. // vec3 color = lerp(colors[index], colors[index + 1], t);
  167. // gl_FragColor = vec4(color, 1.0);
  168. // }
  169. // }
  170. // `;
  171. // 创建着色器材质
  172. const material = new THREE.ShaderMaterial({
  173. uniforms: {
  174. ratios: { value: accumulatedRatios },
  175. colors: { value: colorsArray },
  176. },
  177. vertexShader: vertexShader,
  178. fragmentShader: fragmentShader,
  179. depthTest: false,
  180. depthWrite: false,
  181. });
  182. // // 当 sizeList 数据变化时调用此函数
  183. // function updateSizeList(newSizeList) {
  184. // accumulatedRatios.length = 0; // 清空累积比例数组
  185. // updateShaderData(newSizeList);
  186. // material.uniforms.ratios.value = accumulatedRatios;
  187. // material.uniforms.colors.value = colorsArray;
  188. // material.needsUpdate = true;
  189. // }
  190. // 创建网格并添加到场景中
  191. const plane = new THREE.Mesh(geometry, material);
  192. plane.rotation.x = -Math.PI / 2;
  193. plane.position.set(-0.2, 0.15, -0.03);
  194. plane.name = 'unit';
  195. this.planeGroup.add(plane);
  196. this.group.add(this.planeGroup);
  197. };
  198. // 清除抽采单元绘制面
  199. clearPlanes = () => {
  200. for (let i = 0; i < this.planeNum; i++) {
  201. const plane = this.planeGroup.getObjectByName(`unit${i}`);
  202. const label = this.planeGroup.getObjectByName(`planeText${i}`);
  203. if (plane) this.planeGroup.remove(plane);
  204. if (label) this.planeGroup.remove(label);
  205. }
  206. };
  207. // 抽采单元内容显示
  208. setCss3D = () => {
  209. const sizeList = [0.5, 0.3, 0.2];
  210. // const sizeList = [0.4, 0.5, 0.1];
  211. // width = 7.713 height =3.717
  212. let leftW = 0;
  213. for (let i = 0; i < sizeList.length; i++) {
  214. const label = setTag3D(`抽采单元${i + 1}`, 'gas_unit_text');
  215. label.scale.set(0.02, 0.02, 0.02); //根据相机渲染范围控制HTML 3D标签尺寸
  216. label.position.set((leftW + sizeList[i] / 2) * 7.913 - 4.22, 0.015, 0.142);
  217. label.name = 'planeText' + i;
  218. this.planeGroup.add(label);
  219. const obj = this.group.getObjectByName(`unitText${i}`);
  220. if (!obj) {
  221. const element = document.getElementById(`gasUnitBox${i + 1}`) as HTMLElement;
  222. if (element) {
  223. const gasUnitCSS3D = new CSS3DObject(element);
  224. gasUnitCSS3D.name = `unitText${i}`;
  225. gasUnitCSS3D.scale.set(0.01, 0.01, 0.01);
  226. gasUnitCSS3D.position.set((leftW + sizeList[i] / 2) * 10.93 - 11.17, 0.015, -3.442);
  227. gasUnitCSS3D.lookAt(gasUnitCSS3D.position.x, gasUnitCSS3D.position.y + 1.2, gasUnitCSS3D.position.y);
  228. this.planeGroup.add(gasUnitCSS3D);
  229. }
  230. }
  231. leftW += sizeList[i];
  232. }
  233. };
  234. // 显示或隐藏抽采单元显示内容
  235. changeCss3D = (isHide) => {
  236. for (let i = 0; i < this.planeNum; i++) {
  237. const obj = this.group.getObjectByName(`unitText${i}`);
  238. if (obj) {
  239. obj.visible = isHide;
  240. }
  241. }
  242. };
  243. // 清除抽采单元显示内容
  244. clearCss3D = () => {
  245. const obj = this.group.getObjectByName(`unitText`);
  246. if (obj) this.group.remove(obj);
  247. const element = document.getElementById(`gasUnitBox`) as HTMLElement;
  248. if (element) {
  249. element.remove();
  250. }
  251. for (let i = 0; i < this.planeNum; i++) {
  252. const label = this.planeGroup.getObjectByName(`planeText${i}`);
  253. if (label) this.planeGroup.remove(label);
  254. }
  255. };
  256. /* 点击 */
  257. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  258. // 判断是否点击到视频
  259. return new Promise((resolve) => {
  260. intersects.find((intersect) => {
  261. const intersectedObject = intersect.object;
  262. if (intersectedObject.name == 'unit') {
  263. // 如果对象是我们的平面,并且它有自定义属性来标识它的 UV 范围
  264. if (intersectedObject && intersectedObject.material && intersectedObject.material.uniforms) {
  265. const uv = intersect.uv; // 点击点的 UV 坐标
  266. const clickedRatio = uv.x; // 使用 UV 的 x 分量作为比例值
  267. // 根据点击的比例找到对应的色块
  268. let clickedColorIndex = -1;
  269. let accRatio = 0;
  270. for (let i = 0; i < this.unitList.length - 1; i++) {
  271. const unitData = this.unitList[i];
  272. accRatio += unitData.ratio;
  273. if (clickedRatio < accRatio) {
  274. clickedColorIndex = i;
  275. break;
  276. }
  277. }
  278. if (clickedColorIndex !== -1) {
  279. const unitData = this.unitList[clickedColorIndex];
  280. const unitId = unitData.id;
  281. // window.open(`${location.origin}/gasUnitAssessment/home?id=${unitId}`);
  282. resolve(unitId);
  283. }
  284. }
  285. return true;
  286. }
  287. return false;
  288. });
  289. this.render();
  290. });
  291. }
  292. mouseUpModel() {
  293. //
  294. }
  295. mountedThree() {
  296. return new Promise(async (resolve) => {
  297. this.model.renderer.sortObjects = true;
  298. this.model.orbitControls.update();
  299. this.model.setGLTFModel(['workFace1'], this.group).then(async () => {
  300. this.group.children.forEach((object: THREE.Object3D) => {
  301. if (object.name.startsWith('workFace')) {
  302. setModalCenter(object);
  303. }
  304. });
  305. this.group.name = this.modelName;
  306. this.addLight();
  307. resolve(null);
  308. });
  309. });
  310. }
  311. destroy() {
  312. this.model.clearGroup(this.group);
  313. this.model = null;
  314. this.group = null;
  315. this.bloomComposer?.dispose();
  316. this.finalComposer?.dispose();
  317. }
  318. }
  319. export default GasAssessmen;