public MaterialDescription Run(MaterialDescription material, UDirectory materialPath, PixelFormat outputFormat = PixelFormat.ETC1) { if (material == null) throw new ArgumentNullException("material"); var assetManager = new AssetManager(); var modifiedMaterial = material.Clone(); var textureVisitor = new MaterialTextureVisitor(modifiedMaterial); var nodeReplacer = new MaterialNodeReplacer(modifiedMaterial); var textureNodes = textureVisitor.GetAllModelTextureValues(); foreach (var textureNode in textureNodes) { var itemAsset = assetSession.FindAsset(textureNode.TextureReference.Id); if(itemAsset == null) throw new InvalidOperationException("The referenced texture is not included in the project session."); var textureAsset = (TextureAsset)itemAsset.Asset; if (textureAsset.Format != TextureFormat.Compressed || textureAsset.Alpha == AlphaFormat.None) continue; // the texture has no alpha so there is no need to divide the texture into two sub-textures var originalLocation = textureNode.TextureReference.Location; throw new NotImplementedException("TODO: Need to reimplement this with removed data layer."); using (var image = assetManager.Load<Image>(originalLocation)) { CreateAndSaveSeparateTextures(image, originalLocation, textureAsset.GenerateMipmaps, outputFormat); assetManager.Unload(image); // matching unload to the previous asset manager load call } // make new tree var colorNode = new MaterialTextureNode(GenerateColorTextureURL(originalLocation), textureNode.TexcoordIndex, Vector2.One, Vector2.Zero); var alphaNode = new MaterialTextureNode(GenerateAlphaTextureURL(originalLocation), textureNode.TexcoordIndex, Vector2.One, Vector2.Zero); var substituteAlphaNode = new MaterialShaderClassNode { MixinReference = new AssetReference<EffectShaderAsset>(Guid.Empty, "ComputeColorSubstituteAlphaWithColor") }; substituteAlphaNode.CompositionNodes.Add("color1", colorNode); substituteAlphaNode.CompositionNodes.Add("color2", alphaNode); // set the parameters of the children so that they match the original texture var children = new[] { colorNode, alphaNode }; foreach (var childTexture in children) { childTexture.Sampler.AddressModeU = textureNode.Sampler.AddressModeU; childTexture.Sampler.AddressModeV = textureNode.Sampler.AddressModeV; childTexture.Sampler.Filtering = textureNode.Sampler.Filtering; childTexture.Offset = textureNode.Offset; childTexture.Sampler.SamplerParameterKey = textureNode.Sampler.SamplerParameterKey; childTexture.Scale = textureNode.Scale; childTexture.TexcoordIndex = textureNode.TexcoordIndex; } // copy the parameter key on the color and let the one of the alpha null so that it is set automatically to available value later colorNode.Key = textureNode.Key; alphaNode.Key = null; // update all the material references to the new node nodeReplacer.Replace(textureNode, substituteAlphaNode); } return modifiedMaterial; }
public unsafe void TestSaveAndLoadAssetAndIndexFileManyTimes() { var assetManager = new AssetManager(); var simpleAsset = new SimpleAsset("Grandpa", null) { Dble = 0.0 }; assetManager.SaveSingle(simpleAsset); assetManager.Unload(simpleAsset); var databaseFileProvider = (DatabaseFileProvider)VirtualFileSystem.ResolveProvider("/db", true).Provider; databaseFileProvider.AssetIndexMap.WaitPendingOperations(); simpleAsset = null; GC.Collect(); for (double d = 0; d < 10.0; ++d) { var anotherAssetManager = new AssetManager(); simpleAsset = anotherAssetManager.Load<SimpleAsset>("SimpleAssets/Grandpa"); Assert.That(simpleAsset.Dble, Is.EqualTo(d)); simpleAsset.Dble += 1.0; anotherAssetManager.SaveSingle(simpleAsset); anotherAssetManager.Unload(simpleAsset); databaseFileProvider.AssetIndexMap.WaitPendingOperations(); simpleAsset = null; GC.Collect(); } }
public unsafe void TestSaveAndLoadAssetManyTimes() { var assetManager = new AssetManager(); var simpleAsset = new SimpleAsset("Grandpa", null) { Dble = 0.0 }; assetManager.SaveSingle(simpleAsset); assetManager.Unload(simpleAsset); simpleAsset = null; GC.Collect(); for (double d = 0; d < 10.0; ++d) { simpleAsset = assetManager.Load<SimpleAsset>("SimpleAssets/Grandpa"); Assert.That(simpleAsset.Dble, Is.EqualTo(d)); simpleAsset.Dble += 1.0; assetManager.SaveSingle(simpleAsset); assetManager.Unload(simpleAsset); simpleAsset = null; GC.Collect(); } }