< Summary

Class:MainScripts.DCL.Components.Avatar.VRMExporter.VRMExporterUtils
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/VRMExporter/VRMExporterUtils.cs
Covered lines:0
Uncovered lines:97
Coverable lines:97
Total lines:168
Line coverage:0% (0 of 97)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:9
Method coverage:0% (0 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
VRMExporterUtils()0%2100%
CacheFBXBones(...)0%2100%
CloneSmr(...)0%2100%
ConvertEyesMaterial(...)0%6200%
ConvertLitMaterial(...)0%30500%
SetBlendMode(...)0%12300%
ExtractComposedEyesTexture(...)0%2100%
ExtractComposedBaseMapTexture(...)0%2100%
ExtractComposedTexture(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/VRMExporter/VRMExporterUtils.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.Rendering;
 5
 6namespace MainScripts.DCL.Components.Avatar.VRMExporter
 7{
 8    public static class VRMExporterUtils
 9    {
 010        private static readonly int _Color = Shader.PropertyToID("_Color");
 011        private static readonly int _BaseColor = Shader.PropertyToID("_BaseColor");
 012        private static readonly int _ShadeTexture = Shader.PropertyToID("_ShadeTexture");
 013        private static readonly int _ShadeColor = Shader.PropertyToID("_ShadeColor");
 014        private static readonly int _CullMode = Shader.PropertyToID("_CullMode");
 015        private static readonly int _Cull = Shader.PropertyToID("_Cull");
 016        private static readonly int _EmissionColor = Shader.PropertyToID("_EmissionColor");
 017        private static readonly int _EmissionMap = Shader.PropertyToID("_EmissionMap");
 018        private static readonly int _BlendMode = Shader.PropertyToID("_BlendMode");
 019        private static readonly int _SrcBlend = Shader.PropertyToID("_SrcBlend");
 020        private static readonly int _DstBlend = Shader.PropertyToID("_DstBlend");
 021        private static readonly int _ZWrite = Shader.PropertyToID("_ZWrite");
 022        private static readonly int _Cutoff = Shader.PropertyToID("_Cutoff");
 023        private static readonly int _AlphaToMask = Shader.PropertyToID("_AlphaToMask");
 024        private static readonly int _EyesTexture = Shader.PropertyToID("_EyesTexture");
 025        private static readonly int _BaseMap = Shader.PropertyToID("_BaseMap");
 26        private const string _ALPHATEST_ON = "_ALPHATEST_ON";
 27        private const string _ALPHABLEND_ON = "_ALPHABLEND_ON";
 28        private const string _ALPHAPREMULTIPLY_ON = "_ALPHAPREMULTIPLY_ON";
 29        private const string RenderType = "RenderType";
 30        private const string Opaque = "Opaque";
 31        private const string TransparentCutout = "TransparentCutout";
 32        private const string Transparent = "Transparent";
 33
 34        public static Dictionary<string, Transform> CacheFBXBones(Transform root)
 35        {
 36            void GatherBonesRecursively(Transform current, Dictionary<string, Transform> bones)
 37            {
 038                bones.Add(current.name, current);
 39
 040                foreach (Transform child in current)
 041                    GatherBonesRecursively(child, bones);
 042            }
 43
 044            var bones = new Dictionary<string, Transform>();
 045            GatherBonesRecursively(root, bones);
 046            return bones;
 47        }
 48
 49        public static SkinnedMeshRenderer CloneSmr(GameObject container, SkinnedMeshRenderer source)
 50        {
 051            var smr = container.AddComponent<SkinnedMeshRenderer>();
 052            smr.sharedMesh = source.sharedMesh;
 053            smr.quality = source.quality;
 054            smr.updateWhenOffscreen = source.updateWhenOffscreen;
 055            return smr;
 56        }
 57
 58        public static void ConvertEyesMaterial(Material fromMaterial, Texture mainTex, Material outMaterial)
 59        {
 060            if (fromMaterial == null)
 061                return;
 62
 063            outMaterial.name = fromMaterial.name;
 64
 65            // Set main textures and colors
 066            outMaterial.mainTexture = mainTex;
 067            outMaterial.SetFloat(_Cutoff, outMaterial.GetFloat(_Cutoff));
 68
 69            // Set render queue and blend mode
 070            outMaterial.renderQueue = fromMaterial.renderQueue;
 071            SetBlendMode(outMaterial, 1);
 072        }
 73
 74        public static void ConvertLitMaterial(Material fromMaterial, Material outMaterial)
 75        {
 76            const int EMISSIVE_FACTOR = 20;
 77
 078            if (fromMaterial == null)
 079                return;
 80
 081            var mainTex = fromMaterial.mainTexture;
 82
 083            outMaterial.name = fromMaterial.name;
 84
 85            // Set main textures and colors
 086            outMaterial.mainTexture = mainTex;
 087            outMaterial.SetTexture(_ShadeTexture, mainTex);
 88
 089            var baseColor = fromMaterial.GetColor(_BaseColor);
 090            outMaterial.SetColor(_Color, baseColor);
 091            outMaterial.SetColor(_ShadeColor, baseColor);
 92
 093            outMaterial.SetColor(_EmissionColor, fromMaterial.GetColor(_EmissionColor) * EMISSIVE_FACTOR);
 094            outMaterial.SetFloat(_CullMode, fromMaterial.GetFloat(_Cull));
 095            if(fromMaterial.HasProperty(_EmissionMap))
 096                outMaterial.SetTexture(_EmissionMap, fromMaterial.GetTexture(_EmissionMap));
 97
 98            // Set render queue and blend mode
 099            outMaterial.renderQueue = fromMaterial.renderQueue;
 0100            if (fromMaterial.renderQueue <= 2450)
 0101                SetBlendMode(outMaterial, 0);
 0102            else if (fromMaterial.renderQueue < 3000)
 0103                SetBlendMode(outMaterial, 1);
 104            else
 0105                SetBlendMode(outMaterial, 2);
 0106        }
 107
 108        private static void SetBlendMode(Material material, int value)
 109        {
 0110            if (value == 0)
 111            {
 0112                material.SetFloat(_BlendMode, 0);
 113
 0114                material.SetOverrideTag(RenderType, Opaque);
 0115                material.SetInt(_SrcBlend, (int)BlendMode.One);
 0116                material.SetInt(_DstBlend, (int)BlendMode.Zero);
 0117                material.SetInt(_ZWrite, 1);
 0118                material.SetInt(_AlphaToMask, 0);
 0119                material.DisableKeyword(_ALPHATEST_ON);
 0120                material.DisableKeyword(_ALPHABLEND_ON);
 0121                material.DisableKeyword(_ALPHAPREMULTIPLY_ON);
 122            }
 0123            else if (value == 1)
 124            {
 0125                material.SetFloat(_BlendMode, 1);
 126
 0127                material.SetOverrideTag(RenderType, TransparentCutout);
 0128                material.SetInt(_SrcBlend, (int)BlendMode.One);
 0129                material.SetInt(_DstBlend, (int)BlendMode.Zero);
 0130                material.SetInt(_ZWrite, 1);
 0131                material.SetInt(_AlphaToMask, 1);
 0132                material.EnableKeyword(_ALPHATEST_ON);
 0133                material.DisableKeyword(_ALPHABLEND_ON);
 0134                material.DisableKeyword(_ALPHAPREMULTIPLY_ON);
 135            }
 136            else
 137            {
 0138                material.SetFloat(_BlendMode, 2);
 139
 0140                material.SetOverrideTag(RenderType, Transparent);
 0141                material.SetInt(_SrcBlend, (int)BlendMode.SrcAlpha);
 0142                material.SetInt(_DstBlend, (int)BlendMode.OneMinusSrcAlpha);
 0143                material.SetInt(_ZWrite, 0);
 0144                material.SetInt(_AlphaToMask, 0);
 0145                material.DisableKeyword(_ALPHATEST_ON);
 0146                material.EnableKeyword(_ALPHABLEND_ON);
 0147                material.DisableKeyword(_ALPHAPREMULTIPLY_ON);
 148            }
 0149        }
 150
 0151        public static Texture2D ExtractComposedEyesTexture(Material source) => ExtractComposedTexture(source, source.Get
 0152        public static Texture2D ExtractComposedBaseMapTexture(Material source) => ExtractComposedTexture(source, source.
 153
 154        private static Texture2D ExtractComposedTexture(Material source, Texture sourceMainTex)
 155        {
 0156            float cutoff = source.GetFloat(_Cutoff);
 0157            source.SetFloat(_Cutoff, 0.0f);
 0158            var rt = RenderTexture.GetTemporary(sourceMainTex.width, sourceMainTex.height);
 0159            Graphics.Blit(sourceMainTex, rt, source);
 0160            source.SetFloat(_Cutoff, cutoff);
 161
 0162            Texture2D tex = new Texture2D(sourceMainTex.width, sourceMainTex.height);
 0163            tex.ReadPixels(new Rect(0,0, tex.width, tex.height), 0, 0);
 0164            tex.Apply();
 0165            return tex;
 166        }
 167    }
 168}