Skip to content

Commit

Permalink
Added cap to tube
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed May 30, 2017
1 parent b5f7a8c commit 02032c8
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 154 deletions.
5 changes: 3 additions & 2 deletions Assets/Swarm/Shader/Tube.shader
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
const float radius = 0.01;

float phi = v.vertex.x; // Angle in slice
float cap = v.vertex.y; // -1:head, +1:tail
float seg = v.vertex.z; // Segment index
uint iseg = (uint)seg;

Expand All @@ -75,8 +76,8 @@
float3 normal = n * cos(phi) + b * sin(phi); // Surface normal

// Feedback the results.
v.vertex = float4(p + normal * radius, 1);
v.normal = normal;
v.vertex = float4(p + normal * radius * (1 - abs(cap)), 1);
v.normal = normal * (1 - abs(cap)) + n * cap;
v.color = param;

#endif
Expand Down
52 changes: 47 additions & 5 deletions Assets/Swarm/TubeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,78 @@ public void Rebuild()
// Vertex array
var vertices = new List<Vector3>();

// Head tip
vertices.Add(new Vector3(0, -1, 0));

// Tube body vertices
for (var i = 0; i < _segments + 1; i++)
{
for (var j = 0; j < _divisions + 1; j++)
for (var j = 0; j < _divisions; j++)
{
var phi = Mathf.PI * 2 * j / _divisions;
vertices.Add(new Vector3(phi, 0, i));
}
}

// Tail tip
vertices.Add(new Vector3(0, 1, _segments)); // tail

// Index array
var indices = new List<int>();
var refi = 0;

// Head cap
for (var i = 0; i < _divisions - 1; i++)
{
indices.Add(0);
indices.Add(i + 2);
indices.Add(i + 1);
}

indices.Add(0);
indices.Add(1);
indices.Add(_divisions);

// Tube body indices
var refi = 1;

for (var i = 0; i < _segments; i++)
{
for (var j = 0; j < _divisions; j++)
for (var j = 0; j < _divisions - 1; j++)
{
indices.Add(refi);
indices.Add(refi + 1);
indices.Add(refi + 1 + _divisions);
indices.Add(refi + _divisions);

indices.Add(refi + 1);
indices.Add(refi + 2 + _divisions);
indices.Add(refi + 1 + _divisions);
indices.Add(refi + _divisions);

refi++;
}

indices.Add(refi);
indices.Add(refi + 1 - _divisions);
indices.Add(refi + _divisions);

indices.Add(refi + 1 - _divisions);
indices.Add(refi + 1);
indices.Add(refi + _divisions);

refi++;
}

// Tail cap
for (var i = 0; i < _divisions - 1; i++)
{
indices.Add(refi + i);
indices.Add(refi + i + 1);
indices.Add(refi + _divisions);
}

indices.Add(refi + _divisions - 1);
indices.Add(refi);
indices.Add(refi + _divisions);

// Mesh rebuilding
_mesh.Clear();
_mesh.SetVertices(vertices);
Expand Down
144 changes: 0 additions & 144 deletions Assets/Test/Template.asset

This file was deleted.

2 changes: 1 addition & 1 deletion Assets/Test/Test.unity
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_instanceCount: 4000
_template: {fileID: 11400000, guid: 14bd2a077b97cb042ac835ef28e4add9, type: 2}
_template: {fileID: 11400000, guid: 407b7afb8fbe855408cc9bf4dd39058a, type: 2}
_material: {fileID: 2100000, guid: b3e3f432bd29ac1499686dbad7ae39ef, type: 2}
_gradient: {fileID: 11400000, guid: 202a0875a5bc2554a932e60d8ed7e7ec, type: 2}
_volume: {fileID: 11400000, guid: 33827603f26d861438dab734dfca23e1, type: 2}
Expand Down
144 changes: 144 additions & 0 deletions Assets/Test/Tube Template.asset

Large diffs are not rendered by default.

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

0 comments on commit 02032c8

Please sign in to comment.