Example #1
0
    public override void ApplyMaterialPatch(Material material, MWAssets.Material patch)
    {
        base.ApplyMaterialPatch(material, patch);

        if (patch.AlphaCutoff != null)
        {
            material.SetFloat("_AlphaCutoff", patch.AlphaCutoff.Value);
        }

        switch (patch.AlphaMode)
        {
        case MWAssets.AlphaMode.Opaque:
            material.renderQueue = (int)RenderQueue.Geometry;
            material.SetOverrideTag("RenderMode", "Opaque");
            material.SetInt("_ZWrite", 1);
            material.SetInt("_SrcBlend", (int)BlendMode.One);
            material.SetInt("_DstBlend", (int)BlendMode.Zero);
            material.SetInt("_ShouldCutout", 0);
            break;

        case MWAssets.AlphaMode.Mask:
            material.renderQueue = (int)RenderQueue.AlphaTest;
            material.SetOverrideTag("RenderMode", "TransparentCutout");
            material.SetInt("_ZWrite", 1);
            material.SetInt("_SrcBlend", (int)BlendMode.One);
            material.SetInt("_DstBlend", (int)BlendMode.Zero);
            material.SetInt("_ShouldCutout", 1);
            break;

        case MWAssets.AlphaMode.Blend:
            material.renderQueue = (int)RenderQueue.Transparent;
            material.SetOverrideTag("RenderMode", "Transparent");
            material.SetInt("_ZWrite", 0);
            material.SetInt("_SrcBlend", (int)BlendMode.SrcAlpha);
            material.SetInt("_DstBlend", (int)BlendMode.OneMinusSrcAlpha);
            material.SetInt("_ShouldCutout", 0);
            break;
            // ignore default case, i.e. null
        }
    }
    public override void ApplyMaterialPatch(IMixedRealityExtensionApp app, Material material, MWAssets.Material patch)
    {
        base.ApplyMaterialPatch(app, material, patch);

        if (patch.EmissiveColor != null)
        {
            var color = material.GetColor(EmissiveColorProp);
            color.r = patch.EmissiveColor.R ?? color.r;
            color.g = patch.EmissiveColor.G ?? color.g;
            color.b = patch.EmissiveColor.B ?? color.b;
            color.a = patch.EmissiveColor.A ?? color.a;
            material.SetColor(EmissiveColorProp, color);
        }

        if (patch.EmissiveTextureOffset != null)
        {
            var offset = material.GetTextureOffset(EmissiveTexProp);
            offset.x = patch.EmissiveTextureOffset.X ?? offset.x;
            offset.y = patch.EmissiveTextureOffset.Y ?? offset.y;
            material.SetTextureOffset(EmissiveTexProp, offset);
        }

        if (patch.EmissiveTextureScale != null)
        {
            var scale = material.GetTextureScale(EmissiveTexProp);
            scale.x = patch.EmissiveTextureScale.X ?? scale.x;
            scale.y = patch.EmissiveTextureScale.Y ?? scale.y;
            material.SetTextureScale(EmissiveTexProp, scale);
        }

        if (patch.EmissiveTextureId != null)
        {
            var textureId = patch.EmissiveTextureId.Value;
            emissiveTextureAssignments[material.GetInstanceID()] = textureId;
            if (textureId == Guid.Empty)
            {
                material.SetTexture(EmissiveTexProp, null);
            }
            else
            {
                app.AssetManager.OnSet(textureId, tex =>
                {
                    if (!material || emissiveTextureAssignments[material.GetInstanceID()] != textureId)
                    {
                        return;
                    }
                    material.SetTexture(EmissiveTexProp, (Texture)tex.Asset);
                });
            }
        }

        if (patch.AlphaCutoff != null)
        {
            material.SetFloat("_AlphaCutoff", patch.AlphaCutoff.Value);
        }

        switch (patch.AlphaMode)
        {
        case MWAssets.AlphaMode.Opaque:
            material.renderQueue = (int)RenderQueue.Geometry;
            material.SetOverrideTag("RenderMode", "Opaque");
            material.SetInt("_ZWrite", 1);
            material.SetInt("_SrcBlend", (int)BlendMode.One);
            material.SetInt("_DstBlend", (int)BlendMode.Zero);
            material.SetInt("_ShouldCutout", 0);
            break;

        case MWAssets.AlphaMode.Mask:
            material.renderQueue = (int)RenderQueue.AlphaTest;
            material.SetOverrideTag("RenderMode", "TransparentCutout");
            material.SetInt("_ZWrite", 1);
            material.SetInt("_SrcBlend", (int)BlendMode.One);
            material.SetInt("_DstBlend", (int)BlendMode.Zero);
            material.SetInt("_ShouldCutout", 1);
            break;

        case MWAssets.AlphaMode.Blend:
            material.renderQueue = (int)RenderQueue.Transparent;
            material.SetOverrideTag("RenderMode", "Transparent");
            material.SetInt("_ZWrite", 0);
            material.SetInt("_SrcBlend", (int)BlendMode.SrcAlpha);
            material.SetInt("_DstBlend", (int)BlendMode.OneMinusSrcAlpha);
            material.SetInt("_ShouldCutout", 0);
            break;
            // ignore default case, i.e. null
        }
    }
Example #3
0
        /// <inheritdoc />
        public virtual void ApplyMaterialPatch(IMixedRealityExtensionApp app, Material material, MWMaterial patch)
        {
            if (patch.Color != null)
            {
                _materialColor.FromGodotColor(material.AlbedoColor);
                _materialColor.ApplyPatch(patch.Color);
                material.AlbedoColor = _materialColor.ToColor();
            }

            if (patch.MainTextureOffset != null)
            {
                _textureOffset.FromGodotVector3(material.Uv1Offset);
                _textureOffset.ApplyPatch(patch.MainTextureOffset);
                _textureOffset.Y  *= -1;
                material.Uv1Offset = _textureOffset.ToVector3();
            }

            if (patch.MainTextureScale != null)
            {
                _textureScale.FromGodotVector3(material.Uv1Scale);
                _textureScale.ApplyPatch(patch.MainTextureScale);
                material.Uv1Scale = _textureScale.ToVector3();
            }

            if (patch.MainTextureId != null)
            {
                var textureId = patch.MainTextureId.Value;
                mainTextureAssignments[material.GetInstanceId()] = textureId;
                if (patch.MainTextureId == Guid.Empty)
                {
                    material.AlbedoTexture = null;
                }
                else
                {
                    app.AssetManager.OnSet(textureId, tex =>
                    {
                        if (material == null || mainTextureAssignments[material.GetInstanceId()] != textureId)
                        {
                            return;
                        }
                        material.AlbedoTexture = (Texture)tex.Asset;
                    });
                }
            }

            if (patch.EmissiveColor != null)
            {
                material.EmissionEnabled = true;
                var color = material.Emission;
                color.r           = patch.EmissiveColor.R ?? color.r;
                color.g           = patch.EmissiveColor.G ?? color.g;
                color.b           = patch.EmissiveColor.B ?? color.b;
                color.a           = patch.EmissiveColor.A ?? color.a;
                material.Emission = color;
            }

            if (patch.EmissiveTextureOffset != null)
            {
                material.EmissionEnabled = true;
                var offset = material.Uv2Offset;
                offset.x           = patch.EmissiveTextureOffset.X ?? offset.x;
                offset.y           = patch.EmissiveTextureOffset.Y ?? offset.y;
                material.Uv2Offset = offset;
            }

            if (patch.EmissiveTextureScale != null)
            {
                material.EmissionEnabled = true;
                var scale = material.Uv2Scale;
                scale.x           = patch.EmissiveTextureScale.X ?? scale.x;
                scale.y           = patch.EmissiveTextureScale.Y ?? scale.y;
                material.Uv2Scale = scale;
            }

            if (patch.EmissiveTextureId != null)
            {
                material.EmissionEnabled = true;
                var textureId = patch.EmissiveTextureId.Value;
                emissiveTextureAssignments[material.GetInstanceId()] = textureId;
                if (textureId == Guid.Empty)
                {
                    material.EmissionTexture = null;
                }
                else
                {
                    app.AssetManager.OnSet(textureId, tex =>
                    {
                        if (material == null || emissiveTextureAssignments[material.GetInstanceId()] != textureId)
                        {
                            return;
                        }
                        material.EmissionTexture = (Texture)tex.Asset;
                    });
                }
            }

            if (patch.AlphaCutoff != null)
            {
                material.ParamsUseAlphaScissor       = true;
                material.ParamsAlphaScissorThreshold = patch.AlphaCutoff.Value;
            }

            switch (patch.AlphaMode)
            {
            case MWAssets.AlphaMode.Opaque:
                material.FlagsTransparent = false;
                break;

            case MWAssets.AlphaMode.Mask:
                material.ParamsUseAlphaScissor       = true;
                material.ParamsAlphaScissorThreshold = 1.0f;
                break;

            case MWAssets.AlphaMode.Blend:
                material.FlagsTransparent      = true;
                material.ParamsUseAlphaScissor = false;
                break;
                // ignore default case, i.e. null
            }
        }