EloiStree / CodeAndQuestsEveryDay

Regular research on the Quest for developers.
http://codeandquests.page.link/discord
32 stars 3 forks source link

Daily Log 11, 2019_06_01 #123

Open EloiStree opened 5 years ago

EloiStree commented 5 years ago

Today we experimented Some basketbal. Observation:

Other Observation:

EloiStree commented 5 years ago

Spend hours trying to use Mesh Baker with Blocks asset to reduce the scene in one DrawCall. The main issue is that block don't assign texture to material and MeshBaker don't deal with that. image

Unsuccessful attempt:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.IO;

public class MBC_MaterialColorToTexture : MonoBehaviour
{
    public List<Renderer> m_renderersWithoutTexture;
    public List<Material> m_materialsWithoutTexture;

    private void Reset()
    {
        string folderPath = Application.dataPath + "/TmpTexture/";
        m_materialsWithoutTexture.Clear();
        m_renderersWithoutTexture.Clear();
        m_renderersWithoutTexture = GameObject.FindObjectsOfType<Renderer>().ToList();

        for (int i = m_renderersWithoutTexture.Count-1; i >=0; i--)
        {
            Renderer renderer = m_renderersWithoutTexture[i];
            if (renderer.material.GetTexture("_MainTex") != null)
            {
                m_renderersWithoutTexture.RemoveAt(i);
                continue;
            }

        }
        for (int i = 0; i < m_renderersWithoutTexture.Count; i++)
        {
            m_materialsWithoutTexture.AddRange(m_renderersWithoutTexture[i].sharedMaterials);
        }
        Debug.Log("App Path" + Application.dataPath);
        Directory.CreateDirectory(folderPath);
        for (int i = 0; i < m_materialsWithoutTexture.Count; i++)
        {
            Material mat = m_materialsWithoutTexture[i];
            Texture2D text = new Texture2D(2, 2);
            Color c = mat.color;
            text.SetPixels( new Color[] { c,c,c,c});
            text.Apply();
            File.WriteAllBytes(folderPath + "/" + mat.name + ".jpg", ImageConversion.EncodeToJPG(text) );
            mat.SetTexture("_MainTex",  text);

        }

        UnityEditor.AssetDatabase.Refresh();
    }

}