示例#1
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = Empty.GetHashCode();
         hashCode = (hashCode * 397) ^ Color.GetHashCode();
         hashCode = (hashCode * 397) ^ Metallic.GetHashCode();
         hashCode = (hashCode * 397) ^ Smoothness.GetHashCode();
         hashCode = (hashCode * 397) ^ Emission.GetHashCode();
         return(hashCode);
     }
 }
示例#2
0
 public virtual void Release()
 {
     if (Albedo != null)
     {
         Albedo.Release();
         Albedo = null;
     }
     if (Height != null)
     {
         Height.Release();
         Height = null;
     }
     if (Normal != null)
     {
         Normal.Release();
         Normal = null;
     }
     if (Metallic != null)
     {
         Metallic.Release();
         Metallic = null;
     }
     if (Roughness != null)
     {
         Roughness.Release();
         Roughness = null;
     }
     if (Occlusion != null)
     {
         Occlusion.Release();
         Occlusion = null;
     }
     if (Thickness != null)
     {
         Thickness.Release();
         Thickness = null;
     }
     if (Emission != null)
     {
         Emission.Release();
         Emission = null;
     }
 }
示例#3
0
        public void ComputeScatteringFunctions(SurfaceInteraction si,
                                               IObjectArena arena,
                                               TransportMode mode,
                                               bool allowMultipleLobes)
        {
            BumpMap?.Bump(si);

            si.BSDF.Initialize(si);

            // Diffuse
            var c = Color.Evaluate(si).Clamp();
            var metallicWeight = Metallic.Evaluate(si);
            var e             = Eta.Evaluate(si);
            var strans        = SpecTrans.Evaluate(si);
            var diffuseWeight = (1f - metallicWeight) * (1f - strans);
            var dt            = DiffTrans.Evaluate(si) / 2f;
            var rough         = Roughness.Evaluate(si);
            var lum           = c.YComponent();
            var Ctint         = lum > 0f ? c / lum : Spectrum.One;

            if (diffuseWeight > 0f)
            {
                if (IsThin)
                {
                    var flat = Flatness.Evaluate(si);
                    si.BSDF.Add(arena.Create <DisneyDiffuse>().Initialize(diffuseWeight * (1f - flat) * (1 - dt) * c));
                    si.BSDF.Add(arena.Create <DisneyFakeSS>().Initialize(diffuseWeight * flat * (1f - dt) * c, rough));
                }
                else
                {
                    var sd = ScatterDistance.Evaluate(si);
                    if (sd.IsBlack())
                    {
                        si.BSDF.Add(arena.Create <DisneyDiffuse>().Initialize(diffuseWeight * c));
                    }
                    else
                    {
                        // The line below was the original code but produces some odd results.
                        si.BSDF.Add(arena.Create <SpecularTransmission>().Initialize(Spectrum.One, 1f, e, mode));
                        si.BSSRDF = arena.Create <DisneyBSSRDF>().Initialize(diffuseWeight * c, sd, si, e, this, mode);
                    }
                }

                // Retro-reflection.
                si.BSDF.Add(arena.Create <DisneyRetro>().Initialize(diffuseWeight * c, rough));

                // Sheen
                var sheenWeight = Sheen.Evaluate(si);
                if (sheenWeight > 0f)
                {
                    var stint  = SheenTint.Evaluate(si);
                    var Csheen = Spectrum.Lerp(Spectrum.One, Ctint, stint);
                    si.BSDF.Add(arena.Create <DisneySheen>().Initialize(diffuseWeight * sheenWeight * Csheen));
                }
            }

            // Microfacet distribution
            var aspect = Sqrt(1f - Anisotropic.Evaluate(si) * 0.9f);
            var ax     = Max(0.001f, Sqr(rough) / aspect);
            var ay     = Max(0.001f, Sqr(rough) * aspect);
            var dist   = arena.Create <DisneyMicrofacetDistribution>().Initialize(ax, ay);

            // Specular = Trowbridge-Reitz with modified Fresnel function.
            var specTint = SpecularTint.Evaluate(si);
            var Cspec0   = Spectrum.Lerp(SchlickR0FromEta(e) * Spectrum.Lerp(Spectrum.One, Ctint, specTint), c,
                                         metallicWeight);
            var fresnel = arena.Create <DisneyFresnel>().Initialize(Cspec0, metallicWeight, e);

            si.BSDF.Add(arena.Create <MicrofacetReflection>().Initialize(c, dist, fresnel));

            // Clearcoat
            var cc = Clearcoat.Evaluate(si);

            if (cc > 0f)
            {
                si.BSDF.Add(arena.Create <DisneyClearcoat>()
                            .Initialize(cc, Lerp(0.1f, 0.001f, ClearcoatGloss.Evaluate(si))));
            }

            // BTDF
            if (strans > 0f)
            {
                // Walter et al's model, with the provided transmissive term scaled
                // by sqrt(color), so that after two refractions, we're back to the
                // provided color.
                var T = strans * c.Sqrt();
                if (IsThin)
                {
                    var rScaled    = (0.65f * e - 0.35f) * rough;
                    var atx        = Max(0.001f, Sqr(rScaled) / aspect);
                    var aty        = Max(0.001f, Sqr(rScaled) * aspect);
                    var scaledDist = arena.Create <TrowbridgeReitzDistribution>().Initialize(atx, aty);
                    si.BSDF.Add(arena.Create <MicrofacetTransmission>().Initialize(T, scaledDist, 1f, e, mode));
                }
                else
                {
                    si.BSDF.Add(arena.Create <MicrofacetTransmission>().Initialize(T, dist, 1f, e, mode));
                }
            }

            if (IsThin)
            {
                si.BSDF.Add(arena.Create <LambertianTransmission>().Initialize(dt * c));
            }
        }
示例#4
0
        public override void ImportFromUsd()
        {
            Material mat = Material;

            if (DiffuseMap)
            {
                Debug.Log("here");
                mat.SetTexture("_BaseColorMap", DiffuseMap);
                mat.SetColor("_BaseColor", Color.white);
            }
            else
            {
                mat.SetColor("_BaseColor", Diffuse.GetValueOrDefault(mat.color));
            }

            // TODO: What about opacity map?

            if (!IsSpecularWorkflow)
            {
                // Robustness: It would be ideal if this parameter were provided by HDRP, however that
                // would require this asset package having a dependency on the HDRP package itself,
                // which is (yet) not desirable.
                mat.SetFloat("_MaterialID", /*Standard Metallic*/ 1);
            }
            else
            {
                mat.SetFloat("_MaterialID", /*Spec Color*/ 4);
                mat.EnableKeyword("_MATERIAL_FEATURE_SPECULAR_COLOR");
                mat.EnableKeyword("_SPECULARCOLORMAP");
            }

            // R=Metallic, G=Occlusion, B=Displacement, A=Roughness(Smoothness)
            var MaskMap = BuildMaskMap(!IsSpecularWorkflow ? MetallicMap : null, OcclusionMap, DisplacementMap, RoughnessMap);

            if (MaskMap)
            {
                mat.SetTexture("_MaskMap", MaskMap);
                mat.EnableKeyword("_MASKMAP");
            }

            if (!IsSpecularWorkflow)
            {
                if (!MetallicMap)
                {
                    mat.SetFloat("_Metallic", Metallic.GetValueOrDefault());
                }
            }
            else
            {
                if (SpecularMap)
                {
                    mat.SetTexture("_SpecularColorMap", SpecularMap);
                }
                else
                {
                    mat.SetColor("_SpecularColor", Specular.GetValueOrDefault());
                }
            }

            if (!RoughnessMap)
            {
                var smoothness = 1 - Roughness.GetValueOrDefault();
                mat.SetFloat("_Smoothness", smoothness);
                // HDRP Lit does not seem to respect smoothness, so just clamp to the correct value.
                mat.SetFloat("_SmoothnessRemapMin", smoothness);
                mat.SetFloat("_SmoothnessRemapMax", smoothness);
            }

            if (!OcclusionMap)
            {
                mat.SetFloat("_AORemapMin", Occlusion.GetValueOrDefault());
                mat.SetFloat("_AORemapMax", Occlusion.GetValueOrDefault());
            }

            // Single displacement scalar value not supported.

            if (ClearcoatMap)
            {
                mat.SetTexture("_CoatMaskMap", ClearcoatMap);
                mat.EnableKeyword("_MATERIAL_FEATURE_CLEAR_COAT");
            }
            mat.SetFloat("_CoatMask", ClearcoatRoughness.GetValueOrDefault());

            if (NormalMap)
            {
                mat.SetTexture("_NormalMap", NormalMap);
                mat.EnableKeyword("_NORMALMAP");
            }

            if (EmissionMap)
            {
                mat.SetTexture("_EmissionMap", EmissionMap);
                mat.EnableKeyword("_EMISSIVE_COLOR_MAP");
            }
            else
            {
                mat.SetColor("_EmissionColor", Emission.GetValueOrDefault());
            }
        }
示例#5
0
        public override void ImportFromUsd()
        {
            Material mat = Material;

            if (DiffuseMap)
            {
                mat.SetTexture("_MainTex", DiffuseMap);
            }
            else
            {
                mat.color = Diffuse.GetValueOrDefault(mat.color);
            }

            if (NormalMap)
            {
                mat.SetTexture("_BumpMap", NormalMap);
                mat.EnableKeyword("_NORMALMAP");
            }
            else
            {
                // TODO: Unity has no notion of a constant normal map.
            }

            if (DisplacementMap)
            {
                mat.SetTexture("_ParallaxMap", DisplacementMap);
                mat.EnableKeyword("_PARALLAXMAP");
            }
            else
            {
                // TODO: Unity has no notion of a parallax map.
            }

            if (OcclusionMap)
            {
                mat.SetTexture("_OcclusionMap", OcclusionMap);
            }
            else
            {
                // TODO: Unity has no notion of a constant occlusion value.
            }

            if (EmissionMap)
            {
                mat.SetTexture("_EmissionMap", EmissionMap);
                mat.EnableKeyword("_EMISSION");
            }
            else
            {
                var rgb = Emission.GetValueOrDefault(Color.black);
                mat.SetColor("_EmissionColor", rgb);
                if (rgb.r > 0 || rgb.g > 0 || rgb.b > 0)
                {
                    mat.EnableKeyword("_EMISSION");
                }
            }

            if (!IsMetallicWorkflow)
            {
                if (SpecularMap)
                {
                    mat.SetTexture("_SpecGlossMap", SpecularMap);
                    mat.EnableKeyword("_SPECGLOSSMAP");
                }
                else
                {
                    var rgb = Specular.GetValueOrDefault(Color.gray);
                    mat.SetColor("_SpecColor", rgb);
                }

                if (RoughnessMap)
                {
                    // Roughness for spec setup is tricky, since it may require merging two textures.
                    // For now, just detect that case and issue a warning (e.g. when roughness has a map,
                    // but isn't the albedo or spec map).
                    // Roughness also needs to be converted to glossiness.
                    if (RoughnessMap != SpecularMap && SpecularMap != null)
                    {
                        var specGlossTex = MaterialImporter.CombineRoughnessToGloss(SpecularMap, RoughnessMap);
                        mat.SetTexture("_SpecGlossMap", specGlossTex);
                        mat.EnableKeyword("_SPECGLOSSMAP");
                    }
                    else if (SpecularMap == null && RoughnessMap != DiffuseMap)
                    {
                        var mainGlossTex = MaterialImporter.CombineRoughnessToGloss(DiffuseMap, RoughnessMap);
                        mat.SetTexture("_SpecGlossMap", mainGlossTex);
                        mat.EnableKeyword("_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A");
                    }
                    else
                    {
                        // TODO: create a new texture with constant spec value, combined with roughness texture.
                    }
                }
                else
                {
                    float smoothness = 1 - Roughness.GetValueOrDefault(.5f);
                    mat.SetFloat("_Glossiness", smoothness);
                    mat.SetFloat("_GlossMapScale", smoothness);
                }
            }
            else
            {
                if (MetallicMap)
                {
                    mat.SetTexture("_MetallicGlossMap", MetallicMap);
                    mat.EnableKeyword("_METALLICGLOSSMAP");
                }
                else
                {
                    mat.SetFloat("_Metallic", Metallic.GetValueOrDefault(0));
                }

                if (RoughnessMap)
                {
                    // In this case roughness get its own map, but still must be converted to glossiness.
                    mat.SetTexture("_SpecGlossMap", RoughnessMap);
                }
                else
                {
                    float smoothness = 1 - Roughness.GetValueOrDefault(.5f);
                    mat.SetFloat("_Glossiness", smoothness);
                    mat.SetFloat("_GlossMapScale", smoothness);
                }
            }
        }
示例#6
0
 public virtual void Release()
 {
     if (UI3DPreview.Instance != null)
     {
         var prev = UI3DPreview.Instance;
         if (Albedo != null && Albedo != prev.defaultBlack && Albedo != prev.defaultDarkGray && Albedo != prev.defaultGray && Albedo != prev.defaultWhite)
         {
             Albedo.Release();
             Albedo = null;
         }
         if (Height != null && Height != prev.defaultBlack && Height != prev.defaultDarkGray && Height != prev.defaultGray && Height != prev.defaultWhite)
         {
             Height.Release();
             Height = null;
         }
         if (Normal != null && Normal != prev.defaultBlack && Normal != prev.defaultDarkGray && Normal != prev.defaultGray && Normal != prev.defaultWhite)
         {
             Normal.Release();
             Normal = null;
         }
         if (Metallic != null && Metallic != prev.defaultBlack && Metallic != prev.defaultDarkGray && Metallic != prev.defaultGray && Metallic != prev.defaultWhite)
         {
             Metallic.Release();
             Metallic = null;
         }
         if (Roughness != null && Roughness != prev.defaultBlack && Roughness != prev.defaultDarkGray && Roughness != prev.defaultGray && Roughness != prev.defaultWhite)
         {
             Roughness.Release();
             Roughness = null;
         }
         if (Occlusion != null && Occlusion != prev.defaultBlack && Occlusion != prev.defaultDarkGray && Occlusion != prev.defaultGray && Occlusion != prev.defaultWhite)
         {
             Occlusion.Release();
             Occlusion = null;
         }
     }
     else
     {
         if (Albedo != null)
         {
             Albedo.Release();
             Albedo = null;
         }
         if (Height != null)
         {
             Height.Release();
             Height = null;
         }
         if (Normal != null)
         {
             Normal.Release();
             Normal = null;
         }
         if (Metallic != null)
         {
             Metallic.Release();
             Metallic = null;
         }
         if (Roughness != null)
         {
             Roughness.Release();
             Roughness = null;
         }
         if (Occlusion != null)
         {
             Occlusion.Release();
             Occlusion = null;
         }
     }
 }
示例#7
0
        public override void ImportFromUsd()
        {
            Material mat = Material;

            if (DiffuseMap)
            {
                mat.SetTexture("_MainTex", DiffuseMap);
                // Albedo texture is modulated by the material color, so setting to white preserves the pure
                // texture color.
                mat.color = Color.white;
            }
            else
            {
                mat.color = Diffuse.GetValueOrDefault(mat.color);
            }

            if (NormalMap)
            {
                mat.SetTexture("_BumpMap", NormalMap);
                mat.EnableKeyword("_NORMALMAP");
            }
            else
            {
                // TODO: Unity has no notion of a constant normal map.
            }

            if (DisplacementMap)
            {
                mat.SetTexture("_ParallaxMap", DisplacementMap);
                mat.EnableKeyword("_PARALLAXMAP");
            }
            else
            {
                // TODO: Unity has no notion of a parallax map.
            }

            if (OcclusionMap)
            {
                mat.SetTexture("_OcclusionMap", OcclusionMap);
            }
            else
            {
                // TODO: Unity has no notion of a constant occlusion value.
            }

            if (EmissionMap)
            {
                mat.SetTexture("_EmissionMap", EmissionMap);
                mat.SetColor("_EmissionColor", Color.white);
                mat.globalIlluminationFlags = MaterialGlobalIlluminationFlags.BakedEmissive;
                mat.EnableKeyword("_EMISSION");
            }
            else
            {
                var rgb = Emission.GetValueOrDefault(Color.black);
                mat.SetColor("_EmissionColor", rgb);
                if (rgb.r > 0 || rgb.g > 0 || rgb.b > 0)
                {
                    mat.EnableKeyword("_EMISSION");
                }
            }

            if (IsSpecularWorkflow)
            {
                if (SpecularMap)
                {
                    mat.SetTexture("_SpecGlossMap", SpecularMap);
                    mat.EnableKeyword("_SPECGLOSSMAP");
                }
                else
                {
                    var rgb = Specular.GetValueOrDefault(Color.gray);
                    mat.SetColor("_SpecColor", rgb);
                }

                if (RoughnessMap)
                {
                    // Roughness for spec setup is tricky, since it may require merging two textures.
                    // For now, just detect that case and issue a warning (e.g. when roughness has a map,
                    // but isn't the albedo or spec map).
                    // Roughness also needs to be converted to glossiness.
                    if (RoughnessMap != SpecularMap && SpecularMap != null)
                    {
                        var specGlossTex = MaterialImporter.CombineRoughness(SpecularMap, RoughnessMap, "specGloss");
                        mat.SetTexture("_SpecGlossMap", specGlossTex);
                        mat.EnableKeyword("_SPECGLOSSMAP");
                    }
                    else if (SpecularMap == null && RoughnessMap != DiffuseMap)
                    {
                        var mainGlossTex = MaterialImporter.CombineRoughness(DiffuseMap, RoughnessMap, "specGloss");
                        mat.SetTexture("_SpecGlossMap", mainGlossTex);
                        mat.EnableKeyword("_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A");
                    }
                    else
                    {
                        // TODO: create a new texture with constant spec value, combined with roughness texture.
                    }
                }
                else
                {
                    float smoothness = 1 - Roughness.GetValueOrDefault(.5f);
                    mat.SetFloat("_Glossiness", smoothness);
                    mat.SetFloat("_GlossMapScale", smoothness);
                }
            }
            else
            {
                if (MetallicMap)
                {
                    mat.SetTexture("_MetallicGlossMap", MetallicMap);
                    mat.EnableKeyword("_METALLICGLOSSMAP");
                }
                else
                {
                    mat.SetFloat("_Metallic", Metallic.GetValueOrDefault(0));
                }

                float smoothness = 1 - Roughness.GetValueOrDefault(.5f);
                mat.SetFloat("_Glossiness", smoothness);
                mat.SetFloat("_GlossMapScale", smoothness);

                if (RoughnessMap)
                {
                    var metalicRough = MaterialImporter.CombineRoughness(MetallicMap, RoughnessMap, "metalicRough");
                    // In this case roughness get its own map, but still must be converted to glossiness.
                    mat.SetTexture("_MetallicGlossMap", metalicRough);
                    mat.EnableKeyword("_METALLICGLOSSMAP");

                    // The scalar Glossiness modulates the roughness/glossiness map, however USD has no
                    // concept of this, so setting it to 1.0 effectively disables the scalar effect when
                    // the map is present.
                    mat.SetFloat("_Glossiness", 1.0f);
                    mat.SetFloat("_GlossMapScale", 1.0f);
                }
            }
        }