Ejemplo n.º 1
0
 protected override void SetShaderModeBlend(Schema.Material gltfMaterial, Material material)
 {
     material.SetOverrideTag(TAG_RENDER_TYPE, TAG_RENDER_TYPE_TRANSPARENT);
     material.EnableKeyword(KW_SURFACE_TYPE_TRANSPARENT);
     material.EnableKeyword(KW_DISABLE_SSR_TRANSPARENT);
     material.EnableKeyword(KW_ENABLE_FOG_ON_TRANSPARENT);
     material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPrepass, false);
     material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPostpass, false);
     material.SetShaderPassEnabled(k_ShaderPassTransparentBackface, false);
     material.SetShaderPassEnabled(k_ShaderPassRayTracingPrepass, false);
     material.SetShaderPassEnabled(k_ShaderPassDepthOnlyPass, false);
     material.SetFloat(srcBlendPropId, (int)BlendMode.SrcAlpha);                //5
     material.SetFloat(dstBlendPropId, (int)BlendMode.OneMinusSrcAlpha);        //10
     material.SetFloat(k_ZTestGBufferPropId, (int)CompareFunction.Equal);       //3
     material.SetFloat(k_AlphaDstBlendPropId, (int)BlendMode.OneMinusSrcAlpha); //10
     material.SetFloat(k_Surface, 1);
     material.SetFloat(zWritePropId, 0);
 }
Ejemplo n.º 2
0
        public UnityEngine.Material ToUnity(Scene scene, Utils.Progress progress = null)
        {
            if (unityMaterial == null)
            {
                // Get the material shader
                Shader unity_shader = GetUnityShader(shader);

                if (unity_shader != null)
                {
                    // Create a new material with the selected shader
                    unityMaterial = new UnityEngine.Material(unity_shader);

                    // Set material options
                    unityMaterial.name                    = name;
                    unityMaterial.renderQueue             = renderQueue;
                    unityMaterial.hideFlags               = hideFlags;
                    unityMaterial.globalIlluminationFlags = globalIlluminationFlags;
                    unityMaterial.shaderKeywords          = keywords;

                    // Activate required shader passes
                    if (passes != null)
                    {
                        for (int i = 0; i < passes.Length; i++)
                        {
#if UNITY_5_6_OR_NEWER
                            unityMaterial.SetShaderPassEnabled(unityMaterial.GetPassName(i), passes[i]);
#endif
                        }
                    }

                    // Set material properties
                    ToUnityMaterialProperties <int, int>(unityMaterial, ints, (m, p, v) => m.SetInt(p, v));
                    ToUnityMaterialProperties <float, float>(unityMaterial, floats, (m, p, v) => m.SetFloat(p, v));
                    ToUnityMaterialProperties <Vector3, Vector3>(unityMaterial, vectors, (m, p, v) => m.SetVector(p, v));
                    ToUnityMaterialProperties <Color, Color>(unityMaterial, colors, (m, p, v) => m.SetColor(p, v));
                    ToUnityMaterialProperties <TextureParams, UnityEngine.Texture>(unityMaterial, textures, (m, p, v) =>
                    {
                        m.SetTexture(p, scene.textures[v.index].ToUnity(progress));
                        m.SetTextureOffset(p, v.offset);
                        m.SetTextureScale(p, v.scale);
                    });

                    // Set materials missing properties / keywords if material is imported from assimp
                    if ((keywords == null || keywords.Length <= 0) && shader == Constants.defaultAssimpShader)
                    {
                        CLARTE.Shaders.Standard.Utility.MaterialChanged(unityMaterial);
                    }
                }

                if (progress != null)
                {
                    progress.Update(1);
                }
            }

            return(unityMaterial);
        }
        private static void ApplyBlendMode(UnityEngine.Material unityMaterial, BlendMode blendMode)
        {
            // disable shader passes
            unityMaterial.SetShaderPassEnabled("DistortionVectors", false);
            unityMaterial.SetShaderPassEnabled("MOTIONVECTORS", false);
            unityMaterial.SetShaderPassEnabled("TransparentDepthPrepass", false);
            unityMaterial.SetShaderPassEnabled("TransparentDepthPostpass", false);
            unityMaterial.SetShaderPassEnabled("TransparentBackface", false);

            // reset existing blend modes, will be enabled explicitly
            unityMaterial.DisableKeyword("_BLENDMODE_ALPHA");
            unityMaterial.DisableKeyword("_BLENDMODE_ADD");
            unityMaterial.DisableKeyword("_BLENDMODE_PRE_MULTIPLY");

            switch (blendMode)
            {
            case Engine.VPT.BlendMode.Opaque:

                // required for the blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);

                // properties
                unityMaterial.SetFloat(SurfaceType, 0);                         // 0 = Opaque; 1 = Transparent
                unityMaterial.SetFloat(AlphaCutoffEnable, 0);

                // render queue
                unityMaterial.renderQueue = -1;

                break;

            case Engine.VPT.BlendMode.Cutout:

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "TransparentCutout");

                // keywords
                unityMaterial.EnableKeyword("_ALPHATEST_ON");
                unityMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");

                // required for the blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);

                // properties
                unityMaterial.SetFloat(SurfaceType, 0);                         // 0 = Opaque; 1 = Transparent
                unityMaterial.SetFloat(AlphaCutoffEnable, 1);

                unityMaterial.SetFloat(ZTestDepthEqualForOpaque, 3);
                unityMaterial.SetFloat(ZTestModeDistortion, 4);
                unityMaterial.SetFloat(ZTestGBuffer, 3);

                // render queue
                unityMaterial.renderQueue = 2450;

                break;

            case Engine.VPT.BlendMode.Translucent:

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "Transparent");

                // keywords
                //unityMaterial.EnableKeyword("_ALPHATEST_ON"); // required for _AlphaCutoffEnable
                unityMaterial.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING");
                unityMaterial.EnableKeyword("_BLENDMODE_PRE_MULTIPLY");
                unityMaterial.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT");
                unityMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");
                unityMaterial.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");

                // required for the blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                unityMaterial.SetInt(ZWrite, 0);

                // properties
                unityMaterial.SetFloat(SurfaceType, 1);                       // 0 = Opaque; 1 = Transparent
                //unityMaterial.SetFloat("_AlphaCutoffEnable", 1); // enable keyword _ALPHATEST_ON if this is required
                unityMaterial.SetFloat(BlendMode, 4);                         // 0 = Alpha, 1 = Additive, 4 = PreMultiply

                // render queue
                const int transparentRenderQueueBase = 3000;
                const int transparentSortingPriority = 0;
                unityMaterial.SetInt(TransparentSortPriority, transparentSortingPriority);
                unityMaterial.renderQueue = transparentRenderQueueBase + transparentSortingPriority;

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 4
0
        private void ApplyBlendMode(UnityEngine.Material unityMaterial, BlendMode blendMode)
        {
            switch (blendMode)
            {
            case BlendMode.Opaque:

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "Opaque");

                // required for the blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);

                // properties
                unityMaterial.SetFloat(Surface, 0);                         // 0 = Opaque; 1 = Transparent

                // render queue
                unityMaterial.renderQueue = -1;

                break;

            case BlendMode.Cutout:

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "TransparentCutout");

                // keywords
                unityMaterial.EnableKeyword("_ALPHATEST_ON");

                // required for blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);

                // properties
                unityMaterial.SetFloat(Surface, 0);                         // 0 = Opaque; 1 = Transparent
                unityMaterial.SetInt(AlphaClip, 1);

                // render queue
                unityMaterial.renderQueue = 2450;

                break;

            case BlendMode.Translucent:

                // disable shader passes
                unityMaterial.SetShaderPassEnabled("SHADOWCASTER", false);

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "Transparent");

                // keywords
                unityMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");

                // required for blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                unityMaterial.SetInt(ZWrite, 0);

                // properties
                unityMaterial.SetFloat(Surface, 1);                       // 0 = Opaque; 1 = Transparent
                unityMaterial.SetFloat(Blend, 1);                         // 0 = Alpha, 1 = Premultiply, 2 = Additive, 3 = Multiply

                // render queue
                const int transparentRenderQueueBase = 3000;
                const int transparentSortingPriority = 0;
                unityMaterial.SetInt(QueueOffset, transparentSortingPriority);
                unityMaterial.renderQueue = transparentRenderQueueBase + transparentSortingPriority;

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }