protected bool TrySetTexture( TextureInfo textureInfo, UnityEngine.Material material, int propertyId, IGltfReadable gltf ) { if (textureInfo != null && textureInfo.index >= 0) { int textureIndex = textureInfo.index; var srcTexture = gltf.GetSourceTexture(textureIndex); if (srcTexture != null) { var texture = gltf.GetTexture(textureIndex); if (texture != null) { if (textureInfo.texCoord != 0) { logger?.Error(LogCode.UVMulti); } material.SetTexture(propertyId, texture); var isKtx = srcTexture.isKtx; TrySetTextureTransform(textureInfo, material, propertyId, isKtx); return(true); } logger?.Error(LogCode.TextureLoadFailed, textureIndex.ToString()); } else { logger?.Error(LogCode.TextureNotFound, textureIndex.ToString()); } } return(false); }
protected override RenderQueue?ApplyTransmission( ref Color baseColorLinear, IGltfReadable gltf, Transmission transmission, Material material, RenderQueue?renderQueue ) { if (supportsCameraOpaqueTexture) { if (transmission.transmissionFactor > 0f) { material.EnableKeyword("TRANSMISSION"); material.SetFloat(transmissionFactorPropId, transmission.transmissionFactor); renderQueue = RenderQueue.Transparent; if (TrySetTexture(transmission.transmissionTexture, material, transmissionTexturePropId, gltf)) { } } return(renderQueue); } return(base.ApplyTransmission( ref baseColorLinear, gltf, transmission, material, renderQueue )); }
/// <inheritdoc /> public GameObjectBoundsInstantiator( IGltfReadable gltf, Transform parent, ICodeLogger logger = null, InstantiationSettings settings = null ) : base(gltf, parent, logger, settings) { }
/// <summary> /// Constructs a GameObjectInstantiator /// </summary> /// <param name="gltf">glTF to instantiate from</param> /// <param name="parent">Generated GameObjects will get parented to this Transform</param> /// <param name="logger">Custom logger</param> /// <param name="settings">Instantiation settings</param> public GameObjectInstantiator( IGltfReadable gltf, Transform parent, ICodeLogger logger = null, InstantiationSettings settings = null ) { this.gltf = gltf; this.parent = parent; this.logger = logger; this.settings = settings ?? new InstantiationSettings(); }
public EntityInstantiator( IGltfReadable gltf, Entity parent, ICodeLogger logger = null, InstantiationSettings settings = null ) { this.gltf = gltf; this.parent = parent; this.logger = logger; this.settings = settings ?? new InstantiationSettings(); }
/// <summary> /// Attempts assigning a glTF texture to a Unity material. /// </summary> /// <param name="textureInfo">glTF source texture</param> /// <param name="material">target material</param> /// <param name="gltf">Context glTF</param> /// <param name="texturePropertyId">Target texture property</param> /// <param name="scaleTransformPropertyId">Scale/transform (_ST) property</param> /// <param name="rotationPropertyId">Rotation property</param> /// <param name="uvChannelPropertyId">UV channel selection property</param> /// <returns>True if texture assignment was successful, false otherwise.</returns> protected bool TrySetTexture( TextureInfo textureInfo, UnityEngine.Material material, IGltfReadable gltf, int texturePropertyId, int scaleTransformPropertyId = -1, int rotationPropertyId = -1, int uvChannelPropertyId = -1 ) { if (textureInfo != null && textureInfo.index >= 0) { int textureIndex = textureInfo.index; var srcTexture = gltf.GetSourceTexture(textureIndex); if (srcTexture != null) { var texture = gltf.GetTexture(textureIndex); if (texture != null) { material.SetTexture(texturePropertyId, texture); var isKtx = srcTexture.isKtx; TrySetTextureTransform( textureInfo, material, texturePropertyId, scaleTransformPropertyId, rotationPropertyId, uvChannelPropertyId, isKtx ); return(true); } #if UNITY_IMAGECONVERSION logger?.Error(LogCode.TextureLoadFailed, textureIndex.ToString()); #endif } else { logger?.Error(LogCode.TextureNotFound, textureIndex.ToString()); } } return(false); }
protected virtual RenderQueue?ApplyTransmission( ref Color baseColorLinear, IGltfReadable gltf, Transmission transmission, Material material, RenderQueue?renderQueue ) { #if UNITY_EDITOR // ReSharper disable once Unity.PerformanceCriticalCodeInvocation logger.Warning(LogCode.MaterialTransmissionApproxURP); #endif // Correct transmission is not supported in Built-In renderer // This is an approximation for some corner cases if (transmission.transmissionFactor > 0f && transmission.transmissionTexture.index < 0) { var premul = TransmissionWorkaroundShaderMode(transmission, ref baseColorLinear); } return(renderQueue); }
public abstract UnityEngine.Material GenerateMaterial(Schema.Material gltfMaterial, IGltfReadable gltf);
public override Material GenerateMaterial( Schema.Material gltfMaterial, IGltfReadable gltf ) { Material material; if (gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness != null) { material = GetPbrSpecularGlossinessMaterial(gltfMaterial.doubleSided); } else if (gltfMaterial.extensions?.KHR_materials_unlit != null) { material = GetUnlitMaterial(gltfMaterial.doubleSided); } else { material = GetPbrMetallicRoughnessMaterial(gltfMaterial.doubleSided); } if (material == null) { return(null); } material.name = gltfMaterial.name; StandardShaderMode shaderMode = StandardShaderMode.Opaque; Color baseColorLinear = Color.white; if (gltfMaterial.alphaModeEnum == AlphaMode.MASK) { material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff); shaderMode = StandardShaderMode.Cutout; } else if (gltfMaterial.alphaModeEnum == AlphaMode.BLEND) { SetAlphaModeBlend(material); shaderMode = StandardShaderMode.Fade; } if (gltfMaterial.extensions != null) { // Specular glossiness Schema.PbrSpecularGlossiness specGloss = gltfMaterial.extensions.KHR_materials_pbrSpecularGlossiness; if (specGloss != null) { baseColorLinear = specGloss.diffuseColor; material.SetVector(specColorPropId, specGloss.specularColor); material.SetFloat(glossinessPropId, specGloss.glossinessFactor); TrySetTexture(specGloss.diffuseTexture, material, mainTexPropId, gltf); if (TrySetTexture(specGloss.specularGlossinessTexture, material, specGlossMapPropId, gltf)) { material.EnableKeyword(KW_SPEC_GLOSS_MAP); } } } if (gltfMaterial.pbrMetallicRoughness != null) { baseColorLinear = gltfMaterial.pbrMetallicRoughness.baseColor; material.SetFloat(metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor); material.SetFloat(roughnessPropId, gltfMaterial.pbrMetallicRoughness.roughnessFactor); TrySetTexture(gltfMaterial.pbrMetallicRoughness.baseColorTexture, material, mainTexPropId, gltf); if (TrySetTexture(gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture, material, metallicGlossMapPropId, gltf)) { material.EnableKeyword(KW_METALLIC_ROUGNESS_MAP); } } if (TrySetTexture(gltfMaterial.normalTexture, material, bumpMapPropId, gltf)) { material.EnableKeyword(KW_NORMALMAP); material.SetFloat(bumpScalePropId, gltfMaterial.normalTexture.scale); } if (TrySetTexture(gltfMaterial.occlusionTexture, material, occlusionMapPropId, gltf)) { material.EnableKeyword(KW_OCCLUSION); material.SetFloat(occlusionStrengthPropId, gltfMaterial.occlusionTexture.strength); } if (TrySetTexture(gltfMaterial.emissiveTexture, material, emissionMapPropId, gltf)) { material.EnableKeyword(KW_EMISSION); } if (gltfMaterial.extensions != null) { // Transmission - Approximation var transmission = gltfMaterial.extensions.KHR_materials_transmission; if (transmission != null) { #if UNITY_EDITOR logger?.Warning(LogCode.MaterialTransmissionApprox); #endif // Correct transmission is not supported in Built-In renderer // This is an approximation for some corner cases if (transmission.transmissionFactor > 0f && transmission.transmissionTexture.index < 0) { var premul = TransmissionWorkaroundShaderMode(transmission, ref baseColorLinear); shaderMode = premul ? StandardShaderMode.Transparent : StandardShaderMode.Fade; } } } switch (shaderMode) { case StandardShaderMode.Cutout: SetAlphaModeMask(material, gltfMaterial); break; case StandardShaderMode.Fade: SetAlphaModeBlend(material); break; case StandardShaderMode.Transparent: SetAlphaModeTransparent(material); break; default: SetOpaqueMode(material); break; } material.color = baseColorLinear.gamma; if (gltfMaterial.emissive != Color.black) { material.SetColor(emissionColorPropId, gltfMaterial.emissive.gamma); material.EnableKeyword(KW_EMISSION); } return(material); }
public GameObjectInstantiator(IGltfReadable gltf, Transform parent, ILogger logger = null) { this.gltf = gltf; this.parent = parent; this.logger = logger; }
public override Material GenerateMaterial(Schema.Material gltfMaterial, IGltfReadable gltf) { Material material; MaterialType?materialType = null; ShaderMode shaderMode = ShaderMode.Opaque; if (gltfMaterial.extensions?.KHR_materials_unlit != null) { material = GetUnlitMaterial(gltfMaterial); materialType = MaterialType.Unlit; shaderMode = gltfMaterial.alphaModeEnum != AlphaMode.OPAQUE ? ShaderMode.Blend : ShaderMode.Opaque; } else { bool isMetallicRoughness = gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness == null; if (isMetallicRoughness) { materialType = MaterialType.MetallicRoughness; var metallicShaderFeatures = GetMetallicShaderFeatures(gltfMaterial); material = GetMetallicMaterial(metallicShaderFeatures); shaderMode = (ShaderMode)(metallicShaderFeatures & MetallicShaderFeatures.ModeMask); } else { materialType = MaterialType.SpecularGlossiness; var specularShaderFeatures = GetSpecularShaderFeatures(gltfMaterial); material = GetSpecularMaterial(specularShaderFeatures); if ((specularShaderFeatures & SpecularShaderFeatures.AlphaBlend) != 0) { shaderMode = ShaderMode.Blend; } } } if (material == null) { return(null); } material.name = gltfMaterial.name; Color baseColorLinear = Color.white; RenderQueue?renderQueue = null; //added support for KHR_materials_pbrSpecularGlossiness if (gltfMaterial.extensions != null) { Schema.PbrSpecularGlossiness specGloss = gltfMaterial.extensions.KHR_materials_pbrSpecularGlossiness; if (specGloss != null) { baseColorLinear = specGloss.diffuseColor; material.SetVector(specColorPropId, specGloss.specularColor); material.SetFloat(smoothnessPropId, specGloss.glossinessFactor); TrySetTexture(specGloss.diffuseTexture, material, baseMapPropId, gltf); if (TrySetTexture(specGloss.specularGlossinessTexture, material, specGlossMapPropId, gltf)) { // material.EnableKeyword(); } } } if (gltfMaterial.pbrMetallicRoughness != null) { baseColorLinear = gltfMaterial.pbrMetallicRoughness.baseColor; if (materialType != MaterialType.SpecularGlossiness) { // baseColorTexture can be used by both MetallicRoughness AND Unlit materials TrySetTexture(gltfMaterial.pbrMetallicRoughness.baseColorTexture, material, baseMapPropId, gltf); } if (materialType == MaterialType.MetallicRoughness) { material.SetFloat(metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor); material.SetFloat(smoothnessPropId, 1 - gltfMaterial.pbrMetallicRoughness.roughnessFactor); if (TrySetTexture(gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture, material, metallicRoughnessTexturePropId, gltf)) { // material.EnableKeyword(KW_METALLIC_ROUGHNESS_MAP); } // TODO: When the occlusionTexture equals the metallicRoughnessTexture, we could sample just once instead of twice. // if (!DifferentIndex(gltfMaterial.occlusionTexture,gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture)) { // ... // } } } if (TrySetTexture(gltfMaterial.normalTexture, material, bumpMapPropId, gltf)) { // material.EnableKeyword(KW_NORMALMAP); material.SetFloat(bumpScalePropId, gltfMaterial.normalTexture.scale); } if (TrySetTexture(gltfMaterial.occlusionTexture, material, occlusionMapPropId, gltf)) { material.EnableKeyword(KW_OCCLUSION); material.SetFloat(occlusionStrengthPropId, gltfMaterial.occlusionTexture.strength); } if (TrySetTexture(gltfMaterial.emissiveTexture, material, emissionMapPropId, gltf)) { material.EnableKeyword(KW_EMISSION); } if (gltfMaterial.extensions != null) { // Transmission - Approximation var transmission = gltfMaterial.extensions.KHR_materials_transmission; if (transmission != null) { renderQueue = ApplyTransmission(ref baseColorLinear, gltf, transmission, material, renderQueue); } } if (gltfMaterial.alphaModeEnum == AlphaMode.MASK) { material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff); } else { material.SetFloat(cutoffPropId, 0); // double sided opaque would make errors in HDRP 7.3 otherwise material.SetOverrideTag("MotionVector", "User"); material.SetShaderPassEnabled("MOTIONVECTORS", false); } if (!renderQueue.HasValue) { if (shaderMode == ShaderMode.Opaque) { renderQueue = gltfMaterial.alphaModeEnum == AlphaMode.MASK ? RenderQueue.AlphaTest : RenderQueue.Geometry; } else { renderQueue = RenderQueue.Transparent; } } material.renderQueue = (int)renderQueue.Value; material.SetVector(baseColorPropId, baseColorLinear); if (gltfMaterial.emissive != Color.black) { material.SetColor(emissionColorPropId, gltfMaterial.emissive); material.EnableKeyword(KW_EMISSION); } return(material); }
public GameObjectBoundsInstantiator(IGltfReadable gltf, Transform parent, ICodeLogger logger = null) : base(gltf, parent, logger) { }
public override Material GenerateMaterial(Schema.Material gltfMaterial, IGltfReadable gltf) { Material material; MaterialType?materialType = null; ShaderMode shaderMode = ShaderMode.Opaque; if (gltfMaterial.extensions?.KHR_materials_unlit != null) { material = GetUnlitMaterial(gltfMaterial); materialType = MaterialType.Unlit; shaderMode = gltfMaterial.alphaModeEnum == AlphaMode.BLEND ? ShaderMode.Blend : ShaderMode.Opaque; } else { bool isMetallicRoughness = gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness == null; if (isMetallicRoughness) { materialType = MaterialType.MetallicRoughness; var metallicShaderFeatures = GetMetallicShaderFeatures(gltfMaterial); material = GetMetallicMaterial(metallicShaderFeatures); shaderMode = (ShaderMode)(metallicShaderFeatures & MetallicShaderFeatures.ModeMask); } else { materialType = MaterialType.SpecularGlossiness; var specularShaderFeatures = GetSpecularShaderFeatures(gltfMaterial); material = GetSpecularMaterial(specularShaderFeatures); if ((specularShaderFeatures & SpecularShaderFeatures.AlphaBlend) != 0) { shaderMode = ShaderMode.Blend; } } } if (material == null) { return(null); } material.name = gltfMaterial.name; Color baseColorLinear = Color.white; RenderQueue?renderQueue = null; //added support for KHR_materials_pbrSpecularGlossiness if (gltfMaterial.extensions != null) { Schema.PbrSpecularGlossiness specGloss = gltfMaterial.extensions.KHR_materials_pbrSpecularGlossiness; if (specGloss != null) { baseColorLinear = specGloss.diffuseColor; #if UNITY_SHADER_GRAPH_12_OR_NEWER material.SetVector(specularFactorPropId, specGloss.specularColor); #else material.SetVector(specColorPropId, specGloss.specularColor); #endif material.SetFloat(smoothnessPropId, specGloss.glossinessFactor); TrySetTexture( specGloss.diffuseTexture, material, gltf, baseMapPropId, baseMapScaleTransformPropId, baseMapRotationPropId, baseMapUVChannelPropId ); if (TrySetTexture( specGloss.specularGlossinessTexture, material, gltf, specGlossMapPropId, specGlossScaleTransformMapPropId, specGlossMapRotationPropId, specGlossMapUVChannelPropId )) { // material.EnableKeyword(); } } } if (gltfMaterial.pbrMetallicRoughness != null // If there's a specular-glossiness extension, ignore metallic-roughness // (according to extension specification) && gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness == null) { baseColorLinear = gltfMaterial.pbrMetallicRoughness.baseColor; if (materialType != MaterialType.SpecularGlossiness) { // baseColorTexture can be used by both MetallicRoughness AND Unlit materials TrySetTexture( gltfMaterial.pbrMetallicRoughness.baseColorTexture, material, gltf, baseMapPropId, baseMapScaleTransformPropId, baseMapRotationPropId, baseMapUVChannelPropId ); } if (materialType == MaterialType.MetallicRoughness) { material.SetFloat(metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor); material.SetFloat(smoothnessPropId, 1 - gltfMaterial.pbrMetallicRoughness.roughnessFactor); if (TrySetTexture( gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture, material, gltf, metallicRoughnessMapPropId, metallicRoughnessMapScaleTransformPropId, metallicRoughnessMapRotationPropId, metallicRoughnessMapUVChannelPropId )) { // material.EnableKeyword(KW_METALLIC_ROUGHNESS_MAP); } // TODO: When the occlusionTexture equals the metallicRoughnessTexture, we could sample just once instead of twice. // if (!DifferentIndex(gltfMaterial.occlusionTexture,gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture)) { // ... // } } } if (TrySetTexture( gltfMaterial.normalTexture, material, gltf, bumpMapPropId, bumpMapScaleTransformPropId, bumpMapRotationPropId, bumpMapUVChannelPropId )) { // material.EnableKeyword(Constants.kwNormalMap); material.SetFloat(bumpScalePropId, gltfMaterial.normalTexture.scale); } if (TrySetTexture( gltfMaterial.occlusionTexture, material, gltf, occlusionMapPropId, occlusionMapScaleTransformPropId, occlusionMapRotationPropId, occlusionMapUVChannelPropId )) { material.EnableKeyword(KW_OCCLUSION); material.SetFloat(occlusionStrengthPropId, gltfMaterial.occlusionTexture.strength); } if (TrySetTexture( gltfMaterial.emissiveTexture, material, gltf, emissionMapPropId, emissionMapScaleTransformPropId, emissionMapRotationPropId, emissionMapUVChannelPropId )) { material.EnableKeyword(KW_EMISSION); } if (gltfMaterial.extensions != null) { // Transmission - Approximation var transmission = gltfMaterial.extensions.KHR_materials_transmission; if (transmission != null) { renderQueue = ApplyTransmission(ref baseColorLinear, gltf, transmission, material, renderQueue); } } if (gltfMaterial.alphaModeEnum == AlphaMode.MASK) { SetAlphaModeMask(gltfMaterial, material); #if USING_HDRP if (gltfMaterial.extensions?.KHR_materials_unlit != null) { renderQueue = RenderQueue.Transparent; } else #endif renderQueue = RenderQueue.AlphaTest; } else { material.SetFloat(cutoffPropId, 0); // double sided opaque would make errors in HDRP 7.3 otherwise material.SetOverrideTag(TAG_MOTION_VECTOR, TAG_MOTION_VECTOR_USER); material.SetShaderPassEnabled(k_MotionVectorsPass, false); } if (!renderQueue.HasValue) { if (shaderMode == ShaderMode.Opaque) { renderQueue = gltfMaterial.alphaModeEnum == AlphaMode.MASK ? RenderQueue.AlphaTest : RenderQueue.Geometry; } else { renderQueue = RenderQueue.Transparent; } } material.renderQueue = (int)renderQueue.Value; if (gltfMaterial.doubleSided) { SetDoubleSided(gltfMaterial, material); } switch (shaderMode) { case ShaderMode.Opaque: SetShaderModeOpaque(gltfMaterial, material); break; case ShaderMode.Blend: SetShaderModeBlend(gltfMaterial, material); break; case ShaderMode.Premultiply: SetShaderModePremultiply(gltfMaterial, material); break; } material.SetVector(baseColorPropId, baseColorLinear); if (gltfMaterial.emissive != Color.black) { material.SetColor(emissionColorPropId, gltfMaterial.emissive); material.EnableKeyword(KW_EMISSION); } return(material); }
public EntityInstantiator(IGltfReadable gltf, Entity parent, ICodeLogger logger = null) { this.gltf = gltf; this.parent = parent; this.logger = logger; }