Example #1
0
        /// <summary>
        /// Get environment bitmap from texture evaluator
        /// </summary>
        /// <param name="rm"></param>
        /// <param name="teximg"></param>
        /// <param name="gamma"></param>
        /// <param name="floatAsByte"></param>
        /// <param name="planarProjection"></param>
        public static void EnvironmentBitmapFromEvaluator(RenderEnvironment rm, CyclesTextureImage teximg, float gamma, bool floatAsByte, bool planarProjection)
        {
            RenderTexture renderTexture = null;

            if(rm!=null)
                renderTexture = rm.FindChild("texture") as RenderTexture;

            if (renderTexture == null)
            {
                teximg.TexByte = null;
                teximg.TexFloat = null;
                teximg.TexWidth = teximg.TexHeight = 0;
                teximg.Name = "";
                return;
            }

            var rId = renderTexture.RenderHashWithoutLocalMapping;

            var rhinotfm = renderTexture.LocalMappingTransform;

            using (var textureEvaluator = renderTexture.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.DisableLocalMapping))
            {
                if (textureEvaluator == null)
                {
                    teximg.TexByte = null;
                    teximg.TexFloat = null;
                    teximg.TexWidth = teximg.TexHeight = 0;
                    teximg.Name = "";
                    return;
                }
                try
                {
                    int u, v, w;
                    renderTexture.PixelSize(out u, out v, out w);
                    teximg.TexWidth = u;
                    teximg.TexHeight = v;
                }
                catch (Exception)
                {
                    teximg.TexHeight = teximg.TexWidth = 1024;
                }

                if (teximg.TexHeight == 0 || teximg.TexWidth == 0)
                {
                    teximg.TexHeight = teximg.TexWidth = 1024;
                }

                Transform t = new Transform(
                    rhinotfm.ToFloatArray(true)
                    );

                var isFloat = renderTexture.IsHdrCapable();

                var isLinear = teximg.IsLinear = renderTexture.IsLinear();

                if (isFloat && !floatAsByte)
                {
                    var img = RetrieveFloatsImg(rId, teximg.TexWidth, teximg.TexHeight, textureEvaluator, true, planarProjection, isLinear);
                    img.ApplyGamma(gamma);
                    teximg.TexFloat = img.Data;
                    teximg.TexByte = null;
                }
                else
                {
                    var img = RetrieveBytesImg(rId, teximg.TexWidth, teximg.TexHeight, textureEvaluator, true, planarProjection, isLinear);
                    img.ApplyGamma(gamma);
                    teximg.TexByte = img.Data;
                    teximg.TexFloat = null;
                }
                teximg.Name = rId.ToString(CultureInfo.InvariantCulture);

                teximg.Transform = t;
            }
        }
        /// <summary>
        /// Set the RenderEnvironment for usage
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="usage"></param>
        public void SetBackground(RenderEnvironment environment, RenderEnvironment.Usage usage)
        {
            switch (usage)
            {
                case RenderEnvironment.Usage.Background:
                    _cqBackground.Xml = "";
                    var xmlenv = (environment?.TopLevelParent as Materials.ICyclesMaterial);
                    if(xmlenv?.MaterialType == RhinoCyclesCore.CyclesShader.CyclesMaterial.XmlEnvironment)
                    {
                        _cqBackground.Xml = xmlenv.MaterialXml;
                    }
                    _cqBackground.background_environment = environment;
                    if (environment != null)
                    {
                        var s = environment.GetParameter("background-projection") as IConvertible;
                        var proj = Convert.ToString(s, CultureInfo.InvariantCulture);
                        _cqBackground.PlanarProjection = proj.Equals("planar");
                    } else
                    {
                        _cqBackground.PlanarProjection = false;
                    }
                    break;
                case RenderEnvironment.Usage.Skylighting:
                    _cqBackground.skylight_environment = environment;
                    break;
                case RenderEnvironment.Usage.ReflectionAndRefraction:
                    _cqBackground.reflection_environment = environment;
                    break;
            }

            _cqBackground.HandleEnvironments();

            _cqBackground.modified = true;
        }
Example #3
0
        /// <summary>
        /// Handle environment changes
        /// </summary>
        /// <param name="usage"></param>
        protected override void ApplyEnvironmentChanges(RenderEnvironment.Usage usage)
        {
            /* instead of just reading the one environment we have to read everything.
             *
             * The earlier assumption that non-changing EnvironmentIdForUsage meant non-changing
             * environment instance is wrong. See http://mcneel.myjetbrains.com/youtrack/issue/RH-32418
             */
            UpdateAllEnvironments();
            _environmentDatabase.SetGamma(PreProcessGamma);

            //System.Diagnostics.Debug.WriteLine("{0}, env {1}", usage, env);
        }