Material from color, without texture

Material from color, without texture

E3DE
Enthusiast Enthusiast
1,279 Views
6 Replies
Message 1 of 7

Material from color, without texture

E3DE
Enthusiast
Enthusiast

Hi,

Is it possible to create a material only with color, without texture?

I use the follow code to create a marerial from texture, how modify it to use a simple color?

Thank you

 

 

// Create the texture map image
ImageFileTexture tex = new ImageFileTexture();
tex.SourceFileName = sTextureMapPath;
                    
// We need a cylindrical mapping with no tiling
Mapper mapper = new Mapper(PlatformDb.GraphicsInterface.Projection.Box,
                                            Tiling.Clamp,
                                            Tiling.Clamp,
                                            AutoTransform.TransformObject,
                                            Matrix3d.Identity);
MaterialMap map = new MaterialMap(Source.File,
                                        tex,
                                        1.0,
                                        mapper);
                    
// Set the opacity and refraction maps
MaterialDiffuseComponent mdc = new MaterialDiffuseComponent(new 
  MaterialColor(), map);

MaterialRefractionComponent mrc = new MaterialRefractionComponent(2.0, map);

// Create a new material
Material mat = new Material();
mat.Name = sMaterialName;
mat.Diffuse = mdc;
mat.Refraction = mrc;
mat.Mode = Mode.Realistic;
mat.Reflectivity = 1.0;
mat.IlluminationModel = IlluminationModel.BlinnShader;
                    
mat.Opacity = new MaterialOpacityComponent(opacityPercentage, new 
  MaterialMap());
                    
// Add it to the library
matLib.UpgradeOpen();
materialId = matLib.SetAt(sMaterialName, mat);

 

 

 

0 Likes
Accepted solutions (1)
1,280 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant

Hi,

What about using a GenericTexture instead of an ImageFileTexture.

 

MaterialMap map = new MaterialMap(Source.File,
                                        new GenericTexture(),
                                        1.0,
                                        mapper);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

E3DE
Enthusiast
Enthusiast

Thank you,

where can I set the color, for example red?

For example in a solid, I want to set to some faces a texture (from image file) material and other with a simple color (red, blue, etc).

 

0 Likes
Message 4 of 7

_gile
Consultant
Consultant
var mat = new Material();
var color = new EntityColor(ColorMethod.ByAci, 1); // or: new EntityColor(255, 0, 0);
mat.Ambient = new MaterialColor(Method.Override, 1.0, color);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 7

E3DE
Enthusiast
Enthusiast

Thank you,

I modify my method, the materil is created but without color inside, something is probably missing.

public static ObjectId AddMaterialFromColor(String sMaterialName, EntityColor entityColor, double opacityPercentage = 1)
        {

            ObjectId materialId = ObjectId.Null;
            Document doc = Application.DocumentManager.MdiActiveDocument;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                // Get the material library
                DBDictionary matLib = tr.GetObject(doc.Database.MaterialDictionaryId, OpenMode.ForRead) as DBDictionary;

                // If this material does not exist
                if (matLib.Contains(sMaterialName) == false)
                {
                    // Create a new material
                    Material mat = new Material();
                    mat.Name = sMaterialName;
                    
                    mat.Ambient = new MaterialColor(Method.Override, 1.0, entityColor);

                    mat.Mode = Mode.Realistic;
                    mat.Reflectivity = 1.0;
                    mat.IlluminationModel = IlluminationModel.BlinnShader;

                    mat.Opacity = new MaterialOpacityComponent(opacityPercentage, new MaterialMap());

                    // Add it to the library
                    matLib.UpgradeOpen();
                    materialId = matLib.SetAt(sMaterialName, mat);

                    tr.AddNewlyCreatedDBObject(mat, true);
                    tr.Commit();
                }
                else
                {
                    materialId = matLib.GetAt(sMaterialName);
                }
            }
            return materialId;
        }

.

Thank you

 

0 Likes
Message 6 of 7

_gile
Consultant
Consultant
Accepted solution

It looks like you also need to define the Diffuse and Specular properies with the color.

var matColor = new MaterialColor(Method.Override, 1.0, color);
mat.Ambient = matColor;
mat.Diffuse = new MaterialDiffuseComponent(matColor, map);
mat.Specular = new MaterialSpecularComponent(matColor, map, 1.0);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 7

E3DE
Enthusiast
Enthusiast

Thank you @_gile , now it works fine.

Here the complete method:

public static ObjectId AddMaterialFromColor(String sMaterialName, EntityColor entityColor, double opacityPercentage = 1)
        {

            ObjectId materialId = ObjectId.Null;
            Document doc = Application.DocumentManager.MdiActiveDocument;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                // Get the material library
                DBDictionary matLib = tr.GetObject(doc.Database.MaterialDictionaryId, OpenMode.ForRead) as DBDictionary;

                // If this material does not exist
                if (matLib.Contains(sMaterialName) == false)
                {
                    // Create a new material
                    Material mat = new Material();
                    mat.Name = sMaterialName;

                    Mapper mapper = new Mapper(PlatformDb.GraphicsInterface.Projection.Box,
                                            Tiling.Clamp,
                                            Tiling.Clamp,
                                            AutoTransform.TransformObject,
                                            Matrix3d.Identity);

                    MaterialMap map = new MaterialMap(Source.File,
                                        new GenericTexture(),
                                        1.0,
                                        mapper);

                    var matColor = new MaterialColor(Method.Override, 1.0, entityColor);
                    mat.Ambient = matColor;

                    mat.Diffuse = new MaterialDiffuseComponent(matColor, map);
                    mat.Specular = new MaterialSpecularComponent(matColor, map, 1.0);

                    mat.Mode = Mode.Realistic;
                    mat.Reflectivity = 1.0;
                    mat.IlluminationModel = IlluminationModel.BlinnShader;

                    mat.Opacity = new MaterialOpacityComponent(opacityPercentage, new MaterialMap());

                    // Add it to the library
                    matLib.UpgradeOpen();
                    materialId = matLib.SetAt(sMaterialName, mat);

                    tr.AddNewlyCreatedDBObject(mat, true);
                    tr.Commit();
                }
                else
                {
                    materialId = matLib.GetAt(sMaterialName);
                }
            }
            return materialId;
        }

  

0 Likes