static float calcDirectSunLight(RayCastManager rayTraceManager, Vector3 sunDir, int globalX, int globalY, int globalZ, int faceIndex) { //计算ao Vector3 faceNormal = Const.getFaceNormal(faceIndex); float faceLightFactor = Vector3.Dot(faceNormal, -sunDir); if (faceLightFactor < 0) { return(0); } else { Vector3 startPos = (new Vector3(globalX, globalY, globalZ) + Vector3.one * 0.5f + faceNormal * 0.501f) * Const.BlockSize; float rayLength = 100.0f; //float brightness = 0.0f; RayCastRestult rlt = rayTraceManager.rayCast(startPos, -sunDir, rayLength, RayCastBlockType.All); if (rlt.bHit) { return(0); } else { return(faceLightFactor); } } }
public static Vector3[] getRandomRays(int num, int faceIndex) { //UnityEngine.Random.seed = 12345678; RandomPoints randomPoints = new RandomPoints(); Vector2[] angles = randomPoints.getRandomPoints(num); Vector3[] rays = new Vector3[num]; Vector3 faceNormal = Const.getFaceNormal(faceIndex); for (uint i = 0; i < angles.Length; i++) { rays[i] = RandomPoints.getNewDir(angles[i].x, angles[i].y, faceNormal); } return(rays); }
static float calcAo(RayCastManager rayTraceManager, Vector3[] rays, int globalX, int globalY, int globalZ, int faceIndex) { //计算ao Vector3 startPos = (new Vector3(globalX, globalY, globalZ) + Vector3.one * 0.5f + Const.getFaceNormal(faceIndex) * 0.51f) * Const.BlockSize; float rayLength = 16; float brightness = 0.0f; Vector3[] startPosArray = new Vector3[rays.Length]; float[] rayLenghtArray = new float[rays.Length]; for (int i = 0; i < startPosArray.Length; i++) { startPosArray[i] = startPos; rayLenghtArray[i] = rayLength; } RayCastRestult[] rlts = rayTraceManager.batchRayCast(startPosArray, rays, rayLenghtArray, RayCastBlockType.Opacity); for (uint i = 0; i < rays.Length; i++) { RayCastRestult rlt = rlts[i];//rayTraceManager.rayCast(startPos, rays[i], rayLength); if (rlt.bHit && rlt.hitLength < rayLength) { float a = ((rlt.hitLength) / rayLength); brightness += (a / rays.Length); } else { brightness += 1.0f / rays.Length; } } brightness = Mathf.Clamp(brightness, 0.0f, 1.0f); return(Mathf.Pow(brightness, 0.8f) * 0.9f + 0.1f); }