SetInt() public method

Set a named integer value.

public SetInt ( int nameID, int value ) : void
nameID int Property name ID, use Shader.PropertyToID to get it.
value int Integer value to set.
return void
        public static void CreateLineMaterial(int iMat = 1)
        {
            Material mMat = lineMaterial1;
            if (iMat == 2) mMat = lineMaterial2;
            if (iMat == 3) mMat = lineMaterial3;
            if (iMat == 4) mMat = lineMaterial4;
            if (iMat == 5) mMat = lineMaterial5;

            if (mMat == null)
            {
                var shader = Shader.Find("Hidden/Internal-Colored");
                mMat = new Material(shader);
                mMat.hideFlags = HideFlags.HideAndDontSave;
                // Turn on alpha blending
                mMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                mMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                // Turn backface culling off
                mMat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
                // Turn off depth writes
                mMat.SetInt("_ZWrite", 0);

                if (iMat == 1) lineMaterial1 = mMat;
                if (iMat == 2) lineMaterial2 = mMat;
                if (iMat == 3) lineMaterial3 = mMat;
                if (iMat == 4) lineMaterial4 = mMat;
                if (iMat == 5) lineMaterial5 = mMat;
            }
        }
示例#2
0
    IEnumerator Start()
    {
        foreach (var r in GetComponentsInChildren<Renderer>()) {
            renderers.Add(r);
            originalMaterials[r] = r.sharedMaterial;

            var material = new Material(r.material);
            material.SetFloat("_Mode", 2);
            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            material.SetInt("_ZWrite", 0);
            material.DisableKeyword("_ALPHATEST_ON");
            material.EnableKeyword("_ALPHABLEND_ON");
            material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = 3000;
            r.material = material;
        }

        for (float t = 0; t < 1f; t += Time.deltaTime) {
            if (!UpdateRenderers(t)) {
                yield break;
            }

            yield return null;
        }

        foreach (var r in renderers) {
            if (r) {
                r.material = originalMaterials[r];
            }
        }

        Destroy(this);
    }
    // Use this for initialization
    void Awake()
    {
        //Initalize render textures
        m_inputRenderTex = new RenderTexture(m_shadowSize, m_shadowSize, 0, RenderTextureFormat.ARGB32);
        m_middleRenderTex = new RenderTexture(m_shadowSize, 1, 0, RenderTextureFormat.ARGB32);

        //Initialize light material
        m_lightMaterial = new Material(Shader.Find("2DLighting/Deferred_Point_2"));
        m_lightMaterial.SetTexture("_DepthMap", m_middleRenderTex);
        m_lightMaterial.SetInt("_Penumbra", m_penumbra ? 1 : 0);
        m_lightMaterial.SetInt("_GradientFalloff", m_gradientFalloff ? 1 : 0);

        //Initialize phase one material
        phaseOneMaterial = new Material(Shader.Find("2DLighting/Deferred_Point_1"));
        phaseOneMaterial.SetFloat("_DistanceModifier", 0.001f);

        //Aquire camera component and initialize it
        m_camera = GetComponent<Camera>();
        m_camera.enabled = true;
        m_camera.targetTexture = m_inputRenderTex;

        //Create culling mask from given occluder layers
        int occluders = 0;
        for (int i = 0; i < m_occluderLayers.Count; i++)
        {
            occluders = occluders | (1 << LayerMask.NameToLayer(m_occluderLayers[i]));
        }
        m_camera.cullingMask = occluders;

        //Assign mesh material
        m_meshRenderer.material = m_lightMaterial;
    }
 public override Material CloneMaterial(Material src, int nth)
 {
     Material m = new Material(src);
     m.SetInt("g_batch_begin", nth * m_instances_par_batch);
     m.SetBuffer("particles", m_world.GetParticleBuffer());
     if (m_hdr)
     {
         m.SetInt("_SrcBlend", (int)BlendMode.One);
         m.SetInt("_DstBlend", (int)BlendMode.One);
     }
     else
     {
         m.SetInt("_SrcBlend", (int)BlendMode.DstColor);
         m.SetInt("_DstBlend", (int)BlendMode.Zero);
     }
     if(m_enable_shadow)
     {
         m.EnableKeyword("ENABLE_SHADOW");
         switch (m_sample)
         {
             case Sample.Fast:
                 m.EnableKeyword("QUALITY_FAST");
                 break;
             case Sample.Medium:
                 m.EnableKeyword("QUALITY_MEDIUM");
                 break;
             case Sample.High:
                 m.EnableKeyword("QUALITY_HIGH");
                 break;
         }
     }
     return m;
 }
示例#5
0
    public static void DrawProteinsAtoms(Material renderProteinsMaterial, Camera camera, RenderBuffer instanceId, RenderBuffer atomId, RenderBuffer depthBuffer, int pass)
    {
        // Protein params
        renderProteinsMaterial.SetInt("_EnableLod", Convert.ToInt32(GlobalProperties.Get.EnableLod));
        renderProteinsMaterial.SetFloat("_Scale", GlobalProperties.Get.Scale);
        renderProteinsMaterial.SetFloat("_FirstLevelBeingRange", GlobalProperties.Get.FirstLevelOffset);
        renderProteinsMaterial.SetVector("_CameraForward", camera.transform.forward);

        renderProteinsMaterial.SetBuffer("_LodLevelsInfos", GPUBuffers.Get.LodInfo);
        renderProteinsMaterial.SetBuffer("_ProteinInstanceInfo", GPUBuffers.Get.ProteinInstancesInfo);
        renderProteinsMaterial.SetBuffer("_ProteinInstancePositions", GPUBuffers.Get.ProteinInstancePositions);
        renderProteinsMaterial.SetBuffer("_ProteinInstanceRotations", GPUBuffers.Get.ProteinInstanceRotations);

        renderProteinsMaterial.SetBuffer("_ProteinColors", GPUBuffers.Get.IngredientsColors);
        renderProteinsMaterial.SetBuffer("_ProteinAtomInfo", GPUBuffers.Get.ProteinAtomInfo);
        renderProteinsMaterial.SetBuffer("_ProteinAtomPositions", GPUBuffers.Get.ProteinAtoms);
        renderProteinsMaterial.SetBuffer("_ProteinClusterPositions", GPUBuffers.Get.ProteinAtomClusters);
        renderProteinsMaterial.SetBuffer("_ProteinSphereBatchInfos", GPUBuffers.Get.SphereBatches);

        /****/
        renderProteinsMaterial.SetInt("_NumCutObjects", SceneManager.Get.NumCutObjects);
        renderProteinsMaterial.SetInt("_NumIngredientTypes", SceneManager.Get.NumAllIngredients);
        

        renderProteinsMaterial.SetBuffer("_CutInfos", GPUBuffers.Get.CutInfo);
        renderProteinsMaterial.SetBuffer("_CutScales", GPUBuffers.Get.CutScales);
        renderProteinsMaterial.SetBuffer("_CutPositions", GPUBuffers.Get.CutPositions);
        renderProteinsMaterial.SetBuffer("_CutRotations", GPUBuffers.Get.CutRotations);
        /****/

        Graphics.SetRenderTarget(new[] { instanceId, atomId }, depthBuffer);
        renderProteinsMaterial.SetPass(1);
        Graphics.DrawProceduralIndirect(MeshTopology.Points, GPUBuffers.Get.ArgBuffer);
    }
    public override Material CloneMaterial(Material src, int nth)
    {
        var instance_texture = m_world.GetInstanceTexture();

        Material m = new Material(src);
        m.SetInt("g_batch_begin", nth * m_instances_par_batch);
        m.SetTexture("g_instance_data", instance_texture);

        Vector4 ts = new Vector4(
            1.0f / instance_texture.width,
            1.0f / instance_texture.height,
            instance_texture.width,
            instance_texture.height);
        m.SetVector("g_instance_data_size", ts);

        if (m_hdr)
        {
            m.SetInt("_SrcBlend", (int)BlendMode.One);
            m.SetInt("_DstBlend", (int)BlendMode.One);
        }
        else
        {
            m.SetInt("_SrcBlend", (int)BlendMode.DstColor);
            m.SetInt("_DstBlend", (int)BlendMode.Zero);
        }

        if (m_enable_shadow)
        {
            m.EnableKeyword("ENABLE_SHADOW");
            switch (m_sample)
            {
                case Sample.Fast:
                    m.EnableKeyword ("QUALITY_FAST");
                    m.DisableKeyword("QUALITY_MEDIUM");
                    m.DisableKeyword("QUALITY_HIGH");
                    break;
                case Sample.Medium:
                    m.DisableKeyword("QUALITY_FAST");
                    m.EnableKeyword ("QUALITY_MEDIUM");
                    m.DisableKeyword("QUALITY_HIGH");
                    break;
                case Sample.High:
                    m.DisableKeyword("QUALITY_FAST");
                    m.DisableKeyword("QUALITY_MEDIUM");
                    m.EnableKeyword ("QUALITY_HIGH");
                    break;
            }
        }
        else
        {
            m.DisableKeyword("ENABLE_SHADOW");
        }

        return m;
    }
 private void setMaterialToFade(Material m)
 {
     m.SetFloat("_Mode", 2);
     m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
     m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     m.SetInt("_ZWrite", 0);
     m.DisableKeyword("_ALPHATEST_ON");
     m.EnableKeyword("_ALPHABLEND_ON");
     m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     m.renderQueue = 3000;
 }
 private void setMaterialToOpaque(Material m)
 {
     m.SetFloat("_Mode", 0);
     m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
     m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
     m.SetInt("_ZWrite", 1);
     m.DisableKeyword("_ALPHATEST_ON");
     m.DisableKeyword("_ALPHABLEND_ON");
     m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     m.renderQueue = m.shader.renderQueue;
 }
示例#9
0
 /// <summary>
 /// sets rendering mode of 'material' to transparent
 /// </summary>
 public static void MakeMaterialTransparent(UnityEngine.Material material)
 {
     material.SetFloat("_Mode", 2);
     material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
     material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     material.SetInt("_ZWrite", 0);
     material.DisableKeyword("_ALPHATEST_ON");
     material.EnableKeyword("_ALPHABLEND_ON");
     material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     material.renderQueue = 3000;
 }
 public static void SetTransparent(Material material)
 {
     material.SetOverrideTag("RenderType", "Transparent");
     material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
     material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     material.SetInt("_ZWrite", 0);
     material.DisableKeyword("_ALPHATEST_ON");
     material.DisableKeyword("_ALPHABLEND_ON");
     material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
     material.renderQueue = 3000;
 }
示例#11
0
 void Transparent(Material material)
 {
     material.SetFloat("_Mode", 3);
     material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
     material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     material.SetInt("_ZWrite", 0);
     material.DisableKeyword("_ALPHATEST_ON");
     material.DisableKeyword("_ALPHABLEND_ON");
     material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
     material.renderQueue = 3000;
 }
 public static void SetOpaqueMode(UnityEngine.Material material)
 {
     material.SetOverrideTag("RenderType", "Opaque");
     material.SetInt(srcBlendPropId, (int)UnityEngine.Rendering.BlendMode.One);
     material.SetInt(dstBlendPropId, (int)UnityEngine.Rendering.BlendMode.Zero);
     material.SetInt(zWritePropId, 1);
     material.DisableKeyword("_ALPHATEST_ON");
     material.DisableKeyword("_ALPHABLEND_ON");
     material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     material.renderQueue = -1;
 }
示例#13
0
 void Opaque(Material material)
 {
     material.SetFloat("_Mode", 0);
     material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
     material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
     material.SetInt("_ZWrite", 1);
     material.DisableKeyword("_ALPHATEST_ON");
     material.DisableKeyword("_ALPHABLEND_ON");
     material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     material.renderQueue = -1;
 }
示例#14
0
 public static void SetOpaque(Material material)
 {
     material.SetOverrideTag("RenderType", "");
     material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
     material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
     material.SetInt("_ZWrite", 1);
     material.DisableKeyword("_ALPHATEST_ON");
     material.DisableKeyword("_ALPHABLEND_ON");
     material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     material.renderQueue = -1;
 }
        public override void Import(EditorImporter importer, UnityEngine.Material material, GLTF.Schema.Material gltfMat, Extension extension)
        {
            var mat       = (Sein_customMaterialExtension)extension;
            var className = "Sein/" + mat.className;
            var shader    = Shader.Find(className);

            var alphaMode = gltfMat.AlphaMode;

            if (alphaMode == AlphaMode.BLEND)
            {
                mat.transparent = true;
            }
            else if (alphaMode == AlphaMode.MASK)
            {
                mat.transparent = true;
            }

            if (shader == null)
            {
                shader = Shader.Find(className.Replace("Material", ""));
            }

            if (shader == null)
            {
                mat.material    = material;
                mat.isComponent = true;
                return;
            }

            material.shader = shader;
            if (material.HasProperty("cloneForInst"))
            {
                material.SetInt("cloneForInst", 1);
            }


            material.SetInt("_Mode", (int)alphaMode);
            material.SetFloat("_Cutoff", (float)gltfMat.AlphaCutoff);

            WriteUiforms(importer, material, mat.uniformsTexture);
            WriteUiforms(importer, material, mat.uniformsCubeTexture);
            WriteUiforms(importer, material, mat.uniformsFloat);
            WriteUiforms(importer, material, mat.uniformsFloatVec2);
            WriteUiforms(importer, material, mat.uniformsFloatVec3);
            WriteUiforms(importer, material, mat.uniformsFloatVec4);
            WriteUiforms(importer, material, mat.uniformsColor);
            WriteUiforms(importer, material, mat.uniformsFloatMat2);
            WriteUiforms(importer, material, mat.uniformsFloatMat3);
            WriteUiforms(importer, material, mat.uniformsFloatMat4);
            WriteUiforms(importer, material, mat.uniformsInt);
            WriteUiforms(importer, material, mat.uniformsIntVec2);
            WriteUiforms(importer, material, mat.uniformsIntVec3);
            WriteUiforms(importer, material, mat.uniformsIntVec4);
        }
示例#16
0
 void CreateMaterial()
 {
     var shader = Shader.Find("Hidden/Internal-Colored");
     mat = new Material(shader);
     mat.hideFlags = HideFlags.HideAndDontSave;
     mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusDstColor);
     mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
     // Turn off backface culling, depth writes, depth test.
     mat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
     mat.SetInt("_ZWrite", 0);
     mat.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
 }
示例#17
0
 void Start()
 {
     Material m = new Material(Shader.Find("Custom/StandardDepthBuffer"));
     m.SetFloat("_Mode", 2);
     m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
     m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     m.SetInt("_ZWrite", 0);
     m.DisableKeyword("_ALPHATEST_ON");
     m.EnableKeyword("_ALPHABLEND_ON");
     m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     m.renderQueue = 3000;
 }
 public void ApplyOpaque(Color col)
 {
     rend = this.gameObject.GetComponent<Renderer>().material;
     rend.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
     rend.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
     rend.SetInt("_ZWrite", 1);
     rend.DisableKeyword("_ALPHATEST_ON");
     rend.DisableKeyword("_ALPHABLEND_ON");
     rend.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     rend.renderQueue = -1;
     StartCoroutine(colorLerp1(rend.GetColor("_Color"), col));
 }
 public static void SetAlphaModeBlend(UnityEngine.Material material)
 {
     material.SetFloat(modePropId, (int)StandardShaderMode.Transparent);
     material.SetOverrideTag("RenderType", "Transparent");
     material.SetInt(srcBlendPropId, (int)UnityEngine.Rendering.BlendMode.SrcAlpha);         //5
     material.SetInt(dstBlendPropId, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); //10
     material.SetInt(zWritePropId, 0);
     material.DisableKeyword("_ALPHATEST_ON");
     material.EnableKeyword("_ALPHABLEND_ON");
     material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;  //3000
 }
示例#20
0
 Material CreateMaterial(Color emissionColor)
 {
     Material m = new Material(outlineBufferShader);
     m.SetColor("_Color", emissionColor);
     m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
     m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     m.SetInt("_ZWrite", 0);
     m.DisableKeyword("_ALPHATEST_ON");
     m.EnableKeyword("_ALPHABLEND_ON");
     m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     m.renderQueue = 3000;
     return m;
 }
示例#21
0
 static void CreateLineMaterial()
 {
     if (!lineMaterial)
     {
         var shader = Shader.Find("Hidden/Internal-Colored");
         lineMaterial = new Material(shader);
         lineMaterial.hideFlags = HideFlags.HideAndDontSave;
         lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
         lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
         lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
         lineMaterial.SetInt("_ZWrite", 0);
     }
 }
 public static void SetAlphaModeMask(UnityEngine.Material material, Schema.Material gltfMaterial)
 {
     material.SetFloat(modePropId, (int)StandardShaderMode.Cutout);
     material.SetOverrideTag("RenderType", "TransparentCutout");
     material.SetInt(srcBlendPropId, (int)UnityEngine.Rendering.BlendMode.One);
     material.SetInt(dstBlendPropId, (int)UnityEngine.Rendering.BlendMode.Zero);
     material.SetInt(zWritePropId, 1);
     material.EnableKeyword("_ALPHATEST_ON");
     material.DisableKeyword("_ALPHABLEND_ON");
     material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
     material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;  //2450
     material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff);
 }
 void ApplyTransparent(Color col)
 {
     rend = this.gameObject.GetComponent<Renderer>().material;
     rend.SetFloat("_Mode", 3f);
     rend.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
     rend.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     rend.SetInt("_ZWrite", 0);
     rend.DisableKeyword("_ALPHATEST_ON");
     rend.DisableKeyword("_ALPHABLEND_ON");
     rend.EnableKeyword("_ALPHAPREMULTIPLY_ON");
     rend.renderQueue = 3000;
     StartCoroutine(colorLerp1(rend.GetColor("_Color"), col));
 }
 public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode)
 {
     switch (blendMode)
     {
     case BlendMode.Opaque:
         material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
         material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
         material.SetInt("_ZWrite", 1);
         material.DisableKeyword("_ALPHATEST_ON");
         material.DisableKeyword("_ALPHABLEND_ON");
         material.renderQueue = -1;
         break;
     case BlendMode.Cutout:
         material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
         material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
         material.SetInt("_ZWrite", 1);
         material.EnableKeyword("_ALPHATEST_ON");
         material.DisableKeyword("_ALPHABLEND_ON");
         material.renderQueue = 2450;
         break;
     case BlendMode.Transparent:
         material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
         material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
         material.SetInt("_ZWrite", 0);
         material.DisableKeyword("_ALPHATEST_ON");
         material.EnableKeyword("_ALPHABLEND_ON");
         material.renderQueue = 3000;
         break;
     }
 }
示例#25
0
 void CreateLineMaterial()
 {
     // Unity has a built-in shader that is useful for drawing simple colored things
     var shader = Shader.Find("Hidden/Internal-Colored");
     lineMaterial = new Material(shader);
     lineMaterial.hideFlags = HideFlags.HideAndDontSave;
     // Turn on alpha blending
     lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
     lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     // Turn backface culling off
     lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
     // Turn off depth writes
     lineMaterial.SetInt("_ZWrite", 0);
 }
示例#26
0
        public static void SetOpaque(Material material)
        {
            if (material.shader != StandardShader)
                Debug.LogWarning("Trying to set opaque mode on non-standard shader. Please use the Standard Shader instead or modify this method.");

            material.SetOverrideTag("RenderType", "");
            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
            material.SetInt("_ZWrite", 1);
            material.DisableKeyword("_ALPHATEST_ON");
            material.DisableKeyword("_ALPHABLEND_ON");
            material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = -1;
        }
 public static Material CreateFontMaterial()
 {
     var shader = Shader.Find ("Hidden/Internal-Colored");
     var mat = new Material (shader);
     mat.hideFlags = HideFlags.HideAndDontSave;
     // Turn on alpha blending
     mat.SetInt ("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
     mat.SetInt ("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
     // Turn backface culling off
     mat.SetInt ("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
     // Turn off depth writes and depth testing
     mat.SetInt ("_ZWrite", 0);
     mat.SetInt ("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
     return mat;
 }
示例#28
0
    public void Start()
    {
        var shader = Shader.Find("Hidden/Internal-Colored");
        MatLine = new Material(shader);

        MatLine.hideFlags = HideFlags.HideAndDontSave;

        // Set blend mode to invert destination colors.
        MatLine.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusDstColor);
        MatLine.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);

        // Turn off backface culling, depth writes, depth test.
        MatLine.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
        MatLine.SetInt("_ZWrite", 0);
        MatLine.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
    }
示例#29
0
 static public int SetInt(IntPtr l)
 {
     try{
         if (matchType(l, 2, typeof(System.String), typeof(System.Int32)))
         {
             UnityEngine.Material self = (UnityEngine.Material)checkSelf(l);
             System.String        a1;
             checkType(l, 2, out a1);
             System.Int32 a2;
             checkType(l, 3, out a2);
             self.SetInt(a1, a2);
             return(0);
         }
         else if (matchType(l, 2, typeof(System.Int32), typeof(System.Int32)))
         {
             UnityEngine.Material self = (UnityEngine.Material)checkSelf(l);
             System.Int32         a1;
             checkType(l, 2, out a1);
             System.Int32 a2;
             checkType(l, 3, out a2);
             self.SetInt(a1, a2);
             return(0);
         }
         LuaDLL.luaL_error(l, "No matched override function to call");
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
示例#30
0
    public static void DrawProteinSphereBatches(Material renderProteinsMaterial, Camera camera, RenderBuffer colorBuffer, RenderBuffer depthBuffer, int pass, bool animated = false)
    {
        // Protein params
        renderProteinsMaterial.SetInt("_EnableLod", Convert.ToInt32(PersistantSettings.Instance.EnableLod));
        renderProteinsMaterial.SetFloat("_Scale", PersistantSettings.Instance.Scale);
        renderProteinsMaterial.SetFloat("_FirstLevelBeingRange", PersistantSettings.Instance.FirstLevelOffset);
        renderProteinsMaterial.SetVector("_CameraForward", camera.transform.forward);

        renderProteinsMaterial.SetBuffer("_LodLevelsInfos", GPUBuffers.Instance.LodInfo);
        renderProteinsMaterial.SetBuffer("_ProteinInstanceInfo", GPUBuffers.Instance.ProteinInstanceInfo);

        if(animated) renderProteinsMaterial.SetBuffer("_ProteinInstancePositions", GPUBuffers.Instance.ProteinAnimationPositions);
        else renderProteinsMaterial.SetBuffer("_ProteinInstancePositions", GPUBuffers.Instance.ProteinInstancePositions);

        if (animated) renderProteinsMaterial.SetBuffer("_ProteinInstanceRotations", GPUBuffers.Instance.ProteinAnimationRotations);
        else renderProteinsMaterial.SetBuffer("_ProteinInstanceRotations", GPUBuffers.Instance.ProteinInstanceRotations);

        renderProteinsMaterial.SetBuffer("_ProteinAtomCount", GPUBuffers.Instance.ProteinAtomCount);
        renderProteinsMaterial.SetBuffer("_ProteinColors", GPUBuffers.Instance.ProteinColors);
        renderProteinsMaterial.SetBuffer("_ProteinAtomPositions", GPUBuffers.Instance.ProteinAtoms);
        renderProteinsMaterial.SetBuffer("_ProteinClusterPositions", GPUBuffers.Instance.ProteinAtomClusters);
        renderProteinsMaterial.SetBuffer("_ProteinSphereBatchInfos", GPUBuffers.Instance.SphereBatches);

        Graphics.SetRenderTarget(colorBuffer, depthBuffer);
        renderProteinsMaterial.SetPass(pass);
        Graphics.DrawProceduralIndirect(MeshTopology.Points, GPUBuffers.Instance.ArgBuffer);
    }
 public override void Import(EditorImporter importer, UnityEngine.Material material, GLTF.Schema.Material gltfMat, Extension extension)
 {
     if (material.HasProperty("unlit"))
     {
         material.SetInt("unlit", 1);
     }
 }
    static int SetInt(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 3 && TypeChecker.CheckTypes(L, 1, typeof(UnityEngine.Material), typeof(int), typeof(int)))
            {
                UnityEngine.Material obj = (UnityEngine.Material)ToLua.ToObject(L, 1);
                int arg0 = (int)LuaDLL.lua_tonumber(L, 2);
                int arg1 = (int)LuaDLL.lua_tonumber(L, 3);
                obj.SetInt(arg0, arg1);
                return(0);
            }
            else if (count == 3 && TypeChecker.CheckTypes(L, 1, typeof(UnityEngine.Material), typeof(string), typeof(int)))
            {
                UnityEngine.Material obj = (UnityEngine.Material)ToLua.ToObject(L, 1);
                string arg0 = ToLua.ToString(L, 2);
                int    arg1 = (int)LuaDLL.lua_tonumber(L, 3);
                obj.SetInt(arg0, arg1);
                return(0);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Material.SetInt"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
示例#33
0
 static public int SetInt(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, argc, 2, typeof(int), typeof(int)))
         {
             UnityEngine.Material self = (UnityEngine.Material)checkSelf(l);
             System.Int32         a1;
             checkType(l, 2, out a1);
             System.Int32 a2;
             checkType(l, 3, out a2);
             self.SetInt(a1, a2);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, argc, 2, typeof(string), typeof(int)))
         {
             UnityEngine.Material self = (UnityEngine.Material)checkSelf(l);
             System.String        a1;
             checkType(l, 2, out a1);
             System.Int32 a2;
             checkType(l, 3, out a2);
             self.SetInt(a1, a2);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
示例#34
0
 /// <summary>
 /// Permet de changer le mode du shader standard
 /// </summary>
 /// <param name="material">le material à utiliser</param>
 /// <param name="blendMode"> le mode voulu : Opaque , Cutout, Fade, Transparent</param>
 public static void SetupMaterialWithBlendMode(Material material, string blendMode)
 {
     switch (blendMode)
     {
         case "Opaque":
             material.SetFloat("_Mode", 0);
             material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
             material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
             material.SetInt("_ZWrite", 1);
             material.DisableKeyword("_ALPHATEST_ON");
             material.DisableKeyword("_ALPHABLEND_ON");
             material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
             material.renderQueue = -1;
             break;
         case "Cutout":
             material.SetFloat("_Mode", 1);
             material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
             material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
             material.SetInt("_ZWrite", 1);
             material.EnableKeyword("_ALPHATEST_ON");
             material.DisableKeyword("_ALPHABLEND_ON");
             material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
             material.renderQueue = 2450;
             break;
         case "Fade":
             material.SetFloat("_Mode", 2);
             material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
             material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
             material.SetInt("_ZWrite", 0);
             material.DisableKeyword("_ALPHATEST_ON");
             material.EnableKeyword("_ALPHABLEND_ON");
             material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
             material.renderQueue = 3000;
             break;
         case "Transparent":
             material.SetFloat("_Mode", 3);
             material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
             material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
             material.SetInt("_ZWrite", 0);
             material.DisableKeyword("_ALPHATEST_ON");
             material.DisableKeyword("_ALPHABLEND_ON");
             material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
             material.renderQueue = 3000;
             break;
     }
 }
        private void SetupMaterialBlendMode(UnityEngine.Material material)
        {
            bool isUsingGeometryRenderQueue = material.renderQueue == 2000;

            material.SetOverrideTag("RenderType", isUsingGeometryRenderQueue ? "Opaque" : "Transparent");
            material.SetInt("_ZWrite", isUsingGeometryRenderQueue ? 1 : 0);

            if (isUsingGeometryRenderQueue)
            {
                material.SetInt("_SrcBlend", (int)BlendMode.One);
                material.SetInt("_DstBlend", (int)BlendMode.Zero);
            }
            else
            {
                material.SetInt("_SrcBlend", (int)BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)BlendMode.OneMinusSrcAlpha);
            }
        }
示例#36
0
    public static void SetMaterialRenderingMode(Material material, StandardShaderRenderingMode mode)
    {
        switch (mode)
        {
        case StandardShaderRenderingMode.Opaque:
            material.SetOverrideTag("RenderType", "");
            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
            material.SetInt("_ZWrite", 1);
            material.DisableKeyword("_ALPHATEST_ON");
            material.DisableKeyword("_ALPHABLEND_ON");
            material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = -1;
            break;

        case StandardShaderRenderingMode.Cutout:
            material.SetOverrideTag("RenderType", "TransparentCutout");
            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
            material.SetInt("_ZWrite", 1);
            material.EnableKeyword("_ALPHATEST_ON");
            material.DisableKeyword("_ALPHABLEND_ON");
            material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = 2450;
            break;

        case StandardShaderRenderingMode.Fade:
            material.SetOverrideTag("RenderType", "Transparent");
            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            material.SetInt("_ZWrite", 0);
            material.DisableKeyword("_ALPHATEST_ON");
            material.EnableKeyword("_ALPHABLEND_ON");
            material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = 3000;
            break;

        case StandardShaderRenderingMode.Transparent:
            material.SetOverrideTag("RenderType", "Transparent");
            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            material.SetInt("_ZWrite", 0);
            material.DisableKeyword("_ALPHATEST_ON");
            material.DisableKeyword("_ALPHABLEND_ON");
            material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
            material.renderQueue = 3000;
            break;
        }
    }
示例#37
0
        public static void SetTransparent(Material material, Color? newcolor = null)
        {
            material.SetOverrideTag("RenderType", "Transparent");
            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            material.SetInt("_ZWrite", 0);
            material.DisableKeyword("_ALPHATEST_ON");
            material.DisableKeyword("_ALPHABLEND_ON");
            material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
            material.SetFloat("_Metallic", 0f);
            material.SetFloat("_Glossiness", 0f);
            material.renderQueue = 3000;
            material.mainTexture = null;

            if (newcolor != null)
            {
                material.color = newcolor.Value;
            }
        }
        public override bool CheckResources()
        {
            this.CheckSupport(false);
            _mat = this.CheckShaderAndCreateMaterial(_shader, _mat);

            if (isSupported) {
                _mat.SetTexture("_AsciiMapTex", _asciiMapTexture);
                _mat.SetInt("_AsciiMapCharacterCount", _asciiMapCharacterCount);

                _mat.SetFloat("_tilesX", _tilesX);
                _mat.SetFloat("_tilesY", _tilesY);

                _mat.SetInt("_tilesW", (int)(1 / _tilesX));
                _mat.SetInt("_tilesH", (int)(1 / _tilesY));

                _mat.SetFloat("_darkness", _darkness);
            }

            return isSupported;
        }
示例#39
0
	private void OnRender(Material mat)
	{
		//
		if( !m_bInit )
			return;

		if( m_DepthPoints == null || m_BodyIndexPoints == null)
		{
			return;
		}

		// TODO: fix perf on this call.
		m_DepthBuffer.SetData(m_DepthPoints);
		//m_DepthBuffer.SetData(dss);		
		mat.SetBuffer( "depthCoordinates", m_DepthBuffer );
		// ComputeBuffers do not accept bytes, so we need to convert to float.
		float[] buffer = new float[m_BodyIndexPoints.Length];
		for (int i = 0; i < m_BodyIndexPoints.Length; i++)
		{
			buffer[i] = (float)m_BodyIndexPoints[i];
		}

		m_BodyIndexBuffer.SetData(buffer);
		mat.SetBuffer ( "bodyIndexBuffer", m_BodyIndexBuffer );
		for(int i = 0;i<6;i++)
		{
			mat.SetColor("bodyIndexColor" + i,BodyColor[i]);
		}

		buffer = null;

		UserManager m_UserManager = UserManager.Instance;
		if (!m_UserManager.Inited || !m_UserManager.HasLockUser)
		{
			mat.SetInt("lockBodyIndex",255);
			return;
		}
		
		Windows.Kinect.Body lockBody = m_UserManager.LockBody;
		
		if (lockBody != null)
		{
			mat.SetInt("lockBodyIndex",m_UserManager.LockBodyIndex);

			Debug.Log("lockBodyIndex : " + m_UserManager.LockBodyIndex);
		} 
		else
		{
			mat.SetInt("lockBodyIndex",255);
		}

	}
示例#40
0
        public static void SetTransparent(Material material, Color? newcolor = null)
        {
            if (material.shader != StandardShader)
                Debug.LogWarning("Trying to set transparent mode on non-standard shader. Please use the Standard Shader instead or modify this method.");

            material.SetOverrideTag("RenderType", "Transparent");
            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            material.SetInt("_ZWrite", 0);
            material.DisableKeyword("_ALPHATEST_ON");
            material.DisableKeyword("_ALPHABLEND_ON");
            material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
            material.SetFloat("_Metallic", 0f);
            material.SetFloat("_Glossiness", 0f);
            material.renderQueue = 3000;
            material.mainTexture = null;

            if (newcolor != null)
            {
                material.color = newcolor.Value;
            }
        }
示例#41
0
    public static UnityEngine.Material ToUnityMaterial(this Elements.Material material)
    {
        var uMaterial = new UnityEngine.Material(Shader.Find("Standard"));

        uMaterial.name  = material.Name;
        uMaterial.color = material.Color.ToUnityColor();
        uMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
        if (material.Color.Alpha < 1.0f)
        {
            uMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            uMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            uMaterial.SetInt("_ZWrite", 0);
            uMaterial.DisableKeyword("_ALPHATEST_ON");
            uMaterial.DisableKeyword("_ALPHABLEND_ON");
            uMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
            uMaterial.SetFloat("_Glossiness", material.GlossinessFactor);
            // uMaterial.SetFloat("_Metallic", material.SpecularFactor);
            uMaterial.renderQueue = 3000;
        }

        return(uMaterial);
    }
示例#42
0
        private static void ApplyBlendMode(UnityEngine.Material unityMaterial, BlendMode blendMode)
        {
            switch (blendMode)
            {
            case BlendMode.Opaque:
                unityMaterial.SetFloat(Mode, 0);
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);
                unityMaterial.DisableKeyword("_ALPHATEST_ON");
                unityMaterial.DisableKeyword("_ALPHABLEND_ON");
                unityMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                unityMaterial.renderQueue = -1;
                break;

            case BlendMode.Cutout:
                unityMaterial.SetFloat(Mode, 1);
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);
                unityMaterial.EnableKeyword("_ALPHATEST_ON");
                unityMaterial.DisableKeyword("_ALPHABLEND_ON");
                unityMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                unityMaterial.renderQueue = 2450;
                break;

            case BlendMode.Translucent:
                unityMaterial.SetFloat(Mode, 3);
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                //!!!!!!! this is normally switched off but somehow enabling it seems to resolve so many issues.. keep an eye out for weirld opacity issues
                //unityMaterial.SetInt("_ZWrite", 0);
                unityMaterial.SetInt(ZWrite, 1);
                unityMaterial.DisableKeyword("_ALPHATEST_ON");
                unityMaterial.DisableKeyword("_ALPHABLEND_ON");
                unityMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
                unityMaterial.renderQueue = 3000;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
示例#43
0
        void DrawMesh(UnityEngine.Mesh mesh, float4x4 localToWorld)
        {
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }
            var wireFrame = GL.wireframe;

            GL.wireframe = true;
            var lighting = Handles.lighting;

            Handles.lighting = false;
            Shader.SetGlobalColor("_HandleColor", Handles.color);
            Shader.SetGlobalFloat("_HandleSize", 1f);
            Shader.SetGlobalMatrix("_ObjectToWorld", localToWorld);
            m_PreviewMeshMaterial.SetInt("_HandleZTest", (int)Handles.zTest);
            m_PreviewMeshMaterial.SetPass(0);
            Graphics.DrawMeshNow(mesh, localToWorld);
            Handles.lighting = lighting;
            GL.wireframe     = wireFrame;
        }
示例#44
0
        protected virtual UnityEngine.Material CreateMaterial(Material def, bool useVertexColors)
        {
            if (def.ContentsWithVC == null || def.ContentsWithoutVC == null)
            {
                Shader shader;

                // get the shader to use for this material
                try
                {
                    if (def.PbrMetallicRoughness != null)
                    {
                        shader = _shaderCache[MaterialType.PbrMetallicRoughness];
                    }
                    else if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains("KHR_materials_common") &&
                             def.CommonConstant != null)
                    {
                        shader = _shaderCache[MaterialType.CommonConstant];
                    }
                    else
                    {
                        shader = _shaderCache[MaterialType.PbrMetallicRoughness];
                    }
                }
                catch (KeyNotFoundException e)
                {
                    Debug.LogWarningFormat("No shader supplied for type of glTF material {0}, using Standard fallback", def.Name);
                    shader = Shader.Find("Standard");
                }

                shader.maximumLOD = MaximumLod;

                var material = new UnityEngine.Material(shader);

                if (def.AlphaMode == AlphaMode.MASK)
                {
                    material.SetOverrideTag("RenderType", "TransparentCutout");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.SetInt("_ZWrite", 1);
                    material.EnableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
                    material.SetFloat("_Cutoff", (float)def.AlphaCutoff);
                }
                else if (def.AlphaMode == AlphaMode.BLEND)
                {
                    material.SetOverrideTag("RenderType", "Transparent");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    material.SetInt("_ZWrite", 0);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.EnableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
                }
                else
                {
                    material.SetOverrideTag("RenderType", "Opaque");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.SetInt("_ZWrite", 1);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = -1;
                }

                if (def.DoubleSided)
                {
                    material.SetInt("_Cull", (int)CullMode.Off);
                }
                else
                {
                    material.SetInt("_Cull", (int)CullMode.Back);
                }

                if (def.PbrMetallicRoughness != null)
                {
                    var pbr = def.PbrMetallicRoughness;

                    material.SetColor("_Color", pbr.BaseColorFactor);

                    if (pbr.BaseColorTexture != null)
                    {
                        var texture = pbr.BaseColorTexture.Index.Value;
                        material.SetTexture("_MainTex", CreateTexture(texture));
                    }

                    material.SetFloat("_Metallic", (float)pbr.MetallicFactor);

                    if (pbr.MetallicRoughnessTexture != null)
                    {
                        var texture = pbr.MetallicRoughnessTexture.Index.Value;
                        material.SetTexture("_MetallicRoughnessMap", CreateTexture(texture));
                    }

                    material.SetFloat("_Roughness", (float)pbr.RoughnessFactor);
                }

                if (def.CommonConstant != null)
                {
                    material.SetColor("_AmbientFactor", def.CommonConstant.AmbientFactor);

                    if (def.CommonConstant.LightmapTexture != null)
                    {
                        material.EnableKeyword("LIGHTMAP_ON");

                        var texture = def.CommonConstant.LightmapTexture.Index.Value;
                        material.SetTexture("_LightMap", CreateTexture(texture));
                        material.SetInt("_LightUV", def.CommonConstant.LightmapTexture.TexCoord);
                    }

                    material.SetColor("_LightFactor", def.CommonConstant.LightmapFactor);
                }

                if (def.NormalTexture != null)
                {
                    var texture = def.NormalTexture.Index.Value;
                    material.SetTexture("_BumpMap", CreateTexture(texture));
                    material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale);
                }

                if (def.OcclusionTexture != null)
                {
                    var texture = def.OcclusionTexture.Index;

                    material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength);

                    if (def.PbrMetallicRoughness != null &&
                        def.PbrMetallicRoughness.MetallicRoughnessTexture != null &&
                        def.PbrMetallicRoughness.MetallicRoughnessTexture.Index.Id == texture.Id)
                    {
                        material.EnableKeyword("OCC_METAL_ROUGH_ON");
                    }
                    else
                    {
                        material.SetTexture("_OcclusionMap", CreateTexture(texture.Value));
                    }
                }

                if (def.EmissiveTexture != null)
                {
                    var texture = def.EmissiveTexture.Index.Value;
                    material.EnableKeyword("EMISSION_MAP_ON");
                    material.SetTexture("_EmissionMap", CreateTexture(texture));
                    material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord);
                }

                material.SetColor("_EmissionColor", def.EmissiveFactor);

                def.ContentsWithoutVC = material;
                def.ContentsWithVC    = new UnityEngine.Material(material);
                def.ContentsWithVC.EnableKeyword("VERTEX_COLOR_ON");
            }

            return(def.GetContents(useVertexColors));
        }
        private void WriteUiforms <TValue>(EditorImporter importer, UnityEngine.Material material, SeinMaterialUniform <TValue>[] uniforms)
        {
            foreach (SeinMaterialUniform <TValue> uniform in uniforms)
            {
                var name = uniform.name;
                switch (uniform.type)
                {
                case (ESeinMaterialUniformType.FLOAT):
                    material.SetFloat(name, (uniform as SeinMaterialUniformFloat).value);
                    break;

                case (ESeinMaterialUniformType.INT):
                    material.SetInt(name, (uniform as SeinMaterialUniformInt).value);
                    break;

                case (ESeinMaterialUniformType.SAMPLER_2D):
                    var tex = importer.GetTexture((uniform as SeinMaterialUniformTexture).id.Id);
                    material.SetTexture(name, tex);
                    break;

                // todo: support cubemap
                case (ESeinMaterialUniformType.SAMPLER_CUBE):
                    break;

                case (ESeinMaterialUniformType.FLOAT_VEC2):
                    var fv2 = (uniform as SeinMaterialUniformFloatVec2).value;
                    material.SetFloatArray(name, new List <float> {
                        fv2.x, fv2.y
                    });
                    break;

                case (ESeinMaterialUniformType.FLOAT_VEC3):
                    var fv3 = (uniform as SeinMaterialUniformFloatVec3).value;
                    material.SetFloatArray(name, new List <float> {
                        fv3.x, fv3.y, fv3.z
                    });
                    break;

                case (ESeinMaterialUniformType.FLOAT_VEC4):
                    if (uniform.GetType() == typeof(SeinMaterialUniformColor))
                    {
                        material.SetColor(name, (uniform as SeinMaterialUniformColor).value);
                    }
                    material.SetVector(name, (uniform as SeinMaterialUniformFloatVec4).value);
                    break;

                case (ESeinMaterialUniformType.FLOAT_MAT2):
                    material.SetMatrix(name, (uniform as SeinMaterialUniformFloatMat2).value);
                    break;

                case (ESeinMaterialUniformType.FLOAT_MAT3):
                    material.SetMatrix(name, (uniform as SeinMaterialUniformFloatMat3).value);
                    break;

                case (ESeinMaterialUniformType.FLOAT_MAT4):
                    material.SetMatrix(name, (uniform as SeinMaterialUniformFloatMat4).value);
                    break;

                default:
                    break;
                }
            }
        }
        protected virtual MaterialCacheData CreateMaterial(GLTF.Schema.Material def, int materialIndex)
        {
            MaterialCacheData materialWrapper = null;

            if (materialIndex < 0 || _assetCache.MaterialCache[materialIndex] == null)
            {
                Shader shader;

                // get the shader to use for this material
                try
                {
                    if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains("KHR_materials_pbrSpecularGlossiness"))
                    {
                        shader = _shaderCache[MaterialType.KHR_materials_pbrSpecularGlossiness];
                    }
                    else if (def.PbrMetallicRoughness != null)
                    {
                        shader = _shaderCache[MaterialType.PbrMetallicRoughness];
                    }
                    else if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains("KHR_materials_common") &&
                             def.CommonConstant != null)
                    {
                        shader = _shaderCache[MaterialType.CommonConstant];
                    }
                    else
                    {
                        shader = _shaderCache[MaterialType.PbrMetallicRoughness];
                    }
                }
                catch (KeyNotFoundException)
                {
                    Debug.LogWarningFormat("No shader supplied for type of glTF material {0}, using Standard fallback", def.Name);
                    shader = Shader.Find("Standard");
                }

                shader.maximumLOD = MaximumLod;

                var material = new UnityEngine.Material(shader);

                if (def.AlphaMode == AlphaMode.MASK)
                {
                    material.SetOverrideTag("RenderType", "TransparentCutout");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.SetInt("_ZWrite", 1);
                    material.EnableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
                    material.SetFloat("_Cutoff", (float)def.AlphaCutoff);
                }
                else if (def.AlphaMode == AlphaMode.BLEND)
                {
                    material.SetOverrideTag("RenderType", "Transparent");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    material.SetInt("_ZWrite", 0);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.EnableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
                }
                else
                {
                    material.SetOverrideTag("RenderType", "Opaque");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.SetInt("_ZWrite", 1);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = -1;
                }

                if (def.DoubleSided)
                {
                    material.SetInt("_Cull", (int)CullMode.Off);
                }
                else
                {
                    material.SetInt("_Cull", (int)CullMode.Back);
                }

                if (def.PbrMetallicRoughness != null)
                {
                    var pbr = def.PbrMetallicRoughness;

                    material.SetColor("_Color", pbr.BaseColorFactor.ToUnityColor());

                    if (pbr.BaseColorTexture != null)
                    {
                        var textureDef = pbr.BaseColorTexture.Index.Value;
                        material.SetTexture("_MainTex", CreateTexture(textureDef));

                        ApplyTextureTransform(pbr.BaseColorTexture, material, "_MainTex");
                    }

                    material.SetFloat("_Metallic", (float)pbr.MetallicFactor);

                    if (pbr.MetallicRoughnessTexture != null)
                    {
                        var texture = pbr.MetallicRoughnessTexture.Index.Value;
                        material.SetTexture("_MetallicRoughnessMap", CreateTexture(texture));

                        ApplyTextureTransform(pbr.MetallicRoughnessTexture, material, "_MetallicRoughnessMap");
                    }

                    material.SetFloat("_Roughness", (float)pbr.RoughnessFactor);
                }

                if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains(KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME))
                {
                    KHR_materials_pbrSpecularGlossinessExtension specGloss = def.Extensions[KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME] as KHR_materials_pbrSpecularGlossinessExtension;

                    if (specGloss.DiffuseTexture != null)
                    {
                        var texture = specGloss.DiffuseTexture.Index.Value;
                        material.SetTexture("_MainTex", CreateTexture(texture));

                        ApplyTextureTransform(specGloss.DiffuseTexture, material, "_MainTex");
                    }
                    else
                    {
                        material.SetColor("_Color", specGloss.DiffuseFactor.ToUnityColor());
                    }

                    if (specGloss.SpecularGlossinessTexture != null)
                    {
                        var texture = specGloss.SpecularGlossinessTexture.Index.Value;
                        material.SetTexture("_SpecGlossMap", CreateTexture(texture));
                        material.EnableKeyword("_SPECGLOSSMAP");

                        ApplyTextureTransform(specGloss.SpecularGlossinessTexture, material, "_SpecGlossMap");
                    }
                    else
                    {
                        material.SetVector("_SpecColor", specGloss.SpecularFactor.ToUnityVector3());
                        material.SetFloat("_Glossiness", (float)specGloss.GlossinessFactor);
                    }
                }

                if (def.CommonConstant != null)
                {
                    material.SetColor("_AmbientFactor", def.CommonConstant.AmbientFactor.ToUnityColor());

                    if (def.CommonConstant.LightmapTexture != null)
                    {
                        material.EnableKeyword("LIGHTMAP_ON");

                        var texture = def.CommonConstant.LightmapTexture.Index.Value;
                        material.SetTexture("_LightMap", CreateTexture(texture));
                        material.SetInt("_LightUV", def.CommonConstant.LightmapTexture.TexCoord);

                        ApplyTextureTransform(def.CommonConstant.LightmapTexture, material, "_LightMap");
                    }

                    material.SetColor("_LightFactor", def.CommonConstant.LightmapFactor.ToUnityColor());
                }

                if (def.NormalTexture != null)
                {
                    var texture = def.NormalTexture.Index.Value;
                    material.SetTexture("_BumpMap", CreateTexture(texture));
                    material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale);
                    material.EnableKeyword("_NORMALMAP");

                    ApplyTextureTransform(def.NormalTexture, material, "_BumpMap");
                }

                if (def.OcclusionTexture != null)
                {
                    var texture = def.OcclusionTexture.Index;

                    material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength);

                    if (def.PbrMetallicRoughness != null &&
                        def.PbrMetallicRoughness.MetallicRoughnessTexture != null &&
                        def.PbrMetallicRoughness.MetallicRoughnessTexture.Index.Id == texture.Id)
                    {
                        material.EnableKeyword("OCC_METAL_ROUGH_ON");
                    }
                    else
                    {
                        material.SetTexture("_OcclusionMap", CreateTexture(texture.Value));

                        ApplyTextureTransform(def.OcclusionTexture, material, "_OcclusionMap");
                    }
                }

                if (def.EmissiveTexture != null)
                {
                    var texture = def.EmissiveTexture.Index.Value;
                    material.EnableKeyword("EMISSION_MAP_ON");
                    material.EnableKeyword("_EMISSION");
                    material.SetTexture("_EmissionMap", CreateTexture(texture));
                    material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord);

                    ApplyTextureTransform(def.EmissiveTexture, material, "_EmissionMap");
                }

                material.SetColor("_EmissionColor", def.EmissiveFactor.ToUnityColor());

                materialWrapper = new MaterialCacheData
                {
                    UnityMaterial = material,
                    UnityMaterialWithVertexColor = new UnityEngine.Material(material),
                    GLTFMaterial = def
                };

                materialWrapper.UnityMaterialWithVertexColor.EnableKeyword("VERTEX_COLOR_ON");

                if (materialIndex > 0)
                {
                    _assetCache.MaterialCache[materialIndex] = materialWrapper;
                }
            }

            return(materialIndex > 0 ? _assetCache.MaterialCache[materialIndex] : materialWrapper);
        }
示例#47
0
		void UpdatePreview()
		{
			if (!this) return;
#if BVH
			if (previewBvh)
			{
				if (!entityBuffer.IsCreated) RebuildEntityBuffers();
				if (!bvhNodeBuffer.IsCreated) RebuildBvh();
			}
			else
			{
				sphereBuffer.SafeDispose();
				bvhNodeBuffer.SafeDispose();
				entityBuffer.SafeDispose();

				ActiveEntities.Clear();
			}
#endif // BVH

			if (scene)
			{
				Transform cameraTransform = targetCamera.transform;
				cameraTransform.position = scene.CameraPosition;
				cameraTransform.rotation = Quaternion.LookRotation(scene.CameraTarget - scene.CameraPosition);
				targetCamera.fieldOfView = scene.CameraFieldOfView;
			}

			Action updateDelegate = OnEditorUpdate;
			if (EditorApplication.update.GetInvocationList().All(x => x != (Delegate) updateDelegate))
				EditorApplication.update += OnEditorUpdate;

			if (opaquePreviewCommandBuffer == null)
				opaquePreviewCommandBuffer = new CommandBuffer { name = "World Preview (Opaque)" };
			if (transparentPreviewCommandBuffer == null)
				transparentPreviewCommandBuffer = new CommandBuffer { name = "World Preview (Transparent)" };

			targetCamera.RemoveAllCommandBuffers();

			targetCamera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, opaquePreviewCommandBuffer);
			targetCamera.AddCommandBuffer(CameraEvent.AfterForwardAlpha, transparentPreviewCommandBuffer);

			opaquePreviewCommandBuffer.Clear();
			transparentPreviewCommandBuffer.Clear();

			foreach (UnityEngine.Material material in previewMaterials)
				DestroyImmediate(material);
			previewMaterials.Clear();

			var skybox = targetCamera.GetComponent<Skybox>();
			if (AssetDatabase.IsMainAsset(skybox.material))
				RenderSettings.skybox = skybox.material = new UnityEngine.Material(skybox.material);

			skybox.material.SetColor("_Color1", scene.SkyBottomColor);
			skybox.material.SetColor("_Color2", scene.SkyTopColor);

			EnsurePreviewObjectExists(PrimitiveType.Sphere, ref previewSphere);
			EnsurePreviewObjectExists(PrimitiveType.Quad, ref previewRect);
			EnsurePreviewObjectExists(PrimitiveType.Cube, ref previewBox);

			var previewMeshRenderer = previewSphere.GetComponent<MeshRenderer>();

			CollectActiveEntities();

			opaquePreviewCommandBuffer.EnableShaderKeyword("LIGHTPROBE_SH");
			transparentPreviewCommandBuffer.EnableShaderKeyword("LIGHTPROBE_SH");

			foreach (EntityData entity in ActiveEntities)
			{
				if (!entity.Material) continue;

				bool transparent = entity.Material.Type == MaterialType.Dielectric;
				bool emissive = entity.Material.Type == MaterialType.DiffuseLight;

				Color albedoMainColor = entity.Material.Albedo ? entity.Material.Albedo.MainColor : Color.white;
				Color color = transparent ? albedoMainColor.GetAlphaReplaced(0.5f) : albedoMainColor;
				var material = new UnityEngine.Material(previewMeshRenderer.sharedMaterial) { color = color };
				previewMaterials.Add(material);

				material.SetFloat("_Metallic", entity.Material.Type == MaterialType.Metal ? 1 : 0);
				material.SetFloat("_Glossiness",
					entity.Material.Type == MaterialType.Metal ? 1 - entity.Material.Roughness : transparent ? 1 : 0);
				material.SetTexture("_MainTex", entity.Material.Albedo ? entity.Material.Albedo.Image : null);

				if (transparent)
				{
					material.SetInt("_SrcBlend", (int) BlendMode.One);
					material.SetInt("_DstBlend", (int) BlendMode.OneMinusSrcAlpha);
					material.SetInt("_ZWrite", 0);
					material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
					material.renderQueue = 3000;
				}

				if (emissive)
				{
					material.EnableKeyword("_EMISSION");
					material.SetColor("_EmissionColor",
						entity.Material.Emission ? entity.Material.Emission.MainColor : Color.black);
				}

				CommandBuffer previewCommandBuffer =
					transparent ? transparentPreviewCommandBuffer : opaquePreviewCommandBuffer;

				switch (entity.Type)
				{
					case EntityType.Sphere:
						SphereData s = entity.SphereData;
						previewCommandBuffer.DrawMesh(previewSphere.sharedMesh,
							Matrix4x4.TRS(entity.Position, entity.Rotation, s.Radius * 2 * Vector3.one),
							material, 0,
							material.FindPass("FORWARD"));
						break;

					case EntityType.Rect:
						RectData r = entity.RectData;
						previewCommandBuffer.DrawMesh(previewRect.sharedMesh,
							Matrix4x4.TRS(entity.Position, Quaternion.LookRotation(Vector3.forward) * entity.Rotation,float3(r.Size, 1)),
							material, 0,
							material.FindPass("FORWARD"));
						break;

					case EntityType.Box:
						BoxData b = entity.BoxData;
						previewCommandBuffer.DrawMesh(previewBox.sharedMesh,
							Matrix4x4.TRS(entity.Position, entity.Rotation, b.Size), material, 0,
							material.FindPass("FORWARD"));
						break;
				}
			}
		}
示例#48
0
        private void ApplyBlendMode(UnityEngine.Material unityMaterial, BlendMode blendMode)
        {
            switch (blendMode)
            {
            case BlendMode.Opaque:

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "Opaque");

                // required for the blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);

                // properties
                unityMaterial.SetFloat(Surface, 0);                         // 0 = Opaque; 1 = Transparent

                // render queue
                unityMaterial.renderQueue = -1;

                break;

            case BlendMode.Cutout:

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "TransparentCutout");

                // keywords
                unityMaterial.EnableKeyword("_ALPHATEST_ON");

                // required for blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);

                // properties
                unityMaterial.SetFloat(Surface, 0);                         // 0 = Opaque; 1 = Transparent
                unityMaterial.SetInt(AlphaClip, 1);

                // render queue
                unityMaterial.renderQueue = 2450;

                break;

            case BlendMode.Translucent:

                // disable shader passes
                unityMaterial.SetShaderPassEnabled("SHADOWCASTER", false);

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "Transparent");

                // keywords
                unityMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");

                // required for blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                unityMaterial.SetInt(ZWrite, 0);

                // properties
                unityMaterial.SetFloat(Surface, 1);                       // 0 = Opaque; 1 = Transparent
                unityMaterial.SetFloat(Blend, 1);                         // 0 = Alpha, 1 = Premultiply, 2 = Additive, 3 = Multiply

                // render queue
                const int transparentRenderQueueBase = 3000;
                const int transparentSortingPriority = 0;
                unityMaterial.SetInt(QueueOffset, transparentSortingPriority);
                unityMaterial.renderQueue = transparentRenderQueueBase + transparentSortingPriority;

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        protected virtual void CreateUnityMaterial(GLTF.Schema.Material def, int materialIndex)
        {
            Extension specularGlossinessExtension = null;
            bool      isSpecularPBR = def.Extensions != null && def.Extensions.TryGetValue("KHR_materials_pbrSpecularGlossiness", out specularGlossinessExtension);

            Shader shader = isSpecularPBR ? Shader.Find("Standard (Specular setup)") : Shader.Find("Standard");

            var material = new UnityEngine.Material(shader);

            material.hideFlags = HideFlags.DontUnloadUnusedAsset;             // Avoid material to be deleted while being built
            material.name      = def.Name;

            //Transparency
            if (def.AlphaMode == AlphaMode.MASK)
            {
                GLTFUtils.SetupMaterialWithBlendMode(material, GLTFUtils.BlendMode.Cutout);
                material.SetFloat("_Mode", 1);
                material.SetFloat("_Cutoff", (float)def.AlphaCutoff);
            }
            else if (def.AlphaMode == AlphaMode.BLEND)
            {
                GLTFUtils.SetupMaterialWithBlendMode(material, GLTFUtils.BlendMode.Fade);
                material.SetFloat("_Mode", 3);
            }

            if (def.NormalTexture != null)
            {
                var       texture       = def.NormalTexture.Index.Id;
                Texture2D normalTexture = getTexture(texture) as Texture2D;

                //Automatically set it to normal map
                TextureImporter im = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(normalTexture)) as TextureImporter;
                im.textureType = TextureImporterType.NormalMap;
                im.SaveAndReimport();
                material.SetTexture("_BumpMap", getTexture(texture));
                material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale);
            }

            if (def.EmissiveTexture != null)
            {
                material.EnableKeyword("EMISSION_MAP_ON");
                var texture = def.EmissiveTexture.Index.Id;
                material.SetTexture("_EmissionMap", getTexture(texture));
                material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord);
            }

            // PBR channels
            if (specularGlossinessExtension != null)
            {
                KHR_materials_pbrSpecularGlossinessExtension pbr = (KHR_materials_pbrSpecularGlossinessExtension)specularGlossinessExtension;
                material.SetColor("_Color", pbr.DiffuseFactor.ToUnityColor().gamma);
                if (pbr.DiffuseTexture != null)
                {
                    var texture = pbr.DiffuseTexture.Index.Id;
                    material.SetTexture("_MainTex", getTexture(texture));
                }

                if (pbr.SpecularGlossinessTexture != null)
                {
                    var texture = pbr.SpecularGlossinessTexture.Index.Id;
                    material.SetTexture("_SpecGlossMap", getTexture(texture));
                    material.SetFloat("_GlossMapScale", (float)pbr.GlossinessFactor);
                    material.SetFloat("_Glossiness", (float)pbr.GlossinessFactor);
                }
                else
                {
                    material.SetFloat("_Glossiness", (float)pbr.GlossinessFactor);
                }

                Vector3 specularVec3 = pbr.SpecularFactor.ToUnityVector3();
                material.SetColor("_SpecColor", new Color(specularVec3.x, specularVec3.y, specularVec3.z, 1.0f));

                if (def.OcclusionTexture != null)
                {
                    var texture = def.OcclusionTexture.Index.Id;
                    material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength);
                    material.SetTexture("_OcclusionMap", getTexture(texture));
                }

                GLTFUtils.SetMaterialKeywords(material, GLTFUtils.WorkflowMode.Specular);
            }
            else if (def.PbrMetallicRoughness != null)
            {
                var pbr = def.PbrMetallicRoughness;

                material.SetColor("_Color", pbr.BaseColorFactor.ToUnityColor().gamma);
                if (pbr.BaseColorTexture != null)
                {
                    var texture = pbr.BaseColorTexture.Index.Id;
                    material.SetTexture("_MainTex", getTexture(texture));
                }

                material.SetFloat("_Metallic", (float)pbr.MetallicFactor);
                material.SetFloat("_Glossiness", 1.0f - (float)pbr.RoughnessFactor);

                if (pbr.MetallicRoughnessTexture != null)
                {
                    var texture = pbr.MetallicRoughnessTexture.Index.Id;
                    UnityEngine.Texture2D inputTexture  = getTexture(texture) as Texture2D;
                    List <Texture2D>      splitTextures = splitMetalRoughTexture(inputTexture, def.OcclusionTexture != null, (float)pbr.MetallicFactor, (float)pbr.RoughnessFactor);
                    material.SetTexture("_MetallicGlossMap", splitTextures[0]);

                    if (def.OcclusionTexture != null)
                    {
                        material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength);
                        material.SetTexture("_OcclusionMap", splitTextures[1]);
                    }
                }

                GLTFUtils.SetMaterialKeywords(material, GLTFUtils.WorkflowMode.Metallic);
            }

            material.SetColor("_EmissionColor", def.EmissiveFactor.ToUnityColor().gamma);
            material = _assetManager.saveMaterial(material, materialIndex);
            _assetManager._parsedMaterials.Add(material);
            material.hideFlags = HideFlags.None;
        }
        private UnityEngine.Material CreateMaterial(Material def, bool useVertexColors)
        {
            UnityEngine.Material material = new UnityEngine.Material(useVertexColors ? ColorMaterial : NoColorMaterial);

            material.shader.maximumLOD = MaximumLod;

            if (def.AlphaMode == AlphaMode.MASK)
            {
                material.SetOverrideTag("RenderType", "TransparentCutout");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.EnableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
                material.SetFloat("_Cutoff", (float)def.AlphaCutoff);
            }
            else if (def.AlphaMode == AlphaMode.BLEND)
            {
                material.SetOverrideTag("RenderType", "Transparent");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                material.SetInt("_ZWrite", 0);
                material.DisableKeyword("_ALPHATEST_ON");
                material.EnableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
            }
            else
            {
                material.SetOverrideTag("RenderType", "Opaque");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.DisableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = -1;
            }

            if (def.DoubleSided)
            {
                material.SetInt("_Cull", (int)CullMode.Off);
            }
            else
            {
                material.SetInt("_Cull", (int)CullMode.Back);
            }

            if (useVertexColors)
            {
                material.EnableKeyword("VERTEX_COLOR_ON");
            }

            if (def.PbrMetallicRoughness != null)
            {
                var pbr = def.PbrMetallicRoughness;

                material.SetColor("_Color", pbr.BaseColorFactor);

                if (pbr.BaseColorTexture != null)
                {
                    var texture = pbr.BaseColorTexture.Index.Value;
                    material.SetTexture("_MainTex", _imageCache[texture.Source.Value]);
                }

                material.SetFloat("_Metallic", (float)pbr.MetallicFactor);

                if (pbr.MetallicRoughnessTexture != null)
                {
                    var texture = pbr.MetallicRoughnessTexture.Index.Value;
                    material.SetTexture("_MetallicRoughnessMap", _imageCache[texture.Source.Value]);
                }

                material.SetFloat("_Roughness", (float)pbr.RoughnessFactor);
            }

            if (def.CommonConstant != null)
            {
                material.SetColor("_AmbientFactor", def.CommonConstant.AmbientFactor);

                if (def.CommonConstant.LightmapTexture != null)
                {
                    material.EnableKeyword("LIGHTMAP_ON");

                    var texture = def.CommonConstant.LightmapTexture.Index.Value;
                    material.SetTexture("_LightMap", _imageCache[texture.Source.Value]);
                    material.SetInt("_LightUV", def.CommonConstant.LightmapTexture.TexCoord);
                }

                material.SetColor("_LightFactor", def.CommonConstant.LightmapFactor);
            }

            if (def.NormalTexture != null)
            {
                var texture = def.NormalTexture.Index.Value;
                material.EnableKeyword("_NORMALMAP");
                material.SetTexture("_BumpMap", _imageCache[texture.Source.Value]);
                material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale);
            }
            else
            {
                material.SetTexture("_BumpMap", null);
                material.DisableKeyword("_NORMALMAP");
            }

            if (def.OcclusionTexture != null)
            {
                var texture = def.OcclusionTexture.Index;

                material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength);

                if (def.PbrMetallicRoughness != null &&
                    def.PbrMetallicRoughness.MetallicRoughnessTexture.Index.Id == texture.Id)
                {
                    material.EnableKeyword("OCC_METAL_ROUGH_ON");
                }
                else
                {
                    material.SetTexture("_OcclusionMap", _imageCache[texture.Value.Source.Value]);
                }
            }

            if (def.EmissiveTexture != null)
            {
                var texture = def.EmissiveTexture.Index.Value;
                material.EnableKeyword("_EMISSION");
                material.EnableKeyword("EMISSION_MAP_ON");
                material.SetTexture("_EmissionMap", _imageCache[texture.Source.Value]);
                material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord);
            }

            material.SetColor("_EmissionColor", def.EmissiveFactor);

            return(material);
        }
        private static void ApplyBlendMode(UnityEngine.Material unityMaterial, BlendMode blendMode)
        {
            // disable shader passes
            unityMaterial.SetShaderPassEnabled("DistortionVectors", false);
            unityMaterial.SetShaderPassEnabled("MOTIONVECTORS", false);
            unityMaterial.SetShaderPassEnabled("TransparentDepthPrepass", false);
            unityMaterial.SetShaderPassEnabled("TransparentDepthPostpass", false);
            unityMaterial.SetShaderPassEnabled("TransparentBackface", false);

            // reset existing blend modes, will be enabled explicitly
            unityMaterial.DisableKeyword("_BLENDMODE_ALPHA");
            unityMaterial.DisableKeyword("_BLENDMODE_ADD");
            unityMaterial.DisableKeyword("_BLENDMODE_PRE_MULTIPLY");

            switch (blendMode)
            {
            case Engine.VPT.BlendMode.Opaque:

                // required for the blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);

                // properties
                unityMaterial.SetFloat(SurfaceType, 0);                         // 0 = Opaque; 1 = Transparent
                unityMaterial.SetFloat(AlphaCutoffEnable, 0);

                // render queue
                unityMaterial.renderQueue = -1;

                break;

            case Engine.VPT.BlendMode.Cutout:

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "TransparentCutout");

                // keywords
                unityMaterial.EnableKeyword("_ALPHATEST_ON");
                unityMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");

                // required for the blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                unityMaterial.SetInt(ZWrite, 1);

                // properties
                unityMaterial.SetFloat(SurfaceType, 0);                         // 0 = Opaque; 1 = Transparent
                unityMaterial.SetFloat(AlphaCutoffEnable, 1);

                unityMaterial.SetFloat(ZTestDepthEqualForOpaque, 3);
                unityMaterial.SetFloat(ZTestModeDistortion, 4);
                unityMaterial.SetFloat(ZTestGBuffer, 3);

                // render queue
                unityMaterial.renderQueue = 2450;

                break;

            case Engine.VPT.BlendMode.Translucent:

                // set render type
                unityMaterial.SetOverrideTag("RenderType", "Transparent");

                // keywords
                //unityMaterial.EnableKeyword("_ALPHATEST_ON"); // required for _AlphaCutoffEnable
                unityMaterial.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING");
                unityMaterial.EnableKeyword("_BLENDMODE_PRE_MULTIPLY");
                unityMaterial.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT");
                unityMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");
                unityMaterial.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");

                // required for the blend mode
                unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                unityMaterial.SetInt(ZWrite, 0);

                // properties
                unityMaterial.SetFloat(SurfaceType, 1);                       // 0 = Opaque; 1 = Transparent
                //unityMaterial.SetFloat("_AlphaCutoffEnable", 1); // enable keyword _ALPHATEST_ON if this is required
                unityMaterial.SetFloat(BlendMode, 4);                         // 0 = Alpha, 1 = Additive, 4 = PreMultiply

                // render queue
                const int transparentRenderQueueBase = 3000;
                const int transparentSortingPriority = 0;
                unityMaterial.SetInt(TransparentSortPriority, transparentSortingPriority);
                unityMaterial.renderQueue = transparentRenderQueueBase + transparentSortingPriority;

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public UnityEngine.Material CreateMaterial(PbrMaterial vpxMaterial, TableBehavior table, StringBuilder debug = null)
        {
            var unityMaterial = new UnityEngine.Material(GetShader())
            {
                name = vpxMaterial.Id
            };

            // apply some basic manipulations to the color. this just makes very
            // very white colors be clipped to 0.8204 aka 204/255 is 0.8
            // this is to give room to lighting values. so there is more modulation
            // of brighter colors when being lit without blow outs too soon.
            var col = vpxMaterial.Color.ToUnityColor();

            if (vpxMaterial.Color.IsGray() && col.grayscale > 0.8)
            {
                debug?.AppendLine("Color manipulation performed, brightness reduced.");
                col.r = col.g = col.b = 0.8f;
            }

            // alpha for color depending on blend mode
            ApplyBlendMode(unityMaterial, vpxMaterial.MapBlendMode);
            if (vpxMaterial.MapBlendMode == Engine.VPT.BlendMode.Translucent)
            {
                col.a = Mathf.Min(1, Mathf.Max(0, vpxMaterial.Opacity));
            }
            unityMaterial.SetColor(BaseColor, col);

            // validate IsMetal. if true, set the metallic value.
            // found VPX authors setting metallic as well as translucent at the
            // same time, which does not render correctly in unity so we have
            // to check if this value is true and also if opacity <= 1.
            if (vpxMaterial.IsMetal && (!vpxMaterial.IsOpacityActive || vpxMaterial.Opacity >= 1))
            {
                unityMaterial.SetFloat(Metallic, 1f);
                debug?.AppendLine("Metallic set to 1.");
            }

            // roughness / glossiness
            unityMaterial.SetFloat(Smoothness, vpxMaterial.Roughness);

            // map
            if (table != null && vpxMaterial.HasMap)
            {
                unityMaterial.SetTexture(BaseColorMap, table.GetTexture(vpxMaterial.Map.Name));
            }

            // normal map
            if (table != null && vpxMaterial.HasNormalMap)
            {
                unityMaterial.EnableKeyword("_NORMALMAP");
                unityMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");

                unityMaterial.SetInt(NormalMapSpace, 0);                 // 0 = TangentSpace, 1 = ObjectSpace
                unityMaterial.SetFloat(NormalScale, 0f);                 // TODO FIXME: setting the scale to 0 for now. anything above 0 makes the entire unity editor window become black which is more likely a unity bug

                unityMaterial.SetTexture(NormalMap, table.GetTexture(vpxMaterial.NormalMap.Name));
            }

            // GI hack. This is a necessary step, see respective code in BaseUnlitGUI.cs of the HDRP source
            SetupMainTexForAlphaTestGI(unityMaterial, "_BaseColorMap", "_BaseColor");

            return(unityMaterial);
        }
示例#53
0
        private static UnityEngine.Material Convert(RenderWareStream.Material src, TextureDictionary[] txds, MaterialFlags flags)
        {
            LoadedTexture diffuse;
            LoadedTexture mask = null;

            var overrideAlpha = (flags & MaterialFlags.OverrideAlpha) == MaterialFlags.OverrideAlpha;
            var vehicle       = (flags & MaterialFlags.Vehicle) == MaterialFlags.Vehicle;

            if (!overrideAlpha && src.Colour.A != 255)
            {
                flags |= MaterialFlags.Alpha;
            }

            if (src.TextureCount > 0)
            {
                var tex = src.Textures[0];
                diffuse = txds.GetDiffuse(tex.TextureName);

                if (src.TextureCount > 1)
                {
                    Debug.LogFormat("Something has {0} textures!", src.TextureCount);
                }

                if (diffuse == null)
                {
                    Debug.LogWarningFormat("Unable to find texture {0}", tex.TextureName);
                }

                if (!string.IsNullOrEmpty(tex.MaskName))
                {
                    mask = txds.GetAlpha(tex.MaskName) ?? diffuse;
                }
                else if (vehicle)
                {
                    mask = diffuse;
                }

                if (!overrideAlpha && mask != null && mask.HasAlpha)
                {
                    flags |= MaterialFlags.Alpha;
                }
            }
            else
            {
                diffuse = WhiteTex;
            }

            var shader = GetShader(flags);
            var mat    = new UnityEngine.Material(shader);

            var clr = Types.Convert(src.Colour);

            if (vehicle)
            {
                var found = false;
                for (var i = 1; i < _sKeyColors.Length; ++i)
                {
                    var key = _sKeyColors[i];
                    if (key.r != clr.r || key.g != clr.g || key.b != clr.b)
                    {
                        continue;
                    }
                    mat.SetInt(CarColorIndexId, i);
                    found = true;
                    break;
                }

                if (found)
                {
                    mat.color = Color.white;
                }
                else
                {
                    mat.color = clr;
                }
            }
            else
            {
                mat.color = clr;
            }

            if (diffuse != null)
            {
                mat.SetTexture(MainTexId, diffuse.Texture);
            }
            if (mask != null)
            {
                mat.SetTexture(MaskTexId, mask.Texture);
            }

            mat.SetFloat(MetallicId, src.Specular);
            mat.SetFloat(SmoothnessId, src.Smoothness);

            return(mat);
        }