Skip to content

Commit

Permalink
Merge pull request #13 from keijiro/dev-vfxgraph
Browse files Browse the repository at this point in the history
Add BakedPointCloud support
  • Loading branch information
keijiro authored Mar 1, 2019
2 parents 05a4961 + 02d23fe commit 6b5999d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
36 changes: 34 additions & 2 deletions Assets/Pcx/Editor/PlyImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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<BakedPointCloud>();
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();
Expand Down
77 changes: 77 additions & 0 deletions Assets/Pcx/Runtime/BakedPointCloud.cs
Original file line number Diff line number Diff line change
@@ -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<Vector3> positions, List<Color32> 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
}
}
11 changes: 11 additions & 0 deletions Assets/Pcx/Runtime/BakedPointCloud.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b5999d

Please sign in to comment.