Ejemplo n.º 1
0
        Material GetMetallicMaterial(MetallicShaderFeatures metallicShaderFeatures)
        {
            bool doubleSided = (metallicShaderFeatures & MetallicShaderFeatures.DoubleSided) != 0;

            if (!metallicShaders.TryGetValue(metallicShaderFeatures, value: out var shader))
            {
                ShaderMode mode = (ShaderMode)(metallicShaderFeatures & MetallicShaderFeatures.ModeMask);
                // TODO: add ClearCoat support
                bool coat = false;  // (metallicShaderFeatures & MetallicShaderFeatures.ClearCoat) != 0;
                // TODO: add sheen support
                bool sheen = false; // (metallicShaderFeatures & MetallicShaderFeatures.Sheen) != 0;

                var shaderName = string.Format(
                    "Shader Graphs/glTF-metallic-{0}{1}{2}{3}",
                    mode,
                    coat ? "-coat" : "",
                    sheen ? "-sheen" : "",
                    doubleSided ? "-double" : ""
                    );
                shader = FindShader(shaderName);
                metallicShaders[metallicShaderFeatures] = shader;
            }
            if (shader == null)
            {
                return(null);
            }
            var mat = new Material(shader);

#if UNITY_EDITOR
            mat.doubleSidedGI = doubleSided;
#endif
            return(mat);
        }
Ejemplo n.º 2
0
        private bool compileShader(string source, ShaderMode shaderType)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(true);
            }

            int shader = _graphics.CreateShader(shaderType);

            _graphics.ShaderSource(shader, source);
            _graphics.CompileShader(shader);
            string info      = _graphics.GetShaderInfoLog(shader);
            int    errorCode = _graphics.GetShaderCompilationErrorCode(shader);

            if (errorCode != 1)
            {
                Debug.WriteLine($@"Failed to compile {shaderType}.{Environment.NewLine}Error code: {errorCode}.{Environment.NewLine}
                    Error message(s): {info ?? "null"}.{Environment.NewLine}Shader Source: {source}{Environment.NewLine}");
                _graphics.DeleteShader(shader);
                _program = 0;
                return(false);
            }

            _graphics.AttachShader(_program, shader);
            _graphics.DeleteShader(shader);
            return(true);
        }
Ejemplo n.º 3
0
 private void lblShaderMode_OnClick(object sender, EventArgs e)
 {
     fShaderMode = fShaderMode + 1;
     if (fShaderMode == ShaderMode.Last)
     {
         fShaderMode = ShaderMode.Basic;
     }
 }
Ejemplo n.º 4
0
    public static void setShader(ShaderMode mode, Material mat, Color color, bool show = true)
    {
        switch (mode)
        {
        case ShaderMode.Normal:
            mat.color = new Color(color.r, color.g, color.b, 120);
            //mat.shader = Shaders.transparent;
            break;

        case ShaderMode.RedOutline:
            mat.shader = Shaders.transparent;
            mat.color  = new Color(color.r, color.g, color.b, 120);
            break;
        }
    }
Ejemplo n.º 5
0
    public void setShader(ShaderMode mode, Color color = default(Color))
    {
        meshRendered.enabled = true;

        switch (mode)
        {
        case ShaderMode.Normal:
            mat.shader = originalShader;

            if (color == default(Color))
            {
                mat.color = originalColor;
            }
            else
            {
                mat.color = color;
            }

            break;

        case ShaderMode.RedOutline:
            mat.shader = Shaders.outline;
            mat.color  = new Color(221, 0, 0, 255);
            break;

        case ShaderMode.Outline:
            mat.shader = Shaders.outline;
            mat.color  = originalColor;
            break;

        case ShaderMode.Transparent:
            mat.shader    = Shaders.silhouetteOnly;
            mat.color     = originalColor;
            isTransparent = true;
            break;

        case ShaderMode.Invisible:
            mat.shader = originalShader;
            mat.color  = originalColor;

            meshRendered.enabled = false;
            break;
        }
    }
Ejemplo n.º 6
0
        private ShaderType getShaderType(ShaderMode shader)
        {
            switch (shader)
            {
            case ShaderMode.ComputeShader: return(ShaderType.ComputeShader);

            case ShaderMode.FragmentShader: return(ShaderType.FragmentShader);

            case ShaderMode.GeometryShader: return(ShaderType.GeometryShader);

            case ShaderMode.GeometryShaderExt: return(ShaderType.GeometryShaderExt);

            case ShaderMode.TessControlShader: return(ShaderType.TessControlShader);

            case ShaderMode.TessEvaluationShader: return(ShaderType.TessEvaluationShader);

            case ShaderMode.VertexShader: return(ShaderType.VertexShader);

            default: throw new NotSupportedException(shader.ToString());
            }
        }
Ejemplo n.º 7
0
    public void setShader(ShaderMode mode, Color color = default(Color))
    {
        meshRendered.enabled = true;

        switch (mode)
        {
        case ShaderMode.Normal:
            mat.shader = originalShader;

            if (color == default(Color)) {
                mat.color = originalColor;
            } else {
                mat.color = color;
            }

            break;
        case ShaderMode.RedOutline:
            mat.shader = Shaders.outline;
            mat.color = new Color(221, 0, 0, 255);
            break;
        case ShaderMode.Outline:
            mat.shader = Shaders.outline;
            mat.color = originalColor;
            break;
        case ShaderMode.Transparent:
            mat.shader = Shaders.silhouetteOnly;
            mat.color = originalColor;
            isTransparent = true;
            break;
        case ShaderMode.Invisible:
            mat.shader = originalShader;
            mat.color = originalColor;

            meshRendered.enabled = false;
            break;
        }
    }
Ejemplo n.º 8
0
 public int CreateShader(ShaderMode shaderType) => GL.CreateShader(getShaderType(shaderType));
Ejemplo n.º 9
0
        public override Material GenerateMaterial(
            Schema.Material gltfMaterial,
            ref Schema.Texture[] textures,
            ref Schema.Image[] schemaImages,
            ref Dictionary <int, Texture2D>[] imageVariants
            )
        {
            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, ref textures, ref schemaImages, ref imageVariants);

                    if (TrySetTexture(specGloss.specularGlossinessTexture, material, specGlossMapPropId, ref textures, ref schemaImages, ref imageVariants))
                    {
                        // 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,
                        ref textures,
                        ref schemaImages,
                        ref imageVariants
                        );
                }

                if (materialType == MaterialType.MetallicRoughness)
                {
                    material.SetFloat(metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor);
                    material.SetFloat(smoothnessPropId, 1 - gltfMaterial.pbrMetallicRoughness.roughnessFactor);

                    if (TrySetTexture(gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture, material, metallicRoughnessTexturePropId, ref textures, ref schemaImages, ref imageVariants))
                    {
                        // 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, ref textures, ref schemaImages, ref imageVariants))
            {
                // material.EnableKeyword(KW_NORMALMAP);
                material.SetFloat(bumpScalePropId, gltfMaterial.normalTexture.scale);
            }

            if (TrySetTexture(gltfMaterial.occlusionTexture, material, occlusionMapPropId, ref textures, ref schemaImages, ref imageVariants))
            {
                material.EnableKeyword(KW_OCCLUSION);
                material.SetFloat(occlusionStrengthPropId, gltfMaterial.occlusionTexture.strength);
            }

            if (TrySetTexture(gltfMaterial.emissiveTexture, material, emissionMapPropId, ref textures, ref schemaImages, ref imageVariants))
            {
                material.EnableKeyword(KW_EMISSION);
            }

            if (gltfMaterial.extensions != null)
            {
                // Transmission - Approximation
                var transmission = gltfMaterial.extensions.KHR_materials_transmission;
                if (transmission != null)
                {
                    renderQueue = ApplyTransmission(ref baseColorLinear, ref textures, ref schemaImages, ref imageVariants, 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);
        }
Ejemplo n.º 10
0
 public Shader(ShaderMode mode)
 {
     this.mode = mode;
     this.fragmentShaderFilename = "shader/fragment_shader.glsl";
     this.vertexShaderFilename   = "shader/vertex_shader.glsl";
 }
Ejemplo n.º 11
0
 public int CreateShader(ShaderMode shaderType)
 {
     return(GL.CreateShader(getShaderType(shaderType)));
 }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
0
		private bool compileShader(string source, ShaderMode shaderType)
		{
			if (string.IsNullOrEmpty(source)) return true;

			int shader = _graphics.CreateShader(shaderType);
			_graphics.ShaderSource(shader, source);
			_graphics.CompileShader(shader);
			string info = _graphics.GetShaderInfoLog(shader);
            int errorCode = _graphics.GetShaderCompilationErrorCode(shader);

			if (errorCode != 1)
			{
				Debug.WriteLine(string.Format("Failed to compile {0}.{4}Error code: {1}.{4}Error message(s): {2}.{4}Shader Source: {3}{4}",
					shaderType, errorCode, info ?? "null", source, Environment.NewLine));
				_graphics.DeleteShader(shader);
				_program = 0;
				return false;
			}

			_graphics.AttachShader(_program, shader);
			_graphics.DeleteShader(shader);
			return true;
		}