public static MaterialPropertyDescriptor[] GetProperties(Material material)
        {
            RuntimeShaderInfo  shaderInfo = null;
            IRuntimeShaderUtil shaderUtil = IOC.Resolve <IRuntimeShaderUtil>();

            if (shaderUtil != null)
            {
                shaderInfo = shaderUtil.GetShaderInfo(material.shader);
            }

            if (shaderInfo == null)
            {
                return(null);
            }

            List <MaterialPropertyDescriptor> descriptors = new List <MaterialPropertyDescriptor>();

            if (shaderInfo != null)
            {
                for (int i = 0; i < shaderInfo.PropertyCount; ++i)
                {
                    bool isHidden = shaderInfo.IsHidden[i];
                    if (isHidden)
                    {
                        continue;
                    }

                    string propertyDescr = shaderInfo.PropertyDescriptions[i];
                    string propertyName  = shaderInfo.PropertyNames[i];
                    RTShaderPropertyType          propertyType = shaderInfo.PropertyTypes[i];
                    RuntimeShaderInfo.RangeLimits limits       = shaderInfo.PropertyRangeLimits[i];
                    TextureDimension dim          = shaderInfo.PropertyTexDims[i];
                    PropertyInfo     propertyInfo = GetPropertyInfo(propertyType, dim);

                    if (propertyInfo == null)
                    {
                        continue;
                    }

                    MaterialPropertyDescriptor propertyDescriptor = CreatePropertyDescriptor(material, propertyInfo, propertyDescr, propertyName, propertyType, dim, limits);
                    descriptors.Add(propertyDescriptor);
                }
            }
            return(descriptors.ToArray());
        }
Example #2
0
        public void BuildEditor()
        {
            foreach (Transform t in EditorsPanel)
            {
                Destroy(t.gameObject);
            }

            IMaterialDescriptor selector;

            if (!m_propertySelectors.TryGetValue(Material.shader.name, out selector))
            {
                selector = new MaterialDescriptor();
            }


            object converter = selector.CreateConverter(this);

            MaterialPropertyDescriptor[] descriptors = selector.GetProperties(this, converter);
            if (descriptors == null)
            {
                Destroy(gameObject);
                return;
            }

            for (int i = 0; i < descriptors.Length; ++i)
            {
                MaterialPropertyDescriptor descriptor = descriptors[i];
                PropertyEditor             editor     = null;
                PropertyInfo propertyInfo             = descriptor.PropertyInfo;

                RTShaderPropertyType propertyType = descriptor.Type;

                switch (propertyType)
                {
                case RTShaderPropertyType.Range:
                    if (RangeEditor != null)
                    {
                        RangeEditor range = Instantiate(RangeEditor);
                        range.transform.SetParent(EditorsPanel, false);

                        var rangeLimits = descriptor.Limits;
                        range.Min = rangeLimits.Min;
                        range.Max = rangeLimits.Max;
                        editor    = range;
                    }
                    break;

                default:
                    if (m_editorsMap.IsPropertyEditorEnabled(propertyInfo.PropertyType))
                    {
                        GameObject editorPrefab = m_editorsMap.GetPropertyEditor(propertyInfo.PropertyType);
                        GameObject instance     = Instantiate(editorPrefab);
                        instance.transform.SetParent(EditorsPanel, false);

                        if (instance != null)
                        {
                            editor = instance.GetComponent <PropertyEditor>();
                        }
                    }
                    break;
                }


                if (editor == null)
                {
                    continue;
                }

                editor.Init(descriptor.Target, descriptor.Accessor, propertyInfo, descriptor.EraseTargetCallback, descriptor.Label, null, descriptor.ValueChangedCallback, () =>
                {
                    m_editor.IsDirty = true;
                    UpdatePreview(Material);
                });
            }
        }
        public void BuildEditor()
        {
            foreach (Transform t in EditorsPanel)
            {
                Destroy(t.gameObject);
            }

            IMaterialDescriptor selector;

            if (!m_propertySelectors.TryGetValue(Material.shader.name, out selector))
            {
                selector = new MaterialDescriptor();
            }


            object converter = selector.CreateConverter(this);

            MaterialPropertyDescriptor[] descriptors = selector.GetProperties(this, converter);
            if (descriptors == null)
            {
                Destroy(gameObject);
                return;
            }

            for (int i = 0; i < descriptors.Length; ++i)
            {
                MaterialPropertyDescriptor descriptor = descriptors[i];
                PropertyEditor             editor     = null;
                object       target       = descriptor.Target;
                PropertyInfo propertyInfo = descriptor.PropertyInfo;
#if !UNITY_WEBGL && PROC_MATERIAL
                if (descriptor.ProceduralDescription == null)
#endif
                {
                    RTShaderPropertyType propertyType = descriptor.Type;

                    switch (propertyType)
                    {
                    case RTShaderPropertyType.Range:
                        if (RangeEditor != null)
                        {
                            RangeEditor range = Instantiate(RangeEditor);
                            range.transform.SetParent(EditorsPanel, false);

                            var rangeLimits = descriptor.Limits;
                            range.Min = rangeLimits.Min;
                            range.Max = rangeLimits.Max;
                            editor    = range;
                        }
                        break;

                    default:
                        if (EditorsMap.IsPropertyEditorEnabled(propertyInfo.PropertyType))
                        {
                            GameObject editorPrefab = EditorsMap.GetPropertyEditor(propertyInfo.PropertyType);
                            GameObject instance     = Instantiate(editorPrefab);
                            instance.transform.SetParent(EditorsPanel, false);

                            if (instance != null)
                            {
                                editor = instance.GetComponent <PropertyEditor>();
                            }
                        }
                        break;
                    }
                }
#if !UNITY_WEBGL && PROC_MATERIAL
                else
                {
                    ProcPropertyDescription input = descriptor.ProceduralDescription;
                    if (input.hasRange)
                    {
                        if (input.type == ProcPropertyType.Float)
                        {
                            if (RangeEditor != null)
                            {
                                RangeEditor range = Instantiate(RangeEditor);
                                range.transform.SetParent(EditorsPanel, false);
                                range.Min = input.minimum;
                                range.Max = input.maximum;
                                //TODO implement step on range editor // = input.step
                                editor = range;
                            }
                        }
                        else
                        {
                            //TODO: Implement range on vector editors

                            if (EditorsMap.IsPropertyEditorEnabled(propertyInfo.PropertyType))
                            {
                                GameObject editorPrefab = EditorsMap.GetPropertyEditor(propertyInfo.PropertyType);
                                GameObject instance     = Instantiate(editorPrefab);
                                instance.transform.SetParent(EditorsPanel, false);

                                if (instance != null)
                                {
                                    editor = instance.GetComponent <PropertyEditor>();
                                }
                            }
                        }
                    }
                    else
                    {
                        //if(input.type == ProceduralPropertyType.Enum)
                        //TODO: Implement enum from string array editor. //input.enumOptions

                        if (EditorsMap.IsPropertyEditorEnabled(propertyInfo.PropertyType))
                        {
                            GameObject editorPrefab = EditorsMap.GetPropertyEditor(propertyInfo.PropertyType);
                            GameObject instance     = Instantiate(editorPrefab);
                            instance.transform.SetParent(EditorsPanel, false);

                            if (instance != null)
                            {
                                editor = instance.GetComponent <PropertyEditor>();
                            }
                        }
                    }
                }
#endif

                if (editor == null)
                {
                    continue;
                }

                editor.Init(target, propertyInfo, descriptor.Label, null, descriptor.ValueChangedCallback, () =>
                {
                    RuntimeEditorApplication.SaveSelectedObjects();
                });
            }
        }
Example #4
0
        public MaterialPropertyDescriptor[] GetProperties(MaterialEditor editor)
        {
            RuntimeShaderInfo shaderInfo = RuntimeShaderUtil.GetShaderInfo(editor.Material.shader);

            if (shaderInfo == null)
            {
                return(null);
            }
            List <MaterialPropertyDescriptor> descriptors = new List <MaterialPropertyDescriptor>();

            for (int i = 0; i < shaderInfo.PropertyCount; ++i)
            {
                bool isHidden = shaderInfo.IsHidden[i];
                if (isHidden)
                {
                    continue;
                }

                string propertyDescr = shaderInfo.PropertyDescriptions[i];
                string propertyName  = shaderInfo.PropertyNames[i];
                if (propertyName != "_Color" && propertyName != "_MainTex")
                {
                    continue;
                }

                RTShaderPropertyType          propertyType = shaderInfo.PropertyTypes[i];
                RuntimeShaderInfo.RangeLimits limits       = shaderInfo.PropertyRangeLimits[i];
                TextureDimension dim          = shaderInfo.PropertyTexDims[i];
                PropertyInfo     propertyInfo = null;
                switch (propertyType)
                {
                case RTShaderPropertyType.Color:
                    propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Color);
                    break;

                case RTShaderPropertyType.Float:
                    propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Float);
                    break;

                case RTShaderPropertyType.Range:
                    propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Float);
                    break;

                case RTShaderPropertyType.TexEnv:
                    switch (dim)
                    {
                    case TextureDimension.Any:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Texture);
                        break;

                    case TextureDimension.Cube:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Cubemap);
                        break;

                    case TextureDimension.None:
                        propertyInfo = null;
                        break;

                    case TextureDimension.Tex2D:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Texture2D);
                        break;

                    case TextureDimension.Tex2DArray:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Texture2DArray);
                        break;

                    case TextureDimension.Tex3D:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Texture3D);
                        break;

                    case TextureDimension.Unknown:
                        propertyInfo = null;
                        break;
                    }

                    break;

                case RTShaderPropertyType.Vector:
                    propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Float);
                    break;
                }

                if (propertyInfo == null)
                {
                    continue;
                }

                MaterialPropertyAccessor   accessor           = new MaterialPropertyAccessor(editor.Material, propertyName);
                MaterialPropertyDescriptor propertyDescriptor = new MaterialPropertyDescriptor(accessor, propertyDescr, propertyType, propertyInfo, limits, dim, null);
                descriptors.Add(propertyDescriptor);
            }

            return(descriptors.ToArray());
        }
Example #5
0
        public MaterialPropertyDescriptor[] GetProperties(MaterialEditor editor, object converter)
        {
            RuntimeShaderInfo  shaderInfo = null;
            IRuntimeShaderUtil shaderUtil = IOC.Resolve <IRuntimeShaderUtil>();

            if (shaderUtil != null)
            {
                shaderInfo = shaderUtil.GetShaderInfo(editor.Material.shader);
            }


            if (shaderInfo == null)
            {
                return(null);
            }

            List <MaterialPropertyDescriptor> descriptors = new List <MaterialPropertyDescriptor>();

            if (shaderInfo != null)
            {
                for (int i = 0; i < shaderInfo.PropertyCount; ++i)
                {
                    bool isHidden = shaderInfo.IsHidden[i];
                    if (isHidden)
                    {
                        continue;
                    }

                    string propertyDescr = shaderInfo.PropertyDescriptions[i];
                    string propertyName  = shaderInfo.PropertyNames[i];
                    RTShaderPropertyType          propertyType = shaderInfo.PropertyTypes[i];
                    RuntimeShaderInfo.RangeLimits limits       = shaderInfo.PropertyRangeLimits[i];
                    TextureDimension dim          = shaderInfo.PropertyTexDims[i];
                    PropertyInfo     propertyInfo = null;
                    switch (propertyType)
                    {
                    case RTShaderPropertyType.Color:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Color, "Color");
                        break;

                    case RTShaderPropertyType.Float:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Float, "Float");
                        break;

                    case RTShaderPropertyType.Range:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Float, "Float");
                        break;

                    case RTShaderPropertyType.TexEnv:
                        switch (dim)
                        {
                        case TextureDimension.Any:
                            propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Texture, "Texture");
                            break;

                        case TextureDimension.Cube:
                            propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Cubemap, "Cubemap");
                            break;

                        case TextureDimension.None:
                            propertyInfo = null;
                            break;

                        case TextureDimension.Tex2D:
                            propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Texture2D, "Texture2D");
                            break;

                        case TextureDimension.Tex2DArray:
                            propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Texture2DArray, "Texture2DArray");
                            break;

                        case TextureDimension.Tex3D:
                            propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Texture3D, "Texture3D");
                            break;

                        case TextureDimension.Unknown:
                            propertyInfo = null;
                            break;
                        }

                        break;

                    case RTShaderPropertyType.Vector:
                        propertyInfo = Strong.PropertyInfo((MaterialPropertyAccessor x) => x.Vector, "Vector");
                        break;
                    }

                    if (propertyInfo == null)
                    {
                        continue;
                    }

                    MaterialPropertyDescriptor propertyDescriptor = new MaterialPropertyDescriptor(
                        editor.Material,
                        new MaterialPropertyAccessor(editor.Material, propertyName),
                        propertyDescr, propertyType, propertyInfo, limits, dim, null,
                        (accessorRef, newTarget) =>
                    {
                        MaterialPropertyAccessor accessor = (MaterialPropertyAccessor)accessorRef;
                        accessor.Material = newTarget as Material;
                    });
                    descriptors.Add(propertyDescriptor);
                }
            }

            return(descriptors.ToArray());
        }