Created
November 16, 2020 19:59
-
-
Save melMass/ea9fda937d0ed21b9632241198923b21 to your computer and use it in GitHub Desktop.
Alembic Point Clouds to VFX Graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
using UnityEngine.Formats.Alembic.Importer; | |
using UnityEngine.VFX; | |
using UnityEngine.VFX.Utility; | |
namespace MTB.VFX | |
{ | |
[AddComponentMenu("VFX/Property Binders/MTB/Alembic Point Cloud")] | |
[VFXBinder("MTB/Alembic Point Cloud")] | |
sealed class AlembicPointCloudBinder : VFXBinderBase | |
{ | |
public ExposedProperty PositionMapProperty = "PositionMap"; | |
public ExposedProperty PositionCountProperty = "PositionCount"; | |
public AlembicPointsCloud target; | |
public bool EveryFrame = false; | |
private Texture2D positionMap; | |
private int count = 0; | |
protected override void OnEnable() | |
{ | |
base.OnEnable(); | |
UpdateTexture(); | |
} | |
public override bool IsValid(VisualEffect component) | |
{ | |
return target != null && | |
component.HasTexture(PositionMapProperty) && | |
component.HasInt(PositionCountProperty); | |
} | |
public override void UpdateBinding(VisualEffect component) | |
{ | |
if (EveryFrame || Application.isEditor) | |
UpdateTexture(); | |
component.SetTexture(PositionMapProperty, positionMap); | |
component.SetInt(PositionCountProperty, count); | |
} | |
void UpdateTexture() | |
{ | |
if (target == null || target.Positions.Count == 0) | |
return; | |
count = target.Positions.Count; | |
var width = Mathf.CeilToInt(Mathf.Sqrt(count)); | |
positionMap = new Texture2D(width, width, TextureFormat.RGBAFloat, false) | |
{ | |
name = gameObject.name + "_PositionMap", | |
filterMode = FilterMode.Point, | |
wrapMode = TextureWrapMode.Repeat | |
}; | |
var i1 = 0; | |
var i2 = 0U; | |
for (var y = 0; y < width; y++) | |
{ | |
for (var x = 0; x < width; x++) | |
{ | |
var i = i1 < count ? i1 : (int) (i2 % count); | |
var p = target.Positions[i]; | |
positionMap.SetPixel(x, y, new Color(p.x, p.y, p.z)); | |
i1++; | |
i2 += 132049U; // prime | |
} | |
} | |
positionMap.Apply(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment