public static void AllMaterialShadersAreNotBuiltin(string projectName, ShaderPathID shaderPathID) { string shaderPath = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(shaderPathID)); Assert.IsFalse(string.IsNullOrEmpty(shaderPath)); var shader = AssetDatabase.LoadAssetAtPath <Shader>(shaderPath); Assert.AreEqual(shader.name, ShaderUtils.GetShaderPath(shaderPathID)); var guids = AssetDatabase.FindAssets("t:Material"); foreach (var guid in guids) { var path = AssetDatabase.GUIDToAssetPath(guid); // We only care what is in assets folder if (!path.StartsWith("Assets")) { continue; } Material material = AssetDatabase.LoadAssetAtPath <Material>(path); Assert.AreNotEqual(material.shader, shader, $"Material ({path}) has excluded shader. {projectName} project should not have shader {shader.name}"); } }
public void ValidateShaderResources(ShaderPathID shaderPathID) { string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(shaderPathID)); Assert.IsFalse(string.IsNullOrEmpty(path)); var shader = AssetDatabase.LoadAssetAtPath <Shader>(path); Assert.AreEqual(shader.name, ShaderUtils.GetShaderPath(shaderPathID)); }
public void ValidateShaderResources(ShaderPathID shaderPathID) { string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(shaderPathID)); Assert.IsFalse(string.IsNullOrEmpty(path)); var shader = AssetDatabase.LoadAssetAtPath <Shader>(path); Assert.AreEqual(shader.name, ShaderUtils.GetShaderPath(shaderPathID)); var propertyNames = new System.Collections.Generic.HashSet <string>(); for (int j = 0; j < shader.GetPropertyCount(); ++j) { string propertyName = shader.GetPropertyName(j); Assert.IsFalse(propertyNames.Contains(propertyName), $"{shader.name} has duplicated property {propertyName}!"); propertyNames.Add(propertyName); } }
public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips) { var pipelineAsset = GraphicsSettings.currentRenderPipeline; if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset)) { return; } var lowerCasePath = Path.GetExtension(assetPath).ToLower(); if (lowerCasePath != ".skp") { return; } string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit)); var shader = AssetDatabase.LoadAssetAtPath <Shader>(path); if (shader == null) { return; } material.shader = shader; float floatProperty; Vector4 vectorProperty; TexturePropertyDescription textureProperty; if (description.TryGetProperty("DiffuseMap", out textureProperty) && textureProperty.texture != null) { SetMaterialTextureProperty("_BaseMap", material, textureProperty); SetMaterialTextureProperty("_MainTex", material, textureProperty); var color = new Color(1.0f, 1.0f, 1.0f, 1.0f); material.SetColor("_BaseColor", color); material.SetColor("_Color", color); } else if (description.TryGetProperty("DiffuseColor", out vectorProperty)) { Color diffuseColor = vectorProperty; diffuseColor = PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor; material.SetColor("_BaseColor", diffuseColor); material.SetColor("_Color", diffuseColor); } if (description.TryGetProperty("IsTransparent", out floatProperty) && floatProperty == 1.0f) { material.SetFloat("_Mode", (float)3.0); // From C# enum BlendMode material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; material.SetInt("_Surface", 1); } else { material.SetFloat("_Mode", (float)0.0); // From C# enum BlendMode material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; material.SetInt("_Surface", 0); } }
public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips) { var pipelineAsset = GraphicsSettings.currentRenderPipeline; if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset)) { return; } var lowerCasePath = Path.GetExtension(assetPath).ToLower(); if (lowerCasePath != ".3ds") { return; } string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit)); var shader = AssetDatabase.LoadAssetAtPath <Shader>(path); if (shader == null) { return; } material.shader = shader; TexturePropertyDescription textureProperty; float floatProperty; Vector4 vectorProperty; description.TryGetProperty("diffuse", out vectorProperty); vectorProperty.x /= 255.0f; vectorProperty.y /= 255.0f; vectorProperty.z /= 255.0f; vectorProperty.w /= 255.0f; description.TryGetProperty("transparency", out floatProperty); bool isTransparent = vectorProperty.w <= 0.99f || floatProperty > .0f; if (isTransparent) { material.SetFloat("_Mode", 3.0f); // From C# enum BlendMode material.SetOverrideTag("RenderType", "Transparent"); material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One); material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetFloat("_ZWrite", 0.0f); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; material.SetFloat("_Surface", 1.0f); material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); } else { material.SetFloat("_Mode", 0.0f); // From C# enum BlendMode material.SetOverrideTag("RenderType", ""); material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One); material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.Zero); material.SetFloat("_ZWrite", 1.0f); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; material.SetFloat("_Surface", 0.0f); material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT"); } if (floatProperty > .0f) { vectorProperty.w = 1.0f - floatProperty; } Color diffuseColor = vectorProperty; material.SetColor("_Color", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor); material.SetColor("_BaseColor", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor); if (description.TryGetProperty("EmissiveColor", out vectorProperty)) { material.SetColor("_EmissionColor", vectorProperty); material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive; material.EnableKeyword("_EMISSION"); } if (description.TryGetProperty("texturemap1", out textureProperty)) { SetMaterialTextureProperty("_BaseMap", material, textureProperty); } if (description.TryGetProperty("bumpmap", out textureProperty)) { if (material.HasProperty("_BumpMap")) { SetMaterialTextureProperty("_BumpMap", material, textureProperty); material.EnableKeyword("_NORMALMAP"); } } }
public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips) { var pipelineAsset = GraphicsSettings.currentRenderPipeline; if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset)) { return; } var lowerCaseExtension = Path.GetExtension(assetPath).ToLower(); if (lowerCaseExtension != ".fbx" && lowerCaseExtension != ".obj" && lowerCaseExtension != ".blend" && lowerCaseExtension != ".mb" && lowerCaseExtension != ".ma" && lowerCaseExtension != ".max") { return; } string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit)); var shader = AssetDatabase.LoadAssetAtPath <Shader>(path); if (shader == null) { return; } material.shader = shader; Vector4 vectorProperty; float floatProperty; TexturePropertyDescription textureProperty; bool isTransparent = false; float opacity; float transparencyFactor; if (!description.TryGetProperty("Opacity", out opacity)) { if (description.TryGetProperty("TransparencyFactor", out transparencyFactor)) { opacity = transparencyFactor == 1.0f ? 1.0f : 1.0f - transparencyFactor; } if (opacity == 1.0f && description.TryGetProperty("TransparentColor", out vectorProperty)) { opacity = vectorProperty.x == 1.0f ? 1.0f : 1.0f - vectorProperty.x; } } if (opacity < 1.0f || (opacity == 1.0f && description.TryGetProperty("TransparentColor", out textureProperty))) { isTransparent = true; } else if (description.HasAnimationCurve("TransparencyFactor") || description.HasAnimationCurve("TransparentColor")) { isTransparent = true; } if (isTransparent) { material.SetInt("_Mode", 3); material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; material.SetInt("_Surface", 1); } else { material.SetInt("_Mode", 0); material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; material.SetInt("_Surface", 0); } if (description.TryGetProperty("DiffuseColor", out textureProperty) && textureProperty.texture != null) { Color diffuseColor = new Color(1.0f, 1.0f, 1.0f, 1.0f); if (description.TryGetProperty("DiffuseFactor", out floatProperty)) { diffuseColor *= floatProperty; } diffuseColor.a = opacity; SetMaterialTextureProperty("_BaseMap", material, textureProperty); material.SetColor("_BaseColor", diffuseColor); } else if (description.TryGetProperty("DiffuseColor", out vectorProperty)) { Color diffuseColor = vectorProperty; diffuseColor.a = opacity; material.SetColor("_BaseColor", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor); } if (description.TryGetProperty("Bump", out textureProperty)) { SetMaterialTextureProperty("_BumpMap", material, textureProperty); material.EnableKeyword("_NORMALMAP"); if (description.TryGetProperty("BumpFactor", out floatProperty)) { material.SetFloat("_BumpScale", floatProperty); } } else if (description.TryGetProperty("NormalMap", out textureProperty)) { SetMaterialTextureProperty("_BumpMap", material, textureProperty); material.EnableKeyword("_NORMALMAP"); if (description.TryGetProperty("BumpFactor", out floatProperty)) { material.SetFloat("_BumpScale", floatProperty); } } if (description.TryGetProperty("EmissiveColor", out textureProperty)) { Color emissiveColor = new Color(1.0f, 1.0f, 1.0f, 1.0f); material.SetColor("_EmissionColor", emissiveColor); SetMaterialTextureProperty("_EmissionMap", material, textureProperty); if (description.TryGetProperty("EmissiveFactor", out floatProperty) && floatProperty > 0.0f) { material.EnableKeyword("_EMISSION"); material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive; } } else if ( description.TryGetProperty("EmissiveColor", out vectorProperty) && vectorProperty.magnitude > vectorProperty.w || description.HasAnimationCurve("EmissiveColor.x")) { if (description.TryGetProperty("EmissiveFactor", out floatProperty)) { vectorProperty *= floatProperty; } material.SetColor("_EmissionColor", vectorProperty); if (floatProperty > 0.0f) { material.EnableKeyword("_EMISSION"); material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive; } } material.SetFloat("_Glossiness", 0.0f); if (PlayerSettings.colorSpace == ColorSpace.Linear) { RemapAndTransformColorCurves(description, clips, "DiffuseColor", "_BaseColor", ConvertFloatLinearToGamma); } else { RemapColorCurves(description, clips, "DiffuseColor", "_BaseColor"); } RemapTransparencyCurves(description, clips); RemapColorCurves(description, clips, "EmissiveColor", "_EmissionColor"); }