Coal Wall Creation And Code Optimization

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using ZongCaiGongZuoMian;public class CoalWall : MonoBehaviour
{[SerializeField]Material mat;float zjInternal;//支架间隔float zjHeightMax;Transform trWheel01;//第一个滚筒Transform trWheel02;//第二个滚筒Transform[] gbTrans;//刮板机组float vertInternal;//点间隔float vertInternalSqrt;[SerializeField]float wheelRadius = 1f;//滚筒半径public float radiusOfWheel { get { return wheelRadius; } }float wheelRadiusSqrt;[SerializeField]float distGBToWall = 1;Vector3 curPos01;Vector3 curPos02;List<Vector3> listPos01 = new List<Vector3>();List<Vector3> listPos02 = new List<Vector3>();Mesh mesh;int wVertCount;int hVertCount;float wSize;float hSize;int countWRadius;int countHRadius;Transform trCoalWall;IEnumerator Start(){zjInternal = ZongCaiManager.instance.zjInternal;zjHeightMax = 3;//yield return new WaitUntil(delegate { return ZongCaiManager.instance.caiMeiJi; });trWheel01 = ZongCaiManager.instance.caiMeiJi.coalWallPointLeft;trWheel02 = ZongCaiManager.instance.caiMeiJi.coalWallPointRight;//curPos随便赋值一下,最好远离煤壁实际位置,这样在更新的时候直接发现位置有区别,就开始计算了。curPos01 = Vector3.left * 100;curPos02 = Vector3.left * 100;listPos01.Add(curPos01);listPos02.Add(curPos02);wheelRadiusSqrt = wheelRadius * wheelRadius;//yield return new WaitUntil(delegate { return ZongCaiManager.instance.gbTrans.Length > 0; });gbTrans = ZongCaiManager.instance.gbTrans;vertInternal = zjInternal * 0.12f;vertInternalSqrt = vertInternal * vertInternal;//Create(zjInternal * (gbTrans.Length + 1), zjHeightMax, vertInternal);}public void Create(float width, float height, float unitSize){GameObject objCoalWall = new GameObject("CoalWall");trCoalWall = objCoalWall.transform;trCoalWall.SetParent(transform);trCoalWall.position = gbTrans[0].position + Vector3.forward * distGBToWall;int wCount = Mathf.CeilToInt(width / unitSize);int hCount = Mathf.CeilToInt(height / unitSize);wVertCount = wCount + 1;hVertCount = hCount + 1;wSize = width / wCount;hSize = height / hCount;countWRadius = (int)(wheelRadius / wSize) + 1;countHRadius = (int)(wheelRadius / hSize) + 1;Vector3[] vs = new Vector3[wVertCount * hVertCount];for (int i = 0; i < wVertCount; i++){for (int j = 0; j < hVertCount; j++){vs[i * hVertCount + j] = new Vector3(i * wSize, j * hSize, 0);}}int[] ts = new int[wCount * hCount * 6];int k = 0;for (int i = 0; i < wCount; i++){for (int j = 0; j < hCount; j++){ts[k + 0] = i * hVertCount + j;ts[k + 1] = ts[k + 0] + 1;ts[k + 2] = (i + 1) * hVertCount + j;ts[k + 3] = ts[k + 1];ts[k + 4] = ts[k + 2] + 1;ts[k + 5] = ts[k + 2];k += 6;}}Vector2[] uvs = new Vector2[vs.Length];for (int i = 0; i < uvs.Length; i++){uvs[i] = vs[i] * 0.2f;}mesh = new Mesh();mesh.vertices = vs;mesh.triangles = ts;mesh.RecalculateNormals();mesh.uv = uvs;MeshFilter filter = objCoalWall.AddComponent<MeshFilter>();filter.mesh = mesh;MeshRenderer render = objCoalWall.AddComponent<MeshRenderer>();render.material = mat;}void Update(){UpdateListPos();UpdateMeshByLastPos();UpdateByGuaBan();}int listMaxCount = 16;void UpdateListPos(){if (!trCoalWall) return;if (!trWheel01) return;if (!trWheel02) return;//Vector3 pos = trCoalWall.InverseTransformPoint(trWheel01.position);if ((pos - listPos01[listPos01.Count - 1]).sqrMagnitude > vertInternalSqrt){listPos01.Add(pos);while (listPos01.Count > listMaxCount){listPos01.RemoveAt(0);}}pos = trCoalWall.InverseTransformPoint(trWheel02.position);if ((pos - listPos02[listPos02.Count - 1]).sqrMagnitude > vertInternalSqrt){listPos02.Add(pos);while (listPos02.Count > listMaxCount){listPos02.RemoveAt(0);}}}UnityAction<Transform> onCutLeft;public void AddActCutLeft(UnityAction<Transform> act) { onCutLeft -= act; onCutLeft += act; }public void RemoveActCutLeft(UnityAction<Transform> act) { onCutLeft -= act; }UnityAction<Transform> onCutRight;public void AddActCutRight(UnityAction<Transform> act) { onCutRight -= act; onCutRight += act; }public void RemoveActCutRight(UnityAction<Transform> act) { onCutRight -= act; }void UpdateMeshByLastPos(){if (!mesh) return;//bool changed01 = false;bool changed02 = false;if (!Mathf.Approximately((curPos01 - listPos01[listPos01.Count - 1]).sqrMagnitude, 0)){changed01 = true;curPos01 = listPos01[listPos01.Count - 1];}if (!Mathf.Approximately((curPos02 - listPos02[listPos02.Count - 1]).sqrMagnitude, 0)){changed02 = true;curPos02 = listPos02[listPos02.Count - 1];}if (changed01 || changed02){Vector3[] vs = mesh.vertices;if (changed01){UpdateVerticeByWheel(vs, trWheel01, curPos01, onCutLeft);}if (changed02){UpdateVerticeByWheel(vs, trWheel02, curPos02, onCutRight);}mesh.vertices = vs;mesh.RecalculateNormals();}}void UpdateVerticeByWheel(Vector3[] vertices, Transform trWheel, Vector3 curPos, UnityAction<Transform> act){Vector3 posToWall = trCoalWall.InverseTransformPoint(trWheel.position);//int iW = Mathf.RoundToInt(posToWall.x / wSize);int iWMin = Mathf.Clamp(iW - countWRadius, 0, wVertCount);int iWMax = Mathf.Clamp(iW + countWRadius, 0, wVertCount);//int iH = Mathf.RoundToInt(posToWall.y / hSize);int iHMin = Mathf.Clamp(iH - countHRadius, 0, hVertCount);int iHMax = Mathf.Clamp(iH + countHRadius, 0, hVertCount);for (int i = iWMin; i < iWMax; i++){for (int j = iHMin; j < iHMax; j++){int index = i * hVertCount + j;Vector2 v2 = vertices[index] - curPos;if (v2.sqrMagnitude > wheelRadiusSqrt) continue;if (vertices[index].z < curPos.z) vertices[index].z = curPos.z;act?.Invoke(trWheel01);}}}float timeUpdateByGuaBan = -100;void UpdateByGuaBan(){if (!mesh) return;if (Time.time - timeUpdateByGuaBan < 5) return;timeUpdateByGuaBan = Time.time;Vector3[] vs = mesh.vertices;Vector3[] colPs = new Vector3[gbTrans.Length];for (int i = 0; i < gbTrans.Length; i++){colPs[i] = trCoalWall.InverseTransformPoint(gbTrans[i].position);colPs[i].z += distGBToWall;}int indexGB = 0;for (int i = 0; i < wVertCount; i++){int indexW = i * hVertCount;Vector3 v = vs[indexW];while (v.x > colPs[indexGB].x){if (indexGB >= colPs.Length - 1) break;indexGB++;}for (int j = 0; j < hVertCount; j++){vs[indexW + j].z = colPs[indexGB].z;}}//float xMin = float.MaxValue;float xMax = float.MinValue;float yMin = float.MaxValue;float yMax = float.MinValue;foreach (Vector3 pos in listPos01){if (xMin > pos.x) xMin = pos.x;if (xMax < pos.x) xMax = pos.x;if (yMin > pos.y) yMin = pos.y;if (yMax < pos.y) yMax = pos.y;}foreach (Vector3 pos in listPos02){if (xMin > pos.x) xMin = pos.x;if (xMax < pos.x) xMax = pos.x;if (yMin > pos.y) yMin = pos.y;if (yMax < pos.y) yMax = pos.y;}int iWMin = Mathf.Clamp(Mathf.RoundToInt(xMin / wSize) - countWRadius, 0, wVertCount);int iWMax = Mathf.Clamp(Mathf.RoundToInt(xMax / wSize) + countWRadius, 0, wVertCount);int iHMin = Mathf.Clamp(Mathf.RoundToInt(yMin / hSize) - countHRadius, 0, hVertCount);int iHMax = Mathf.Clamp(Mathf.RoundToInt(yMax / hSize) + countHRadius, 0, hVertCount);for (int i = iWMin; i < iWMax; i++){for (int j = iHMin; j < iHMax; j++){int index = i * hVertCount + j;foreach (Vector3 pos in listPos01){Vector2 v2 = vs[index] - pos;if (v2.sqrMagnitude > wheelRadiusSqrt) continue;if (vs[index].z < pos.z) vs[index].z = pos.z;}foreach (Vector3 pos in listPos02){Vector2 v2 = vs[index] - pos;if (v2.sqrMagnitude > wheelRadiusSqrt) continue;if (vs[index].z < pos.z) vs[index].z = pos.z;}}}//mesh.vertices = vs;mesh.RecalculateNormals();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/58901.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Royal TSX 6 Mac多协议远程软件

Royal TSX是一款功能强大的远程桌面管理软件&#xff0c;适用于Mac操作系统。它允许用户通过一个集成的界面来管理和访问多个远程计算机和服务器。 Royal TSX支持多种远程协议&#xff0c;包括RDP、VNC、SSH、Telnet和FTP等&#xff0c;可以方便地连接到Windows、Linux、Mac和其…

非煤矿山风险监测预警算法 yolov8

非煤矿山风险监测预警算法通过yolov8网络模型深度学习算法框架&#xff0c;非煤矿山风险监测预警算法在煤矿关键地点安装摄像机等设备利用智能化视频识别技术&#xff0c;能够实时分析人员出入井口的情况&#xff0c;人数变化并检测作业状态。YOLO的结构非常简单&#xff0c;就…

ios开发 swift5 苹果系统自带的图标 SF Symbols

文章目录 1.官网app的下载和使用2.使用代码 1.官网app的下载和使用 苹果官网网址&#xff1a;SF Symbols 通过上面的网址可以下载dmg, 安装到自己的mac上 貌似下面这样不能展示出动画&#xff0c;还是要使用动画的代码 .bounce.up.byLayer2.使用代码 UIKit UIImage(system…

解决报错Java: 非法字符: ‘\ufeff‘

方法一&#xff1a;直接remove BOM&#xff0c;再重新启动程序。 方法二&#xff1a;用notpad打开&#xff0c;点击编码为utf-8格式&#xff0c;保存。

设置好以下几点Idea配置,极大提升研发效率

这里分享几点配置IntelliJ IDEA的建议,可以提升开发效率: 1.自定义代码风格(Code Style),统一项目代码风格 在IntelliJ IDEA中可以自定义代码风格,主要包括: 设置代码的缩进规则,如:缩进空格数,tab是否使用空格代替等。 在Settings - Editor - Code Style - Java中配置。 …

DockerFile常用命令

以下是常见的Dockerfile命令&#xff1a; FROM&#xff1a;FROM命令用于指定基础镜像。基础镜像是构建镜像的起点。例如&#xff0c;FROM ubuntu:latest表示使用最新版本的Ubuntu作为基础镜像。 MAINTAINER&#xff1a;MAINTAINER命令用于指定镜像的维护者信息。一般格式为&am…

GrapeCity Documents V6.0 Update 2发布,新增支持SpreadJS的.sjs文件格式

近日&#xff0c;GrapeCity Documents 正式迎来其V6.2 的发布更新&#xff0c;能够支持 SpreadJS 中 .sjs 类型的文件。这一重大更新将为用户带来更多地惊喜。 .sjs文件有两个关键优势&#xff1a;空间更小且导入导出速度更快。通过采用 .sjs格式&#xff0c;GcExcel实现了更高…

PO设计模式是selenium自动化测试中最佳的设计模式之一

Page Object Model&#xff1a;PO设计模式是selenium自动化测试中最佳的设计模式之一&#xff0c;主要体现在对界面交互细节的封装&#xff0c;也就是在实际测试中只关注业务流程就OK了传统的设计中&#xff0c;在新增测试用例之后&#xff0c;代码会有以下几个问题&#xff1a…

maven部署

一、下载Maven 地址&#xff1a;Maven – Download Apache Maven 二、解压缩&#xff0c;设置环境变量 tar -xvf apache-maven-3.8.8-bin.tar.gz export MAVEN_HOME/opt/apache-maven-3.8.8 export PATH$MAVEN_HOME/bin:$PATH echo $MAVEN_HOME echo $PATH mvn -v

uniapp中引入axios的错误?

场景 在unaipp中使用axios npm i axios 下载完成后 然后在页面中使用 axios.get(“http://3000/searchS”) 然后报错 Adapter http’ is not available in the build 原因 在 UniApp 中使用 Axios 发送 HTTP 请求时&#xff0c;如果出现错误 “Adapter http’ is not available…

Stable Diffusion Web UI的原理与使用

Stable Diffusion是一套基于Diffusion扩散模型生成技术的图片生成方案&#xff0c;随着技术的不断发展以及工业界对这套工程细节的不断优化&#xff0c;使其终于能在个人电脑上运行&#xff0c;本文将从github下载开始讲一讲如何使用Stable Diffusion Web UI进行AI图像的生成。…

水稻叶病害数据集(目标检测,yolo使用)

1.数据集文件夹 train文件夹&#xff08;44229张&#xff09;&#xff0c;test文件夹&#xff08;4741张&#xff09;&#xff0c;valid文件夹&#xff08;6000张&#xff09; 2.train文件夹展示 labels展示 标签txt展示 data.yaml文件展示 对数据集感兴趣的可以关注最后一行…

网络安全法+网络安全等级保护

网络安全法 2014年2月&#xff0c;中央网络安全和信息化领导小组成立&#xff0c;习主席当组长 2017年6月1日&#xff0c;网络安全法正式成立 网络安全是国家安全的重要组成部分没有网络安全就没有国家安全&#xff0c;没有信息化就没有现代化 网络安全法21条 网络安全法31条 …

[C/C++]天天酷跑游戏超详细教程-上篇

个人主页&#xff1a;北海 &#x1f390;CSDN新晋作者 &#x1f389;欢迎 &#x1f44d;点赞✍评论⭐收藏✨收录专栏&#xff1a;C/C&#x1f91d;希望作者的文章能对你有所帮助&#xff0c;有不足的地方请在评论区留言指正&#xff0c;大家一起学习交流&#xff01;&#x1f9…

视频剪辑音效处理软件有哪些?视频剪辑软件那个好用

音效是视频剪辑的重要部分&#xff0c;能起到画龙点睛的作用。在短视频平台中&#xff0c;一段出彩的音效能将原本平平无奇的视频变得生动有趣。那么&#xff0c;视频剪辑音效处理软件有哪些&#xff1f;本文会给大家介绍好用的音效处理软件&#xff0c;同时也会介绍视频剪辑音…

危险的套娃:攻击者在 PDF 文件中隐藏恶意Word 文档

据BleepingComputer消息&#xff0c;日本计算机紧急响应小组 (JPCERT) 日前分享了在2023 年 7 月检测到的利用PDF文档的新型攻击——PDF MalDoc攻击&#xff0c;能将恶意 Word 文件嵌入 PDF 来绕过安全检测。 JPCERT采样了一种多格式文件&#xff0c;能被大多数扫描引擎和工具识…

如何使用CSS实现一个带有动画效果的进度条?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ HTML 结构&#xff1a;⭐ CSS 样式&#xff1a;⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅&#xff01;这个专栏是为那…

BT8918D---按键模块

1 Preface/Foreword 中科蓝讯BT8918D模块支持&#xff1a;IO按键、AD按键 2 数据结构 2.1 按键事件映射表 typedef struct { u8 adc_val; u8 usage_id; } adkey_tbl_t; 按键功能配置表&#xff1a;adkey_table const adkey_tbl_t adkey_table[] { {0x0A, KEY_P…

使用finksql方式将mysql数据同步到kafka中,每次只能同步一张表

使用finksql方式将mysql数据同步到kafka中&#xff0c;每次只能同步一张表 package flink;import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.table.api.TableResult; import org.apache.flink.table.api.bridge.java.…

adb 查找应用包名,应用 Activity 等信息

列出设备上的包 不使用参数&#xff1a;adb shell pm list packages&#xff0c;打印设备/模拟器上的所有软件包 根据包名查看应用的activity 命令&#xff1a; dumpsys package 包名 adb shell dumpsys package 包名 petrel-cv96d:/data/app # dumpsys package com.instal…