public void Add(Rhino.Geometry.Sphere sphere, RenderMaterial material)
 {
   UnsafeNativeMethods.Rdk_CustomMeshes_AddSphere(NonConstPointer(), sphere.Center, 
                                                  sphere.EquitorialPlane.XAxis, 
                                                  sphere.EquitorialPlane.YAxis, 
                                                  sphere.Radius, 
                                                  material.ConstPointer());
 }
 public void Add(Rhino.Geometry.Box box, RenderMaterial material)
 {
   UnsafeNativeMethods.Rdk_CustomMeshes_AddBox(NonConstPointer(), 
                                               box.Plane.Origin, 
                                               box.Plane.XAxis, 
                                               box.Plane.YAxis, 
                                               box.X.Min, box.X.Max, 
                                               box.Y.Min, box.Y.Max, 
                                               box.Z.Min, box.Z.Max, 
                                               material.ConstPointer());
 }
 public void Add(Rhino.Geometry.Cone cone, Rhino.Geometry.Plane truncation, RenderMaterial material)
 {
   UnsafeNativeMethods.Rdk_CustomMeshes_AddCone(NonConstPointer(), cone.BasePoint, 
                                                cone.Plane.XAxis, 
                                                cone.Plane.YAxis, 
                                                cone.Height,
                                                cone.Radius,
                                                truncation.Origin, 
                                                truncation.XAxis, 
                                                truncation.YAxis, 
                                                material.ConstPointer());
 }
Пример #4
0
        private RenderMaterial GetObjectMaterial(Rhino.DocObjects.RhinoObject rhinoObject)
        {
            ObjectMaterialSource source = rhinoObject.Attributes.MaterialSource;

            Rhino.Render.RenderMaterial renderMaterial = null;

            if (source == ObjectMaterialSource.MaterialFromObject)
            {
                renderMaterial = rhinoObject.RenderMaterial;
            }
            else if (source == ObjectMaterialSource.MaterialFromLayer)
            {
                int layerIndex = rhinoObject.Attributes.LayerIndex;

                renderMaterial = GetLayerMaterial(layerIndex);
            }

            return(renderMaterial);
        }
 public void Add(Rhino.Geometry.PlaneSurface plane, RenderMaterial material)
 {
   UnsafeNativeMethods.Rdk_CustomMeshes_AddPlane(NonConstPointer(), plane.ConstPointer(), material.ConstPointer());
 }
 /// <summary>
 /// Add primitive box and material.
 /// </summary>
 /// <param name="box">Box to add.</param>
 /// <param name="material">
 /// Material to add, may be null if not needed.
 /// </param>
 public void Add(Box box, RenderMaterial material)
 {
   var material_pointer = (null == material ? IntPtr.Zero : material.ConstPointer());
   var pointer = NonConstPointer();
   UnsafeNativeMethods.Rdk_CustomMeshes_AddBox(pointer, 
                                               box.Plane.Origin, 
                                               box.Plane.XAxis, 
                                               box.Plane.YAxis, 
                                               box.X.Min, box.X.Max, 
                                               box.Y.Min, box.Y.Max, 
                                               box.Z.Min, box.Z.Max,
                                               material_pointer);
 }
 public void Add(Rhino.Geometry.Mesh mesh, RenderMaterial material)
 {
   UnsafeNativeMethods.Rdk_CustomMeshes_AddMesh(NonConstPointer(), mesh.ConstPointer(), material.ConstPointer());
 }
 /// <summary>
 /// Add primitive cone and material.
 /// </summary>
 /// <param name="cone">Cone to add.</param>
 /// <param name="truncation">
 /// The plane used to cut the cone (the non-apex end is kept). Should be
 /// equal to cone.plane if not truncated.
 /// </param>
 /// <param name="material">
 /// Material to add, may be null if not needed.
 /// </param>
 public void Add(Cone cone, Plane truncation, RenderMaterial material)
 {
   var material_pointer = (null == material ? IntPtr.Zero : material.ConstPointer());
   var pointer = NonConstPointer();
   UnsafeNativeMethods.Rdk_CustomMeshes_AddCone(pointer,
                                                cone.BasePoint, 
                                                cone.Plane.XAxis, 
                                                cone.Plane.YAxis, 
                                                cone.Height,
                                                cone.Radius,
                                                truncation.Origin, 
                                                truncation.XAxis, 
                                                truncation.YAxis,
                                                material_pointer);
 }
 /// <summary>
 /// Add primitive plane and material.
 /// </summary>
 /// <param name="plane">Plane to add.</param>
 /// <param name="material">
 /// Material to add, may be null if not needed.
 /// </param>
 public void Add(PlaneSurface plane, RenderMaterial material)
 {
   var material_pointer = (null == material ? IntPtr.Zero : material.ConstPointer());
   var pointer = NonConstPointer();
   UnsafeNativeMethods.Rdk_CustomMeshes_AddPlane(pointer, plane.ConstPointer(), material_pointer);
 }
 /// <summary>
 /// Add mesh and material.
 /// </summary>
 /// <param name="mesh">Mesh to add.</param>
 /// <param name="material">
 /// Material to add, may be null if not needed.
 /// </param>
 public void Add(Mesh mesh, RenderMaterial material)
 {
   var material_pointer = (null == material ? IntPtr.Zero : material.ConstPointer());
   var pointer = NonConstPointer();
   UnsafeNativeMethods.Rdk_CustomMeshes_AddMesh(pointer, mesh.ConstPointer(), material_pointer);
 }
 /// <summary>
 /// Add primitive sphere and material.
 /// </summary>
 /// <param name="sphere">Sphere to add.</param>
 /// <param name="material">
 /// Material to add, may be null if not needed.
 /// </param>
 public void Add(Sphere sphere, RenderMaterial material)
 {
   var material_pointer = (null == material ? IntPtr.Zero : material.ConstPointer());
   var pointer = NonConstPointer();
   UnsafeNativeMethods.Rdk_CustomMeshes_AddSphere(pointer,
                                                  sphere.Center,
                                                  sphere.EquitorialPlane.XAxis,
                                                  sphere.EquitorialPlane.YAxis,
                                                  sphere.Radius,
                                                  material_pointer);
 }
Пример #12
0
        private static void InternalMaterialBitmapFromEvaluator(CyclesShader shader, RenderTexture renderTexture,
			RenderMaterial.StandardChildSlots textureType, Rhino.Geometry.Transform rhinotfm, uint rId, TextureEvaluator actualEvaluator,
			TextureProjectionMode projectionMode, TextureEnvironmentMappingMode envProjectionMode)
        {
            int pheight;
            int pwidth;
            try
            {
                int u, v, w;
                renderTexture.PixelSize(out u, out v, out w);
                pheight = u;
                pwidth = v;
            }
            catch (Exception)
            {
                pheight = 1024;
                pwidth = 1024;
            }

            if (pheight == 0 || pwidth == 0)
            {
                pheight = 1024;
                pwidth = 1024;
            }

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

            var isFloat = renderTexture.IsHdrCapable();
            var isLinear = renderTexture.IsLinear();

            if (isFloat)
            {
                var img = RetrieveFloatsImg(rId, pwidth, pheight, actualEvaluator, false, false, isLinear);
                img.ApplyGamma(shader.Gamma);
                switch (textureType)
                {
                    case RenderMaterial.StandardChildSlots.Diffuse:
                        shader.DiffuseTexture.IsLinear = isLinear;
                        shader.DiffuseTexture.TexFloat = img.Data;
                        shader.DiffuseTexture.TexByte = null;
                        break;
                    case RenderMaterial.StandardChildSlots.Bump:
                        shader.BumpTexture.IsLinear = isLinear;
                        shader.BumpTexture.TexFloat = img.Data;
                        shader.BumpTexture.TexByte = null;
                        break;
                    case RenderMaterial.StandardChildSlots.Transparency:
                        shader.TransparencyTexture.IsLinear = isLinear;
                        shader.TransparencyTexture.TexFloat = img.Data;
                        shader.TransparencyTexture.TexByte = null;
                        break;
                    case RenderMaterial.StandardChildSlots.Environment:
                        shader.EnvironmentTexture.IsLinear = isLinear;
                        shader.EnvironmentTexture.TexFloat = img.Data;
                        shader.EnvironmentTexture.TexByte = null;
                        break;
                }
            }
            else
            {
                var img = RetrieveBytesImg(rId, pwidth, pheight, actualEvaluator, false, false, isLinear);
                img.ApplyGamma(shader.Gamma);
                switch (textureType)
                {
                    case RenderMaterial.StandardChildSlots.Diffuse:
                        shader.DiffuseTexture.IsLinear = isLinear;
                        shader.DiffuseTexture.TexFloat = null;
                        shader.DiffuseTexture.TexByte = img.Data;
                        break;
                    case RenderMaterial.StandardChildSlots.Bump:
                        shader.BumpTexture.IsLinear = isLinear;
                        shader.BumpTexture.TexFloat = null;
                        shader.BumpTexture.TexByte = img.Data;
                        break;
                    case RenderMaterial.StandardChildSlots.Transparency:
                        shader.TransparencyTexture.IsLinear = isLinear;
                        shader.TransparencyTexture.TexFloat = null;
                        shader.TransparencyTexture.TexByte = img.Data;
                        break;
                    case RenderMaterial.StandardChildSlots.Environment:
                        shader.EnvironmentTexture.IsLinear = isLinear;
                        shader.EnvironmentTexture.TexFloat = null;
                        shader.EnvironmentTexture.TexByte = img.Data;
                        break;
                }
            }
            switch (textureType)
            {
                case RenderMaterial.StandardChildSlots.Diffuse:
                    shader.DiffuseTexture.TexWidth = pwidth;
                    shader.DiffuseTexture.TexHeight = pheight;
                    shader.DiffuseTexture.ProjectionMode = projectionMode;
                    shader.DiffuseTexture.EnvProjectionMode = envProjectionMode;
                    shader.DiffuseTexture.Transform = t;
                    shader.DiffuseTexture.Name = rId.ToString(CultureInfo.InvariantCulture);
                    break;
                case RenderMaterial.StandardChildSlots.Bump:
                    shader.BumpTexture.TexWidth = pwidth;
                    shader.BumpTexture.TexHeight = pheight;
                    shader.BumpTexture.ProjectionMode = projectionMode;
                    shader.BumpTexture.EnvProjectionMode = envProjectionMode;
                    shader.BumpTexture.Transform = t;
                    shader.BumpTexture.Name = rId.ToString(CultureInfo.InvariantCulture);
                    break;
                case RenderMaterial.StandardChildSlots.Transparency:
                    shader.TransparencyTexture.TexWidth = pwidth;
                    shader.TransparencyTexture.TexHeight = pheight;
                    shader.TransparencyTexture.ProjectionMode = projectionMode;
                    shader.TransparencyTexture.EnvProjectionMode = envProjectionMode;
                    shader.TransparencyTexture.Transform = t;
                    shader.TransparencyTexture.Name = rId.ToString(CultureInfo.InvariantCulture);
                    break;
                case RenderMaterial.StandardChildSlots.Environment:
                    shader.EnvironmentTexture.TexWidth = pwidth;
                    shader.EnvironmentTexture.TexHeight = pheight;
                    // special texture, always set to Environment/Emap
                    shader.EnvironmentTexture.ProjectionMode = TextureProjectionMode.EnvironmentMap;
                    shader.EnvironmentTexture.EnvProjectionMode = TextureEnvironmentMappingMode.EnvironmentMap;
                    shader.EnvironmentTexture.Transform = t;
                    shader.EnvironmentTexture.Name = rId.ToString(CultureInfo.InvariantCulture);
                    break;
            }
        }
Пример #13
0
        /// <summary>
        /// Get material bitmap from texture evaluator
        /// </summary>
        /// <param name="shader"></param>
        /// <param name="rm"></param>
        /// <param name="renderTexture"></param>
        /// <param name="textureType"></param>
        internal static void MaterialBitmapFromEvaluator(ref CyclesShader shader, RenderMaterial rm, RenderTexture renderTexture, RenderMaterial.StandardChildSlots textureType)
        {
            if (renderTexture == null) return;

            var rId = renderTexture.RenderHashWithoutLocalMapping;

            var rhinotfm = renderTexture.LocalMappingTransform;

            var projectionMode = renderTexture.GetProjectionMode();
            var envProjectionMode = renderTexture.GetInternalEnvironmentMappingMode();

            using (var textureEvaluator = renderTexture.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.DisableLocalMapping))
            {
                SimulatedTexture st = textureEvaluator == null ? renderTexture.SimulatedTexture(RenderTexture.TextureGeneration.Disallow) : null;
                using (
                    var actualEvaluator = textureEvaluator ?? RenderTexture.NewBitmapTexture(st).CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal))
                {
                    InternalMaterialBitmapFromEvaluator(shader, renderTexture, textureType, rhinotfm, rId, actualEvaluator, projectionMode, envProjectionMode);

                }
            }
        }
Пример #14
0
        /// <summary>
        /// Determine material type using smell, but also querying for
        /// specific parameters on the RenderMaterial
        /// </summary>
        /// <param name="rm"></param>
        /// <returns>ProbableMaterial</returns>
        private ProbableMaterial GuessMaterialFromSmell(RenderMaterial rm)
        {
            if(rm.SmellsLikePlaster) return ProbableMaterial.Plaster;

            if(rm.SmellsLikeGem && rm.GetParameter("type")!=null) return ProbableMaterial.Gem;
            if(rm.SmellsLikeGlass && rm.GetParameter("ior")!=null) return ProbableMaterial.Glass;
            if(rm.SmellsLikePlastic) return ProbableMaterial.Plastic;
            if(rm.SmellsLikePaint) return ProbableMaterial.Paint;

            if(rm.SmellsLikeMetal) return ProbableMaterial.Metal;

            return ProbableMaterial.Custom;
        }
Пример #15
0
        private static ProbableMaterial WhatMaterial(RenderMaterial rm, Rhino.DocObjects.Material m)
        {
            if (rm.TypeId.Equals(RenderMaterial.PictureMaterialGuid))
            {
                return ProbableMaterial.Picture;

            }
            if (rm.TypeId.Equals(RenderMaterial.PlasterMaterialGuid))
            {
                return ProbableMaterial.Plaster;

            }
            if (rm.TypeId.Equals(RenderMaterial.GlassMaterialGuid))
            {
                return ProbableMaterial.Glass;

            }
            if (rm.TypeId.Equals(RenderMaterial.GemMaterialGuid))
            {
                return ProbableMaterial.Gem;

            }
            if (rm.TypeId.Equals(RenderMaterial.PaintMaterialGuid))
            {
                return ProbableMaterial.Paint;

            }
            if (rm.TypeId.Equals(RenderMaterial.PlasticMaterialGuid))
            {
                return ProbableMaterial.Plastic;

            }
            if (rm.TypeId.Equals(RenderMaterial.MetalMaterialGuid))
            {
                return ProbableMaterial.Metal;
            }

            if (rm.SmellsLikePlaster || rm.SmellsLikeTexturedPlaster)
            {
                return ProbableMaterial.Plaster;

            }
            if (rm.SmellsLikeGlass || rm.SmellsLikeTexturedGlass)
            {
                return ProbableMaterial.Glass;

            }
            if (rm.SmellsLikeGem || rm.SmellsLikeTexturedGem)
            {
                return ProbableMaterial.Gem;

            }
            if (rm.SmellsLikePaint || rm.SmellsLikeTexturedPaint)
            {
                return ProbableMaterial.Paint;

            }
            if (rm.SmellsLikePlastic || rm.SmellsLikeTexturedPlastic)
            {
                return ProbableMaterial.Plastic;

            }
            if (rm.SmellsLikeMetal || rm.SmellsLikeTexturedMetal)
            {
                return ProbableMaterial.Metal;
            }

            return ProbableMaterial.Custom;
        }
Пример #16
0
        /// <summary>
        /// Create a CyclesShader based on given Material m
        /// </summary>
        /// <param name="rm">Material to convert to CyclesShader</param>
        /// <param name="gamma">gamma to use for this shader</param>
        /// <returns>The CyclesShader</returns>
        internal CyclesShader CreateCyclesShader(RenderMaterial rm, float gamma)
        {
            var mid = rm.RenderHash;
            CyclesShader shader = null;

            var crm = rm as ICyclesMaterial;
            CyclesShader.CyclesMaterial mattype = CyclesShader.CyclesMaterial.No;

            if (crm == null)
            {
                // always simulate material, need to know now myself
                // what to read out from the simulated material to
                // populate my own material descriptions.
                var m = rm.SimulateMaterial(true);
                // figure out what type of material we are.
                //var probemat = GuessMaterialFromSmell(rm);
                var probemat = WhatMaterial(rm, m);

                rm.BeginChange(RenderContent.ChangeContexts.Ignore);
                var dcl = m.DiffuseColor;
                var scl = m.SpecularColor;
                var rcl = m.ReflectionColor;
                var rfcl = m.TransparentColor;
                var emcl = m.EmissionColor;
                var polish = (float)m.ReflectionGlossiness;
                var reflectivity = (float)m.Reflectivity;
                var metalic = 0f;
                var shine = (float)(m.Shine / Material.MaxShine);

                switch (probemat)
                {
                    case ProbableMaterial.Plaster:
                        mattype = CyclesShader.CyclesMaterial.Diffuse;
                        break;
                    case ProbableMaterial.Glass:
                    case ProbableMaterial.Gem:
                        metalic = 0f;
                        mattype = CyclesShader.CyclesMaterial.Glass;
                        break;
                    case ProbableMaterial.Metal:
                        metalic = 1.0f;
                        mattype = CyclesShader.CyclesMaterial.SimpleMetal;
                        break;
                    case ProbableMaterial.Plastic:
                        polish = reflectivity;
                        shine = polish;
                        reflectivity = 0f;
                        metalic = 0f;
                        mattype = CyclesShader.CyclesMaterial.SimplePlastic;
                        break;
                    case ProbableMaterial.Paint:
                        mattype = CyclesShader.CyclesMaterial.Paint;
                        break;
                    case ProbableMaterial.Custom:
                        metalic = m.FresnelReflections ? 0.0f : reflectivity;
                        break;
                }

                var difftexAlpha = m.AlphaTransparency;

                var col = RenderEngine.CreateFloat4(dcl.R, dcl.G, dcl.B, 255);
                var spec = RenderEngine.CreateFloat4(scl.R, scl.G, scl.B, 255);
                var refl = RenderEngine.CreateFloat4(rcl.R, rcl.G, rcl.B, 255);
                var transp = RenderEngine.CreateFloat4(rfcl.R, rfcl.G, rfcl.B, 255);
                var refr = RenderEngine.CreateFloat4(rfcl.R, rfcl.G, rfcl.B, 255);
                var emis = RenderEngine.CreateFloat4(emcl.R, emcl.G, emcl.B, 255);

                shader = new CyclesShader
                {
                    Id = mid,
                    Type = CyclesShader.Shader.Diffuse,
                    CyclesMaterialType = mattype,

                    Shadeless = m.DisableLighting,

                    DiffuseColor = col,
                    SpecularColor = spec,
                    ReflectionColor = refl,
                    ReflectionRoughness = polish,
                    RefractionColor = refr,
                    RefractionRoughness = (float)m.RefractionGlossiness,
                    TransparencyColor = transp,
                    EmissionColor = emis,

                    FresnelIOR = (float)m.FresnelIndexOfRefraction,
                    IOR = (float)m.IndexOfRefraction,
                    Roughness = (float)m.ReflectionGlossiness,
                    Reflectivity = reflectivity,
                    Metalic = metalic,
                    Transparency = (float)m.Transparency,
                    Shine = shine,
                    Gloss = metalic > 0.0f ? polish : 0.0f,

                    FresnelReflections = m.FresnelReflections,

                    Gamma = gamma,

                    Name = m.Name ?? ""
                };

                shader.DiffuseTexture.Amount = 0.0f;
                shader.BumpTexture.Amount = 0.0f;
                shader.TransparencyTexture.Amount = 0.0f;
                shader.EnvironmentTexture.Amount = 0.0f;

                if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Diffuse))
                {
                    var difftex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Diffuse);

                    BitmapConverter.MaterialBitmapFromEvaluator(ref shader, rm, difftex, RenderMaterial.StandardChildSlots.Diffuse);
                    if (shader.HasDiffuseTexture)
                    {
                        shader.DiffuseTexture.UseAlpha = difftexAlpha;
                        shader.DiffuseTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Diffuse) / 100.0f, 1.0f);
                    }
                }

                if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Bump))
                {
                    var bumptex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Bump);
                    BitmapConverter.MaterialBitmapFromEvaluator(ref shader, rm, bumptex, RenderMaterial.StandardChildSlots.Bump);
                    if (shader.HasBumpTexture)
                    {
                        shader.BumpTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Bump) / 100.0f, 1.0f);
                    }
                }

                if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Transparency))
                {
                    var transtex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Transparency);
                    BitmapConverter.MaterialBitmapFromEvaluator(ref shader, rm, transtex,
                        RenderMaterial.StandardChildSlots.Transparency);
                    if (shader.HasTransparencyTexture)
                    {
                        shader.TransparencyTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Transparency) / 100.0f, 1.0f);
                    }
                }

                if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Environment))
                {
                    var envtex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Environment);
                    BitmapConverter.MaterialBitmapFromEvaluator(ref shader, rm, envtex,
                        RenderMaterial.StandardChildSlots.Environment);
                    if (shader.HasEnvironmentTexture)
                    {
                        shader.EnvironmentTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Environment) / 100.0f, 1.0f);
                    }
                }

                rm.EndChange();

            }
            if (crm != null)
            {
                shader = new CyclesShader
                {
                    Id = mid,
                    CyclesMaterialType = crm.MaterialType,
                    Gamma = gamma,
                    Crm = crm
                };
            }

            shader.Gamma = gamma;

            return shader;
        }