// 初期化 void Init() { // 初期化済みでなければ if (!_isInit) { // 参照を取得 if (_clothSim == null) { _clothSim = GetComponent <GPUClothSimulation>(); } if (_clothSim != null && _clothSim.IsInit) { // メッシュを生成 GenerateMesh(); // メッシュ描画のためのマテリアルをMeshRendererのセット _meshRenderer.material = MeshMat; MeshMat.SetTexture("_PositionTex", _clothSim.GetPositionBuffer()); MeshMat.SetTexture("_NormalTex", _clothSim.GetNormalBuffer()); // 初期化完了のフラグをセット _isInit = true; } } }
void Update() { if (!_isInit) { if (_clothSim == null) { _clothSim = GetComponent <GPUClothSimulation>(); } if (_clothSim != null && _clothSim.IsInit) { var gridList = new List <SpringIds>(); var gridDiagonalList = new List <SpringIds>(); var gridDiagonalAlternateList = new List <SpringIds>(); var res = _clothSim.GetClothResolution(); for (var y = 0; y < res.y; y++) { for (var x = 0; x < res.x; x++) { gridList.Add(new SpringIds(new Vector2Int(x, y), new Vector2Int(x, y + 1))); gridList.Add(new SpringIds(new Vector2Int(x, y), new Vector2Int(x + 1, y))); if (x < res.x - 1 && y < res.y - 1) { gridDiagonalList.Add(new SpringIds(new Vector2Int(x, y), new Vector2Int(x + 1, y + 1))); gridDiagonalList.Add(new SpringIds(new Vector2Int(x + 1, y), new Vector2Int(x, y + 1))); } if (x < res.x - 2 && y < res.y - 2) { gridDiagonalAlternateList.Add(new SpringIds(new Vector2Int(x, y), new Vector2Int(x + 2, y + 2))); } if (x >= 2 && y < res.y - 2) { gridDiagonalAlternateList.Add(new SpringIds(new Vector2Int(x, y), new Vector2Int(x - 2, y + 2))); } } } _gridBuffer = new ComputeBuffer(gridList.Count, Marshal.SizeOf(typeof(SpringIds))); _gridBuffer.SetData(gridList.ToArray()); _gridDiagonalBuffer = new ComputeBuffer(gridDiagonalList.Count, Marshal.SizeOf(typeof(SpringIds))); _gridDiagonalBuffer.SetData(gridDiagonalList.ToArray()); _gridDiagonalAlternateBuffer = new ComputeBuffer(gridDiagonalAlternateList.Count, Marshal.SizeOf(typeof(SpringIds))); _gridDiagonalAlternateBuffer.SetData(gridDiagonalAlternateList.ToArray()); gridList = null; gridDiagonalList = null; gridDiagonalAlternateList = null; _isInit = true; } } }