KITASENJU DESIGN BLOG

memo, html, javascript, unity

Bake vertices to PNG

Bake vertices to PNG 頂点をPNGにベイク

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

#if UNITY_EDITOR
using UnityEditor;
using System.IO;
#endif

namespace HOGE
{
public class MeshVertBaker1f : MonoBehaviour
{
    [SerializeField] private Mesh mesh;
    [SerializeField] private int W = 256;
    [SerializeField] private int H = 256;
    [SerializeField] private bool _useTri=false;
    [SerializeField] private bool _useUV=false;
    [SerializeField] private string _savePath="Project/Textures/BakeTex";
    private Texture2D _tex;
    // Start is called before the first frame update
    private Color[] _colors;
    private Color[] _uvs;
    private int _idx = 0;
    void Start()
    {
        
        Debug.Log(mesh.vertexCount);

        int ww = W;
        int hh = H;
        _colors = new Color[ww*hh];
        _uvs = new Color[ww*hh];
               
    }

    void OnGUI() {
        
        var style = new GUIStyle();
        GUI.Label(new Rect(0,0,600,400), (float)_idx/(float)(W*H)*100f +"%");

    }

    void Update(){

        Debug.Log((float)_idx/(float)(W*H)*100f +"%");
        
        for(int i=0;i<10;i++){
            var b = Bake();
            if(b){
                gameObject.SetActive(false);
                break;
            }
        }

    }

    bool Bake(){

        Vector3 v;
        Vector2 uv;
        int ww = W;
        int hh = H;
                
        if(_useTri){
            v = RandomPointOnMesh.GetRandomPointOnMesh(mesh);
        }else{
            v = mesh.vertices[
                Mathf.FloorToInt(mesh.vertexCount*Random.value*0.9999f)//randomにしてみる
            ];
        } 
//        Debug.Log(v);

        if(_useUV){
            _uvs[_idx] = new Color();
        }

        _colors[_idx] = new Color(v.x,v.y,v.z);                    
                

        if(_idx==ww*hh-1){

            var tt = new Texture2D(ww,hh,TextureFormat.RGBAHalf,0,true);
            tt.SetPixels(0,0,ww,hh,_colors);

            //べいくする
            #if UNITY_EDITOR
                        var folderName = _savePath;//"BakedVertTex";
                        var folderPath = Path.Combine("Assets", folderName);
                        if (!AssetDatabase.IsValidFolder(folderPath))
                            AssetDatabase.CreateFolder("Assets", folderName);

                        var subFolder = name;
                        var subFolderPath = Path.Combine(folderPath, subFolder);
                        if (!AssetDatabase.IsValidFolder(subFolderPath))
                            AssetDatabase.CreateFolder(folderPath, subFolder);

                        AssetDatabase.CreateAsset(tt, Path.Combine(subFolderPath, mesh.name + ".asset"));
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
            #endif

            //gameObject.SetActive(false);
            return true;
        }

        _idx++;
        return false;

    }

}

}
"FOOTER"