// All Setup Keyword functions must be static. It allow to create script to automatically update the shaders with a script if code change static new public void SetupMaterialKeywordsAndPass(Material material) { SetupBaseLitKeywords(material); SetupBaseLitMaterialPass(material); SetupLayersMappingKeywords(material); for (int i = 0; i < kMaxLayerCount; ++i) { NormalMapSpace normalMapSpace = ((NormalMapSpace)material.GetFloat(kNormalMapSpace + i)); SetKeyword(material, "_NORMALMAP_TANGENT_SPACE" + i, normalMapSpace == NormalMapSpace.TangentSpace); if (normalMapSpace == NormalMapSpace.TangentSpace) { SetKeyword(material, "_NORMALMAP" + i, material.GetTexture(kNormalMap + i) || material.GetTexture(kDetailMap + i)); SetKeyword(material, "_BENTNORMALMAP" + i, material.GetTexture(kBentNormalMap + i)); } else { SetKeyword(material, "_NORMALMAP" + i, material.GetTexture(kNormalMapOS + i) || material.GetTexture(kDetailMap + i)); SetKeyword(material, "_BENTNORMALMAP" + i, material.GetTexture(kBentNormalMapOS + i)); } SetKeyword(material, "_MASKMAP" + i, material.GetTexture(kMaskMap + i)); SetKeyword(material, "_DETAIL_MAP" + i, material.GetTexture(kDetailMap + i)); SetKeyword(material, "_HEIGHTMAP" + i, material.GetTexture(kHeightMap + i)); SetKeyword(material, "_SUBSURFACE_RADIUS_MAP" + i, material.GetTexture(kSubsurfaceRadiusMap + i)); SetKeyword(material, "_THICKNESSMAP" + i, material.GetTexture(kThicknessMap + i)); } SetKeyword(material, "_INFLUENCEMASK_MAP", material.GetTexture(kLayerInfluenceMaskMap) && material.GetFloat(kkUseMainLayerInfluence) != 0.0f); SetKeyword(material, "_EMISSIVE_COLOR_MAP", material.GetTexture(kEmissiveColorMap)); SetKeyword(material, "_ENABLESPECULAROCCLUSION", material.GetFloat(kEnableSpecularOcclusion) > 0.0f); SetKeyword(material, "_MAIN_LAYER_INFLUENCE_MODE", material.GetFloat(kkUseMainLayerInfluence) != 0.0f); VertexColorMode VCMode = (VertexColorMode)material.GetFloat(kVertexColorMode); if (VCMode == VertexColorMode.Multiply) { SetKeyword(material, "_LAYER_MASK_VERTEX_COLOR_MUL", true); SetKeyword(material, "_LAYER_MASK_VERTEX_COLOR_ADD", false); } else if (VCMode == VertexColorMode.Add) { SetKeyword(material, "_LAYER_MASK_VERTEX_COLOR_MUL", false); SetKeyword(material, "_LAYER_MASK_VERTEX_COLOR_ADD", true); } else { SetKeyword(material, "_LAYER_MASK_VERTEX_COLOR_MUL", false); SetKeyword(material, "_LAYER_MASK_VERTEX_COLOR_ADD", false); } bool useHeightBasedBlend = material.GetFloat(kUseHeightBasedBlend) != 0.0f; SetKeyword(material, "_HEIGHT_BASED_BLEND", useHeightBasedBlend); bool useDensityModeEnable = false; for (int i = 0; i < material.GetInt(kLayerCount); ++i) { useDensityModeEnable |= material.GetFloat(kOpacityAsDensity + i) != 0.0f; } SetKeyword(material, "_DENSITY_MODE", useDensityModeEnable); Lit.MaterialId materialId = (Lit.MaterialId)material.GetFloat(kMaterialID); SetKeyword(material, "_MATID_SSS", materialId == Lit.MaterialId.LitSSS); //SetKeyword(material, "_MATID_STANDARD", materialId == Lit.MaterialId.LitStandard); // See comment in Lit.shader, it is the default, we don't define it }
// All Setup Keyword functions must be static. It allow to create script to automatically update the shaders with a script if code change static public void SetupMaterialKeywordsAndPass(Material material) { SetupBaseLitKeywords(material); SetupBaseLitMaterialPass(material); NormalMapSpace normalMapSpace = (NormalMapSpace)material.GetFloat(kNormalMapSpace); // Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material animation // (MaterialProperty value might come from renderer material property block) SetKeyword(material, "_MAPPING_PLANAR", ((UVBaseMapping)material.GetFloat(kUVBase)) == UVBaseMapping.Planar); SetKeyword(material, "_MAPPING_TRIPLANAR", ((UVBaseMapping)material.GetFloat(kUVBase)) == UVBaseMapping.Triplanar); SetKeyword(material, "_NORMALMAP_TANGENT_SPACE", (normalMapSpace == NormalMapSpace.TangentSpace)); if (normalMapSpace == NormalMapSpace.TangentSpace) { // With details map, we always use a normal map and Unity provide a default (0, 0, 1) normal map for it SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap) || material.GetTexture(kDetailMap)); SetKeyword(material, "_TANGENTMAP", material.GetTexture(kTangentMap)); SetKeyword(material, "_BENTNORMALMAP", material.GetTexture(kBentNormalMap)); } else // Object space { // With details map, we always use a normal map but in case of objects space there is no good default, so the result will be weird until users fix it SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMapOS) || material.GetTexture(kDetailMap)); SetKeyword(material, "_TANGENTMAP", material.GetTexture(kTangentMapOS)); SetKeyword(material, "_BENTNORMALMAP", material.GetTexture(kBentNormalMapOS)); } SetKeyword(material, "_MASKMAP", material.GetTexture(kMaskMap)); SetKeyword(material, "_EMISSIVE_COLOR_MAP", material.GetTexture(kEmissiveColorMap)); SetKeyword(material, "_ENABLESPECULAROCCLUSION", material.GetFloat(kEnableSpecularOcclusion) > 0.0f); SetKeyword(material, "_HEIGHTMAP", material.GetTexture(kHeightMap)); SetKeyword(material, "_ANISOTROPYMAP", material.GetTexture(kAnisotropyMap)); SetKeyword(material, "_DETAIL_MAP", material.GetTexture(kDetailMap)); SetKeyword(material, "_SUBSURFACE_RADIUS_MAP", material.GetTexture(kSubsurfaceRadiusMap)); SetKeyword(material, "_THICKNESSMAP", material.GetTexture(kThicknessMap)); SetKeyword(material, "_SPECULARCOLORMAP", material.GetTexture(kSpecularColorMap)); bool needUV2 = (UVDetailMapping)material.GetFloat(kUVDetail) == UVDetailMapping.UV2 && (UVBaseMapping)material.GetFloat(kUVBase) == UVBaseMapping.UV0; bool needUV3 = (UVDetailMapping)material.GetFloat(kUVDetail) == UVDetailMapping.UV3 && (UVBaseMapping)material.GetFloat(kUVBase) == UVBaseMapping.UV0; if (needUV3) { material.DisableKeyword("_REQUIRE_UV2"); material.EnableKeyword("_REQUIRE_UV3"); } else if (needUV2) { material.EnableKeyword("_REQUIRE_UV2"); material.DisableKeyword("_REQUIRE_UV3"); } else { material.DisableKeyword("_REQUIRE_UV2"); material.DisableKeyword("_REQUIRE_UV3"); } Lit.MaterialId materialId = (Lit.MaterialId)material.GetFloat(kMaterialID); SetKeyword(material, "_MATID_SSS", materialId == Lit.MaterialId.LitSSS); //SetKeyword(material, "_MATID_STANDARD", materialId == Lit.MaterialId.LitStandard); // See comment in Lit.shader, it is the default, we don't define it SetKeyword(material, "_MATID_ANISO", materialId == Lit.MaterialId.LitAniso); SetKeyword(material, "_MATID_SPECULAR", materialId == Lit.MaterialId.LitSpecular); SetKeyword(material, "_MATID_CLEARCOAT", materialId == Lit.MaterialId.LitClearCoat); var refractionModeValue = (Lit.RefractionMode)material.GetFloat(kRefractionMode); // We can't have refraction in pre-refraction queue var canHaveRefraction = !material.HasProperty(kPreRefractionPass) || material.GetFloat(kPreRefractionPass) <= 0.0; SetKeyword(material, "_REFRACTION_PLANE", (refractionModeValue == Lit.RefractionMode.Plane) && canHaveRefraction); SetKeyword(material, "_REFRACTION_SPHERE", (refractionModeValue == Lit.RefractionMode.Sphere) && canHaveRefraction); SetKeyword(material, "_TRANSMITTANCECOLORMAP", material.GetTexture(kTransmittanceColorMap) && canHaveRefraction); }