随着最近的VFX Graph的引入,属性贴图被用于'从贴图设置位置/颜色'。
为了获得属性贴图,必须将位置和颜色数据烘焙到渲染纹理中。但是没有提到如何做到这一点,甚至在Unity文档中也没有。
将提供任何有关此操作的帮助!
大多数情况下,您想使用Compute Shader将一连串的点烘焙到纹理中。我建议您检查这些存储库以供参考:
将蒙皮的网格渲染器数据烘焙到纹理中https://github.com/keijiro/Smrvfx
将Kinect数据烘焙到纹理中https://github.com/roelkok/Kinect-VFX-Graph
将pointcloud数据烘焙为纹理:https://github.com/keijiro/Pcx
就我个人而言,尽管我不是Compute Shaders的专家,我仍在使用这些脚本以达到我的目的。]
public class FramePositionBaker { ComputeShader bakerShader; RenderTexture VFXpositionMap; RenderTexture inputPositionTexture; private ComputeBuffer positionBuffer; const int texSize = 256; public FramePositionBaker(RenderTexture _VFXPositionMap) { inputPositionTexture = new RenderTexture(texSize, texSize, 0, RenderTextureFormat.ARGBFloat); inputPositionTexture.enableRandomWrite = true; inputPositionTexture.Create(); bakerShader = (ComputeShader)Resources.Load("FramePositionBaker"); if (bakerShader == null) { Debug.LogError("[FramePositionBaker] baking shader not found in any Resources folder"); } VFXpositionMap = _VFXPositionMap; } public void BakeFrame(ref Vector3[] vertices) { int pointCount = vertices.Length; positionBuffer = new ComputeBuffer(pointCount, 3 * sizeof(float)); positionBuffer.SetData(vertices); //Debug.Log("Length " + vertices.Length); bakerShader.SetInt("dim", texSize); bakerShader.SetTexture(0, "PositionTexture", inputPositionTexture); bakerShader.SetBuffer(0, "PositionBuffer", positionBuffer); bakerShader.Dispatch(0, (texSize / 8) + 1, (texSize / 8) + 1, 1); Graphics.CopyTexture(inputPositionTexture, VFXpositionMap); positionBuffer.Dispose(); } }
计算着色器:
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> PositionTexture;
uint dim;
Buffer<float3> PositionBuffer;
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
// TODO: insert actual code here!
uint index = id.y * dim + id.x;
uint lastIndex = PositionBuffer.Length - 1;
// Trick for generating a pseudo-random number.
// Inspired by a similar trick in Keijiro's PCX repo (BakedPointCloud.cs).
// The points that are in excess because of the square texture, point randomly to a point in the texture.
// e.g. if (index > lastIndex) index = 0 generates excessive particles in the first position, resulting in a visible artifact.
//if (index > lastIndex) index = ( index * 132049U ) % lastIndex;
float3 pos;
if (index > lastIndex && lastIndex != 0) {
//pos = 0;
index = ( index * 132049U ) % lastIndex;
}
pos = PositionBuffer[index];
PositionTexture[id.xy] = float4 (pos.x, pos.y, pos.z, 1);
}