public static void Unregister(FXRenderTexture texture)
    {
        texture.Registration.Area  = new Rect(0, 0, 0, 0);
        texture.Registration.Chart = null;

        var group = Groups.FirstOrDefault(g => g.Name == texture.GroupName);

        if (group == null)
        {
            return;             //throw new Exception("RenderTexture is was not registered.");
        }
        if (!group.Textures.Remove(texture))
        {
            return;             //throw new Exception("RenderTexture is was not registered.");
        }
        if (group.Textures.Count == 0)
        {
            group.Destroy();
            Groups.Remove(group);
        }
        else
        {
            group.SetDirty();
            PackTextures();
        }
    }
 private void FreeRenderTexture()
 {
     if (_renderTexture)
     {
         _renderTexture.Unregister();
     }
     if (_renderTexture && _renderTexture.name == "Auto Generated RenderTexture")
     {
         DestroyImmediate(_renderTexture);
     }
     _renderTexture = null;
 }
    private void EnsureRenderTexture()
    {
        if (!_renderTexture)
        {
            var newRenderTexture = ScriptableObject.CreateInstance <FXRenderTexture>();
            newRenderTexture.name        = "Auto Generated RenderTexture";
            newRenderTexture.DefaultName = "_FXScreenTexture";
            newRenderTexture.SizeMode    = FXRenderTexture.FXRenderTextureSizeMode.ImageEffect;

            RenderTexture = newRenderTexture;
        }
    }
    public static void Register(FXRenderTexture texture)
    {
        var group = Groups.FirstOrDefault(g => g.Name == texture.GroupName);

        if (group == null)
        {
            group          = new FXRenderTextureGroup(texture.GroupName);
            group.Priority = texture.Priority;

            Groups.Add(group);
            Groups = Groups.OrderBy(g => g.Name).ToList();
        }

        if (!group.Textures.Contains(texture))
        {
            group.Textures.Add(texture);
        }

        group.SetDirty();

        PackTextures();
    }
    public static void CreatePhysicalAsset(FXRenderTexture asset, string name)
    {
        asset.hideFlags = HideFlags.None;

        var path = AssetDatabase.GetAssetPath(Selection.activeObject);

        if (path == "")
        {
            path = "Assets";
        }
        else if (Path.GetExtension(path) != "")
        {
            path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
        }

        var assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/" + name + ".asset");

        AssetDatabase.CreateAsset(asset, assetPathAndName);

        AssetDatabase.SaveAssets();
        //EditorUtility.FocusProjectWindow();
        Selection.activeObject = asset;
    }
Beispiel #6
0
        public void Apply(GameObject go)
        {
            if (!Material)
            {
                return;
            }

            if (!RenderTexture || !RenderTexture.IsRegistered)
            {
                FXRenderTexture renderTexture;
                if (FXRenderTexture.LastSetRenderTextures.TryGetValue(TextureName, out renderTexture) && renderTexture != null && renderTexture.IsRegistered)
                {
                    renderTexture.SetShaderData(Material, TextureName);
                }
                else
                {
                    FXRenderTexture.SetShaderDataValues(Material, TextureName, FXRenderTexture.EmptyTexture, Vector2.zero, Vector4.zero);
                }
                return;
            }

            RenderTexture.SetShaderData(Material, TextureName);
        }
    public override void OnInspectorGUI()
    {
        FXRenderTexture data = (FXRenderTexture)target;

        if (data == null)
        {
            return;
        }

        if (data.IsRegistered)
        {
            EnsureDisplayMaterial();

            if (data.IsFloatTexture)
            {
                var current = displayFloatTextureMaterial.GetFloat("_MaxFloat");
                current = Mathf.Max(float.Epsilon, EditorGUILayout.FloatField("Preview Range", current));
                displayFloatTextureMaterial.SetFloat("_MaxFloat", current);
            }

            var maxSize     = 256.0f;
            var previewArea = GUILayoutUtility.GetRect(maxSize, maxSize);

            EditorGUI.DrawRect(previewArea, Color.gray);

            if (data.Registration.Chart != null)
            {
                var area   = data.Registration.Area;
                var width  = area.width;
                var height = area.height;
                var ratio  = Mathf.Min(previewArea.width / width, previewArea.height / height);

                width  *= ratio;
                height *= ratio;

                var texArea = new Rect(previewArea.xMin + previewArea.width / 2 - width / 2, previewArea.yMin + previewArea.height / 2 - height / 2, width, height);

                data.SetShaderData(data.IsFloatTexture ? displayFloatTextureMaterial : displayTextureMaterial, "_MainTex");
                EditorGUI.DrawPreviewTexture(texArea, data.Registration.Chart.Data.Texture, data.IsFloatTexture ? displayFloatTextureMaterial : displayTextureMaterial);

                //EditorGUI.LabelField(previewArea, "Current RenderTargets: " + Resources.FindObjectsOfTypeAll(typeof(FXRenderTexture)).Length.ToString());
            }
        }

        if (!AssetDatabase.Contains(data))
        {
            EditorGUILayout.HelpBox("This RenderTexture is automatic generated and is not an asset which is assignable and will only be usable for global shader properties, click the Export Button to create an asset from it.", MessageType.Info);
            if (GUILayout.Button("Export"))
            {
                CreatePhysicalAsset(data, data.DefaultName.Replace("_", string.Empty) + " RenderTexture");
            }
            EditorGUILayout.Space();
        }

        if (data.SizeMode != FXRenderTexture.FXRenderTextureSizeMode.ImageEffect)
        {
            EditorGUILayout.HelpBox("RenderTextures are grouped for faster scene captures by there Priority, change the Priority to allow one group to be rendered before an other.", MessageType.Info);
            var priority = EditorGUILayout.IntField("Priority", data.Priority);
            data.GroupName = priority.ToString();
        }

        EditorGUILayout.HelpBox("Only the lowest Update Interval per priority group will be used.", MessageType.Info);
        EditorGUILayout.BeginHorizontal();
        data.UpdateIntervalInMilliseconds = EditorGUILayout.IntField("Update Interval (ms)", data.UpdateIntervalInMilliseconds);
        if (data.Registration.Chart != null && GUILayout.Button("For Group"))
        {
            var allTexturesInGroup = FXRenderTextureManager.Groups.First(group => group.Name == data.GroupName).Textures.ToArray();
            foreach (var tex in allTexturesInGroup)
            {
                tex.UpdateIntervalInMilliseconds = data.UpdateIntervalInMilliseconds;
            }
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.HelpBox("The Shader Property Name will be used to set a global texture with the given name for all materials. This will allow to set the default texture for objects without a set material property.", MessageType.Info);
        data.DefaultName = EditorGUILayout.TextField("Shader Property Name", data.DefaultName);

        var newSizeMode = (FXRenderTexture.FXRenderTextureSizeMode)EditorGUILayout.EnumPopup("Size Mode: ", data.SizeMode);

        if (newSizeMode != data.SizeMode && data.SizeMode == FXRenderTexture.FXRenderTextureSizeMode.ImageEffect)
        {
            if (EditorUtility.DisplayDialog("Changing Texture Size Mode", "Changing this mode will make the usage of this RenderTexture for PostProcessing Grabs invalid, are you sure?", "Yes", "No"))
            {
                data.SizeMode = newSizeMode;
            }
        }
        else
        {
            data.SizeMode = newSizeMode;
        }

        if (data.SizeMode == FXRenderTexture.FXRenderTextureSizeMode.Factor)
        {
            data.SizeFactor = EditorGUILayout.Slider("Size Factor", data.SizeFactor, 0.01f, 1.0f);
        }
        else if (data.SizeMode == FXRenderTexture.FXRenderTextureSizeMode.MaxSize)
        {
            data.MaximumSize = EditorGUILayout.Vector2Field("Max Size: ", data.MaximumSize);
        }
    }
Beispiel #8
0
    private void ShaderPropertyImpl(Material owner, Shader shader, int propertyIndex)
    {
        int    i            = propertyIndex;
        string label        = ShaderUtil.GetPropertyDescription(shader, i);
        string propertyName = ShaderUtil.GetPropertyName(shader, i);

        switch (ShaderUtil.GetPropertyType(shader, i))
        {
        case ShaderUtil.ShaderPropertyType.Range:     // float ranges
        {
            GUILayout.BeginHorizontal();
            float v2 = ShaderUtil.GetRangeLimits(shader, i, 1);
            float v3 = ShaderUtil.GetRangeLimits(shader, i, 2);
            RangeProperty(propertyName, label, v2, v3);
            GUILayout.EndHorizontal();

            break;
        }

        case ShaderUtil.ShaderPropertyType.Float:     // floats
        {
            FloatProperty(propertyName, label);
            break;
        }

        case ShaderUtil.ShaderPropertyType.Color:     // colors
        {
            ColorProperty(propertyName, label);
            break;
        }

        case ShaderUtil.ShaderPropertyType.TexEnv:     // textures
        {
            var fxTextureType = FXMaterialHelper.GetFXTextureType(ShaderUtil.GetPropertyDescription(shader, i));
            if (fxTextureType != null)
            {
                if (!(Selection.activeObject is GameObject))
                {
                    if (DisplayHelpTexts)
                    {
                        GUILayout.Label(label);
                        EditorGUILayout.HelpBox(label + " is a FXTexture, you can use a FXTextureAssigner or a FXPostProcess component to set this Texture.", MessageType.Info);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    var             match              = Regex.Match(label, @"(.*)\(\w*\).*", RegexOptions.IgnoreCase);
                    var             description        = match.Groups[1].Value.Trim();
                    var             name               = ShaderUtil.GetPropertyName(shader, i);
                    FXRenderTexture oldFxRenderTexture = GetRenderTextureForProperty(owner, shader, name);
                    var             newFxRenderTexture = (FXRenderTexture)EditorGUILayout.ObjectField(description + " (FXRenderTexture)", oldFxRenderTexture, typeof(FXRenderTexture), false);
                    SetRenderTextureForProperty(owner, shader, name, description, newFxRenderTexture);
                }
            }
            else
            {
                ShaderUtil.ShaderPropertyTexDim desiredTexdim = ShaderUtil.GetTexDim(shader, i);
                TextureProperty(propertyName, label, desiredTexdim);
            }

            GUILayout.Space(6);
            break;
        }

        case ShaderUtil.ShaderPropertyType.Vector:     // vectors
        {
            VectorProperty(propertyName, label);
            break;
        }

        default:
        {
            GUILayout.Label("Unknown " + label + " : " + ShaderUtil.GetPropertyType(shader, i));
            break;
        }
        }
    }
Beispiel #9
0
    private void SetRenderTextureForProperty(Material material, Shader shader, string name, string description, FXRenderTexture newFxRenderTexture)
    {
        var gameObject  = (GameObject)Selection.activeObject;
        var renderer    = gameObject.GetComponents <Renderer>().FirstOrDefault(r => r.sharedMaterials.Any(m => m == material));
        var postProcess = gameObject.GetComponents <FXPostProcess>().FirstOrDefault(pp => pp.Material == material);

        if (renderer != null)
        {
            var assigner = gameObject.GetComponent <FXTextureAssigner>() ?? gameObject.AddComponent <FXTextureAssigner>();
            FXTextureAssigner.RenderTextureAssignment.SetAssignment(assigner.Assignments, material, name, description, newFxRenderTexture);
        }
        else if (postProcess != null)
        {
            FXTextureAssigner.RenderTextureAssignment.SetAssignment(postProcess.Assignments, material, name, description, newFxRenderTexture);
        }
    }
Beispiel #10
0
        public static void SetAssignment(List <RenderTextureAssignment> list, Material material, string name, string description, FXRenderTexture newFxRenderTexture)
        {
            var existing = list.FirstOrDefault(a => a.Material == material && a.TextureName == name);

            if (existing == null)
            {
                var newAssignment = new RenderTextureAssignment()
                {
                    Material           = material,
                    RenderTexture      = null,
                    TextureDescription = description,
                    TextureName        = name
                };
                list.Add(newAssignment);
                return;
            }
            existing.TextureDescription = description;
            existing.RenderTexture      = newFxRenderTexture;
        }