适用于优化模型,对模型动态裁切。目前没有动态LOD。只是显隐。方法如下。
主要用到3个东西文章源自大腿Plus-https://www.zhaoshijun.com/archives/676
- 包围盒
- 相关矩阵
- 像素阈值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
public void StartDynamicCulling() { var bounds = GetComponent<collider>().BoundingBox; CornerPosVec = new Vector4[] { new Vector4(bounds.max.x,bounds.max.y,bounds.max.z,1), new Vector4(bounds.max.x,bounds.min.y,bounds.max.z,1), new Vector4(bounds.max.x,bounds.min.y,bounds.min.z,1), new Vector4(bounds.max.x,bounds.max.y,bounds.min.z,1), new Vector4(bounds.min.x,bounds.min.y,bounds.min.z,1), new Vector4(bounds.min.x,bounds.min.y,bounds.max.z,1), new Vector4(bounds.min.x,bounds.max.y,bounds.max.z,1), new Vector4(bounds.min.x,bounds.max.y,bounds.min.z,1) }; _cornerCameraPosVecByTrans = new Vector4[CornerPosVec.Length]; _cornerProjecionPosVecByTrans = new Vector4[CornerPosVec.Length]; _slefMeshRenderer = GetComponent<MeshRenderer>(); //InvokeRepeating("DynamicOcclusionCulling", 1, 3); StartCoroutine(DynamicOcclusionCulling()); } public static bool IsVisibleFromCameraView(this Renderer render, Camera camera) { Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera); return GeometryUtility.TestPlanesAABB(planes, render.bounds); } private IEnumerator DynamicOcclusionCulling() { do { //先判断是否在视锥体里 if (!GetComponent<Renderer>().IsVisibleFromCameraView(Camera.main)) { GetComponent<Renderer>().enabled = false; } else { //转到camera视角 for (int i = 0; i < CornerPosVec.Length; i++) { _cornerCameraPosVecByTrans[i] = Vector4ByMatrix(Camera.main.worldToCameraMatrix, CornerPosVec[i]); } for (int i = 0; i < CornerPosVec.Length; i++) { _cornerProjecionPosVecByTrans[i] = Vector4ByMatrix(Camera.main.projectionMatrix, _cornerCameraPosVecByTrans[i]); } double min0 = double.MaxValue, min1 = double.MaxValue; double max0 = double.MinValue, max1 = double.MinValue; for (int i = 0; i < 8; i++) { var ndcX = _cornerProjecionPosVecByTrans[i].x / _cornerProjecionPosVecByTrans[i].w; var ndcY = _cornerProjecionPosVecByTrans[i].y / _cornerProjecionPosVecByTrans[i].w; if (ndcX < min0) min0 = ndcX; if (ndcY < min1) min1 = ndcY; if (ndcX < max0) max0 = ndcX; if (ndcY < max1) max1 = ndcY; } var xPixel = max0 - min0; var yPixel = max1 - min1; xPixel *= Screen.width; yPixel *= Screen.height; var threshold = 32; if (xPixel <= threshold || yPixel <= threshold) { _slefMeshRenderer.enabled = false; } else { _slefMeshRenderer.enabled = true; } } yield return new WaitForSeconds(2); } while (true); } |
基本逻辑是这样。有更好想法欢迎提出。文章源自大腿Plus-https://www.zhaoshijun.com/archives/676 文章源自大腿Plus-https://www.zhaoshijun.com/archives/676
我的微信
微信扫一扫
shijun_z
我的QQ
QQ扫一扫
846207670
评论