浏览代码

add safety checks to attachments, to prevent out of bounds addressing if e.g. underlying assets changed

/main
Lasse Jon Fuglsang Pedersen 5 年前
当前提交
580b38d8
共有 3 个文件被更改,包括 26 次插入10 次删除
  1. 2
      Editor/SkinAttachmentTargetEditor.cs
  2. 6
      Runtime/SkinAttachmentData.cs
  3. 28
      Runtime/SkinAttachmentTarget.cs

2
Editor/SkinAttachmentTargetEditor.cs


var checksumFailed = driver.CommitRequired();
if (checksumFailed)
{
EditorGUILayout.HelpBox("Rebuild required: Checksum of one or more subjects does not match checksum of data.", MessageType.Warning);
EditorGUILayout.HelpBox("Rebuild required: Checksum of driver or one or more subjects does not match checksum of data.", MessageType.Warning);
}
else if (driver.attachData.subjectCount != driver.subjects.Count)
{

6
Runtime/SkinAttachmentData.cs


[PreferBinarySerialization]
public class SkinAttachmentData : ScriptableObject
{
//[HideInInspector]
//public int driverChecksum = -1;
[HideInInspector]
public int driverVertexCount = 0;
[HideInInspector]
public ulong checksum0 = 0;
[HideInInspector]

public void Clear()
{
driverVertexCount = 0;
checksum0 = 0;
checksum1 = 0;
poseCount = 0;

28
Runtime/SkinAttachmentTarget.cs


Profiler.EndSample();
}
public ref MeshInfo GetCachedMeshInfo()
public ref MeshInfo GetCachedMeshInfo(bool forceRefresh = false)
if (frameIndex != cachedMeshInfoFrame)
if (frameIndex != cachedMeshInfoFrame || forceRefresh)
{
UpdateMeshInfo(ref cachedMeshInfo);

if (attachData == null)
return false;
if (meshBuffers.vertexCount < attachData.driverVertexCount)
return true;
for (int i = 0, n = subjects.Count; i != n; i++)
{
if (subjects[i].Checksum() != attachData.Checksum())

if (attachData == null)
return;
var meshInfo = GetCachedMeshInfo();
var meshInfo = GetCachedMeshInfo(forceRefresh: true);
attachData.driverVertexCount = meshInfo.meshBuffers.vertexCount;
{
subjects.RemoveAll(p => (p == null));

foreach (var j in subject.meshAdjacency.vertexVertices[i])
{
var targetDelta = targetPositions.val[j] - meshBuffers.vertexPositions[targetNode];
var targetNormalDist = Vector3.Dot(targetDelta, meshBuffers.vertexNormals[targetNode]);
var targetDelta = targetPositions.val[j] - meshInfo.meshBuffers.vertexPositions[targetNode];
var targetNormalDist = Vector3.Dot(targetDelta, meshInfo.meshBuffers.vertexNormals[targetNode]);
if (targetNormalDist < 0.0f)
{
var d = Vector3.SqrMagnitude(targetDelta);

rootGen.val[i] = -1;
// see if node qualifies as second choice root
var targetDelta = targetPositions.val[i] - meshBuffers.vertexPositions[targetNode];
var targetNormalDist = Mathf.Abs(Vector3.Dot(targetDelta, meshBuffers.vertexNormals[targetNode]));
var targetDelta = targetPositions.val[i] - meshInfo.meshBuffers.vertexPositions[targetNode];
var targetNormalDist = Mathf.Abs(Vector3.Dot(targetDelta, meshInfo.meshBuffers.vertexNormals[targetNode]));
if (targetNormalDist < bestDist0)
{
bestDist1 = bestDist0;

{
visitor.Ignore(bestVert0);
rootIdx.val[bestVert0] = bestNode0;
rootDir.val[bestVert0] = Vector3.Normalize(meshBuffers.vertexPositions[bestNode0] - targetPositions.val[bestVert0]);
rootDir.val[bestVert0] = Vector3.Normalize(meshInfo.meshBuffers.vertexPositions[bestNode0] - targetPositions.val[bestVert0]);
rootGen.val[bestVert0] = 0;
rootCount++;

rootIdx.val[bestVert1] = bestNode1;
rootDir.val[bestVert1] = Vector3.Normalize(meshBuffers.vertexPositions[bestNode1] - targetPositions.val[bestVert1]);
rootDir.val[bestVert1] = Vector3.Normalize(meshInfo.meshBuffers.vertexPositions[bestNode1] - targetPositions.val[bestVert1]);
rootGen.val[bestVert1] = 0;
rootCount++;
}

if (attachData == null)
return;
if (attachData.driverVertexCount > meshBuffers.vertexCount)
return;// prevent out of bounds if mesh shrunk since data was built
Profiler.BeginSample("resolve-subj-all");
subjects.RemoveAll(p => p == null);

int attachmentCount = subject.attachmentCount;
if (attachmentIndex == -1)
continue;
if (attachmentIndex + attachmentCount > attachData.itemCount)
continue;// prevent out of bounds if subject holds damaged index/count
var indexPos = i * 2 + 0;
var indexNrm = i * 2 + 1;

正在加载...
取消
保存