Skip to content

Commit

Permalink
triangle index base calculation enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
komietty committed Jul 8, 2021
1 parent 955b306 commit 1104234
Show file tree
Hide file tree
Showing 69 changed files with 3,006 additions and 1,030 deletions.
51 changes: 45 additions & 6 deletions Assets/AnimatedQuickhull/AnimatedQuickhull2D.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using Unity.Mathematics;
using static Unity.Mathematics.math;

Expand All @@ -9,14 +10,52 @@ namespace kmty.geom.d2.animatedquickhull {
using V2 = Vector2;
using V3 = Vector3;

public class AnimatedQuickhull2D {
public abstract class AnimatedQuickhull2D {
public Transform[] bones { get; protected set; }
public Convex convex { get; protected set; }
public float[] distPerBone { get; }
public abstract void Execute();
}

public class AnimatedQuickhull2DIndex: AnimatedQuickhull2D {
protected IEnumerable<int> vids;
protected Mesh mesh;
protected V3[] vrts;
protected int[] tris;

public AnimatedQuickhull2DIndex(SkinnedMeshRenderer skin, IEnumerable<int> tids) {
this.bones = skin.bones;
this.mesh = skin.sharedMesh;
this.vrts = mesh.vertices;
this.tris = mesh.triangles;
vids = tids.Select(tid => tris[tid * 3]);
}

public override void Execute() {
var tgts = new List<f2>();
var mtxs = new float4x4[bones.Length];
for (var i = 0; i < bones.Length; i++) tgts.Add((V2)bones[i].position);
for (int i = 0; i < mtxs.Length; i++) mtxs[i] = bones[i].localToWorldMatrix * mesh.bindposes[i];
foreach (var i in vids) {
var w = mesh.boneWeights[i];
var m = mtxs[w.boneIndex0] * w.weight0 +
mtxs[w.boneIndex1] * w.weight1 +
mtxs[w.boneIndex2] * w.weight2 +
mtxs[w.boneIndex3] * w.weight3;
tgts.Add((V2)((Matrix4x4)m).MultiplyPoint3x4(vrts[i]));
}
convex = new Convex(tgts);
convex.ExpandLoop();
}
}

public class AnimatedQuickhull2DAprox: AnimatedQuickhull2D {
protected float[] distPerBone;
protected float distanceThold;

public AnimatedQuickhull2D(SkinnedMeshRenderer skin, float weightThreshold = 0.5f) {
public AnimatedQuickhull2DAprox(SkinnedMeshRenderer skin, float weightThold = 0.3f, float distanceThold = 0.3f) {
this.bones = skin.bones;
this.distPerBone = new float[bones.Length];
this.distanceThold = distanceThold;
var mesh = skin.sharedMesh;
var vtcs = mesh.vertices;
var bspv = mesh.GetBonesPerVertex();
Expand All @@ -26,7 +65,7 @@ public AnimatedQuickhull2D(SkinnedMeshRenderer skin, float weightThreshold = 0.5

for (var vi = 0; vi < vtcs.Length; vi++) {
var bw = wgts[bwid];
if (bw.weight > weightThreshold) {
if (bw.weight > weightThold) {
var i = bw.boneIndex;
var d1 = distPerBone[i];
var d2 = length(vtcs[vi] - (V3)(bindposes[i].inverse * new Vector4(0, 0, 0, 1)));
Expand All @@ -36,12 +75,12 @@ public AnimatedQuickhull2D(SkinnedMeshRenderer skin, float weightThreshold = 0.5
}
}

public void Execute(float distanceTheshold) {
public override void Execute() {
var bps = new List<f2>();
for (var i = 0; i < bones.Length; i++) {
var b = bones[i];
var d = distPerBone[i];
if (d > distanceTheshold) {
if (d > distanceThold) {
bps.Add((V2)b.position + new V2(-d, -d));
bps.Add((V2)b.position + new V2(+d, -d));
bps.Add((V2)b.position + new V2(-d, +d));
Expand Down
95 changes: 66 additions & 29 deletions Assets/AnimatedQuickhull/AnimatedQuickhull3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,84 @@ namespace kmty.geom.d3.animatedquickhull {
using f3 = float3;
using V3 = Vector3;

public class AnimatedQuickhull3D {
public abstract class AnimatedQuickhull3D {
public Transform[] bones { get; protected set; }
public Convex convex { get; protected set; }
public abstract void Execute(int itr);

public Mesh CreateMesh() {
var mesh = new Mesh();
var nodes = convex.nodes;
var vs = new V3[nodes.Count * 3];
var ts = new int[nodes.Count * 3];
for(var i =0; i < nodes.Count; i++) {
var j = i * 3;
var n = nodes[i];
var v = cross(n.t.b - n.t.a, n.t.c - n.t.a);
var f = dot(v, n.normal) < 0;
vs[j + 0] = (f3)n.t.a;
vs[j + 1] = f ? (f3)n.t.c : (f3)n.t.b;
vs[j + 2] = f ? (f3)n.t.b : (f3)n.t.c;
ts[j + 0] = j;
ts[j + 1] = j + 1;
ts[j + 2] = j + 2;
}
mesh.SetVertices(vs);
mesh.SetTriangles(ts, 0);
mesh.RecalculateNormals();
return mesh;
}
}

public class AnimatedQuickhull3DIndex: AnimatedQuickhull3D {
protected IEnumerable<int> vids;
protected Mesh mesh;
protected V3[] vrts;
protected int[] tris;

public AnimatedQuickhull3DIndex(SkinnedMeshRenderer skin, IEnumerable<int> tids) {
this.bones = skin.bones;
this.mesh = skin.sharedMesh;
this.vrts = mesh.vertices;
this.tris = mesh.triangles;
vids = tids.Select(tid => tris[tid * 3]);
}

public override void Execute(int itr) {
var tgts = new List<f3>();
var mtxs = new float4x4[bones.Length];
for (var i = 0; i < bones.Length; i++) tgts.Add(bones[i].position);
for (int i = 0; i < mtxs.Length; i++) mtxs[i] = bones[i].localToWorldMatrix * mesh.bindposes[i];
foreach (var i in vids) {
var w = mesh.boneWeights[i];
var m = mtxs[w.boneIndex0] * w.weight0 +
mtxs[w.boneIndex1] * w.weight1 +
mtxs[w.boneIndex2] * w.weight2 +
mtxs[w.boneIndex3] * w.weight3;
tgts.Add(((Matrix4x4)m).MultiplyPoint3x4(vrts[i]));
}
convex = new Convex(tgts);
convex.ExpandLoop(itr);
}
}

public class AnimatedQuickhull3DAprox: AnimatedQuickhull3D {
public float[] distPerBone { get; }
public float distanceThold;

public AnimatedQuickhull3D(SkinnedMeshRenderer skin, float weightThreshold) {
public AnimatedQuickhull3DAprox(SkinnedMeshRenderer skin, float weightThold, float distanceThold) {
this.bones = skin.bones;
this.distPerBone = new float[bones.Length];
this.distanceThold = distanceThold;
var mesh = skin.sharedMesh;
var vtcs = mesh.vertices;
var bspv = mesh.GetBonesPerVertex();
var wgts = mesh.GetAllBoneWeights();
var bwid = 0;
var bindposes = mesh.bindposes;

for (var vi = 0; vi < vtcs.Length; vi++) {
var bw = wgts[bwid];
if (bw.weight > weightThreshold) {
if (bw.weight > weightThold) {
var i = bw.boneIndex;
var d1 = distPerBone[i];
var d2 = length(vtcs[vi] - (V3)(bindposes[i].inverse * new Vector4(0, 0, 0, 1)));
Expand All @@ -36,12 +96,12 @@ public AnimatedQuickhull3D(SkinnedMeshRenderer skin, float weightThreshold) {
}
}

public void Execute(float distanceThreshold, int itr) {
public override void Execute(int itr) {
var bps = new List<f3>();
for (var i = 0; i < bones.Length; i++) {
var b = bones[i];
var d = distPerBone[i];
if (d > distanceThreshold) {
if (d > distanceThold) {
bps.Add(b.position + new V3(-d, -d, -d));
bps.Add(b.position + new V3(+d, +d, +d));
bps.Add(b.position + new V3(+d, -d, -d));
Expand All @@ -57,28 +117,5 @@ public void Execute(float distanceThreshold, int itr) {
convex = new Convex(bps);
convex.ExpandLoop(itr);
}

public Mesh CreateMesh() {
var mesh = new Mesh();
var nodes = convex.nodes;
var vs = new V3[nodes.Count * 3];
var ts = new int[nodes.Count * 3];
for(var i =0; i < nodes.Count; i++) {
var j = i * 3;
var n = nodes[i];
var v = cross(n.t.b - n.t.a, n.t.c - n.t.a);
var f = dot(v, n.normal) < 0;
vs[j + 0] = (f3)n.t.a;
vs[j + 1] = f ? (f3)n.t.c : (f3)n.t.b;
vs[j + 2] = f ? (f3)n.t.b : (f3)n.t.c;
ts[j + 0] = j;
ts[j + 1] = j + 1;
ts[j + 2] = j + 2;
}
mesh.SetVertices(vs);
mesh.SetTriangles(ts, 0);
mesh.RecalculateNormals();
return mesh;
}
}
}
4 changes: 2 additions & 2 deletions Assets/Demo/Demo3D.mat → Assets/Demo/Debug.mat
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Material:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Demo3D
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_Name: Debug
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down
2 changes: 1 addition & 1 deletion Assets/Demo/Demo3D.mat.meta → Assets/Demo/Debug.mat.meta

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

80 changes: 80 additions & 0 deletions Assets/Demo/Demo.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Demo
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords: _ALPHAPREMULTIPLY_ON
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap:
RenderType: Transparent
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 10
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 3
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 0
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 0.39607844}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
m_BuildTextureStacks: []
8 changes: 8 additions & 0 deletions Assets/Demo/Demo.mat.meta

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

Loading

0 comments on commit 1104234

Please sign in to comment.