public void StartRender(RayTracingInfo rtInfo, OutputInfo outputInfo) { int sample = (int)Mathf.Sqrt(rtInfo.sampleCount); Vector2 resolution = outputInfo.Resolution; Vector2 ratio = new Vector2(Screen.width / resolution.x, Screen.height / resolution.y); Vector2 s_ratio = ratio / sample; _renderResult = new Texture2D((int)resolution.x, (int)resolution.y, TextureFormat.RGB24, false); Vector2 rayPoint = Vector2.zero; Color pixelColor = new Color(0, 0, 0); for (int w = 0; w < resolution.x; w++) { for (int h = 0; h < resolution.y; h++) { rayPoint = new Vector2(w * ratio.x, h * ratio.y); Color pixel = Color.black; for (int sw = 0; sw < sample; sw++) { for (int sh = 0; sh < sample; sh++) { rayPoint += new Vector2(sw * s_ratio.x, sh * s_ratio.y); Ray ray = Cam.ScreenPointToRay(rayPoint); pixel += AoRayTrace(ray) / sample / sample; } } TextureHelper.SetPixel(_renderResult, w, h, pixel); } } TextureHelper.SaveImg(_renderResult, "output.png"); }
public void GenerateScene(RayTracingInfo rtInfo, OutputInfo outputInfo) { // set screen resolution (not working in editor) Screen.SetResolution((int)outputInfo.Resolution.x, (int)outputInfo.Resolution.y, false); // set camera info Cam.transform.position = rtInfo.cameraPos; Cam.transform.LookAt(rtInfo.cameraCenter, rtInfo.cameraUp); // default perspective if (rtInfo.cameraType == 1) { Cam.orthographic = true; } // create scene objects foreach (RayTracingObject rObj in rtInfo.objects) { // light type if (rObj.o_type == ObjectType.none) { CreateLight(rObj); } //object type if (rObj.l_type == LightType.none) { CreateObject(rObj); } } }
public void StartRenderRect(RayTracingInfo rtInfo, OutputInfo outputInfo) { Debug.Log("Started!"); matIndex = File.ReadAllBytes(Path.Combine(Application.dataPath, "ModelData.bin")); //PreprocessModelMaterial(); //File.WriteAllBytes(Path.Combine(Application.dataPath, "ModelData.bin"), matIndex); Debug.Log("Model Preprocessed!"); int sample = rtInfo.sampleCount; int sq_sample = (int)Mathf.Sqrt(sample); int width = 800; int height = 800; rect = new Rect((800 - width) / 2, (800 - height) / 2, width, height); Vector2 s_ratio = new Vector2(1f / sq_sample, 1f / sq_sample); tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false); int total = (int)(rect.width * rect.height); for (int w = (int)rect.x; w < (int)rect.x + rect.width; w++) { for (int h = (int)rect.y; h < (int)rect.y + rect.height; h++) { Vector2 rayPoint = new Vector2(w, h); Color pixelColor = new Color(0, 0, 0); for (int sw = 0; sw < sq_sample; sw++) { for (int sh = 0; sh < sq_sample; sh++) { int reflCount = 0; Vector2 scRayPoint = new Vector2(rayPoint.x + sw * s_ratio.x, rayPoint.y + sh * s_ratio.y); Ray ray = cam.ScreenPointToRay(scRayPoint); // box filter bool RayFromGlass = false; Collider GlassCollider = null; pixelColor += RayTrace(ray, 0, ref reflCount, ref RayFromGlass, ref GlassCollider) / sample; } } TextureHelper.SetPixel(tex, w - (int)rect.x, h - (int)rect.y, pixelColor); } } Debug.Log("Finished!"); TextureHelper.SaveImg(tex, "Img/output.png"); }
public void StartRender(RayTracingInfo rtInfo, OutputInfo outputInfo) { int sample = rtInfo.sampleCount; int sq_sample = (int)Mathf.Sqrt(sample); // for test Vector2 resol = new Vector2(20, 20); //Vector2 resol = outputInfo.Resolution; Vector2 scResol = new Vector2(Screen.width, Screen.height); Vector2 ratio = new Vector2(scResol.x / resol.x, scResol.y / resol.y); Vector2 s_ratio = new Vector2(ratio.x / sq_sample, ratio.y / sq_sample); rect = new Rect(0, 0, resol.x, resol.y); tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false); for (int w = 0; w < resol.x; w++) { for (int h = 0; h < resol.y; h++) { Vector2 rayPoint = new Vector2(w * ratio.x, h * ratio.y); Color pixelColor = new Color(0, 0, 0); for (int sw = 0; sw < sq_sample; sw++) { for (int sh = 0; sh < sq_sample; sh++) { int reflCount = 0; Vector2 scRayPoint = new Vector2(rayPoint.x + sw * s_ratio.x, rayPoint.y + sh * s_ratio.y); Ray ray = cam.ScreenPointToRay(scRayPoint); // box filter bool RayFromGlass = false; Collider GlassCollider = null; pixelColor += RayTrace(ray, 0, ref reflCount, ref RayFromGlass, ref GlassCollider) / sample; } } TextureHelper.SetPixel(tex, w, h, pixelColor); } } TextureHelper.SaveImg(tex, "Img/output.png"); }