コード例 #1
0
        private static void RandomScene(ref HitList list)
        {
            list.Add(new Sphere(new Vector3(0, -1000, 0), 1000f, new Lambertian(new Vector3(0.5f, 0.5f, 0.5f))));
            for (int i = -11; i < 11; ++i)
            {
                for (int j = -11; j < 11; ++j)
                {
                    Vector3 center     = new Vector3(i + 0.9f * RandomFloat01(gSeed), 0.2f, j + 0.9f * RandomFloat01(gSeed));
                    Vector3 baseCenter = new Vector3(4, 0.2f, 0);
                    float   choose_mat = RandomFloat01(gSeed);
                    if ((center - baseCenter).magnitude > 0.9)
                    {
                        if (choose_mat < 0.5f)
                        {
                            list.Add(new Sphere(center, 0.2f, new Lambertian(new Vector3(RandomFloat01(gSeed) * RandomFloat01(gSeed),
                                                                                         RandomFloat01(gSeed) * RandomFloat01(gSeed), RandomFloat01(gSeed) * RandomFloat01(gSeed)))));
                        }
                        else if (choose_mat < 0.75f)
                        {
                            list.Add(new Sphere(center, 0.2f, new Metal(new Vector3(0.5f * (1 + RandomFloat01(gSeed)),
                                                                                    0.5f * (1 + RandomFloat01(gSeed)),
                                                                                    0.5f * (1 + RandomFloat01(gSeed))), 0.5f * RandomFloat01(gSeed))));
                        }
                        else
                        {
                            list.Add(new Sphere(center, 0.2f, new Dielectric(1.5f)));
                        }
                    }
                }
            }

            list.Add(new Sphere(new Vector3(0, 1, 0), 1f, new Dielectric(1.5f)));
            list.Add(new Sphere(new Vector3(-4, 1, 0), 1f, new Lambertian(new Vector3(0.4f, 0.2f, 0.1f))));
            list.Add(new Sphere(new Vector3(4, 1, 0), 1f, new Metal(new Vector3(0.7f, 0.6f, 0.5f), 0.0f)));
        }
コード例 #2
0
        public static void Main()
        {
            int nx = 640;
            int ny = 480;
            int ns = 32;

            Vector3             lookfrom      = new Vector3(13, 5, 3);
            Vector3             lookat        = new Vector3(0, 0, 0);
            float               dist_to_focus = 10;
            float               aperture      = 0.1f;
            Vector3             up            = new Vector3(0, 1, 0);
            MotionBlurRayCamera camera        = new MotionBlurRayCamera(lookfrom, lookat, up, 20,
                                                                        (float)(nx) / (float)(ny), aperture, dist_to_focus);

            HitList list = new HitList();

            RandomScene(ref list);
            Texture2D tex = ImageHelper.CreateImg(nx, ny);

            for (int j = ny - 1; j >= 0; --j)
            {
                for (int i = 0; i < nx; ++i)
                {
                    Vector3 color = Vector3.zero;
                    for (int k = 0; k < ns; ++k)
                    {
                        float u = (float)(i + RandomFloat01(gSeed)) / (float)(nx);
                        float v = (float)(j + RandomFloat01(gSeed)) / (float)(ny);

                        Ray r = camera.GetRay(u, v);
                        color += RayCast(r, list, gSeed, 0);
                    }
                    color   = color / (float)(ns);
                    color.x = Mathf.Sqrt(color.x);
                    color.y = Mathf.Sqrt(color.y);
                    color.z = Mathf.Sqrt(color.z);
                    ImageHelper.SetPixel(tex, i, j, color);
                }
            }

            ImageHelper.SaveImg(tex, "Img/chapter12_single.png");
            Debug.Log("Chapter 12 done");
        }
コード例 #3
0
        public static void Test()
        {
            var b = ThreadPool.SetMaxThreads(System.Environment.ProcessorCount, System.Environment.ProcessorCount);
            //Debug.Log(b);

            int nx = 1280;
            int ny = 720;
            int ns = 128;

            myThreadCount = nx * ny;

            Vector3             lookfrom      = new Vector3(13, 3, 3);
            Vector3             lookat        = new Vector3(0, 0, 0);
            float               dist_to_focus = 10;
            float               aperture      = 0.1f;
            Vector3             up            = new Vector3(0, 1, 0);
            MotionBlurRayCamera camera        = new MotionBlurRayCamera(lookfrom, lookat, up, 20,
                                                                        (float)(nx) / (float)(ny), aperture, dist_to_focus);

            HitList list = new HitList();

            RandomScene(ref list);
            Texture2D tex = ImageHelper.CreateImg(nx, ny);

            Vector3[,] imgBin = new Vector3[nx, ny];

            ManualResetEvent resetEvent = new ManualResetEvent(false);

            for (int j = ny - 1; j >= 0; --j)
            {
                for (int i = 0; i < nx; ++i)
                {
                    var param = new Param();
                    param.world      = list;
                    param.camera     = camera;
                    param.pos_x      = i;
                    param.pos_y      = j;
                    param.img_width  = nx;
                    param.img_height = ny;
                    param.times      = ns;
                    param.img        = imgBin;
                    param.mre        = resetEvent;
                    param.seed       = new System.Random();

                    while (!ThreadPool.QueueUserWorkItem(ThreadRayMain, param))
                    {
                        Debug.Log("wait ");
                        Thread.Sleep(500);
                    }
                }
            }
            resetEvent.WaitOne();

            for (int j = ny - 1; j >= 0; --j)
            {
                for (int i = 0; i < nx; ++i)
                {
                    ImageHelper.SetPixel(tex, i, j, imgBin[i, j]);
                }
            }
            ImageHelper.SaveImg(tex, "Img/chapter12.png");
            Debug.Log("Chapter 12 done");
        }