Esempio n. 1
0
    private void InitializeSingleComponentMaterial(IntPtr renderPart, int lodIndex)
    {
        ovrAvatarPBSMaterialState materialState =
            CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetPBSMaterialState(renderPart);

        int componentType = (int)OvrAvatarMaterialManager.GetComponentType(gameObject.name);

        var defaultProperties = AvatarMaterialManager.DefaultAvatarConfig.ComponentMaterialProperties;

        var diffuseTexture  = OvrAvatarComponent.GetLoadedTexture(materialState.albedoTextureID);
        var normalTexture   = OvrAvatarComponent.GetLoadedTexture(materialState.normalTextureID);
        var metallicTexture = OvrAvatarComponent.GetLoadedTexture(materialState.metallicnessTextureID);

        if (diffuseTexture == null)
        {
            diffuseTexture = AvatarMaterialManager.DiffuseFallbacks[lodIndex];
        }

        if (normalTexture == null)
        {
            normalTexture = AvatarMaterialManager.NormalFallbacks[lodIndex];
        }

        if (metallicTexture == null)
        {
            metallicTexture = AvatarMaterialManager.DiffuseFallbacks[lodIndex];
        }

        mesh.sharedMaterial.SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_MAINTEX, diffuseTexture);
        mesh.sharedMaterial.SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_NORMALMAP, normalTexture);
        mesh.sharedMaterial.SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_ROUGHNESSMAP, metallicTexture);

        mesh.sharedMaterial.SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_COLOR,
                                      materialState.albedoMultiplier);

        mesh.sharedMaterial.SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_DIFFUSEINTENSITY,
                                     defaultProperties[componentType].DiffuseIntensity);

        mesh.sharedMaterial.SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_RIMINTENSITY,
                                     defaultProperties[componentType].RimIntensity);

        mesh.sharedMaterial.SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_BACKLIGHTINTENSITY,
                                     defaultProperties[componentType].BacklightIntensity);

        mesh.sharedMaterial.SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_REFLECTIONINTENSITY,
                                     defaultProperties[componentType].ReflectionIntensity);

        mesh.GetClosestReflectionProbes(AvatarMaterialManager.ReflectionProbes);
        if (AvatarMaterialManager.ReflectionProbes != null &&
            AvatarMaterialManager.ReflectionProbes.Count > 0)
        {
            mesh.sharedMaterial.SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_CUBEMAP,
                                           AvatarMaterialManager.ReflectionProbes[0].probe.texture);
        }

#if UNITY_EDITOR
        mesh.sharedMaterial.EnableKeyword("FIX_NORMAL_ON");
#endif
        mesh.sharedMaterial.EnableKeyword("PBR_LIGHTING_ON");
    }
    public void UpdateSkinnedMeshRender(OvrAvatarComponent component, OvrAvatar avatar, IntPtr renderPart)
    {
        ovrAvatarVisibilityFlags visibilityMask = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetVisibilityMask(renderPart):
        ovrAvatarTransform localTransform = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetTransform(renderPart):
        UpdateSkinnedMesh(avatar, bones, localTransform, visibilityMask, renderPart):

        UpdateMeshMaterial(visibilityMask, mesh == null ? component.RootMeshComponent : mesh):
        bool isActive = this.gameObject.activeSelf:

        if( mesh != null )
        {
            bool changedMaterial = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_MaterialStateChanged(renderPart):
            if (changedMaterial || (!previouslyActive && isActive))
            {
                ovrAvatarPBSMaterialState materialState = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetPBSMaterialState(renderPart):

                Material mat = mesh.sharedMaterial:
                mat.SetVector("_AlbedoMultiplier", materialState.albedoMultiplier):
                mat.SetTexture("_Albedo", OvrAvatarComponent.GetLoadedTexture(materialState.albedoTextureID)):
                mat.SetTexture("_Metallicness", OvrAvatarComponent.GetLoadedTexture(materialState.metallicnessTextureID)):
                mat.SetFloat("_GlossinessScale", materialState.glossinessScale):
            }
        }
        previouslyActive = isActive:
    }
Esempio n. 3
0
        public static ovrAvatarPBSMaterialState[] ovrAvatar_GetBodyPBSMaterialStates(IntPtr renderPart)
        {
            System.IntPtr countPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UInt32)));
            IntPtr        ptrState = ovrAvatar_GetBodyPBSMaterialStates_Native(renderPart, countPtr);
            UInt32        count    = (UInt32)Marshal.ReadInt32(countPtr);

            ovrAvatarPBSMaterialState[] states = new ovrAvatarPBSMaterialState[count];

            for (int i = 0; i < states.Length; i++)
            {
                IntPtr nextItem = new IntPtr(ptrState.ToInt64() + i * Marshal.SizeOf(typeof(ovrAvatarPBSMaterialState)));
                states[i] = (ovrAvatarPBSMaterialState)Marshal.PtrToStructure(nextItem, typeof(ovrAvatarPBSMaterialState));
            }

            return(states);
        }
    UInt64 GetTextureIDForType(ovrAvatarPBSMaterialState materialState, TextureType type)
    {
        if (type == TextureType.DiffuseTextures)
        {
            return(materialState.albedoTextureID);
        }
        else if (type == TextureType.NormalMaps)
        {
            return(materialState.normalTextureID);
        }
        else if (type == TextureType.RoughnessMaps)
        {
            return(materialState.metallicnessTextureID);
        }

        return(0);
    }
Esempio n. 5
0
    private void InitializeSingleComponentMaterial(IntPtr renderPart, int lodIndex)
    {
        ovrAvatarPBSMaterialState materialState =
            CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetPBSMaterialState(renderPart);

        int componentType = (int)OvrAvatarMaterialManager.GetComponentType(gameObject.name);

        Texture2D diffuseTexture  = OvrAvatarComponent.GetLoadedTexture(materialState.albedoTextureID);
        Texture2D normalTexture   = OvrAvatarComponent.GetLoadedTexture(materialState.normalTextureID);
        Texture2D metallicTexture = OvrAvatarComponent.GetLoadedTexture(materialState.metallicnessTextureID);

        if (diffuseTexture != null)
        {
            avatarMaterialManager.AddTextureIDToTextureManager(materialState.albedoTextureID, true);
        }
        else
        {
            diffuseTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
        }
        diffuseTexture.anisoLevel = 4;
        if (normalTexture != null)
        {
            avatarMaterialManager.AddTextureIDToTextureManager(materialState.normalTextureID, true);
        }
        else
        {
            normalTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].Normal;
        }
        normalTexture.anisoLevel = 4;
        if (metallicTexture != null)
        {
            avatarMaterialManager.AddTextureIDToTextureManager(materialState.metallicnessTextureID, true);
        }
        else
        {
            metallicTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
        }
        metallicTexture.anisoLevel = 16;

        mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_MAINTEX, diffuseTexture);
        mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_NORMALMAP, normalTexture);
        mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_ROUGHNESSMAP, metallicTexture);

        mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_COLOR, materialState.albedoMultiplier);

        mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_DIFFUSEINTENSITY,
                                   OvrAvatarMaterialManager.DiffuseIntensities[componentType]);
        mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_RIMINTENSITY,
                                   OvrAvatarMaterialManager.RimIntensities[componentType]);
        mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_REFLECTIONINTENSITY,
                                   OvrAvatarMaterialManager.ReflectionIntensities[componentType]);

        mesh.GetClosestReflectionProbes(avatarMaterialManager.ReflectionProbes);
        if (avatarMaterialManager.ReflectionProbes != null &&
            avatarMaterialManager.ReflectionProbes.Count > 0)
        {
            mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_CUBEMAP,
                                         avatarMaterialManager.ReflectionProbes[0].probe.texture);
        }

        if (EnableExpressive)
        {
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_IRIS_COLOR,
                                        ExpressiveParameters.irisColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_COLOR,
                                        ExpressiveParameters.lipColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_BROW_COLOR,
                                        ExpressiveParameters.browColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LASH_COLOR,
                                        ExpressiveParameters.lashColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_SCLERA_COLOR,
                                        ExpressiveParameters.scleraColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_GUM_COLOR,
                                        ExpressiveParameters.gumColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_TEETH_COLOR,
                                        ExpressiveParameters.teethColor);
            mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_SMOOTHNESS,
                                       ExpressiveParameters.lipSmoothness);
        }
    }
    public override bool Equals(object obj)
    {
        if (!(obj is ovrAvatarPBSMaterialState))
        {
            return(false);
        }
        ovrAvatarPBSMaterialState other = (ovrAvatarPBSMaterialState)obj;

        if (!VectorEquals(baseColor, other.baseColor))
        {
            return(false);
        }
        if (albedoTextureID != other.albedoTextureID)
        {
            return(false);
        }
        if (!VectorEquals(albedoMultiplier, other.albedoMultiplier))
        {
            return(false);
        }
        if (metallicnessTextureID != other.metallicnessTextureID)
        {
            return(false);
        }
        if (glossinessScale != other.glossinessScale)
        {
            return(false);
        }
        if (normalTextureID != other.normalTextureID)
        {
            return(false);
        }
        if (heightTextureID != other.heightTextureID)
        {
            return(false);
        }
        if (occlusionTextureID != other.occlusionTextureID)
        {
            return(false);
        }
        if (emissionTextureID != other.emissionTextureID)
        {
            return(false);
        }
        if (!VectorEquals(emissionMultiplier, other.emissionMultiplier))
        {
            return(false);
        }
        if (detailMaskTextureID != other.detailMaskTextureID)
        {
            return(false);
        }
        if (detailAlbedoTextureID != other.detailAlbedoTextureID)
        {
            return(false);
        }
        if (detailNormalTextureID != other.detailNormalTextureID)
        {
            return(false);
        }
        return(true);
    }
    private void InitializeSingleComponentMaterial(IntPtr renderPart, int lodIndex)
    {
        ovrAvatarPBSMaterialState materialState =
            CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetPBSMaterialState(renderPart);

        int componentType = (int)OvrAvatarMaterialManager.GetComponentType(gameObject.name);

        var defaultProperties = AvatarMaterialManager.DefaultAvatarConfig.ComponentMaterialProperties;

        var diffuseTexture  = OvrAvatarComponent.GetLoadedTexture(materialState.albedoTextureID);
        var normalTexture   = OvrAvatarComponent.GetLoadedTexture(materialState.normalTextureID);
        var metallicTexture = OvrAvatarComponent.GetLoadedTexture(materialState.metallicnessTextureID);

        if (diffuseTexture == null)
        {
            diffuseTexture = AvatarMaterialManager.DiffuseFallbacks[lodIndex];
        }

        if (normalTexture == null)
        {
            normalTexture = AvatarMaterialManager.NormalFallbacks[lodIndex];
        }

        if (metallicTexture == null)
        {
            metallicTexture = AvatarMaterialManager.DiffuseFallbacks[lodIndex];
        }

        mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_MAINTEX, diffuseTexture);
        mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_NORMALMAP, normalTexture);
        mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_ROUGHNESSMAP, metallicTexture);

        mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_COLOR,
                                    materialState.albedoMultiplier);

        mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_DIFFUSEINTENSITY,
                                   defaultProperties[componentType].DiffuseIntensity);

        mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_RIMINTENSITY,
                                   defaultProperties[componentType].RimIntensity);

        mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_REFLECTIONINTENSITY,
                                   defaultProperties[componentType].ReflectionIntensity);

        mesh.GetClosestReflectionProbes(AvatarMaterialManager.ReflectionProbes);
        if (AvatarMaterialManager.ReflectionProbes != null &&
            AvatarMaterialManager.ReflectionProbes.Count > 0)
        {
            mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_CUBEMAP,
                                         AvatarMaterialManager.ReflectionProbes[0].probe.texture);
        }

        if (EnableExpressive)
        {
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_IRIS_COLOR,
                                        ExpressiveParameters.irisColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_COLOR,
                                        ExpressiveParameters.lipColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_BROW_COLOR,
                                        ExpressiveParameters.browColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LASH_COLOR,
                                        ExpressiveParameters.lashColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_SCLERA_COLOR,
                                        ExpressiveParameters.scleraColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_GUM_COLOR,
                                        ExpressiveParameters.gumColor);
            mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_TEETH_COLOR,
                                        ExpressiveParameters.teethColor);
            mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_SMOOTHNESS,
                                       ExpressiveParameters.lipSmoothness);
        }
    }
        private void UpdateAvatarComponent(IntPtr nativeComponent)
        {
            ovrAvatarComponent nativeAvatarComponent = new ovrAvatarComponent();

            CAPI.ovrAvatarComponent_Get(nativeComponent, false, ref nativeAvatarComponent);
            ConvertTransform(nativeAvatarComponent.transform, transform);
            for (UInt32 renderPartIndex = 0; renderPartIndex < nativeAvatarComponent.renderPartCount; renderPartIndex++)
            {
                if (RenderParts.Count <= renderPartIndex)
                {
                    break;
                }

                OvrAvatarRenderComponent renderComponent = RenderParts[(int)renderPartIndex];
                IntPtr renderPart             = OvrAvatar.GetRenderPart(nativeAvatarComponent, renderPartIndex);
                ovrAvatarRenderPartType  type = CAPI.ovrAvatarRenderPart_GetType(renderPart);
                ovrAvatarTransform       localTransform;
                ovrAvatarVisibilityFlags visibilityMask;
                var mesh  = renderComponent.mesh;
                var bones = renderComponent.bones;
                switch (type)
                {
                case ovrAvatarRenderPartType.SkinnedMeshRenderPBS:
                    visibilityMask = CAPI.ovrAvatarSkinnedMeshRenderPBS_GetVisibilityMask(renderPart);
                    localTransform = CAPI.ovrAvatarSkinnedMeshRenderPBS_GetTransform(renderPart);
                    if ((visibilityMask & ovrAvatarVisibilityFlags.FirstPerson) != 0)
                    {
                        renderComponent.gameObject.SetActive(true);
                        mesh.enabled = true;
                    }

                    UpdateSkinnedMesh(localTransform, renderPart, bones);

                    UInt64 albedoTextureID  = CAPI.ovrAvatarSkinnedMeshRenderPBS_GetAlbedoTextureAssetID(renderPart);
                    UInt64 surfaceTextureID = CAPI.ovrAvatarSkinnedMeshRenderPBS_GetSurfaceTextureAssetID(renderPart);
                    mesh.sharedMaterial.SetTexture("_Albedo", OvrAvatarComponent.GetLoadedTexture(albedoTextureID));
                    mesh.sharedMaterial.SetTexture("_Surface", OvrAvatarComponent.GetLoadedTexture(surfaceTextureID));
                    break;

                case ovrAvatarRenderPartType.SkinnedMeshRenderPBS_V2:
                    visibilityMask = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetVisibilityMask(renderPart);
                    localTransform = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetTransform(renderPart);
                    if ((visibilityMask & ovrAvatarVisibilityFlags.FirstPerson) != 0)
                    {
                        renderComponent.gameObject.SetActive(true);
                        mesh.enabled = true;
                    }

                    UpdateSkinnedMesh(localTransform, renderPart, bones);

                    ovrAvatarPBSMaterialState materialState =
                        CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetPBSMaterialState(renderPart);
                    Texture2D diffuseTexture  = OvrAvatarComponent.GetLoadedTexture(materialState.albedoTextureID);
                    Texture2D normalTexture   = OvrAvatarComponent.GetLoadedTexture(materialState.normalTextureID);
                    Texture2D metallicTexture = OvrAvatarComponent.GetLoadedTexture(materialState.metallicnessTextureID);
                    mesh.materials[0].SetTexture("_MainTex", diffuseTexture);
                    mesh.materials[0].SetTexture("_NormalMap", normalTexture);
                    mesh.materials[0].SetTexture("_RoughnessMap", metallicTexture);
                    break;

                default:
                    break;
                }
            }
        }