/// <summary> /// <para>Get the special ZWrite material used to address the Order /// Independent Transparency (OIT) problem when using URP /// (Universal Render Pipeline). For background about the OIT /// problem, see: /// https://forum.unity.com/threads/render-mode-transparent-doesnt-work-see-video.357853/#post-2315934.</para> /// <para>The ZWrite material writes only to the Z-buffer /// (a.k.a. depth buffer) and not to the RGBA framebuffer like /// a normal shader would. With the built-in render pipeline, /// we can do the Z-write-only pass by adding a (preliminary) /// pass to the shader that renders the mesh. However, since URP /// only supports single-pass shaders, we must instead /// emulate two shader passes by assigning two materials to /// the mesh.</para> /// <para>Note!: This method must be called after populating /// the `Materials` array with all of the materials /// from the glTF file. Otherwise, the indices in the /// `Materials` array will not match the material indices /// in the glTF file, and the importer will assign the wrong /// materials to the meshes.</para> /// </summary> /// <param name="create"> /// create the ZWrite material if it does not already exist /// </param> public Material GetZWriteMaterial(bool create) { if (ZWriteMaterialIndex < 0) { if (!create) { return(null); } var pipeline = RenderPipelineUtil.GetRenderPipeline(true); if (pipeline != RenderPipelineType.URP) { throw new Exception("ZWrite material can only be used with URP"); } ZWriteMaterialIndex = Materials.Count; var shader = Shader.Find("Piglet/URPZWrite"); var zwrite = new Material(shader) { name = "zwrite" }; Materials.Add(zwrite); } return(Materials[ZWriteMaterialIndex]); }
/// <summary> /// <para>Get the default material, which is used whenever /// a mesh is not explicitly assigned a material.</para> /// <para>Note!: This method must be called after populating /// the `Materials` array with all of the materials /// from the glTF file. Otherwise, the indices in the /// `Materials` array will not match the material indices /// in the glTF file, and the importer will assign the wrong /// materials to the meshes.</para> /// </summary> /// <param name="create"> /// create the default material if it does not already exist /// </param> public Material GetDefaultMaterial(bool create) { if (DefaultMaterialIndex < 0) { if (!create) { return(null); } string shaderName; var pipeline = RenderPipelineUtil.GetRenderPipeline(true); switch (pipeline) { case RenderPipelineType.BuiltIn: shaderName = "Piglet/MetallicRoughnessOpaque"; break; case RenderPipelineType.URP: shaderName = "Shader Graphs/URPMetallicRoughnessOpaque"; break; default: throw new Exception("current render pipeline unsupported, " + " GetRenderPipeline should have thrown exception"); } Shader shader = Shader.Find(shaderName); if (shader == null) { if (pipeline == RenderPipelineType.URP) { throw new Exception(String.Format( "Piglet failed to load URP shader \"{0}\". Please ensure that " + "you have installed the URP shaders from the appropriate .unitypackage " + "in Assets/Piglet/Extras, and that the shaders are being included " + "your build.", shaderName)); } throw new Exception(String.Format( "Piglet failed to load shader \"{0}\". Please ensure that " + "this shader is being included your build.", shaderName)); } DefaultMaterialIndex = Materials.Count; var material = new Material(shader) { name = "default" }; Materials.Add(material); } return(Materials[DefaultMaterialIndex]); }