public static string BuildDepMaterial(Material mat, float scaleTexture = 1f)
    {
        CDepCollectInfo buildResult = KDepCollectInfoCaching.GetCache(mat);

        if (buildResult != null)
        {
            return(buildResult.Path);
        }

        KSerializeMaterial sMat = __DoExportMaterial(mat, scaleTexture);

        if (sMat != null)
        {
            string path = AssetDatabase.GetAssetPath(mat);

            bool needBuild = AssetVersionControl.TryCheckNeedBuildWithMeta(path);
            if (needBuild)
            {
                AssetVersionControl.TryMarkBuildVersion(path);
            }

            path        = KDependencyBuild.__GetPrefabBuildPath(path);
            buildResult = KDependencyBuild.__DoBuildScriptableObject("Material/Material_" + path, sMat, needBuild);

            KDepCollectInfoCaching.SetCache(mat, buildResult);

            return(buildResult.Path);
        }

        // 可能没路径的材质,直接忽略
        return("");
    }
    // 只导出材质信息,不实际导出材质
    private static KSerializeMaterial __DoExportMaterial(Material mat, float scaleTexture = 1f)
    {
        string matPath = AssetDatabase.GetAssetPath(mat).Replace("Assets/", "");

        if (string.IsNullOrEmpty(matPath))
        {
            Log.Warning("没有路径材质Material: {0}   可能是动态创建的?", mat.name);
            return(null);
        }

        var props = new List <KSerializeMaterialProperty>();
        IEnumerator <KSerializeMaterialProperty> shaderPropEnumtor = _ShaderPropEnumtor(mat, scaleTexture);

        while (shaderPropEnumtor.MoveNext())
        {
            KSerializeMaterialProperty shaderProp = shaderPropEnumtor.Current;
            if (shaderProp != null)
            {
                props.Add(shaderProp);
            }
        }

        var shaderBuildPath     = _BuildShader(mat.shader);
        KSerializeMaterial xMat = ScriptableObject.CreateInstance <KSerializeMaterial>();

        xMat.MaterialName = matPath;
        xMat.ShaderName   = mat.shader.name;
        xMat.ShaderPath   = shaderBuildPath;
        xMat.Props        = props;

        return(xMat);
    }
示例#3
0
    // 加载材质的图片, 协程等待
    private IEnumerator CoGenerateMaterial(string matPath, KSerializeMaterial sMat)
    {
        // 纹理全部加载完成后到这里
        //if (!CachedMaterials.TryGetValue(matPath, out mat))
        {
            var shaderLoader = KShaderLoader.Load(sMat.ShaderPath);
            while (!shaderLoader.IsCompleted)
            {
                yield return(null);
            }

            var shader = shaderLoader.ShaderAsset;
            if (shader == null)
            {
                shader = KTool.FindShader(sMat.ShaderName);
                Logger.LogWarning("无法加载Shader资源: {0}, 使用Shaders.Find代替", sMat.ShaderName);
                if (shader == null)
                {
                    Logger.LogWarning("找不到Shader: {0}, 使用Diffuse临时代替", sMat.ShaderName);
                    shader = KTool.FindShader("Diffuse");
                }
            }
            Debuger.Assert(shader);

            Mat      = new Material(shader);
            Mat.name = sMat.MaterialName;

            //CachedMaterials[matPath] = mat;

            foreach (KSerializeMaterialProperty shaderProp in sMat.Props)
            {
                switch (shaderProp.Type)
                {
                case KSerializeMaterialProperty.ShaderType.Texture:
                    Vector2 tiling;
                    Vector2 offset;
                    var     texturePath = ParseMaterialStr(shaderProp.PropValue, out tiling, out offset);
                    if (TextureLoaders == null)
                    {
                        TextureLoaders = new List <KTextureLoader>();
                    }

                    var texLoader = KTextureLoader.Load(texturePath);
                    TextureLoaders.Add(texLoader);
                    while (!texLoader.IsCompleted)
                    {
                        yield return(null);
                    }

                    var tex = texLoader.Asset;
                    if (tex == null)
                    {
                        Logger.LogError("找不到纹理: {0}", texturePath);
                    }
                    else
                    {
                        _SetMatTex(Mat, shaderProp.PropName, tex, tiling, offset);
                    }
                    break;

                case KSerializeMaterialProperty.ShaderType.Color:
                    _SetMatColor(Mat, shaderProp.PropName, shaderProp.PropValue);
                    break;

                case KSerializeMaterialProperty.ShaderType.Range:
                    _SetMatRange(Mat, shaderProp.PropName, shaderProp.PropValue);
                    break;

                case KSerializeMaterialProperty.ShaderType.Vector:
                    _SetMatVector(Mat, shaderProp.PropName, shaderProp.PropValue);
                    break;

                case KSerializeMaterialProperty.ShaderType.RenderTexture:
                    // RenderTextures, 不处理, 一般用在水,Water脚本会自动生成
                    break;
                }
            }
        }
    }
示例#4
0
    // 加载材质的图片, 协程等待
    private IEnumerator CoGenerateMaterial(string matPath, KSerializeMaterial sMat)
    {
        // 纹理全部加载完成后到这里
        //if (!CachedMaterials.TryGetValue(matPath, out mat))
        {
            var shaderLoader = KShaderLoader.Load(sMat.ShaderPath);
            while (!shaderLoader.IsCompleted)
            {
                yield return null;
            }

            var shader = shaderLoader.ShaderAsset;
            if (shader == null)
            {
                shader = KTool.FindShader(sMat.ShaderName);
                Logger.LogWarning("无法加载Shader资源: {0}, 使用Shaders.Find代替", sMat.ShaderName);
                if (shader == null)
                {
                    Logger.LogWarning("找不到Shader: {0}, 使用Diffuse临时代替", sMat.ShaderName);
                    shader = KTool.FindShader("Diffuse");
                }
            }
            Logger.Assert(shader);

            Mat = new Material(shader);
            Mat.name = sMat.MaterialName;

            //CachedMaterials[matPath] = mat;

            foreach (KSerializeMaterialProperty shaderProp in sMat.Props)
            {
                switch (shaderProp.Type)
                {
                    case KSerializeMaterialProperty.ShaderType.Texture:
                        Vector2 tiling;
                        Vector2 offset;
                        var texturePath = ParseMaterialStr(shaderProp.PropValue, out tiling, out offset);
                        if (TextureLoaders == null)
                            TextureLoaders = new List<KTextureLoader>();

                        var texLoader = KTextureLoader.Load(texturePath);
                        TextureLoaders.Add(texLoader);
                        while (!texLoader.IsCompleted)
                            yield return null;

                        var tex = texLoader.Asset;
                        if (tex == null)
                        {
                            Logger.LogError("找不到纹理: {0}", texturePath);
                        }
                        else
                        {
                            _SetMatTex(Mat, shaderProp.PropName, tex, tiling, offset);
                        }
                        break;
                    case KSerializeMaterialProperty.ShaderType.Color:
                        _SetMatColor(Mat, shaderProp.PropName, shaderProp.PropValue);
                        break;
                    case KSerializeMaterialProperty.ShaderType.Range:
                        _SetMatRange(Mat, shaderProp.PropName, shaderProp.PropValue);
                        break;
                    case KSerializeMaterialProperty.ShaderType.Vector:
                        _SetMatVector(Mat, shaderProp.PropName, shaderProp.PropValue);
                        break;
                    case KSerializeMaterialProperty.ShaderType.RenderTexture:
                        // RenderTextures, 不处理, 一般用在水,Water脚本会自动生成
                        break;
                }
            }
        }
    }