void Update() { Vector3 centerPos = Vector3.zero; bool bPosValid = false; if (Camera.main) { centerPos = Camera.main.transform.position; bPosValid = true; } #if QINSHI if (CameraManager.Instance.controller.GetFollowTargetObject() != null) { centerPos = CameraManager.Instance.controller.GetFollowTargetObject().transform.position; bPosValid = true; } #endif if (bPosValid) { int currentX = GetGridStartIndex(centerPos.x); int currentY = GetGridStartIndex(centerPos.z);//摄像机当前所处的格子 rollMap.RollTo(currentX, currentY); if (taskList.Count > 0) { int nearestIndex = GetNearestTaskIndex(centerPos, taskList); TerrainGrassTile tile = taskList[nearestIndex]; taskList.RemoveAt(nearestIndex); float top = 0; float bottom = 10000; tile.gameObject.SetActive(true); for (int i = 0; i < tile.grassList.Count; i++) { TerrainGrass grass = tile.grassList[i]; bool hasMesh = grass.CreateMesh(); if (hasMesh) { grass.gameObject.SetActive(true); top = Mathf.Max(grass.top, top); bottom = Mathf.Min(grass.bottom, bottom); } } tile.UpdateBox(top, bottom); //正在生成时,慢慢的修改可视距离,减小跳动 for (int i = 0; i < materials.Length; i++) { materials[i].SetFloat("_ViewDistance", (tile.Center - centerPos).magnitude - PatchSize / 2.0f); } } } Shader.SetGlobalFloat("_GrassTime", Time.time); #if UNITY_EDITOR UpdateGrassShaderParams(); #endif }