Ejemplo n.º 1
0
        public static void AddAndAssignMaterial(
            Scene model,
            Assimp.Material mat,
            HashSet <string> uniqueIdsOfElementsWithMat,
            out bool utilized
            )
        {
            utilized = false;

            // If no elements in the model use this material,
            // don't bother.
            if (uniqueIdsOfElementsWithMat.Count == 0)
            {
                return;
            }

            int matIndex = model.MaterialCount;

            foreach (var mesh in model.Meshes)
            {
                if (uniqueIdsOfElementsWithMat.Contains(mesh.Name))
                {
                    mesh.MaterialIndex = matIndex;
                    utilized           = true;
                }
            }
            if (utilized)
            {
                //Logger.LogInfo($"Adding material {mat.Name} to gltf.");
                model.Materials.Add(mat);
            }
            else
            {
                //Logger.LogInfo($"Won't add material {mat.Name} to gltf " +
                //    $"because no objects utilize it.");
            }
        }
Ejemplo n.º 2
0
        public static Assimp.Material ConvertToAssimpMaterial(TextureBundle bundle, Document doc)
        {
            // Create new material with base props
            // from the Revit material
            var newmat = new Assimp.Material()
            {
                Opacity      = bundle.Material.GetOpacity(),
                Reflectivity = 0f,
                Name         = bundle.Material.Name,
                ColorDiffuse = bundle.Material.ToColor4D()
            };

            // Extract base properties from revit material
            ElementId appearanceAssetId            = bundle.Material.AppearanceAssetId;
            AppearanceAssetElement appearanceAsset = doc.GetElement(appearanceAssetId) as AppearanceAssetElement;
            Asset renderingAsset = appearanceAsset.GetRenderingAsset();
            RenderAppearanceDescriptor rad
                = new RenderAppearanceDescriptor(renderingAsset);
            PropertyDescriptorCollection collection          = rad.GetProperties();
            List <PropertyDescriptor>    orderableCollection = new List <PropertyDescriptor>(collection.Count);

            List <string> allPropNames = orderableCollection.Select(f => f.Name).ToList();

            foreach (PropertyDescriptor descr in collection)
            {
                orderableCollection.Add(descr);
                switch (descr.Name)
                {
                    #region Notes

                    // The commented out properties aren't in use yet,
                    // but do work with revit materials as expected.

                    //case "texture_UScale":
                    //    var uScale = renderingAsset["texture_UScale"] as AssetPropertyDouble;
                    //    break;
                    //case "texture_VScale":
                    //    break;
                    //case "texture_UOffset":
                    //    break;
                    //case "texture_VOffset":
                    //    break;
                    //case "texture_RealWorldScaleX":
                    //    var xScale = renderingAsset["texture_RealWorldScaleX"] as AssetPropertyDistance;
                    //    break;
                    //case "texture_RealWorldScaleY":
                    //    break;

                    #endregion

                case "generic_diffuse":
                    var prop = renderingAsset.GetAssetProperty <AssetPropertyDoubleArray4d>("generic_diffuse");
                    newmat.ColorDiffuse = ColorFromAssetDoubleArray4d(prop);
                    break;

                case "glazing_reflectance":
                    // This is glass, so we should reduce the transparency.
                    var refl = renderingAsset.GetAssetProperty <AssetPropertyDouble>("glazing_reflectance");
                    if (refl == null)
                    {
                        var reflFloat = renderingAsset.GetAssetProperty <AssetPropertyFloat>("glazing_reflectance");
                        newmat.Reflectivity = reflFloat?.Value ?? 0f;
                    }
                    else
                    {
                        newmat.Reflectivity = (float)refl.Value;
                    }
                    newmat.Opacity = Math.Abs(0f - newmat.Reflectivity);
                    break;

                case "common_Tint_color":
                    // Tint shouldn't be used if generic diffuse is set
                    if (
                        renderingAsset.GetAssetProperty <AssetPropertyDoubleArray4d>("generic_diffuse") != null
                        )
                    {
                        continue;
                    }
                    var tintProp = renderingAsset.GetAssetProperty <AssetPropertyDoubleArray4d>("common_Tint_color");
                    newmat.ColorDiffuse = ColorFromAssetDoubleArray4d(tintProp);
                    break;

                default:
                    break;
                }
            }

            // Set textures
            foreach (var tx in bundle.TexturePaths)
            {
                // Get the filename
                var txFileName = tx.Value.SafeFileName;
                if (tx.Key == RevitTextureType.Color)
                {
                    newmat.TextureDiffuse = new TextureSlot(
                        $"Textures/{txFileName}",
                        TextureType.Diffuse,
                        0,    // Texture index in the material
                        TextureMapping.Box,
                        0,    //
                        0.5f, // Blend mode
                        TextureOperation.Add,
                        TextureWrapMode.Clamp,
                        TextureWrapMode.Clamp,
                        0 // Flags,
                        );
                }
                else if (tx.Key == RevitTextureType.Bump)
                {
                    newmat.TextureHeight = new TextureSlot(
                        $"Textures/{txFileName}",
                        TextureType.Diffuse,
                        0,    // Texture index in the material
                        TextureMapping.Box,
                        0,    //
                        0.5f, // Blend mode
                        TextureOperation.Add,
                        TextureWrapMode.Clamp,
                        TextureWrapMode.Clamp,
                        0 // Flags,
                        );
                }
            }
            return(newmat);
        }