private Texture2D EncodeToTexture(FlowMap _inst, FlowPath.Sample[,] _outputs) { Texture2D _tex = new Texture2D(_outputs.GetUpperBound(0) + 1, _outputs.GetUpperBound(1) + 1, TextureFormat.ARGB32, false); _inst.cached = new FlowPath.Sample[_tex.width * _tex.height]; int _c = 0; for (int _y = 0; _y < _tex.height; ++_y) { for (int _x = 0; _x < _tex.width; ++_x) { _inst.cached[_c] = _outputs[_x, _y]; Color _clr = new Color(0.5f, 0.5f, 0.5f, 0.0f); Vector3 _dir = _outputs[_x, _y].direction; _clr.r = _dir.x * 0.5f + 0.5f; _clr.g = _dir.z * 0.5f + 0.5f; //_clr.b = _dir.y * 0.5f + 0.5f; _clr.b = 0.5f; _clr.a = 1.0f; #if false // debug uv mapping _clr.r = (float)_x / _tw; _clr.g = (float)_y / _th; _clr.b = 0; #endif _tex.SetPixel(_x, _y, _clr); _c++; } } if (_inst.blurSize > 0 && _inst.blurIterations > 0) { Texture2D _texBlurred = new Blur().FastBlur(_tex, _inst.blurSize, _inst.blurIterations); DestroyImmediate(_tex); _tex = _texBlurred; } return(_tex); }
private void SaveTexture(FlowMap _inst, Texture2D _tex) { Scene _scene = SceneManager.GetActiveScene(); string _path = System.IO.Path.GetDirectoryName(_scene.path); _path = _path + "/" + _inst.Filename; System.IO.File.WriteAllBytes(Application.dataPath + _path.Remove(0, 6), _tex.EncodeToPNG()); AssetDatabase.ImportAsset(_path); TextureImporter _imp = TextureImporter.GetAtPath(_path) as TextureImporter; _imp.filterMode = FilterMode.Bilinear; _imp.wrapMode = TextureWrapMode.Clamp; _imp.anisoLevel = 0; _imp.mipmapEnabled = false; _imp.sRGBTexture = false; _imp.textureCompression = TextureImporterCompression.Uncompressed; _imp.SaveAndReimport(); }
} // OnSceneGUI private void Bake(FlowMap _inst) { int _tw = Mathf.NextPowerOfTwo((int)_inst.resolution.x); int _th = Mathf.NextPowerOfTwo((int)_inst.resolution.y); Vector2 _delta = new Vector2(_inst.size.x / _tw, _inst.size.y / _th); List <FlowPath.Sample> _samples = _inst.GatherSamples(_delta * _inst.minimumDistance); if (_samples.Count == 0) { return; } FlowPath.Sample[,] _outputs = new FlowPath.Sample[_tw, _th]; InterpolateSamples(_inst, _samples, _delta, _outputs); Texture2D _tex = EncodeToTexture(_inst, _outputs); SaveTexture(_inst, _tex); } // Bake