Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
MGP-Project
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stenvers,V. (Victor)
MGP-Project
Commits
ec6eb465
Commit
ec6eb465
authored
5 years ago
by
Maurice Manshanden
Browse files
Options
Downloads
Patches
Plain Diff
Attempt at triangle splitting
parent
1b68d08b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Mini Project/Assets/Scenes/SampleScene.unity
+1
-0
1 addition, 0 deletions
Mini Project/Assets/Scenes/SampleScene.unity
Mini Project/Assets/Splitter.cs
+67
-14
67 additions, 14 deletions
Mini Project/Assets/Splitter.cs
with
68 additions
and
14 deletions
Mini Project/Assets/Scenes/SampleScene.unity
+
1
−
0
View file @
ec6eb465
...
...
@@ -310,6 +310,7 @@ MonoBehaviour:
m_Script
:
{
fileID
:
11500000
,
guid
:
c08c3492f2af1354c9a8faf4ca0c6381
,
type
:
3
}
m_Name
:
m_EditorClassIdentifier
:
seed
:
82
---
!u!135
&1965633082
SphereCollider
:
m_ObjectHideFlags
:
0
...
...
This diff is collapsed.
Click to expand it.
Mini Project/Assets/Splitter.cs
+
67
−
14
View file @
ec6eb465
...
...
@@ -23,10 +23,12 @@ public class MeshBuilder
public
Mesh
GetMesh
()
{
Mesh
mesh
=
new
Mesh
();
mesh
.
vertices
=
this
.
vertices
.
ToArray
();
mesh
.
normals
=
this
.
normals
.
ToArray
();
mesh
.
uv
=
this
.
uv
.
ToArray
();
Mesh
mesh
=
new
Mesh
{
vertices
=
this
.
vertices
.
ToArray
(),
normals
=
this
.
normals
.
ToArray
(),
uv
=
this
.
uv
.
ToArray
()
};
return
mesh
;
}
...
...
@@ -41,15 +43,15 @@ public class MeshBuilder
float
t
=
Vector3
.
Dot
(
p
-
origin
,
n
)
/
Vector3
.
Dot
(
direction
,
n
);
// Interpolate vertex, normal and uv
Vector3
vertex
=
t
*
this
.
vertices
[
index1
]
+
(
1
-
t
)
*
this
.
vertices
[
index2
]
;
Vector3
vertex
=
origin
+
direction
*
t
;
Vector3
normal
=
t
*
this
.
normals
[
index1
]
+
(
1
-
t
)
*
this
.
normals
[
index2
];
Vector2
uv
=
t
*
this
.
uv
[
index1
]
+
(
1
-
t
)
*
this
.
uv
[
index2
];
this
.
vertices
.
Add
(
vertex
);
this
.
normals
.
Add
(
normal
);
this
.
normals
.
Add
(
normal
.
normalized
);
this
.
uv
.
Add
(
uv
);
return
this
.
vertices
.
Count
;
return
this
.
vertices
.
Count
-
1
;
}
public
Vector3
GetCenter
(
IEnumerable
<
int
>
triangles
)
...
...
@@ -74,6 +76,11 @@ public class Splitter : MonoBehaviour
private
MeshBuilder
mesh
;
private
MeshTreeNode
root
;
float
total
=
0.3f
;
bool
swtch
;
public
int
seed
;
void
Start
()
{
Mesh
mesh
=
this
.
GetComponent
<
MeshFilter
>().
mesh
;
...
...
@@ -82,7 +89,29 @@ public class Splitter : MonoBehaviour
this
.
BuildMeshTree
();
this
.
GetComponent
<
MeshFilter
>().
mesh
=
this
.
mesh
.
GetMesh
();
this
.
GetComponent
<
MeshFilter
>().
mesh
.
triangles
=
this
.
root
.
left
.
triangles
;
this
.
GetComponent
<
MeshFilter
>().
mesh
.
triangles
=
this
.
root
.
triangles
;
Random
.
InitState
(
this
.
seed
);
}
private
void
Update
()
{
this
.
total
+=
Time
.
deltaTime
;
if
(
this
.
total
>
0.4
)
{
this
.
total
=
0
;
this
.
swtch
=
!
this
.
swtch
;
if
(
this
.
swtch
)
{
this
.
GetComponent
<
MeshFilter
>().
mesh
.
triangles
=
this
.
root
.
right
.
triangles
;
}
else
{
this
.
GetComponent
<
MeshFilter
>().
mesh
.
triangles
=
this
.
root
.
left
.
triangles
;
}
}
}
void
BuildMeshTree
()
...
...
@@ -128,9 +157,9 @@ public class Splitter : MonoBehaviour
Vector3
v3
=
this
.
mesh
.
vertices
[
i3
];
// Test for each vertex whether they lie on the same side of the plane
bool
s1
=
v1
.
x
*
n
.
x
+
v1
.
y
*
n
.
y
+
v1
.
z
*
n
.
z
+
d
<
0
;
bool
s2
=
v2
.
x
*
n
.
x
+
v2
.
y
*
n
.
y
+
v2
.
z
*
n
.
z
+
d
<
0
;
bool
s3
=
v3
.
x
*
n
.
x
+
v3
.
y
*
n
.
y
+
v3
.
z
*
n
.
z
+
d
<
0
;
bool
s1
=
v1
.
x
*
n
.
x
+
v1
.
y
*
n
.
y
+
v1
.
z
*
n
.
z
+
d
>
0
;
bool
s2
=
v2
.
x
*
n
.
x
+
v2
.
y
*
n
.
y
+
v2
.
z
*
n
.
z
+
d
>
0
;
bool
s3
=
v3
.
x
*
n
.
x
+
v3
.
y
*
n
.
y
+
v3
.
z
*
n
.
z
+
d
>
0
;
if
(
s1
&&
s2
&&
s3
)
{
...
...
@@ -142,8 +171,32 @@ public class Splitter : MonoBehaviour
}
else
{
// The difficult case where one vertex is on one side, and the other
// The difficult case
s
where one vertex is on one side, and the other
// two vertices are on the other side of the plane.
int
s
=
-
1
,
p1
=
-
1
,
p2
=
-
1
,
l
=
-
1
;
if
(
s1
&&
!
s2
&&
!
s3
)
{
s
=
i1
;
p1
=
i2
;
p2
=
i3
;
l
=
0
;
}
if
(!
s1
&&
s2
&&
!
s3
)
{
s
=
i2
;
p1
=
i1
;
p2
=
i3
;
l
=
0
;
}
if
(!
s1
&&
!
s2
&&
s3
)
{
s
=
i3
;
p1
=
i1
;
p2
=
i2
;
l
=
0
;
}
if
(!
s1
&&
s2
&&
s3
)
{
s
=
i1
;
p1
=
i2
;
p2
=
i3
;
l
=
1
;
}
if
(
s1
&&
!
s2
&&
s3
)
{
s
=
i2
;
p1
=
i1
;
p2
=
i3
;
l
=
1
;
}
if
(
s1
&&
s2
&&
!
s3
)
{
s
=
i3
;
p1
=
i1
;
p2
=
i2
;
l
=
1
;
}
int
x1
=
mesh
.
SplitEdge
(
p1
,
s
,
p
,
n
);
int
x2
=
mesh
.
SplitEdge
(
p2
,
s
,
p
,
n
);
if
(
l
==
0
)
{
left
.
AddRange
(
new
[]
{
x1
,
x2
,
s
});
right
.
AddRange
(
new
[]
{
x1
,
p1
,
p2
,
p2
,
x2
,
x1
});
}
else
{
right
.
AddRange
(
new
[]
{
x1
,
x2
,
s
});
left
.
AddRange
(
new
[]
{
x1
,
p1
,
p2
,
p2
,
x2
,
x1
});
}
}
}
...
...
@@ -159,7 +212,7 @@ public class Splitter : MonoBehaviour
};
// Split children
SplitNode
(
node
.
left
);
SplitNode
(
node
.
right
);
//
SplitNode(node.left);
//
SplitNode(node.right);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment