diff --git a/Assets/Pcx/Editor/PlyImporter.cs b/Assets/Pcx/Editor/PlyImporter.cs index 08c4ef0e..732e78ec 100644 --- a/Assets/Pcx/Editor/PlyImporter.cs +++ b/Assets/Pcx/Editor/PlyImporter.cs @@ -18,7 +18,7 @@ class PlyImporter : ScriptedImporter { #region ScriptedImporter implementation - public enum ContainerType { Mesh, ComputeBuffer } + public enum ContainerType { Mesh, ComputeBuffer, Texture } [SerializeField] ContainerType _containerType = ContainerType.Mesh; @@ -42,7 +42,7 @@ public override void OnImportAsset(AssetImportContext context) context.SetMainObject(gameObject); } - else + else if (_containerType == ContainerType.ComputeBuffer) { // ComputeBuffer container // Create a prefab with PointCloudRenderer. @@ -57,6 +57,19 @@ public override void OnImportAsset(AssetImportContext context) context.SetMainObject(gameObject); } + else // _containerType == ContainerType.Texture + { + // Texture container + // No prefab is available for this type. + var data = ImportAsBakedPointCloud(context.assetPath); + if (data != null) + { + context.AddObjectToAsset("container", data); + context.AddObjectToAsset("position", data.positionMap); + context.AddObjectToAsset("color", data.colorMap); + context.SetMainObject(data); + } + } } #endregion @@ -194,6 +207,25 @@ PointCloudData ImportAsPointCloudData(string path) } } + BakedPointCloud ImportAsBakedPointCloud(string path) + { + try + { + var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); + var header = ReadDataHeader(new StreamReader(stream)); + var body = ReadDataBody(header, new BinaryReader(stream)); + var data = ScriptableObject.CreateInstance(); + data.Initialize(body.vertices, body.colors); + data.name = Path.GetFileNameWithoutExtension(path); + return data; + } + catch (Exception e) + { + Debug.LogError("Failed importing " + path + ". " + e.Message); + return null; + } + } + DataHeader ReadDataHeader(StreamReader reader) { var data = new DataHeader(); diff --git a/Assets/Pcx/Runtime/BakedPointCloud.cs b/Assets/Pcx/Runtime/BakedPointCloud.cs new file mode 100644 index 00000000..c33211de --- /dev/null +++ b/Assets/Pcx/Runtime/BakedPointCloud.cs @@ -0,0 +1,77 @@ +// Pcx - Point cloud importer & renderer for Unity +// https://github.com/keijiro/Pcx + +using UnityEngine; +using System.Collections.Generic; + +namespace Pcx +{ + /// A container class for texture-baked point clouds. + public sealed class BakedPointCloud : ScriptableObject + { + #region Public properties + + /// Number of points + public int pointCount { get { return _pointCount; } } + + /// Position map texture + public Texture2D positionMap { get { return _positionMap; } } + + /// Color map texture + public Texture2D colorMap { get { return _colorMap; } } + + #endregion + + #region Serialized data members + + [SerializeField] int _pointCount; + [SerializeField] Texture2D _positionMap; + [SerializeField] Texture2D _colorMap; + + #endregion + + #region Editor functions + + #if UNITY_EDITOR + + public void Initialize(List positions, List colors) + { + _pointCount = positions.Count; + + var width = Mathf.CeilToInt(Mathf.Sqrt(_pointCount)); + + _positionMap = new Texture2D(width, width, TextureFormat.RGBAHalf, false); + _positionMap.name = "Position Map"; + _positionMap.filterMode = FilterMode.Point; + + _colorMap = new Texture2D(width, width, TextureFormat.RGBA32, false); + _colorMap.name = "Color Map"; + _colorMap.filterMode = FilterMode.Point; + + var i1 = 0; + var i2 = 0U; + + for (var y = 0; y < width; y++) + { + for (var x = 0; x < width; x++) + { + var i = i1 < _pointCount ? i1 : (int)(i2 % _pointCount); + var p = positions[i]; + + _positionMap.SetPixel(x, y, new Color(p.x, p.y, p.z)); + _colorMap.SetPixel(x, y, colors[i]); + + i1 ++; + i2 += 132049U; // prime + } + } + + _positionMap.Apply(false, true); + _colorMap.Apply(false, true); + } + + #endif + + #endregion + } +} diff --git a/Assets/Pcx/Runtime/BakedPointCloud.cs.meta b/Assets/Pcx/Runtime/BakedPointCloud.cs.meta new file mode 100644 index 00000000..34f4fe21 --- /dev/null +++ b/Assets/Pcx/Runtime/BakedPointCloud.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7e24966311c3a42b880056b5d15db86a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: