/** Crop inside image. */ public UVImage cropLocal( Rect rect ) { UVImage result = new UVImage (); float uw = uvRect.width / size.x; float uh = uvRect.height / size.y; // convert rect to UV result.size = new Vector2 ( rect.width, rect.height ); result.uvRect = new Rect (uvRect.x + (rect.x * uw), uvRect.y + (rect.y * uh), rect.width * uw, rect.height * uh); result.material = material; return result; }
public void ReturnsColorByUV() { var ppm = @"P3 10 10 10 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 "; var canvas = PPM.Parse(ppm); var pattern = new UVImage(canvas); pattern.ColorAt(new UVPoint(0f, 0f)).Should().Be(new Color(0.9f, 0.9f, 0.9f)); pattern.ColorAt(new UVPoint(0.3f, 0f)).Should().Be(new Color(0.2f, 0.2f, 0.2f)); pattern.ColorAt(new UVPoint(0.6f, 0.3f)).Should().Be(new Color(0.1f, 0.1f, 0.1f)); pattern.ColorAt(new UVPoint(1f, 1f)).Should().Be(new Color(0.9f, 0.9f, 0.9f)); }
public void TestPatternCheckerPPM() { string ppm = @"P3 10 10 10 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 6 6 6 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 7 7 7 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 8 8 8 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 9 9 9 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8"; var canvas = Canvas.SetPPM(ppm); var pattern = new UVImage(canvas); Assert.AreEqual(pattern.UVPatternAt(0, 0), new Color(0.9, 0.9, 0.9)); Assert.AreEqual(pattern.UVPatternAt(0.3, 0), new Color(0.2, 0.2, 0.2)); Assert.AreEqual(pattern.UVPatternAt(0.6, 0.3), new Color(0.1, 0.1, 0.1)); Assert.AreEqual(pattern.UVPatternAt(1, 1), new Color(0.9, 0.9, 0.9)); }
private static World BuildWorld() { Console.WriteLine("Loading file..."); var filePath = Path.Combine(GetExecutionPath(), "indoor_env.ppm"); //var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm"); Console.WriteLine("Parsing file..."); var textureCanvas = PPM.ParseFile(filePath); var image = new UVImage(textureCanvas); var map = new TextureMap(image, UVMapping.Spherical); var skySphere = new Sphere { Material = { Texture = map, Ambient = 1.5f, CastsShadows = false, Transparency = 0f, Roughness = 1f, SpecularColor = new Color(0.0f, 0.0f, 0.0f) } }; //skySphere.SetTransform(Transforms.RotateY(3.4f).Scale(1000f)); skySphere.SetTransform(Transform.RotateY(3.3f).Scale(10000f)); var s1 = new StripeTexture(Colors.White, Colors.Black); var s2 = new StripeTexture(Colors.White, Colors.Black); s2.SetTransform(Transform.RotateY(MathF.PI / 2)); var pattern = new BlendedCompositeTexture(s1, s2); pattern.SetTransform(Transform.Scale(1f / 20f)); var testPattern = new UVAlignTestPattern(Colors.White, Colors.Red, Colors.Yellow, Colors.Green, Colors.Blue); var testMap = new TextureMap(testPattern, UVMapping.Cylindrical); //var stripe = new StripePattern(new Color(0.9f, 0, 0), new Color(0.0f, 0.0f, 0.9f)); //stripe.SetTransform(Transforms.Scale(0.25f, 0.25f, 0.25f).RotateY(MathF.PI / 4)); //var perlin = new PerlinRippleCompositePattern(stripe, 0.8f); //perlin.SetTransform(Transforms.Scale(0.1f, 0.1f, 0.1f)); var worldFilePath = Path.Combine(GetExecutionPath(), "world.ppm"); var worldCanvas = PPM.ParseFile(worldFilePath); var worldTexture = new UVImage(worldCanvas); var worldPattern = new TextureMap(worldTexture, UVMapping.Spherical); var floor = new Cube { Material = { Texture = pattern, Specular = 0.1f, Reflective = 0.1f, Roughness = 0.22f, Ambient = 0f, SpecularColor = new Color(0.1f, 0.1f, 0.1f) } }; floor.SetTransform(Transform.TranslateY(-1).Scale(20f)); var middle = new Sphere { Material = { Texture = worldPattern, Diffuse = 0.7f, Specular = 1f, Reflective = 0.4f, Shininess = 600, Roughness = 0.2f, Metallic = 0.3f, SpecularColor = new Color(0.1f,0.2f, 0.5f), Ambient = 0f } }; middle.SetTransform(Transform.RotateY(1.5f).Translate(-0.5f, 1f, 0.1f)); var right = new Sphere { Material = { Texture = new TextureMap(new UVCheckers(20, 10, Colors.Black, Colors.White), UVMapping.Spherical), Roughness = 0.5f, Diffuse = 0.7f, Specular = 0.3f, Reflective = 0.2f, Ambient = 0f, SpecularColor = new Color(0.2f, 0.2f, 0.2f) } }; right.SetTransform(Transform.Translate(0.25f, 0.25f, -1f) * Transform.Scale(0.25f)); var rightPlastic = new Sphere { Material = { Texture = new SolidColor(new Color(1f, 0.0f, 0.0f)), SpecularColor = new Color(0.2f, 0.2f, 0.2f), Roughness = 0.25f, Metallic = 0f, Ambient = 0.0f } }; rightPlastic.SetTransform(Transform.Translate(-0.15f, 0.15f, -0.91f) * Transform.Scale(0.15f)); var left = new Sphere { Material = { Texture = new SolidColor(new Color(0.85f, 0.85f, 0.90f)), SpecularColor = new Color(0.3f, 0.3f, 0.3f), Roughness = 0.10f, Metallic = 0.6f, Transparency = 0.95f, RefractiveIndex = 1.52f,Ambient = 0.0f } }; left.SetTransform(Transform.Translate(-1.3f, 0.30f, -0.75f) * Transform.Scale(0.30f)); // left.SetTransform(Transforms.Translate(-2.1f, 0.33f, 0.5f) * Transforms.Scale(0.33f, 0.33f, 0.33f)); var leftChrome = new Sphere { Material = { Texture = new SolidColor(new Color(0.9f, 0.9f, 0.95f)), Diffuse = 0.05f, Specular = 0.9f, SpecularColor = new Color(0.9f, 0.9f, 0.95f), Roughness = 0.55f, Metallic = 1f, Transparency = 0, RefractiveIndex = 1.52f,Reflective = 1.4f, Ambient = 0.0f, Shininess = 300 } }; leftChrome.SetTransform(Transform.Translate(-0.95f, 0.15f, -1.1f) * Transform.Scale(0.15f)); var cube = new Cube { Material = { Texture = new GradientTexture(new Color(1f, 0, 0), new Color(1f, 0.8f, 0f)), Roughness = 0.5f, Ambient = 0f, SpecularColor = new Color(0.2f,0.2f, 0.2f) } }; cube.Material.Texture.SetTransform(Transform.TranslateX(-0.5f).Scale(2f).RotateZ(MathF.PI / 2f)); cube.SetTransform(Transform.RotateY(MathF.PI / 4f).Translate(2.5f, 1f, 3.6f).Scale(1f, 1f, 1f)); var cone = new Cone { IsClosed = true, Minimum = -1f, Maximum = 0f, Material = { Texture = new SolidColor(new Color(0.4f, 0.8f, 0.1f)), Diffuse = 0.7f, Specular = 0.3f, Ambient = 0f, SpecularColor = new Color(0.2f, 0.2f, 0.2f), Roughness = 0.6f, Reflective = 0.2f } }; cone.SetTransform(Transform.Scale(0.6f, 2f, 0.6f).Translate(1.5f, 2.0f, 0)); var cylinder = new Cylinder { Minimum = 0f, Maximum = 3f, IsClosed = true, Material = { Reflective = 0.33f, Specular = 0.9f, Diffuse = 0.1f, Ambient = 0.0f, Shininess = 100, Metallic = 0.76f, SpecularColor = new Color(0.9f, 0.8f, 0.7f), Roughness = 0.02f, Texture = testMap } }; cylinder.SetTransform(Transform.Translate(-3f, 0f, 3.5f)); //var t = new Triangle(new Point(0, 0, 0), new Point(1, 0.5f, 0), new Point(0.5f, 1f, 1f)) //{ // Material = {Texture = new GradientTexture(new Color(0f, 1, 0), new Color(0f, 0f, 1f))} //}; //t.SetTransform(Transforms.Translate(1f, 2f, 1f)); var ringMaterial = new Material { Texture = new SolidColor(new Color(1f, 0.8f, 0f)), Reflective = 0.4f, RefractiveIndex = 0.95f, Roughness = 0.12f, Metallic = 1f, SpecularColor = new Color(0.9f, 0.7f, 0f), //Transparency = 0.95f, Shininess = 300, Specular = 0.9f, Ambient = 0.0f, Diffuse = 0.3f }; var co = new Cylinder { Minimum = -0.01f, Maximum = 0.01f, IsClosed = true }; co.SetTransform(Transform.Scale(1.5f, 1f, 1.5f)); co.SetMaterial(ringMaterial); var ci = new Cylinder { Minimum = -0.1f, Maximum = 0.1f, IsClosed = true }; ci.SetTransform(Transform.Scale(1.2f, 1f, 1.2f)); ci.SetMaterial(ringMaterial); var s = new ConstructiveSolid(ConstructiveOp.Difference, co, ci); s.SetTransform(Transform.RotateZ(-0.2f).RotateX(-0.1f).Translate(-0.5f, 1f, 0.1f)); var gl = new Group(); gl.AddChild(middle); gl.AddChild(left); gl.AddChild(leftChrome); gl.AddChild(cylinder); gl.AddChild(s); gl.AddChild(right); gl.AddChild(rightPlastic); gl.AddChild(cube); gl.AddChild(cone); //gl.AddChild(t); gl.AddChild(floor); gl.AddChild(skySphere); gl.Divide(1); var w = new World(); w.SetLights(new AreaLight(new Point(-80f, 80, -60), new Vector(100f, 0, 0), 6, new Vector(0, 0f, -10f), 3, new Color(1.8f, 1.8f, 1.8f), new Sequence(0.7f, 0.3f, 0.9f, 0.1f, 0.5f))); //w.SetLights(new PointLight(new Point(-3.5f, 4f, -5f), new Color(0.9f, 0.9f, 0.9f))); w.SetObjects(gl); return(w); }
public static void TeapotTest() { Console.WriteLine("Loading file..."); var filePath = Path.Combine(GetExecutionPath(), "indoor_env.ppm"); //var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm"); Console.WriteLine("Parsing file..."); var textureCanvas = PPM.ParseFile(filePath); var image = new UVImage(textureCanvas); var map = new TextureMap(image, UVMapping.Spherical); var skySphere = new Sphere { Material = { Texture = map, Ambient = 2.5f, CastsShadows = false, Transparency = 0f, Roughness = 1f, SpecularColor = new Color(0.0f, 0.0f, 0.0f) } }; //skySphere.SetTransform(Transforms.RotateY(3.4f).Scale(1000f)); skySphere.SetTransform(Transform.RotateY(3.3f).Scale(10000f)); var path = Path.Combine(GetExecutionPath(), "teapot.obj"); Console.WriteLine("Loading file {0}...", path); var data = ObjFile.ParseFile(path); Console.WriteLine("File parsed..."); var triangulated = data.Groups[0]; triangulated.SetTransform(Transform.Scale(0.10f).RotateX(-MathF.PI / 2f).RotateY(MathF.PI / 8f)); var glass = new Material { Texture = new SolidColor(new Color(0.8f, 1f, 0.9f)), Reflective = 0.78f, Roughness = 0.35f, Metallic = 0.65f, SpecularColor = new Color(0.7f, 0.9f, 0.8f), RefractiveIndex = 1.52f, Transparency = 0.80f, Ambient = 0.0f, Diffuse = 0.2f, Shininess = 200f, Specular = 0.9f }; ApplyMaterialToChildren(triangulated, glass); var checkerboard = new Material { Texture = new CheckerTexture(new Color(0.95f, 0.95f, 0.95f), new Color(0.8f, 0.05f, 0.05f)), Metallic = 0.4f, SpecularColor = new Color(0.3f, 0.3f, 0.3f), Reflective = 0.5f, Roughness = 0.1f, Ambient = 0.0f, Diffuse = 0.3f }; checkerboard.Texture.SetTransform(Transform.Scale(0.125f)); var group = new Group(); var floor = new Cube(); floor.SetMaterial(checkerboard); floor.SetTransform(Transform.TranslateY(-1).Scale(5f)); group.AddChild(floor); group.AddChild(triangulated); group.AddChild(skySphere); group.Divide(1); var w = new World(); w.SetLights(new AreaLight(new Point(-80f, 80, -60), new Vector(100f, 0, 0), 6, new Vector(0, 0f, -10f), 3, new Color(1.8f, 1.8f, 1.8f), new Sequence(0.7f, 0.3f, 0.9f, 0.1f, 0.5f))); //w.SetLights(new PointLight(new Point(-30, 30, -30), new Color(1.4f, 1.4f, 1.4f))); w.SetObjects(group); //var width = 600; //var height = 400; //var transform = Transforms.View(new Point(0, 3.0f, -5f), new Point(0, 1, 0), new Vector(0, 1, 0)); //var c = new PinholeCamera(transform, MathF.PI / 3f, width, height); ////var ws = new ComposableWorldShading(3, GGXNormalDistribution.Instance, SchlickBeckmanGeometricShadow.Instance, SchlickFresnelFunction.Instance, w); //var ws = new PhongWorldShading(5, w); //var scene = new Scene(c, ws); //var aaa = new AdaptiveRenderer(3, 0.05f, scene); //var canvas = new Canvas(width, height); var width = 600; var height = 400; var from = new Point(0, 2.5f, -2.5f); var to = new Point(0.25f, 0.5f, 0); var canvas = new Canvas(width, height); var pps = new PerPixelSampler(10); var fov = MathF.PI / 3f; var aspectRatio = (float)width / height; var transform = Transform.LookAt(from, to, new Vector(0, 1, 0)); //var camera = new PinholeCamera(transform, fov, aspectRatio); var camera = new ApertureCamera(fov, aspectRatio, 0.08f, from, to, Vectors.Up); // var cws = new PhongWorldShading(5, w); var cws = new ComposableWorldSampler(2, 16, GGXNormalDistribution.Instance, GGXSmithGeometricShadow.Instance, SchlickFresnelFunction.Instance, w); var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps)); Console.WriteLine("Rendering at {0}x{1}...", width, height); var stopwatch = new Stopwatch(); stopwatch.Start(); //RenderContext.Render(canvas, aaa); ctx.Render(); PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "teapot"); stopwatch.Stop(); Console.WriteLine("Done ({0})", stopwatch.Elapsed); }
SwfSprite createRectTile( GraphicsDrawOP op, Rect localScale9Grid ) { SwfSprite sprite = new SwfSprite (); UVImage img = new UVImage (op); img = img.cropLocal ( localScale9Grid ); img.applyToSprite (sprite); container.addChild (sprite); return sprite; }
public static void SolidTestRender(int spp) { Console.WriteLine("Loading file..."); var filePath = Path.Combine(GetExecutionPath(), "neon_env.ppm"); //var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm"); Console.WriteLine("Parsing file..."); var textureCanvas = PPM.ParseFile(filePath); var image = new UVImage(textureCanvas); var map = new TextureMap(image, UVMapping.Spherical); var skySphere = new Sphere { Material = { Texture = map, Ambient = 1.0f, CastsShadows = false, Transparency = 0f, Roughness = 1f, SpecularColor = new Color(0.0f, 0.0f, 0.0f) } }; skySphere.SetTransform(Transform.RotateY(3.3f).Scale(1000f)); var radius = 0.25f; var red = new Material { Texture = new SolidColor(new Color(1, 0, 0)), Reflective = 0.3f, Roughness = 0.3f, Ambient = 0.0f, Metallic = 0.3f, SpecularColor = new Color(0.3f, 0.3f, 0.3f) }; var blue = new Material { Texture = new SolidColor(new Color(0, 0, 1)), Reflective = 0.3f, Roughness = 0.3f, Ambient = 0.0f, Metallic = 0.3f, SpecularColor = new Color(0.3f, 0.3f, 0.3f) }; var yellow = new Material { Texture = new SolidColor(new Color(1, 1, 0)), Reflective = 0.3f, Roughness = 0.3f, Ambient = 0.0f, Metallic = 0.3f, SpecularColor = new Color(0.3f, 0.3f, 0.3f) }; var white = new Material { Texture = new SolidColor(new Color(1, 1, 1)), Reflective = 0.3f, Roughness = 0.3f, Ambient = 0.0f, Metallic = 0.3f, SpecularColor = new Color(0.3f, 0.3f, 0.3f) }; var blackPip = new Material { Texture = new SolidColor(new Color(0.1f, 0.1f, 0.1f)), Ambient = 0.0f, Roughness = 0.8f, Metallic = 0f, SpecularColor = new Color(0.2f, 0.2f, 0.2f) }; var whitePip = new Material { Texture = new SolidColor(new Color(0.95f, 0.95f, 0.95f)), Ambient = 0.0f, Roughness = 0.8f, Metallic = 0f, SpecularColor = new Color(0.2f, 0.2f, 0.2f) }; var d1 = CutPips(RoundedCube(radius, blue), whitePip); var d2 = CutPips(RoundedCube(radius, red), whitePip); var d3 = CutPips(RoundedCube(radius, white), blackPip); var d4 = CutPips(RoundedCube(radius, yellow), blackPip); d1.SetTransform(Transform.RotateY(-2.2f).TranslateY(1f).Scale(0.5f)); d2.SetTransform(Transform.RotateZ(MathF.PI / 2f).TranslateY(1f).TranslateX(2f).TranslateZ(1f).Scale(0.5f)); d3.SetTransform(Transform.RotateY(0.5f).TranslateY(1f).TranslateX(-4f).TranslateZ(1f).Scale(0.5f)); d4.SetTransform(Transform.RotateY(-0.2f).TranslateY(3f).TranslateX(0.2f).TranslateZ(1.25f).Scale(0.5f)); var lightGray = new Color(0.48f, 0.48f, 0.48f); var darkGray = new Color(0.15f, 0.15f, 0.15f); //var s1 = new StripeTexture(lightGray, darkGray); //var s2 = new StripeTexture(lightGray, darkGray); //s2.SetTransform(Transforms.RotateY(MathF.PI / 2)); //var pattern = new BlendedCompositeTexture(s1, s2); //pattern.SetTransform(Transforms.Scale(1f / 20f)); var pattern = new CheckerTexture(lightGray, darkGray); pattern.SetTransform(Transform.Scale(1f / 20f)); var floor = new Cube { Material = { Texture = pattern, Roughness = 0.5f, Specular = 0.1f, Diffuse = 0.3f, Reflective = 0.15f, SpecularColor = new Color(0.3f, 0.3f, 0.3f), Ambient = 0f } }; floor.SetTransform(Transform.TranslateY(-1f).Scale(20f)); var g = new Group(d1, d2, d3, d4, floor, skySphere); g.Divide(1); var w = new World(); w.SetLights(new AreaLight(new Point(-8, 10, -10), new Vector(12f, 0, 0), 6, new Vector(0, 0f, -10f), 3, new Color(1.1f, 1.1f, 1.1f), new Sequence(0.7f, 0.3f, 0.9f, 0.1f, 0.5f))); //w.SetLights(new PointLight(new Point(-8, 10, -10), new Color(0.9f, 0.9f, 0.9f))); //w.SetLights(new AreaLight(new Point(-3, 6, -4), new Vector(1f, 0, 0), 3, new Vector(0, 1f, 0), 3, // new Color(1.4f, 1.4f, 1.4f), new Sequence(0.7f, 0.3f, 0.9f, 0.1f, 0.5f))); //w.SetLights(new AreaLight(new Point(-10, 10, -10), new Vector(1,0,0), 4, new Vector(0,1,0), 3, Colors.White)); w.SetObjects(g); //var width = 600; //var height = 400; //var transform = Transforms.View(new Point(0, 1.5f, -5f), new Point(0, 1, 0), new Vector(0, 1, 0)); //var c = new PinholeCamera(transform, MathF.PI / 3f, width, height); //var ws = new ComposableWorldShading(2, GGXNormalDistribution.Instance, SchlickBeckmanGeometricShadow.Instance, SchlickFresnelFunction.Instance, w); ////var ws = new PhongWorldShading(2, w); //var scene = new Scene(c, ws); ////var aaa = new AdaptiveRenderer(2, 0.01f, scene); //var aaa = new SamplesPerPixelRenderer(24, scene); //var canvas = new Canvas(width, height); var width = 600; var height = 400; var from = new Point(0, 1.5f, -5f); var to = new Point(0, 1, -0.5f); var canvas = new Canvas(width, height); var pps = new PerPixelSampler(spp); var fov = MathF.PI / 3f; var aspectRatio = (float)width / height; var camera = new ApertureCamera(fov, aspectRatio, 0.15f, from, to, Vectors.Up); //var transform = Transforms.View(from, to, new Vector(0, 1, 0)); //var camera = new PinholeCamera2(transform, fov, aspectRatio); //var cws = new PhongWorldShading2(4, w); var cws = new ComposableWorldSampler(2, 4, GGXNormalDistribution.Instance, SchlickBeckmanGeometricShadow.Instance, SchlickFresnelFunction.Instance, w); var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps)); Console.WriteLine("Rendering at {0}x{1}...", width, height); var stopwatch = new Stopwatch(); stopwatch.Start(); ctx.Render(); // RenderContext.Render(canvas, aaa); PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "solid"); stopwatch.Stop(); Console.WriteLine("Done ({0})", stopwatch.Elapsed); }
static void Main(string[] args) { Console.WriteLine("Generating a skybox scene!"); var s = System.IO.File.ReadAllText(@"..\..\..\..\..\..\Scenes\negx.ppm"); var left = Canvas.SetPPM(s); s = System.IO.File.ReadAllText(@"..\..\..\..\..\..\Scenes\posx.ppm"); var right = Canvas.SetPPM(s); s = System.IO.File.ReadAllText(@"..\..\..\..\..\..\Scenes\posz.ppm"); var front = Canvas.SetPPM(s); s = System.IO.File.ReadAllText(@"..\..\..\..\..\..\Scenes\negz.ppm"); var back = Canvas.SetPPM(s); s = System.IO.File.ReadAllText(@"..\..\..\..\..\..\Scenes\posy.ppm"); var up = Canvas.SetPPM(s); s = System.IO.File.ReadAllText(@"..\..\..\..\..\..\Scenes\negy.ppm"); var down = Canvas.SetPPM(s); if ((left is null) || (right is null) || (front is null) || (back is null) || (up is null) || (down is null)) { Console.WriteLine("Oh no! Skybox image(s) not loaded!"); return; } var negx = new UVImage(left); var posx = new UVImage(right); var posz = new UVImage(front); var negz = new UVImage(back); var posy = new UVImage(up); var negy = new UVImage(down); var sphere = new Sphere(); sphere.Transform = Matrix.Translation(0, 0, 5) * Matrix.Scaling(0.75, 0.75, 0.75); sphere.Material.Color = Color.DarkGrey; sphere.Material.Diffuse = 0.4; sphere.Material.Specular = 0.9; // 0.6; sphere.Material.Shininess = 96; // 20.0 sphere.Material.Reflective = 1; sphere.Material.Ambient = 0; var cube = new Cube(); cube.Transform = Matrix.Scaling(1000, 1000, 1000); cube.Material.Pattern = new CubeMapPattern(negx, posz, posx, negz, posy, negy); sphere.Material.Diffuse = 0; sphere.Material.Specular = 0; sphere.Material.Ambient = 1; var world = new World(); world.Lights.Add(new PointLight(new Point(0, 100, 0), Color.White)); world.Objects.Add(cube); world.Objects.Add(sphere); var camera = new Camera(400, 200, 1.2); camera.Transform = Matrix.ViewTransform(Point.Zero, new Point(0, 0, 5), Vector.VectorY); var c = camera.Render(world); System.IO.File.WriteAllText("skybox.ppm", c.GetPPM()); Console.WriteLine("Done!"); }
public static void SkyBoxMappingTestRender() { Console.WriteLine("Loading sky box file..."); var skyBoxFile = Path.Combine(GetExecutionPath(), "skyboxsun.ppm"); Console.WriteLine("Parsing sky box file..."); var skyBoxCanvas = PPM.ParseFile(skyBoxFile); var skyBoxTexture = new UVImage(skyBoxCanvas); var skyBoxMap = new SkyBoxMap(skyBoxTexture); Cube Cube(float rotY, float rotX, float tx, float ty) { var cube1 = new Cube { Material = { Texture = skyBoxMap } }; cube1.SetTransform(Transform.RotateY(rotY).RotateX(rotX).Translate(tx, ty, 0)); return(cube1); } var g = new Group(); g.AddChild(Cube(0.7854f, 0.7854f, -6, 2)); g.AddChild(Cube(2.3562f, 0.7854f, -2, 2)); g.AddChild(Cube(3.927f, 0.7854f, 2, 2)); g.AddChild(Cube(5.4978f, 0.7854f, 6, 2)); g.AddChild(Cube(0.7854f, -0.7854f, -6, -2)); g.AddChild(Cube(2.3562f, -0.7854f, -2, -2)); g.AddChild(Cube(3.927f, -0.7854f, 2, -2)); g.AddChild(Cube(5.4978f, -0.7854f, 6, -2)); g.Divide(1); var w = new World(); w.SetLights(new PointLight(new Point(0, 2, -100), Colors.White)); w.SetObjects(g); var width = 800; var height = 400; var from = new Point(0, 0, -20f); var to = new Point(0, 0, 0); var canvas = new Canvas(width, height); var pps = new PerPixelSampler(16); var fov = 0.8f; var aspectRatio = (float)width / height; var transform = Transform.LookAt(from, to, new Vector(0, 1, 0)); var camera = new PinholeCamera(transform, fov, aspectRatio); var cws = new PhongWorldShading(1, w); var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps)); Console.WriteLine("Rendering at {0}x{1}...", width, height); var stopwatch = new Stopwatch(); stopwatch.Start(); ctx.Render(); PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "mapping_sky_box"); stopwatch.Stop(); Console.WriteLine("Done ({0})", stopwatch.Elapsed); }
public static void RowTestByDelegate(int spp, string fileSuffix, Func <int, Material> materialFunc) { Console.WriteLine("Loading file..."); var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm"); Console.WriteLine("Parsing file..."); var textureCanvas = PPM.ParseFile(filePath); var image = new UVImage(textureCanvas); var map = new TextureMap(image, UVMapping.Spherical); var skySphere = new Sphere { Material = { Texture = map, Ambient = 1.5f, CastsShadows = false, Transparency = 1f } }; skySphere.SetTransform(Transform.RotateY(3.4f).Scale(1000f)); var g = new Group(); var dx = 2.75f; var dz = 3.5f; var y = 1f; var nX = 10; var nZ = 1; int n = 0; for (var z = 0; z < nZ; z++) { for (var x = 0; x < nX; x++) { var s = new Sphere(); s.SetTransform(Transform.TranslateY(1f).Scale(1.2f).Translate(x * dx, 0, z * dz)); s.SetMaterial(materialFunc(x + 1)); g.AddChild(s); n++; } } var lightGray = new Color(0.9f, 0.9f, 0.9f); var darkGray = new Color(0.1f, 0.9f, 0.1f); var s1 = new StripeTexture(lightGray, darkGray); var s2 = new StripeTexture(lightGray, darkGray); s2.SetTransform(Transform.RotateY(MathF.PI / 2)); var pattern = new BlendedCompositeTexture(s1, s2); pattern.SetTransform(Transform.Scale(1f / 30f)); var text = new CheckerTexture(new Color(0.3f, 0.7f, 0.3f), new Color(0.13f, 0.13f, 0.13f)); text.SetTransform(Transform.Scale(1f / 16f)); var floor = new Cube { Material = { Texture = text, SpecularColor = new Color(0.3f, 0.3f, 0.3f), Metallic = 0f, Roughness = 0.45f, Ambient = 0.15f } }; floor.SetTransform(Transform.TranslateY(-1f).Scale(40f)); var min = g.LocalBounds().Min; var max = g.LocalBounds().Max; var dir = max - min; var mid = min + (dir * 0.5f); var g2 = new Group(g, floor, skySphere); g2.Divide(1); var w = new World(); w.SetLights(new PointLight(new Point(mid.X, 500, -500), new Color(1.7f, 1.7f, 1.7f))); w.SetObjects(g2); var width = 1200; var height = 140; var from = new Point(mid.X, 6f, -32f); var to = mid; var canvas = new Canvas(width, height); var pps = new PerPixelSampler(spp); var fov = MathF.PI / 4f; var aspectRatio = (float)width / height; var camera = new ApertureCamera(fov, aspectRatio, 0.2F, from, to, Vectors.Up); var cws = new ComposableWorldSampler(2, 16, GGXNormalDistribution.Instance, GGXSmithGeometricShadow.Instance, SchlickFresnelFunction.Instance, w); var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps)); Console.WriteLine("Rendering at {0}x{1}...", width, height); var stopwatch = new Stopwatch(); stopwatch.Start(); ctx.Render(); PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "col_row_" + fileSuffix); stopwatch.Stop(); Console.WriteLine("Done ({0})", stopwatch.Elapsed); }
public static void ColRowTestRender() { Console.WriteLine("Loading file..."); //var filePath = Path.Combine(GetExecutionPath(), "indoor_env.ppm"); var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm"); Console.WriteLine("Parsing file..."); var textureCanvas = PPM.ParseFile(filePath); var image = new UVImage(textureCanvas); var map = new TextureMap(image, UVMapping.Spherical); var skySphere = new Sphere() { Material = { Texture = map, Ambient = 1.5f, CastsShadows = false, Transparency = 1f } }; skySphere.SetTransform(Transform.RotateY(3.4f).Scale(1000f)); var g = new Group(); var dx = 2.75f; var dz = 3.5f; var y = 1f; var nX = 10; var nZ = 1; var delta = 1f / (nX * nZ - 1); int n = 0; bool metallic = false; for (var z = 0; z < nZ; z++) { for (var x = 0; x < nX; x++) { var s = new Sphere(); s.SetTransform(Transform.TranslateY(1f).Scale(1.2f).Translate(x * dx, 0, z * dz)); // var color = x % 2 == 0 ? new Color(1f, 1f, 1f) : new Color(1f, 0.3f, 0.3f); var color = new Color(1f, 0.3f, 0.3f); s.Material.Texture = SolidColor.Create(color); s.Material.SpecularColor = metallic ? color : new Color(0.2f, 0.2f, 0.2f); s.Material.Roughness = MathFunction.Saturate(n * delta + 0.01f); s.Material.Metallic = metallic ? 1f : 0f; s.Material.Ambient = 0f; s.Material.Reflective = 0.9f; g.AddChild(s); n++; } } var lightGray = new Color(0.9f, 0.9f, 0.9f); var darkGray = new Color(0.1f, 0.9f, 0.1f); var s1 = new StripeTexture(lightGray, darkGray); var s2 = new StripeTexture(lightGray, darkGray); s2.SetTransform(Transform.RotateY(MathF.PI / 2)); var pattern = new BlendedCompositeTexture(s1, s2); pattern.SetTransform(Transform.Scale(1f / 30f)); var text = new CheckerTexture(new Color(0.3f, 0.7f, 0.3f), new Color(0.13f, 0.13f, 0.13f)); text.SetTransform(Transform.Scale(1f / 16f)); var floor = new Cube { Material = { Texture = text, SpecularColor = new Color(0.3f, 0.3f, 0.3f), Metallic = 0f, Roughness = 0.45f, Ambient = 0.15f } }; floor.SetTransform(Transform.TranslateY(-1f).Scale(40f)); var min = g.LocalBounds().Min; var max = g.LocalBounds().Max; var dir = max - min; var mid = min + (dir * 0.5f); var g2 = new Group(g, floor, skySphere); //var g2 = new Group(g); g2.Divide(1); var w = new World(); w.SetLights(new PointLight(new Point(mid.X, 500, -500), new Color(1.7f, 1.7f, 1.7f))); w.SetObjects(g2); //var width = 1200; //var height = 140; //var transform = Transforms.View(new Point(mid.X, 6f, -32f), mid, new Vector(0, 1, 0)); //var c = new PinholeCamera(transform, MathF.PI / 4f, width, height); ////var c = new ApertureCamera(MathF.PI / 4f, width, height, 0.04f, new Point(mid.X, 6f, -32f), mid); //var ws = new ComposableWorldShading(3, GGXNormalDistribution.Instance, GGXSmithGeometricShadow.Instance, SchlickFresnelFunction.Instance, w); ////var ws = new PhongWorldShading(3, w); //var scene = new Scene(c, ws); //var aaa = new AdaptiveRenderer(4, 0.00001f, scene); //var canvas = new Canvas(width, height); var width = 1200; var height = 140; var from = new Point(mid.X, 6f, -32f); var to = mid; var canvas = new Canvas(width, height); var pps = new PerPixelSampler(400); var fov = MathF.PI / 4f; var aspectRatio = (float)width / height; var camera = new ApertureCamera(fov, aspectRatio, 0.2F, from, to, Vectors.Up); var cws = new ComposableWorldSampler(2, 16, GGXNormalDistribution.Instance, GGXSmithGeometricShadow.Instance, SchlickFresnelFunction.Instance, w); var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps)); Console.WriteLine("Rendering at {0}x{1}...", width, height); var stopwatch = new Stopwatch(); stopwatch.Start(); ctx.Render(); //RenderContext.Render(canvas, aaa); PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "col_row"); stopwatch.Stop(); Console.WriteLine("Done ({0})", stopwatch.Elapsed); }
public static void InsideSphere() { Console.WriteLine("Loading file..."); var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm"); Console.WriteLine("Parsing file..."); var textureCanvas = PPM.ParseFile(filePath); var image = new UVImage(textureCanvas); var map = new TextureMap(image, UVMapping.Spherical); var skySphere = new Sphere() { Material = { Texture = map, Ambient = 1.2f, CastsShadows = false, Transparency = 1f } }; skySphere.SetTransform(Transform.RotateY(2.1f).Scale(1000f)); var s = new Sphere { Material = { Roughness = 0.1f, Texture = SolidColor.Create(new Color(1f, 0.0f, 0.0f)), SpecularColor = new Color(0.2f, 0.2f, 0.2f), Metallic = 0f, Ambient = 0f } }; var g = new Group(); g.AddChild(skySphere); g.AddChild(s); g.Divide(1); var w = new World(); w.SetLights(new PointLight(new Point(100, 100, -100), new Color(1f, 1f, 1f))); w.SetObjects(g); var width = 400; var height = 400; var from = new Point(0, -0.8f, -4f); var to = new Point(0, 0, 0); var canvas = new Canvas(width, height); var pps = new PerPixelSampler(1000); var fov = 0.8f; var aspectRatio = (float)width / height; var camera = new ApertureCamera(fov, aspectRatio, 0.05F, from, to, Vectors.Up); var cws = new ComposableWorldSampler(1, 2, GGXNormalDistribution.Instance, SchlickBeckmanGeometricShadow.Instance, SchlickFresnelFunction.Instance, w); var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps)); Console.WriteLine("Rendering at {0}x{1}...", width, height); var stopwatch = new Stopwatch(); stopwatch.Start(); ctx.Render(); PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "sky_sphere"); stopwatch.Stop(); Console.WriteLine("Done ({0})", stopwatch.Elapsed); }