예제 #1
0
        public override void GetDepsFrom(object obj, GetDepsFromContext context)
        {
            base.GetDepsFrom(obj, context);
            if (obj == null)
            {
                return;
            }

            Material           o          = (Material)obj;
            RuntimeShaderInfo  shaderInfo = null;
            IRuntimeShaderUtil shaderUtil = IOC.Resolve <IRuntimeShaderUtil>();

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

            if (shaderInfo == null)
            {
                return;
            }

            for (int i = 0; i < shaderInfo.PropertyCount; ++i)
            {
                string name = shaderInfo.PropertyNames[i];
                RTShaderPropertyType type = shaderInfo.PropertyTypes[i];
                switch (type)
                {
                case RTShaderPropertyType.TexEnv:
                    AddDep(o.GetTexture(name), context);
                    break;
                }
            }
        }
        public static PropertyInfo GetPropertyInfo(RTShaderPropertyType propertyType, TextureDimension dim)
        {
            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;
            }

            return(propertyInfo);
        }
 public MaterialPropertyDescriptor(object target, string label, ProcPropertyDescription procDescription, PropertyInfo propertyInfo, PropertyEditorCallback callback)
 {
     Target = target;
     Label  = label;
     Type   = RTShaderPropertyType.Procedural;
     ProceduralDescription = procDescription;
     PropertyInfo          = propertyInfo;
     ValueChangedCallback  = callback;
 }
 public MaterialPropertyDescriptor(object target, string label, RTShaderPropertyType type, PropertyInfo propertyInfo, RuntimeShaderInfo.RangeLimits limits, TextureDimension dims, PropertyEditorCallback callback)
 {
     Target               = target;
     Label                = label;
     Type                 = type;
     PropertyInfo         = propertyInfo;
     Limits               = limits;
     TexDims              = dims;
     ValueChangedCallback = callback;
 }
예제 #5
0
 public MaterialPropertyDescriptor(object[] targets, object[] acessors, string label, RTShaderPropertyType type, PropertyInfo propertyInfo, RuntimeShaderInfo.RangeLimits limits, TextureDimension dims, PropertyEditorCallback callback, Action <object, object> eraseTargetCallback)
 {
     Targets              = targets;
     Accessors            = acessors;
     Label                = label;
     Type                 = type;
     PropertyInfo         = propertyInfo;
     Limits               = limits;
     TexDims              = dims;
     ValueChangedCallback = callback;
     EraseTargetCallback  = eraseTargetCallback;
 }
        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());
        }
예제 #7
0
        private static RuntimeShaderInfo Create(Shader shader)
        {
            if (shader == null)
            {
                throw new System.ArgumentNullException("shader");
            }

            int propertyCount = ShaderUtil.GetPropertyCount(shader);

            RuntimeShaderInfo shaderInfo = new RuntimeShaderInfo();

            shaderInfo.Name                 = shader.name;
            shaderInfo.PropertyCount        = propertyCount;
            shaderInfo.PropertyDescriptions = new string[propertyCount];
            shaderInfo.PropertyNames        = new string[propertyCount];
            shaderInfo.PropertyRangeLimits  = new RuntimeShaderInfo.RangeLimits[propertyCount];
            shaderInfo.PropertyTexDims      = new TextureDimension[propertyCount];
            shaderInfo.PropertyTypes        = new RTShaderPropertyType[propertyCount];
            shaderInfo.IsHidden             = new bool[propertyCount];

            for (int i = 0; i < propertyCount; ++i)
            {
                shaderInfo.PropertyDescriptions[i] = ShaderUtil.GetPropertyDescription(shader, i);
                shaderInfo.PropertyNames[i]        = ShaderUtil.GetPropertyName(shader, i);
                shaderInfo.PropertyRangeLimits[i]  = new RuntimeShaderInfo.RangeLimits(
                    ShaderUtil.GetRangeLimits(shader, i, 0),
                    ShaderUtil.GetRangeLimits(shader, i, 1),
                    ShaderUtil.GetRangeLimits(shader, i, 2));
                shaderInfo.PropertyTexDims[i] = ShaderUtil.GetTexDim(shader, i);

                RTShaderPropertyType          rtType = RTShaderPropertyType.Unknown;
                ShaderUtil.ShaderPropertyType type   = ShaderUtil.GetPropertyType(shader, i);
                if (m_typeToType.ContainsKey(type))
                {
                    rtType = m_typeToType[type];
                }

                shaderInfo.PropertyTypes[i] = rtType;
                shaderInfo.IsHidden[i]      = ShaderUtil.IsShaderPropertyHidden(shader, i);
            }
            return(shaderInfo);
        }
예제 #8
0
 public override void GetDeps(GetDepsContext context)
 {
     base.GetDeps(context);
     if (m_propertyValues != null)
     {
         for (int i = 0; i < m_propertyValues.Length; ++i)
         {
             RTShaderPropertyType type = m_propertyTypes[i];
             switch (type)
             {
             case RTShaderPropertyType.TexEnv:
                 if (m_propertyValues[i].ValueBase is long)
                 {
                     AddDep((long)m_propertyValues[i].ValueBase, context);
                 }
                 break;
             }
         }
     }
 }
예제 #9
0
        public override object WriteTo(object obj)
        {
            obj = base.WriteTo(obj);
            if (obj == null)
            {
                return(null);
            }

            Material o = (Material)obj;

            if (m_assetDB.IsMapped(shader))
            {
                o.shader = FromID <Shader>(shader);
            }
            else
            {
                o.shader = Shader.Find(m_shaderName);
            }

            if (m_keywords != null)
            {
                foreach (string keyword in m_keywords)
                {
                    o.EnableKeyword(keyword);
                }
            }

            if (m_propertyNames != null)
            {
                int textureIndex = 0;
                for (int i = 0; i < m_propertyNames.Length; ++i)
                {
                    string name = m_propertyNames[i];
                    RTShaderPropertyType type = m_propertyTypes[i];
                    switch (type)
                    {
                    case RTShaderPropertyType.Color:
                        if (m_propertyValues[i].ValueBase is PersistentColor <TID> )
                        {
                            o.SetColor(name, (PersistentColor <TID>)m_propertyValues[i].ValueBase);
                        }
                        break;

                    case RTShaderPropertyType.Float:
                        if (m_propertyValues[i].ValueBase is float)
                        {
                            o.SetFloat(name, (float)m_propertyValues[i].ValueBase);
                        }
                        break;

                    case RTShaderPropertyType.Range:
                        if (m_propertyValues[i].ValueBase is float)
                        {
                            o.SetFloat(name, (float)m_propertyValues[i].ValueBase);
                        }
                        break;

                    case RTShaderPropertyType.TexEnv:
                        if (m_propertyValues[i].ValueBase is TID)
                        {
                            o.SetTexture(name, FromID <Texture>((TID)m_propertyValues[i].ValueBase));
                            if (m_textureOffset != null)
                            {
                                o.SetTextureOffset(name, m_textureOffset[textureIndex]);
                            }
                            if (m_textureScale != null)
                            {
                                o.SetTextureScale(name, m_textureScale[textureIndex]);
                            }
                            textureIndex++;
                        }
                        break;

                    case RTShaderPropertyType.Vector:
                        if (m_propertyValues[i].ValueBase is PersistentVector4 <TID> )
                        {
                            o.SetVector(name, (PersistentVector4 <TID>)m_propertyValues[i].ValueBase);
                        }
                        break;

                    case RTShaderPropertyType.Unknown:
                        break;
                    }
                }
            }

            IMaterialUtil util = IOC.Resolve <IMaterialUtil>();

            util.SetMaterialKeywords(o);
            o.renderQueue = m_renderQueue;

            return(obj);
        }
        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();
                });
            }
        }
예제 #11
0
 public MaterialPropertyDescriptor(object target, object acessor, string label, RTShaderPropertyType type, PropertyInfo propertyInfo, RuntimeShaderInfo.RangeLimits limits, TextureDimension dims, PropertyEditorCallback callback, Action <object, object> eraseTargetCallback)
     : this(new[] { target }, new[] { acessor }, label, type, propertyInfo, limits, dims, callback, eraseTargetCallback)
 {
 }
예제 #12
0
        public override void ReadFrom(object obj)
        {
            base.ReadFrom(obj);
            if (obj == null)
            {
                return;
            }

            Material o = (Material)obj;

            if (o.HasProperty("_MainTex"))
            {
                mainTextureOffset = o.mainTextureOffset;
                mainTextureScale  = o.mainTextureScale;
            }

            if (o.shader == null)
            {
                shader       = m_assetDB.NullID;
                m_shaderName = null;
                return;
            }

            shader       = m_assetDB.ToID(o.shader);
            m_shaderName = o.shader.name;

            RuntimeShaderInfo  shaderInfo = null;
            IRuntimeShaderUtil shaderUtil = IOC.Resolve <IRuntimeShaderUtil>();

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

            m_propertyNames  = new string[shaderInfo.PropertyCount];
            m_propertyTypes  = new RTShaderPropertyType[shaderInfo.PropertyCount];
            m_propertyValues = new PrimitiveContract[shaderInfo.PropertyCount];

            for (int i = 0; i < shaderInfo.PropertyCount; ++i)
            {
                string name = shaderInfo.PropertyNames[i];
                RTShaderPropertyType type = shaderInfo.PropertyTypes[i];
                m_propertyNames[i] = name;
                m_propertyTypes[i] = type;
                switch (type)
                {
                case RTShaderPropertyType.Color:
                    m_propertyValues[i] = PrimitiveContract.Create((PersistentColor)o.GetColor(name));
                    break;

                case RTShaderPropertyType.Float:
                    m_propertyValues[i] = PrimitiveContract.Create(o.GetFloat(name));
                    break;

                case RTShaderPropertyType.Range:
                    m_propertyValues[i] = PrimitiveContract.Create(o.GetFloat(name));
                    break;

                case RTShaderPropertyType.TexEnv:
                    Texture2D texture = (Texture2D)o.GetTexture(name);
                    if (texture == null)
                    {
                        m_propertyValues[i] = PrimitiveContract.Create(m_assetDB.NullID);
                    }
                    else
                    {
                        m_propertyValues[i] = PrimitiveContract.Create(m_assetDB.ToID(texture));
                    }
                    break;

                case RTShaderPropertyType.Vector:
                    m_propertyValues[i] = PrimitiveContract.Create((PersistentVector4)o.GetVector(name));
                    break;

                case RTShaderPropertyType.Unknown:
                    m_propertyValues[i] = null;
                    break;
                }
            }

            m_keywords = o.shaderKeywords;
        }
예제 #13
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());
        }
예제 #14
0
        private static void _Create(Shader shader, string bundleName, string variantName)
        {
            if (shader == null)
            {
                throw new System.ArgumentNullException("shader");
            }

            int propertyCount = ShaderUtil.GetPropertyCount(shader);

            RuntimeShaderInfo shaderInfo = new RuntimeShaderInfo();

            shaderInfo.Name = shader.name;
            if (!shader.HasMappedInstanceID())
            {
                //bool create = EditorUtility.DisplayDialog("RuntimeShaderInfo Generator", "Unable to create RuntimeShaderInfo. Please Create or Update ResourceMap", "Create", "Cancel");
                //if (create)
                //{
                //    ResourceMapGen.CreateResourceMap(true);
                //}

                return;
            }

            shaderInfo.InstanceId           = shader.GetMappedInstanceID();
            shaderInfo.PropertyCount        = propertyCount;
            shaderInfo.PropertyDescriptions = new string[propertyCount];
            shaderInfo.PropertyNames        = new string[propertyCount];
            shaderInfo.PropertyRangeLimits  = new RuntimeShaderInfo.RangeLimits[propertyCount];
            shaderInfo.PropertyTexDims      = new TextureDimension[propertyCount];
            shaderInfo.PropertyTypes        = new RTShaderPropertyType[propertyCount];
            shaderInfo.IsHidden             = new bool[propertyCount];

            for (int i = 0; i < propertyCount; ++i)
            {
                shaderInfo.PropertyDescriptions[i] = ShaderUtil.GetPropertyDescription(shader, i);
                shaderInfo.PropertyNames[i]        = ShaderUtil.GetPropertyName(shader, i);
                shaderInfo.PropertyRangeLimits[i]  = new RuntimeShaderInfo.RangeLimits(
                    ShaderUtil.GetRangeLimits(shader, i, 0),
                    ShaderUtil.GetRangeLimits(shader, i, 1),
                    ShaderUtil.GetRangeLimits(shader, i, 2));
                shaderInfo.PropertyTexDims[i] = ShaderUtil.GetTexDim(shader, i);

                RTShaderPropertyType          rtType = RTShaderPropertyType.Unknown;
                ShaderUtil.ShaderPropertyType type   = ShaderUtil.GetPropertyType(shader, i);
                if (m_typeToType.ContainsKey(type))
                {
                    rtType = m_typeToType[type];
                }

                shaderInfo.PropertyTypes[i] = rtType;
                shaderInfo.IsHidden[i]      = ShaderUtil.IsShaderPropertyHidden(shader, i);
            }

            string fullPath = Application.dataPath + RuntimeShaderUtil.GetPath(true);

            Directory.CreateDirectory(fullPath);

            string fileName = RuntimeShaderUtil.GetShaderInfoFileName(shader);

            string path = Path.Combine(fullPath, fileName);

            bool refresh = !File.Exists(path);

            File.WriteAllText(path, JsonUtility.ToJson(shaderInfo));

            if (refresh)
            {
                AssetDatabase.Refresh();
            }
        }
 public static MaterialPropertyDescriptor CreatePropertyDescriptor(Material material, PropertyInfo propertyInfo, string propertyDescr, string propertyName, RTShaderPropertyType propertyType)
 {
     return(CreatePropertyDescriptor(material, propertyInfo, propertyDescr, propertyName, propertyType, TextureDimension.Tex2D, new RuntimeShaderInfo.RangeLimits()));
 }
예제 #16
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 static MaterialPropertyDescriptor CreatePropertyDescriptor(Material material, PropertyInfo propertyInfo, string propertyDescr, string propertyName, RTShaderPropertyType propertyType, TextureDimension dim, RuntimeShaderInfo.RangeLimits limits)
 {
     return(new MaterialPropertyDescriptor(
                material,
                new MaterialPropertyAccessor(material, propertyName),
                propertyDescr, propertyType, propertyInfo, limits, dim, null,
                (accessorRef, newTarget) =>
     {
         MaterialPropertyAccessor accessor = (MaterialPropertyAccessor)accessorRef;
         accessor.Material = newTarget as Material;
     }));
 }
예제 #18
0
        public override object WriteTo(object obj)
        {
            obj = base.WriteTo(obj);
            if (obj == null)
            {
                return(null);
            }

            Material o = (Material)obj;

            if (o.HasProperty("_MainTex"))
            {
                o.mainTextureOffset = mainTextureOffset;
                o.mainTextureScale  = mainTextureScale;
            }

            if (m_assetDB.IsMapped(shader))
            {
                o.shader = m_assetDB.FromID <Shader>(shader);
            }
            else
            {
                o.shader = Shader.Find(m_shaderName);
            }

            if (m_keywords != null)
            {
                foreach (string keyword in m_keywords)
                {
                    o.EnableKeyword(keyword);
                }
            }

            if (m_propertyNames != null)
            {
                for (int i = 0; i < m_propertyNames.Length; ++i)
                {
                    string name = m_propertyNames[i];
                    RTShaderPropertyType type = m_propertyTypes[i];
                    switch (type)
                    {
                    case RTShaderPropertyType.Color:
                        if (m_propertyValues[i].ValueBase is PersistentColor)
                        {
                            o.SetColor(name, (PersistentColor)m_propertyValues[i].ValueBase);
                        }
                        break;

                    case RTShaderPropertyType.Float:
                        if (m_propertyValues[i].ValueBase is float)
                        {
                            o.SetFloat(name, (float)m_propertyValues[i].ValueBase);
                        }
                        break;

                    case RTShaderPropertyType.Range:
                        if (m_propertyValues[i].ValueBase is float)
                        {
                            o.SetFloat(name, (float)m_propertyValues[i].ValueBase);
                        }
                        break;

                    case RTShaderPropertyType.TexEnv:
                        if (m_propertyValues[i].ValueBase is long)
                        {
                            o.SetTexture(name, FromID <Texture>((long)m_propertyValues[i].ValueBase));
                        }
                        break;

                    case RTShaderPropertyType.Vector:
                        if (m_propertyValues[i].ValueBase is PersistentVector4)
                        {
                            o.SetVector(name, (PersistentVector4)m_propertyValues[i].ValueBase);
                        }
                        break;

                    case RTShaderPropertyType.Unknown:
                        break;
                    }
                }
            }

            return(obj);
        }
예제 #19
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());
        }