Ejemplo n.º 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 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 rhinotfm  = renderTexture.LocalMappingTransform;
            var guid      = renderTexture.TypeId;
            var nm        = renderTexture.TypeName;
            var rot       = renderTexture.GetRotation();
            var rep       = renderTexture.GetRepeat();
            var tra       = renderTexture.GetOffset();
            var rId       = renderTexture.RenderHashExclude(TextureRenderHashFlags.ExcludeLocalMapping, "azimuth;altitude;multiplier;rdk-texture-repeat;rdk-texture-offset;rdk-texture-rotation;rdk-texture-adjust-multiplier");
            var azimob    = renderTexture.GetParameter("azimuth");
            var altob     = renderTexture.GetParameter("altitude");
            var multob    = renderTexture.GetParameter("multiplier");
            var multadjob = renderTexture.GetParameter("rdk-texture-adjust-multiplier");
            var mult      = 1.0f;
            var multadj   = 1.0f;

            if (multob != null)
            {
                mult = (float)Convert.ToDouble(multob);
            }
            if (multadjob != null)
            {
                multadj = (float)Convert.ToDouble(multadjob);
            }
            if (azimob != null && altob != null)
            {
                var azi  = Convert.ToDouble(azimob);
                var alti = Convert.ToDouble(altob);
                rhinotfm.M20 = alti;
                rhinotfm.M21 = alti;
                rhinotfm.M22 = azi;
            }
            else
            {
                rhinotfm.M00 = tra.X;
                rhinotfm.M01 = tra.Y;
                rhinotfm.M02 = tra.Z;

                rhinotfm.M10 = rep.X;
                rhinotfm.M11 = rep.Y;
                rhinotfm.M12 = rep.Z;

                rhinotfm.M20 = Rhino.RhinoMath.ToRadians(rot.X);
                rhinotfm.M21 = Rhino.RhinoMath.ToRadians(rot.Y);
                rhinotfm.M22 = Rhino.RhinoMath.ToRadians(rot.Z);
            }

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

            var restore = !multadj.FuzzyEquals(1.0f);

            if (restore)
            {
                renderTexture.BeginChange(RenderContent.ChangeContexts.Ignore);
                renderTexture.SetParameter("rdk-texture-adjust-multiplier", 1.0);
            }

            using (var textureEvaluator =
                       renderTexture.CreateEvaluator(
                           RenderTexture.TextureEvaluatorFlags.DisableLocalMapping |
                           RenderTexture.TextureEvaluatorFlags.DisableProjectionChange |
                           RenderTexture.TextureEvaluatorFlags.DisableFiltering
                           ))
            {
                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;
                }

                var isFloat = renderTexture.IsHdrCapable();

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

                if (isFloat)
                {
                    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);
            }
            if (restore)
            {
                renderTexture.SetParameter("rdk-texture-adjust-multiplier", (double)multadj);
                renderTexture.EndChange();
            }
            teximg.Transform = t;
            teximg.Strength  = mult * multadj;
        }