Example #1
0
        /// <summary>
        /// Static constructor for wrapping default surface shader created by Cycles shader manager.
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        static public Shader WrapDefaultSurfaceShader(Client client)
        {
            var shader = new Shader(client, ShaderType.Material, CSycles.DEFAULT_SURFACE_SHADER)
            {
                Name = "default_surface"
            };

            // just add nodes so we have local node presentation, but no need to actually finalise
            // since it already exists in Cycles.
            var diffuse_bsdf = new DiffuseBsdfNode();

            diffuse_bsdf.ins.Color.Value = new float4(0.8f);

            shader.AddNode(diffuse_bsdf);

            diffuse_bsdf.outs.BSDF.Connect(shader.Output.ins.Surface);

            return(shader);
        }
Example #2
0
        /// <summary>
        /// Static constructor for wrapping default light shader created by Cycles shader manager.
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        static public Shader WrapDefaultLightShader(Client client)
        {
            var shader = new Shader(client, ShaderType.Material, CSycles.DEFAULT_LIGHT_SHADER)
            {
                Name = "default_light"
            };

            // just add nodes so we have local node presentation, but no need to actually finalise
            // since it already exists in Cycles.
            var emission_node = new EmissionNode();

            emission_node.ins.Color.Value    = new float4(0.8f);
            emission_node.ins.Strength.Value = 0.0f;

            shader.AddNode(emission_node);

            emission_node.outs.Emission.Connect(shader.Output.ins.Surface);

            return(shader);
        }
Example #3
0
        public static Shader create_some_setup_shader()
        {
            var some_setup = new Shader(Client, Shader.ShaderType.Material)
            {
                Name = "some_setup ",
                UseMis = false,
                UseTransparentShadow = true,
                HeterogeneousVolume = false
            };

            var brick_texture = new BrickTexture();
            brick_texture.ins.Vector.Value = new float4(0.000f, 0.000f, 0.000f);
            brick_texture.ins.Color1.Value = new float4(0.800f, 0.800f, 0.800f);
            brick_texture.ins.Color2.Value = new float4(0.200f, 0.200f, 0.200f);
            brick_texture.ins.Mortar.Value = new float4(0.000f, 0.000f, 0.000f);
            brick_texture.ins.Scale.Value = 1.000f;
            brick_texture.ins.MortarSize.Value = 0.020f;
            brick_texture.ins.Bias.Value = 0.000f;
            brick_texture.ins.BrickWidth.Value = 0.500f;
            brick_texture.ins.RowHeight.Value = 0.250f;

            var checker_texture = new CheckerTexture();
            checker_texture.ins.Vector.Value = new float4(0.000f, 0.000f, 0.000f);
            checker_texture.ins.Color1.Value = new float4(0.000f, 0.004f, 0.800f);
            checker_texture.ins.Color2.Value = new float4(0.200f, 0.000f, 0.007f);
            checker_texture.ins.Scale.Value = 5.000f;

            var diffuse_bsdf = new DiffuseBsdfNode();
            diffuse_bsdf.ins.Color.Value = new float4(0.800f, 0.800f, 0.800f);
            diffuse_bsdf.ins.Roughness.Value = 0.000f;
            diffuse_bsdf.ins.Normal.Value = new float4(0.000f, 0.000f, 0.000f);

            var texture_coordinate = new TextureCoordinateNode();

            some_setup.AddNode(brick_texture);
            some_setup.AddNode(checker_texture);
            some_setup.AddNode(diffuse_bsdf);
            some_setup.AddNode(texture_coordinate);

            brick_texture.outs.Color.Connect(diffuse_bsdf.ins.Color);
            checker_texture.outs.Color.Connect(brick_texture.ins.Mortar);
            texture_coordinate.outs.Normal.Connect(checker_texture.ins.Vector);
            texture_coordinate.outs.UV.Connect(brick_texture.ins.Vector);

            diffuse_bsdf.outs.BSDF.Connect(some_setup.Output.ins.Surface);

            some_setup.FinalizeGraph();

            return some_setup;
        }
Example #4
0
        static void Main(string[] args)
        {
            var file = "";
            if (args.Length < 1 || args.Length > 2)
            {
                Console.WriteLine("Wrong count parameter: csycles_tester [--quiet] file.xml");
                return;
            }

            var s = args[args.Length-1];
            if (!File.Exists(s))
            {
                Console.WriteLine("File {0} doesn't exist.", s);
                return;
            }

            var silent = args.Length == 2 && "--quiet".Equals(args[0]);

            file = Path.GetFullPath(s);
            Console.WriteLine("We get file path: {0}", file);

            var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ?? "";
            var userpath = Path.Combine(path, "userpath");

            CSycles.path_init(path, userpath);
            CSycles.initialise();

            const uint samples = 50;
            g_update_callback = StatusUpdateCallback;
            g_update_render_tile_callback = UpdateRenderTileCallback;
            g_write_render_tile_callback = WriteRenderTileCallback;
            g_logger_callback = LoggerCallback;

            var client = new Client();
            Client = client;
            if (!silent)
            {
                CSycles.set_logger(client.Id, g_logger_callback);
            }

            foreach (var adev in Device.Devices)
            {
                Console.WriteLine("{0}", adev);
            }

            Console.WriteLine("All device capabilities: {0}", Device.Capabilities);

            var dev = Device.FirstCuda;
            Console.WriteLine("Using device {0} {1}", dev.Name, dev.Description);

            var scene_params = new SceneParameters(client, ShadingSystem.SVM, BvhType.Static, false, false, false, false);
            var scene = new Scene(client, scene_params, dev);

            Console.WriteLine("Default surface shader {0}", scene.DefaultSurface.Name);

            #region background shader
            var background_shader = new Shader(client, Shader.ShaderType.World)
            {
                Name = "Background shader"
            };

            var bgnode = new BackgroundNode();
            bgnode.ins.Color.Value = new float4(0.7f);
            bgnode.ins.Strength.Value = 1.0f;

            background_shader.AddNode(bgnode);
            bgnode.outs.Background.Connect(background_shader.Output.ins.Surface);
            background_shader.FinalizeGraph();

            scene.AddShader(background_shader);

            scene.Background.Shader = background_shader;
            scene.Background.AoDistance = 0.0f;
            scene.Background.AoFactor = 0.0f;
            scene.Background.Visibility = PathRay.AllVisibility;
            #endregion
            #region diffuse shader

            var diffuse_shader = create_some_setup_shader();
            scene.AddShader(diffuse_shader);
            scene.DefaultSurface = diffuse_shader;
            #endregion

            #region point light shader

            var light_shader = new Shader(client, Shader.ShaderType.Material)
            {
                Name = "Tester light shader"
            };

            var emission_node = new EmissionNode();
            emission_node.ins.Color.Value = new float4(0.8f);
            emission_node.ins.Strength.Value = 10.0f;

            light_shader.AddNode(emission_node);
            emission_node.outs.Emission.Connect(light_shader.Output.ins.Surface);
            light_shader.FinalizeGraph();
            scene.AddShader(light_shader);
            #endregion

            var xml = new XmlReader(client, file);
            xml.Parse(silent);
            var width = (uint)scene.Camera.Size.Width;
            var height = (uint)scene.Camera.Size.Height;

            var session_params = new SessionParameters(client, dev)
            {
                Experimental = false,
                Samples = (int) samples,
                TileSize = new Size(64, 64),
                StartResolution = 64,
                Threads = 0,
                ShadingSystem = ShadingSystem.SVM,
                Background = true,
                ProgressiveRefine = false
            };
            Session = new Session(client, session_params, scene);
            Session.Reset(width, height, samples);

            /*if (!silent)
            {
                Session.UpdateCallback = g_update_callback;
                Session.UpdateTileCallback = g_update_render_tile_callback;
                Session.WriteTileCallback = g_write_render_tile_callback;
            }*/

            Session.Start();
            Session.Wait();

            uint bufsize;
            uint bufstride;
            CSycles.session_get_buffer_info(client.Id, Session.Id, out bufsize, out bufstride);
            var pixels = CSycles.session_copy_buffer(client.Id, Session.Id, bufsize);

            var bmp = new Bitmap((int)width, (int)height);
            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    var i = y * (int)width * 4 + x * 4;
                    var r = ColorClamp((int)(pixels[i] * 255.0f));
                    var g = ColorClamp((int)(pixels[i + 1] * 255.0f));
                    var b = ColorClamp((int)(pixels[i + 2] * 255.0f));
                    var a = ColorClamp((int)(pixels[i + 3] * 255.0f));
                    bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
                }
            }
            bmp.Save("test.png", ImageFormat.Png);

            Console.WriteLine("Cleaning up :)");

            CSycles.shutdown();

            Console.WriteLine("Done");
        }
Example #5
0
        public void ReadNodeGraph(ref Shader shader, XmlReader xmlNode)
        {
            var nodes = new Dictionary <string, ShaderNode> {
                { "output", shader.Output }
            };

            while (xmlNode.Read())
            {
                ShaderNode shader_node = null;
                if (!xmlNode.IsStartElement())
                {
                    continue;
                }
                var nodename = xmlNode.GetAttribute("name");
                if (string.IsNullOrEmpty(nodename) && xmlNode.Name != "connect")
                {
                    continue;
                }

                if (string.IsNullOrEmpty(nodename))
                {
                    nodename = "";
                }

                switch (xmlNode.Name)
                {
                case "connect":
                    var fromstring = xmlNode.GetAttribute("from");
                    var tostring   = xmlNode.GetAttribute("to");
                    if (fromstring != null && tostring != null)
                    {
                        var from = fromstring.Split(' ');
                        var to   = tostring.Split(' ');

                        if (!nodes.ContainsKey(from[0]))
                        {
                            throw new KeyNotFoundException(string.Format("'from' node [{0}] not defined prior to connection.", from[0]));
                        }
                        var fromnode   = nodes[from[0]];
                        var fromsocket = fromnode.outputs.Socket(from[1]);

                        if (!nodes.ContainsKey(to[0]))
                        {
                            throw new KeyNotFoundException(string.Format("'to' node [{0}] not defined prior to connection.", to[0]));
                        }
                        var tonode   = nodes[to[0]];
                        var tosocket = tonode.inputs.Socket(to[1]);

                        fromsocket.Connect(tosocket);
                    }
                    break;

                default:
                    shader_node = CSycles.CreateShaderNode(xmlNode.Name, nodename);
                    break;
                }
                if (shader_node != null)
                {
                    shader_node.ParseXml(xmlNode);
                    nodes.Add(nodename, shader_node);
                    shader.AddNode(shader_node);
                }
            }

            shader.FinalizeGraph();
        }
Example #6
0
        public void ReadNodeGraph(ref Shader shader, XmlReader xmlNode)
        {
            var nodes = new Dictionary<string, ShaderNode> {{"output", shader.Output}};

            while (xmlNode.Read())
            {
                ShaderNode shader_node = null;
                if (!xmlNode.IsStartElement()) continue;
                var nodename = xmlNode.GetAttribute("name");
                if (string.IsNullOrEmpty(nodename) && xmlNode.Name != "connect") continue;

                if (string.IsNullOrEmpty(nodename)) nodename = "";

                switch (xmlNode.Name)
                {
                    case "connect":
                        var fromstring = xmlNode.GetAttribute("from");
                        var tostring = xmlNode.GetAttribute("to");
                        if (fromstring != null && tostring != null)
                        {
                            var from = fromstring.Split(' ');
                            var to = tostring.Split(' ');

                            if (!nodes.ContainsKey(from[0]))
                                throw new KeyNotFoundException(string.Format("'from' node [{0}] not defined prior to connection.", from[0]));
                            var fromnode = nodes[from[0]];
                            var fromsocket = fromnode.outputs.Socket(from[1]);

                            if (!nodes.ContainsKey(to[0]))
                                throw new KeyNotFoundException(string.Format("'to' node [{0}] not defined prior to connection.", to[0]));
                            var tonode = nodes[to[0]];
                            var tosocket = tonode.inputs.Socket(to[1]);

                            fromsocket.Connect(tosocket);
                        }
                        break;
                    default:
                        shader_node = CSycles.CreateShaderNode(xmlNode.Name, nodename);
                        break;
                }
                if (shader_node != null)
                {
                    shader_node.ParseXml(xmlNode);
                    nodes.Add(nodename, shader_node);
                    shader.AddNode(shader_node);
                }
            }

            shader.FinalizeGraph();
        }
Example #7
0
        /// <summary>
        /// Static constructor for wrapping default surface shader created by Cycles shader manager.
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static Shader WrapDefaultSurfaceShader(Client client)
        {
            var shader = new Shader(client, ShaderType.Material, CSycles.DEFAULT_SURFACE_SHADER) {Name = "default_surface"};

            // just add nodes so we have local node presentation, but no need to actually finalise
            // since it already exists in Cycles.
            var diffuse_bsdf = new DiffuseBsdfNode();
            diffuse_bsdf.ins.Color.Value = new float4(0.8f);

            shader.AddNode(diffuse_bsdf);

            diffuse_bsdf.outs.BSDF.Connect(shader.Output.ins.Surface);

            return shader;
        }
Example #8
0
        /// <summary>
        /// Static constructor for wrapping default light shader created by Cycles shader manager.
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static Shader WrapDefaultLightShader(Client client)
        {
            var shader = new Shader(client, ShaderType.Material, CSycles.DEFAULT_LIGHT_SHADER) {Name = "default_light"};

            // just add nodes so we have local node presentation, but no need to actually finalise
            // since it already exists in Cycles.
            var emission_node = new EmissionNode();
            emission_node.ins.Color.Value = new float4(0.8f);
            emission_node.ins.Strength.Value = 0.0f;

            shader.AddNode(emission_node);

            emission_node.outs.Emission.Connect(shader.Output.ins.Surface);

            return shader;
        }
Example #9
0
        /// <summary>
        /// Set image texture node and link up with correct TextureCoordinateNode output based on
        /// texture ProjectionMode.
        /// 
        /// This may add new nodes to the shader!
        /// </summary>
        /// <param name="shader"></param>
        /// <param name="texture"></param>
        /// <param name="image_node"></param>
        /// <param name="texture_coordinates"></param>
        public static void SetProjectionMode(Shader shader, CyclesTextureImage texture, ImageTextureNode image_node,
			TextureCoordinateNode texture_coordinates)
        {
            if (!texture.HasTextureImage) return;

            Guid g = Guid.NewGuid();

            texture_coordinates.UseTransform = false;

            var tfm = new MatrixMathNode("texture transform" + g.ToString())
            {
                Transform = texture.Transform
            };
            shader.AddNode(tfm);

            image_node.Projection = TextureNode.TextureProjection.Flat;

            if (texture.ProjectionMode == TextureProjectionMode.WcsBox)
            {
                texture_coordinates.UseTransform = true;
                texture_coordinates.outs.WcsBox.Connect(tfm.ins.Vector);
                tfm.outs.Vector.Connect(image_node.ins.Vector);
            }
            else if (texture.ProjectionMode == TextureProjectionMode.Wcs)
            {
                texture_coordinates.UseTransform = true;
                texture_coordinates.outs.Object.Connect(tfm.ins.Vector);
                tfm.outs.Vector.Connect(image_node.ins.Vector);
            }
            else if (texture.ProjectionMode == TextureProjectionMode.Screen)
            {
                SeparateXyzNode sepvec = new SeparateXyzNode();
                CombineXyzNode combvec = new CombineXyzNode();
                MathNode inverty = new MathNode {Operation = MathNode.Operations.Subtract};
                inverty.ins.Value1.Value = 1.0f;
                shader.AddNode(sepvec);
                shader.AddNode(combvec);
                shader.AddNode(inverty);

                texture_coordinates.outs.Window.Connect(sepvec.ins.Vector);

                sepvec.outs.Y.Connect(inverty.ins.Value2);

                sepvec.outs.X.Connect(combvec.ins.X);
                inverty.outs.Value.Connect(combvec.ins.Y);
                sepvec.outs.Z.Connect(combvec.ins.Z);

                combvec.outs.Vector.Connect(tfm.ins.Vector);

                tfm.Transform = tfm.Transform;
                tfm.outs.Vector.Connect(image_node.ins.Vector);
            }
            else if (texture.ProjectionMode == TextureProjectionMode.View)
            {
                texture_coordinates.outs.Camera.Connect(tfm.ins.Vector);
                tfm.outs.Vector.Connect(image_node.ins.Vector);
            }
            else if (texture.ProjectionMode == TextureProjectionMode.EnvironmentMap)
            {
                texture_coordinates.UseTransform = false;
                switch (texture.EnvProjectionMode)
                {
                    case TextureEnvironmentMappingMode.Spherical:
                        texture_coordinates.outs.EnvSpherical.Connect(image_node.ins.Vector);
                        break;
                    case TextureEnvironmentMappingMode.EnvironmentMap:
                        texture_coordinates.outs.EnvEmap.Connect(image_node.ins.Vector);
                        break;
                    case TextureEnvironmentMappingMode.Box:
                        texture_coordinates.outs.EnvBox.Connect(image_node.ins.Vector);
                        break;
                    case TextureEnvironmentMappingMode.LightProbe:
                        texture_coordinates.outs.EnvLightProbe.Connect(image_node.ins.Vector);
                        break;
                    case TextureEnvironmentMappingMode.Cube:
                        texture_coordinates.outs.EnvCubemap.Connect(image_node.ins.Vector);
                        break;
                    case TextureEnvironmentMappingMode.VerticalCrossCube:
                        texture_coordinates.outs.EnvCubemapVerticalCross.Connect(image_node.ins.Vector);
                        break;
                    case TextureEnvironmentMappingMode.HorizontalCrossCube:
                        texture_coordinates.outs.EnvCubemapHorizontalCross.Connect(image_node.ins.Vector);
                        break;
                    case TextureEnvironmentMappingMode.Hemispherical:
                        texture_coordinates.outs.EnvHemispherical.Connect(image_node.ins.Vector);
                        break;
                    default:
                        texture_coordinates.outs.EnvLightProbe.Connect(image_node.ins.Vector);
                        break;
                }
            }
            else
            {
                texture_coordinates.outs.UV.Connect(tfm.ins.Vector);
                tfm.outs.Vector.Connect(image_node.ins.Vector);
            }
        }
Example #10
0
        /// <summary>
        /// create a ccl.Scene
        /// </summary>
        /// <param name="client">Client to create scene for</param>
        /// <param name="render_device">Render device to use</param>
        /// <param name="cycles_engine">Engine instance to create for</param>
        /// <returns></returns>
        protected static Scene CreateScene(Client client, Device render_device,
			RenderEngine cycles_engine)
        {
            #region set up scene parameters
            var scene_params = new SceneParameters(client, ShadingSystem.SVM, BvhType.Dynamic, false, false, false);
            #endregion

            #region create scene
            var scene = new Scene(client, scene_params, render_device)
            {
                #region integrator settings
                Integrator =
                {
                    MaxBounce = cycles_engine.Settings.MaxBounce,
                    MinBounce = cycles_engine.Settings.MinBounce,
                    TransparentMinBounce = cycles_engine.Settings.TransparentMinBounce,
                    TransparentMaxBounce = cycles_engine.Settings.TransparentMaxBounce,
                    MaxDiffuseBounce = cycles_engine.Settings.MaxDiffuseBounce,
                    MaxGlossyBounce = cycles_engine.Settings.MaxGlossyBounce,
                    MaxTransmissionBounce = cycles_engine.Settings.MaxTransmissionBounce,
                    MaxVolumeBounce = cycles_engine.Settings.MaxVolumeBounce,
                    NoCaustics = cycles_engine.Settings.NoCaustics,
                    TransparentShadows = cycles_engine.Settings.TransparentShadows,
                    DiffuseSamples = cycles_engine.Settings.DiffuseSamples,
                    GlossySamples = cycles_engine.Settings.GlossySamples,
                    TransmissionSamples = cycles_engine.Settings.TransmissionSamples,
                    AoSamples = cycles_engine.Settings.AoSamples,
                    MeshLightSamples = cycles_engine.Settings.MeshLightSamples,
                    SubsurfaceSamples = cycles_engine.Settings.SubsurfaceSamples,
                    VolumeSamples = cycles_engine.Settings.VolumeSamples,
                    AaSamples = cycles_engine.Settings.AaSamples,
                    FilterGlossy = cycles_engine.Settings.FilterGlossy,
                    IntegratorMethod = cycles_engine.Settings.IntegratorMethod,
                    SampleAllLightsDirect = cycles_engine.Settings.SampleAllLights,
                    SampleAllLightsIndirect = cycles_engine.Settings.SampleAllLightsIndirect,
                    SampleClampDirect = cycles_engine.Settings.SampleClampDirect,
                    SampleClampIndirect = cycles_engine.Settings.SampleClampIndirect,
                    SamplingPattern = SamplingPattern.CMJ,
                    Seed = cycles_engine.Settings.Seed
                }
                #endregion
            };

            scene.Film.SetFilter(FilterType.Gaussian, 1.5f);
            scene.Film.Exposure = 1.0f;
            scene.Film.Update();

            #endregion

            #region background shader

            // we add here a simple background shader. This will be repopulated with
            // other nodes whenever background changes are detected.
            var background_shader = new Shader(client, Shader.ShaderType.World)
            {
                Name = "Rhino Background"
            };

            var bgnode = new BackgroundNode("orig bg");
            bgnode.ins.Color.Value = new float4(0.7f);
            bgnode.ins.Strength.Value = 1.0f;

            background_shader.AddNode(bgnode);
            bgnode.outs.Background.Connect(background_shader.Output.ins.Surface);
            background_shader.FinalizeGraph();

            scene.AddShader(background_shader);

            scene.Background.Shader = background_shader;
            scene.Background.AoDistance = 0.0f;
            scene.Background.AoFactor = 0.0f;
            scene.Background.Visibility = PathRay.AllVisibility;

            #endregion

            return scene;
        }