KITASENJU DESIGN BLOG

memo, html, javascript, unity

pcache baker

visual effect graph をpackage managerで入れる。

Window>VisualEffects>Utilities から Point cache bake tool でpcacheファイルを作る

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

#if UNITY_EDITOR
using UnityEditor;
using System.IO;
using UnityEditor.Experimental.VFX.Utility;
#endif

namespace Effect
{
    public class PCacheBaker : MonoBehaviour
    {

        public TextAsset _text;
        /*
        property float position.x//1
        property float position.y//2
        property float position.z//3
        property float normal.x//4
        property float normal.y//5
        property float normal.z//6
        property float color.r//7
        property float color.g//8
        property float color.b//9
        property float color.a//10
        */
        [SerializeField] private int _elements;
        [SerializeField] private int W = 256;
        [SerializeField] private int H = 256;        
        [SerializeField] private Vector3[] _positions;
        [SerializeField] private Vector3[] _normals;
        [SerializeField] private Vector3[] _colors;
        [SerializeField] private bool _isColor=false;
        
        [SerializeField] private string _savePath="Project/Textures/BakeTex";
        private float[] _randoms;
        //elements 100000

        void Start(){

            string str = _text.text;
            string[] splitStr = {"end_header"};
            string[] ary =  str.Split(splitStr, StringSplitOptions.None);

            var suuji = ary[1];

            string[] brStr = {"\n"};
            string[] lines =  suuji.Split(brStr, StringSplitOptions.None);

            _positions = new Vector3[_elements];
            _normals = new Vector3[_elements];
            _colors = new Vector3[_elements];
            
            for(int i=0;i<_elements;i++){
                var s = lines[i+1];
                string[] space = {" "};
                string[] values =  s.Split(space, StringSplitOptions.None);
                
                _positions[i] = new Vector3(
                    float.Parse(values[0]),
                    float.Parse(values[1]),
                    float.Parse(values[2])
                );
                _normals[i] = new Vector3(
                    float.Parse(values[3]),
                    float.Parse(values[4]),
                    float.Parse(values[5])
                );

                if(_isColor){
                    _colors[i] = new Vector3(
                        float.Parse(values[6]),
                        float.Parse(values[7]),
                        float.Parse(values[8])
                    );
                }
                
            }

            _randoms = new float[W*H];
            for(int i=0;i<W*H;i++){
                _randoms[i]=UnityEngine.Random.value;
            }

            _saveTex(_positions,"pos");
            if(_isColor) _saveTex(_colors,"col");

        }

        Color[] _Vector2Color(Vector3[] positions, int num){

            var c = new Color[num];
            for(int i=0;i<num;i++){
                int idx = Mathf.FloorToInt( positions.Length *_randoms[i] );
                c[i]=new Color(
                    positions[ idx ].x,
                    positions[ idx ].y,
                    positions[ idx ].z,
                    1f
                );
            }
            return c;

        }

        void _saveTex(Vector3[] array, string texName){

            int ww = W;
            int hh = H;

            //_Vector2Color
            var colors = _Vector2Color(array,ww*hh);
            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, _text.name+"_"+texName+ ".asset"));
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            #endif

        }



    }

        
    


}
"FOOTER"