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); } } }
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); }
public RayCastRestult[] batchRayCast(Vector3[] startPos, Vector3[] dir, float[] lenght) { int num = startPos.Length; RayCastRestult[] rlts = new RayCastRestult[num]; for (int i = 0; i < num; i++) { rlts[i] = rayCast(startPos[i], dir[i], lenght[i]); } return(rlts); }