Exemple #1
0
        void InitMaterial(GameObject meshGameObject)
        {
            if (meshGameObject == null)
            {
                return;
            }

            var meshRenderer = meshGameObject.GetComponent <MeshRenderer>();

            if (meshRenderer == null)
            {
                return;
            }

            meshRenderer.shadowCastingMode = model.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Off;
            if (meshRenderer.sharedMaterial != material)
            {
                MaterialTransitionController
                    matTransition = meshGameObject.GetComponent <MaterialTransitionController>();

                if (matTransition != null && matTransition.canSwitchMaterial)
                {
                    matTransition.finalMaterials = new Material[] { material };
                    matTransition.PopulateTargetRendererWithMaterial(matTransition.finalMaterials);
                }

                SRPBatchingHelper.OptimizeMaterial(meshRenderer, material);
                meshRenderer.sharedMaterial = material;
            }
        }
    /// <summary>
    /// This will search all the transform hierachy for all renderers,
    /// and replace all of its materials containing the specified name by the new one.
    /// </summary>
    /// <param name="transformRoot">Transform where to start the traversal</param>
    /// <param name="replaceThemWith">material to replace them</param>
    /// <param name="materialsContainingThisName">name to filter in materials</param>
    public static List <Material> ReplaceMaterialsWithCopiesOf(Transform transformRoot,
                                                               Material replaceThemWith,
                                                               string materialsContainingThisName = null)
    {
        List <Material> result = new List <Material>();

        MapSharedMaterialsRecursively(
            transformRoot,
            (mat) =>
        {
            Material copy = new Material(replaceThemWith);

            Texture _MatCap  = null;
            Texture _GMatCap = null;
            Texture _FMatCap = null;

            if (replaceThemWith.HasProperty(ShaderUtils.MatCap))
            {
                _MatCap = replaceThemWith.GetTexture(ShaderUtils.MatCap);
            }

            if (replaceThemWith.HasProperty(ShaderUtils.GlossMatCap))
            {
                _GMatCap = replaceThemWith.GetTexture(ShaderUtils.GlossMatCap);
            }

            if (replaceThemWith.HasProperty(ShaderUtils.FresnelMatCap))
            {
                _FMatCap = replaceThemWith.GetTexture(ShaderUtils.FresnelMatCap);
            }

            //NOTE(Brian): This method has a bug, if the material being copied lacks a property of the source material,
            //             the source material property will get erased. It can't be added back and even the material inspector crashes.
            //             Check the comment in Lit.shader.
            copy.CopyPropertiesFromMaterial(mat);

            if (_GMatCap != null)
            {
                copy.SetTexture(ShaderUtils.GlossMatCap, _GMatCap);
            }

            if (_FMatCap != null)
            {
                copy.SetTexture(ShaderUtils.FresnelMatCap, _FMatCap);
            }

            if (_MatCap != null)
            {
                copy.SetTexture(ShaderUtils.MatCap, _MatCap);
            }

            SRPBatchingHelper.OptimizeMaterial(copy);

            result.Add(copy);
            return(copy);
        },
            materialsContainingThisName);

        return(result);
    }