public void EqualSource() { UnityEngine.Material realMaterial = resultMaterial.Unity3dObject as UnityEngine.Material; Assert.AreEqual(material.Name, realMaterial.name); Assert.AreEqual(material.Shader, realMaterial.shader.name); for (int i = 0; i < material.PropertiesLength; i++) { Schema.ShaderProperty p = material.GetProperties(i); Assert.IsTrue(realMaterial.HasProperty(p.Names)); switch (p.Type) { case ShaderPropertyType.Float: case ShaderPropertyType.Range: { Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyFloat); float originValue = realMaterial.GetFloat(p.Names); ShaderPropertyFloat f = p.GetValue <ShaderPropertyFloat>(new ShaderPropertyFloat()); Assert.AreEqual(f.Value, originValue); } break; case ShaderPropertyType.Color: { Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyColor); UnityEngine.Color originValue = realMaterial.GetColor(p.Names); ShaderPropertyColor c = p.GetValue <ShaderPropertyColor>(new ShaderPropertyColor()); Assert.AreEqual(originValue.a, c.Color.A); Assert.AreEqual(originValue.g, c.Color.G); Assert.AreEqual(originValue.b, c.Color.B); Assert.AreEqual(originValue.r, c.Color.R); } break; case ShaderPropertyType.Vector: { Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyVector); UnityEngine.Vector4 originValue = realMaterial.GetVector(p.Names); ShaderPropertyVector v = p.GetValue <ShaderPropertyVector>(new ShaderPropertyVector()); Assert.AreEqual(originValue.x, v.Vector.X); Assert.AreEqual(originValue.y, v.Vector.Y); Assert.AreEqual(originValue.z, v.Vector.Z); Assert.AreEqual(originValue.w, v.Vector.W); } break; case ShaderPropertyType.TexEnv: { Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyTexture); //UnityEngine.Texture texture = realMaterial.GetTexture(p.Names); Vector2 offset = realMaterial.GetTextureOffset(p.Names); Vector2 scale = realMaterial.GetTextureScale(p.Names); //这个测试用例不真正装载 texture. //Assert.IsFalse(texture == null); ShaderPropertyTexture t = p.GetValue <ShaderPropertyTexture>(new ShaderPropertyTexture()); string texturePath = resultMaterial.GetTexturePath(t.Name); Assert.IsTrue(realMaterial.HasProperty(p.Names)); Assert.IsTrue(dictTextures.ContainsKey(texturePath)); Assert.IsNotNull(realMaterial.GetTexture(p.Names)); Assert.AreEqual(dictTextures[texturePath].resourceObject.Unity3dObject.GetInstanceID(), realMaterial.GetTexture(p.Names).GetInstanceID()); Assert.AreEqual(offset.x, t.Offset.X); Assert.AreEqual(offset.y, t.Offset.Y); Assert.AreEqual(scale.x, t.Scale.X); Assert.AreEqual(scale.y, t.Scale.Y); } break; } } }
public static void processBatch() { long totalProcessTime = 0; List <string> configExport = new List <string>(); foreach (TextureSet set in TexturesUnlimitedLoader.loadedTextureSets.Values) { if (set.supportsRecoloring) { MonoBehaviour.print("Processing texture mask generation for texture set: " + set.name); Stopwatch sw = new Stopwatch(); sw.Start(); try { int generatedTextures = 0; int setIndex = 0; foreach (TextureSetMaterialData mat in set.textureData) { float[] diff = new float[3]; float[] met = new float[3]; float[] smth = new float[3]; MaskData data; ShaderPropertyTexture maskProp = Array.Find(mat.shaderProperties, m => m.name == "_MaskTex") as ShaderPropertyTexture; if (maskProp == null) { continue; } //no mask property; Texture2D maskTex = GameDatabase.Instance.GetTexture(maskProp.textureName, false); if (maskTex == null) { continue; } //No mask texture ShaderPropertyTexture mainProp = Array.Find(mat.shaderProperties, m => m.name == "_MainTex") as ShaderPropertyTexture; ShaderPropertyKeyword stockSpecKeyProp = Array.Find(mat.shaderProperties, m => m.name == "TU_STOCK_SPEC") as ShaderPropertyKeyword; if (mainProp != null) { Texture2D diffTex = GameDatabase.Instance.GetTexture(mainProp.textureName, false); if (diffTex != null) { data = processTextures(diffTex, maskTex, set.name + "-" + setIndex + "-diffuse-normalization.png", true, false, false, false, false); diff = new float[] { (float)data.r, (float)data.g, (float)data.b }; generatedTextures++; if (stockSpecKeyProp != null) { data = processTextures(diffTex, maskTex, set.name + "-" + setIndex + "-smooth-normalization.png", false, false, false, false, true); smth = new float[] { (float)data.r, (float)data.g, (float)data.b }; generatedTextures++; } } } ShaderPropertyTexture metalProp = Array.Find(mat.shaderProperties, m => m.name == "_MetallicGlossMap") as ShaderPropertyTexture; if (metalProp != null) { Texture2D metalTex = GameDatabase.Instance.GetTexture(metalProp.textureName, false); if (metalTex != null) { data = processTextures(metalTex, maskTex, set.name + "-" + setIndex + "-metal-normalization.png", false, true, false, false, false); met = new float[] { (float)data.r, (float)data.g, (float)data.b }; generatedTextures++; } } ShaderPropertyTexture specPropLegacy = Array.Find(mat.shaderProperties, m => m.name == "_SpecMap") as ShaderPropertyTexture; //metal from spec.a, smooth from spec.rgb luminance if ((mat.shader == "SSTU/PBR/Masked" || mat.shader == "SSTU/Masked") && specPropLegacy != null) { Texture2D specTex = GameDatabase.Instance.GetTexture(specPropLegacy.textureName, false); if (specTex != null) { data = processTextures(specTex, maskTex, set.name + "-" + setIndex + "-metal-normalization.png", false, false, false, false, true); met = new float[] { (float)data.r, (float)data.g, (float)data.b }; generatedTextures++; data = processTextures(specTex, maskTex, set.name + "-" + setIndex + "-smooth-normalization.png", true, false, false, false, false); smth = new float[] { (float)data.r, (float)data.g, (float)data.b }; generatedTextures++; convertTextures(specTex, specPropLegacy.textureName + ".png"); } } //MonoBehaviour.print("CONFIG EXPORT START---------------"); string patch = "\n@KSP_TEXTURE_SET[" + set.name + "]" + "\n{" + "\n @MATERIAL," + setIndex + "\n {" + "\n vector = _DiffuseNorm," + diff[0] + "," + diff[1] + "," + diff[2] + "\n vector = _SmoothnessNorm," + smth[0] + "," + smth[1] + "," + smth[2] + "\n vector = _MetalNorm," + met[0] + "," + met[1] + "," + met[2] + "\n }" + "\n}"; MonoBehaviour.print(patch); //MonoBehaviour.print("CONFIG EXPORT END------------------"); configExport.Add(patch); setIndex++; } sw.Stop(); totalProcessTime += sw.ElapsedMilliseconds; MonoBehaviour.print("elapsed: " + sw.ElapsedMilliseconds + " generated: " + generatedTextures + " masks for set"); } catch (Exception e) { MonoBehaviour.print(e); } } } string file = "NormalizationDataExport.txt"; string path = System.IO.Path.GetFullPath(file); MonoBehaviour.print("Exporting configs to: " + path); System.IO.File.WriteAllLines(file, configExport.ToArray()); MonoBehaviour.print("Total process time: " + totalProcessTime); }
public static ByteBuffer Save(UnityEngine.Material material) { FlatBufferBuilder builder = new FlatBufferBuilder(InitBufferSize); int count = ShaderUtil.GetPropertyCount(material.shader); List <Offset <ShaderProperty> > listOffShaderProperties = new List <Offset <ShaderProperty> >(); for (int i = 0; i < count; i++) { string name = ShaderUtil.GetPropertyName(material.shader, i); ShaderUtil.ShaderPropertyType type = ShaderUtil.GetPropertyType(material.shader, i); Schema.ShaderPropertyValue valueType = ShaderPropertyValue.NONE; int valueOffset = -1; switch (type) { case ShaderUtil.ShaderPropertyType.Color: { UnityEngine.Color c = material.GetColor(name); ShaderPropertyColor.StartShaderPropertyColor(builder); ShaderPropertyColor.AddColor(builder, Schema.Color.CreateColor(builder, c.a, c.b, c.g, c.r)); Offset <ShaderPropertyColor> offset = ShaderPropertyColor.EndShaderPropertyColor(builder); valueType = ShaderPropertyValue.ShaderPropertyColor; valueOffset = offset.Value; } break; case ShaderUtil.ShaderPropertyType.Vector: { Vector4 v = material.GetVector(name); ShaderPropertyVector.StartShaderPropertyVector(builder); ShaderPropertyVector.AddVector(builder, Schema.Vec4.CreateVec4(builder, v.x, v.y, v.z, v.w)); Offset <ShaderPropertyVector> offset = ShaderPropertyVector.EndShaderPropertyVector(builder); valueType = ShaderPropertyValue.ShaderPropertyVector; valueOffset = offset.Value; } break; case ShaderUtil.ShaderPropertyType.Range: case ShaderUtil.ShaderPropertyType.Float: { float f = material.GetFloat(name); ShaderPropertyFloat.StartShaderPropertyFloat(builder); ShaderPropertyFloat.AddValue(builder, f); Offset <ShaderPropertyFloat> offset = ShaderPropertyFloat.EndShaderPropertyFloat(builder); valueType = ShaderPropertyValue.ShaderPropertyFloat; valueOffset = offset.Value; } break; case ShaderUtil.ShaderPropertyType.TexEnv: { UnityEngine.Texture t = material.GetTexture(name); string textureName = "$NULL_TEXTURE"; if (t != null) { textureName = AssetDatabase.GetAssetPath(t.GetInstanceID()); if (string.IsNullOrEmpty(textureName)) { textureName = t.name; } else { textureName = textureName.Substring(ArtWork.path.Length); textureName = System.IO.Path.GetDirectoryName(textureName) + "/" + System.IO.Path.GetFileNameWithoutExtension(textureName); } } Vector2 toffset = material.GetTextureOffset(name); Vector2 tscale = material.GetTextureScale(name); StringOffset pathOffset = builder.CreateString(textureName); ShaderPropertyTexture.StartShaderPropertyTexture(builder); ShaderPropertyTexture.AddName(builder, pathOffset); ShaderPropertyTexture.AddOffset(builder, Vec2.CreateVec2(builder, toffset.x, toffset.y)); ShaderPropertyTexture.AddScale(builder, Vec2.CreateVec2(builder, tscale.x, tscale.y)); Offset <ShaderPropertyTexture> offset = ShaderPropertyTexture.EndShaderPropertyTexture(builder); valueType = ShaderPropertyValue.ShaderPropertyTexture; valueOffset = offset.Value; } break; } if (valueOffset >= 0) { listOffShaderProperties.Add( ShaderProperty.CreateShaderProperty( builder, builder.CreateString(name), (Schema.ShaderPropertyType)type, valueType, valueOffset )); } } StringOffset offMaterialName = builder.CreateString(material.name); StringOffset offShader = builder.CreateString(material.shader.name); Offset <Schema.Material> offMaterial = Schema.Material.CreateMaterial( builder, offMaterialName, offShader, Schema.Material.CreatePropertiesVector(builder, listOffShaderProperties.ToArray())); builder.Finish(offMaterial.Value); return(builder.DataBuffer); }
public void EqualSource() { Assert.AreEqual(material.Name, originMaterial.name); Assert.AreEqual(material.Shader, originMaterial.shader.name); for (int i = 0; i < material.PropertiesLength; i++) { Schema.ShaderProperty p = material.GetProperties(i); Assert.IsTrue(originMaterial.HasProperty(p.Names)); switch (p.Type) { case ShaderPropertyType.Float: case ShaderPropertyType.Range: { Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyFloat); float originValue = originMaterial.GetFloat(p.Names); ShaderPropertyFloat f = p.GetValue <ShaderPropertyFloat>(new ShaderPropertyFloat()); Assert.AreEqual(f.Value, originValue); } break; case ShaderPropertyType.Color: { Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyColor); UnityEngine.Color originValue = originMaterial.GetColor(p.Names); ShaderPropertyColor c = p.GetValue <ShaderPropertyColor>(new ShaderPropertyColor()); Assert.AreEqual(originValue.a, c.Color.A); Assert.AreEqual(originValue.g, c.Color.G); Assert.AreEqual(originValue.b, c.Color.B); Assert.AreEqual(originValue.r, c.Color.R); } break; case ShaderPropertyType.Vector: { Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyVector); UnityEngine.Vector4 originValue = originMaterial.GetVector(p.Names); ShaderPropertyVector v = p.GetValue <ShaderPropertyVector>(new ShaderPropertyVector()); Assert.AreEqual(originValue.x, v.Vector.X); Assert.AreEqual(originValue.y, v.Vector.Y); Assert.AreEqual(originValue.z, v.Vector.Z); Assert.AreEqual(originValue.w, v.Vector.W); } break; case ShaderPropertyType.TexEnv: { Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyTexture); UnityEngine.Texture texture = originMaterial.GetTexture(p.Names); Vector2 offset = originMaterial.GetTextureOffset(p.Names); Vector2 scale = originMaterial.GetTextureScale(p.Names); Assert.IsFalse(texture == null); ShaderPropertyTexture t = p.GetValue <ShaderPropertyTexture>(new ShaderPropertyTexture()); Assert.AreEqual(texture.name, t.Name); Assert.AreEqual(offset.x, t.Offset.X); Assert.AreEqual(offset.y, t.Offset.Y); Assert.AreEqual(scale.x, t.Scale.X); Assert.AreEqual(scale.y, t.Scale.Y); } break; } } }