Exemplo n.º 1
0
        private void ApplyMaskToChildren()
        {
            Mask   maskTarget = (Mask)target;
            Shader maskedSpriteWorldCoordsShader = Shader.Find("Alpha Masked/Sprites Alpha Masked - World Coords");
            Shader maskedUnlitWorldCoordsShader  = Shader.Find("Alpha Masked/Unlit Alpha Masked - World Coords");
            Shader spriteDefaultShader           = Shader.Find("Sprites/Default");
            Shader unlitTransparentShader        = Shader.Find("Unlit/Transparent");
            Shader UIDefaultShader     = Shader.Find("UI/Default");
            Shader UIDefaultFontShader = Shader.Find("UI/Default Font");

            if ((maskedSpriteWorldCoordsShader == null) || (maskedUnlitWorldCoordsShader == null))
            {
                Debug.Log("Shaders necessary for masking don't seem to be present in the project.");
                return;
            }

            Texture maskTexture = maskTarget.GetComponent <Renderer>().sharedMaterial.mainTexture;

            List <Component> components = new List <Component>();

            components.AddRange(maskTarget.transform.parent.gameObject.GetComponentsInChildren <Renderer>());
            components.AddRange(maskTarget.transform.parent.gameObject.GetComponentsInChildren <Graphic>());
            List <Material> differentOriginalMaterials = new List <Material>();
            List <Material> differentNewMaterials      = new List <Material>();

            List <Material> differentReusableMaterials = GetAllReusableMaterials(components, maskedSpriteWorldCoordsShader, maskedUnlitWorldCoordsShader);           // new List<Material>();

            foreach (Component component in components)
            {
                if (component.gameObject != maskTarget.gameObject)
                {
                    List <Material> currSharedMaterials         = new List <Material>();
                    List <bool>     areCurrSharedMaterialsTexts = new List <bool>();
                    if (component is Renderer)
                    {
                        currSharedMaterials.AddRange(((Renderer)component).sharedMaterials);
                        for (int i = 0; i < ((Renderer)component).sharedMaterials.Length; i++)
                        {
                            areCurrSharedMaterialsTexts.Add(false);
                        }
                    }
                    else if (component is Graphic)
                    {
                        currSharedMaterials.Add(((Graphic)component).material);
                        areCurrSharedMaterialsTexts.Add(component is Text);
                    }

                    bool materialsChanged = false;

                    for (int i = 0; i < currSharedMaterials.Count; i++)
                    {
                        Material material = currSharedMaterials[i];

                        if (!differentOriginalMaterials.Contains(material))
                        {
                            Material materialToBeUsed = null;

                            if ((material.shader == spriteDefaultShader) ||
                                (material.shader == unlitTransparentShader) ||
                                (material.shader == UIDefaultShader) ||
                                (material.shader == UIDefaultFontShader))
                            {
                                Material reusableMaterial = FindSuitableMaskedMaterial(material, differentReusableMaterials,
                                                                                       maskedSpriteWorldCoordsShader, maskedUnlitWorldCoordsShader,
                                                                                       spriteDefaultShader, unlitTransparentShader, UIDefaultShader, UIDefaultFontShader,
                                                                                       areCurrSharedMaterialsTexts[i] ? 1 : 0);

                                if (reusableMaterial == null)
                                {
                                    differentOriginalMaterials.Add(material);

                                    Material newMaterial = new Material(material);
                                    if (material.shader == spriteDefaultShader)
                                    {
                                        newMaterial.shader = maskedSpriteWorldCoordsShader;
                                    }
                                    else if (material.shader == unlitTransparentShader)
                                    {
                                        newMaterial.shader = maskedUnlitWorldCoordsShader;
                                    }
                                    else if (material.shader == UIDefaultShader)
                                    {
                                        newMaterial.shader = maskedSpriteWorldCoordsShader;
                                    }
                                    else if (material.shader == UIDefaultFontShader)
                                    {
                                        newMaterial.shader = maskedSpriteWorldCoordsShader;
                                    }
                                    if (areCurrSharedMaterialsTexts[i] == true)
                                    {
                                        newMaterial.SetFloat("_IsThisText", 1);
                                    }
                                    newMaterial.name = material.name + " Masked";
                                    newMaterial.SetTexture("_AlphaTex", maskTexture);

                                    materialToBeUsed = newMaterial;
                                    differentNewMaterials.Add(newMaterial);
                                    currSharedMaterials[i] = newMaterial;
                                    materialsChanged       = true;
                                }
                                else
                                {
                                    currSharedMaterials[i] = reusableMaterial;
                                    materialsChanged       = true;

                                    reusableMaterial.SetTexture("_AlphaTex", maskTexture);
                                    materialToBeUsed = reusableMaterial;
                                }
                            }
                            else if ((material.shader == maskedSpriteWorldCoordsShader) ||
                                     (material.shader == maskedUnlitWorldCoordsShader))
                            {
                                if (material.GetTexture("_AlphaTex") != maskTexture)
                                {
                                    material.SetTexture("_AlphaTex", maskTexture);
                                }
                                if (areCurrSharedMaterialsTexts[i] == true)
                                {
                                    material.SetFloat("_IsThisText", 1);
                                }
                                materialToBeUsed = material;
                            }

                            if (materialToBeUsed != null)
                            {
                                maskTarget.SetMaskMappingAxisInMaterial(maskTarget.maskMappingWorldAxis, materialToBeUsed);
                                maskTarget.SetMaskBoolValueInMaterial("_ClampHoriz", maskTarget.clampAlphaHorizontally, materialToBeUsed);
                                maskTarget.SetMaskBoolValueInMaterial("_ClampVert", maskTarget.clampAlphaVertically, materialToBeUsed);
                                maskTarget.SetMaskBoolValueInMaterial("_UseAlphaChannel", maskTarget.useMaskAlphaChannel, materialToBeUsed);
                            }
                        }
                        else
                        {
                            int index = differentOriginalMaterials.IndexOf(material);

                            currSharedMaterials[i] = differentNewMaterials[index];
                            materialsChanged       = true;
                        }
                    }

                    if (materialsChanged == true)
                    {
                        if (component is Renderer)
                        {
                            ((Renderer)component).sharedMaterials = currSharedMaterials.ToArray();
                        }
                        else if (component is Graphic)
                        {
                            ((Graphic)component).material = currSharedMaterials[0];
                        }
                    }
                }
            }

            Debug.Log("Mask applied." + (maskTarget.isMaskingEnabled ? "" : " Have in mind that masking is disabled, so you will not see the effect, until you enable masking!"));
        }
Exemplo n.º 2
0
        public override void OnInspectorGUI()
        {
            Mask maskTarget = (Mask)target;

            Texture alphaTexture = maskTarget.GetComponent <Renderer>().sharedMaterial.mainTexture;

            if (maskTarget.GetComponents <Mask>().Length > 1)
            {
                GUILayout.Label("More than one instance of Mask attached.\nPlease only use one.");
                return;
            }

            if ((maskTarget.GetComponent <MeshRenderer>() != null) &&
                (maskTarget.GetComponent <MeshFilter>() != null) &&
                (maskTarget.GetComponent <Renderer>().sharedMaterial != null) &&
                (maskTarget.GetComponent <Renderer>().sharedMaterial.mainTexture != null))
            {
                //maskTarget.maskMappingWorldAxis = (Mask.MappingAxis)EditorGUILayout.EnumPopup("Mask Mapping World Axis", maskTarget.maskMappingWorldAxis);

                bool isMaskingEnabled = EditorGUILayout.Toggle("Masking Enabled", maskTarget.isMaskingEnabled);
                if (isMaskingEnabled != maskTarget.isMaskingEnabled)
                {
                    maskTarget.isMaskingEnabled = isMaskingEnabled;
                }

                Mask.MappingAxis maskMappingWorldAxis = (Mask.MappingAxis)EditorGUILayout.EnumPopup("Mask Mapping World Axis", maskTarget.maskMappingWorldAxis);
                if (maskMappingWorldAxis != maskTarget.maskMappingWorldAxis)
                {
                    maskTarget.maskMappingWorldAxis = maskMappingWorldAxis;
                }


                bool invertAxis = EditorGUILayout.Toggle("Invert Axis", maskTarget.invertAxis);
                if (invertAxis != maskTarget.invertAxis)
                {
                    maskTarget.invertAxis = invertAxis;
                }

                bool clampAlphaHorizontally = EditorGUILayout.Toggle("Clamp Alpha Horizontally", maskTarget.clampAlphaHorizontally);
                if (clampAlphaHorizontally != maskTarget.clampAlphaHorizontally)
                {
                    maskTarget.clampAlphaHorizontally = clampAlphaHorizontally;
                }

                bool clampAlphaVertically = EditorGUILayout.Toggle("Clamp Alpha Vertically", maskTarget.clampAlphaVertically);
                if (clampAlphaVertically != maskTarget.clampAlphaVertically)
                {
                    maskTarget.clampAlphaVertically = clampAlphaVertically;
                }

                float clampingBorder = EditorGUILayout.FloatField("Clamping Border", maskTarget.clampingBorder);
                if (clampingBorder != maskTarget.clampingBorder)
                {
                    maskTarget.clampingBorder = clampingBorder;
                }

                bool useMaskAlphaChannel = EditorGUILayout.Toggle("Use Mask Alpha Channel (not RGB)", maskTarget.useMaskAlphaChannel);
                if (useMaskAlphaChannel != maskTarget.useMaskAlphaChannel)
                {
                    maskTarget.useMaskAlphaChannel = useMaskAlphaChannel;
                }


                if (!Application.isPlaying)
                {
                    bool displayMask = EditorGUILayout.Toggle("Display Mask", maskTarget.GetComponent <Renderer>().enabled);
                    if (displayMask != maskTarget.GetComponent <Renderer>().enabled)
                    {
                        maskTarget.GetComponent <Renderer>().enabled = displayMask;
                    }
                }

                if (!Application.isPlaying)
                {
                    if (GUILayout.Button("Apply Mask to Siblings in Hierarchy"))
                    {
                        ApplyMaskToChildren();
                        // this is needed, so that it would apply the value to the materials (this is done within the setter of this property)
                        maskTarget.isMaskingEnabled = maskTarget.isMaskingEnabled;
                    }
                }

                if ((alphaTexturePrevious != null) && (alphaTexturePrevious != alphaTexture))
                {
                    maskTarget.SetMaskTextureInMaterials(alphaTexture);
                }
                alphaTexturePrevious = alphaTexture;
            }
            else
            {
                GUILayout.Label("Please attach MeshFilter and MeshRenderer.\nAlso assign a texture to MeshRenderer.");
            }

            if (GUI.changed)
            {
                EditorUtility.SetDirty(target);
            }
        }
Exemplo n.º 3
0
        public override void OnInspectorGUI()
        {
            Mask maskTarget = (Mask)target;

            bool changesMade = false;

            if (maskTarget.GetComponents <Mask>().Length > 1)
            {
                GUILayout.Label("More than one instance of Mask attached.\nPlease only use one.");
                return;
            }

            if ((maskTarget.GetComponent <MeshRenderer>().sharedMaterial != null))
            {
                Texture maskTexture = (Texture)EditorGUILayout.ObjectField("Main Tex:", maskTarget.MainTex, typeof(Texture), true);
                if (maskTexture != maskTarget.MainTex)
                {
                    maskTarget.MainTex = maskTexture;
                    changesMade        = true;
                }

                Vector2 tiling = EditorGUILayout.Vector2Field("Tiling:", maskTarget.MainTexTiling);
                if (tiling != maskTarget.MainTexTiling)
                {
                    maskTarget.MainTexTiling = tiling;
                    changesMade = true;
                }

                Vector2 offset = EditorGUILayout.Vector2Field("Offset:", maskTarget.MainTexOffset);
                if (offset != maskTarget.MainTexOffset)
                {
                    maskTarget.MainTexOffset = offset;
                    changesMade = true;
                }


                bool isMaskingEnabled = EditorGUILayout.Toggle("Masking Enabled", maskTarget.IsMaskingEnabled);
                if (isMaskingEnabled != maskTarget.IsMaskingEnabled)
                {
                    maskTarget.IsMaskingEnabled = isMaskingEnabled;
                    changesMade = true;
                }

                bool clampAlphaHorizontally = EditorGUILayout.Toggle("Clamp Alpha Horizontally", maskTarget.ClampAlphaHorizontally);
                if (clampAlphaHorizontally != maskTarget.ClampAlphaHorizontally)
                {
                    maskTarget.ClampAlphaHorizontally = clampAlphaHorizontally;
                    changesMade = true;
                }

                bool clampAlphaVertically = EditorGUILayout.Toggle("Clamp Alpha Vertically", maskTarget.ClampAlphaVertically);
                if (clampAlphaVertically != maskTarget.ClampAlphaVertically)
                {
                    maskTarget.ClampAlphaVertically = clampAlphaVertically;
                    changesMade = true;
                }

                float clampingBorder = EditorGUILayout.FloatField("Clamping Border", maskTarget.ClampingBorder);
                if (clampingBorder != maskTarget.ClampingBorder)
                {
                    maskTarget.ClampingBorder = clampingBorder;
                    changesMade = true;
                }

                bool useMaskAlphaChannel = EditorGUILayout.Toggle("Use Mask Alpha Channel (not RGB)", maskTarget.UseMaskAlphaChannel);
                if (useMaskAlphaChannel != maskTarget.UseMaskAlphaChannel)
                {
                    maskTarget.UseMaskAlphaChannel = useMaskAlphaChannel;
                    changesMade = true;
                }


                if (!Application.isPlaying)
                {
                    bool doDisplayMask = EditorGUILayout.Toggle("Display Mask", maskTarget.displayMaskReference);
                    if (maskTarget.displayMaskReference != doDisplayMask)
                    {
                        maskTarget.displayMaskReference = doDisplayMask;
                        changesMade = true;
                    }
                }

                if (!Application.isPlaying)
                {
                    if (GUILayout.Button("Apply Mask to Siblings in Hierarchy"))
                    {
                        ApplyMask();
                        changesMade = true;
                    }
                }
            }
            else
            {
                GUILayout.Label("Please assign Mask-Material to mesh renderer.");
            }

            if (changesMade)
            {
                EditorUtility.SetDirty(target);
            }
        }