-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshDistortion.cs
More file actions
127 lines (104 loc) · 4.15 KB
/
MeshDistortion.cs
File metadata and controls
127 lines (104 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Linq;
using Sceelix.Core.Annotations;
using Sceelix.Core.Attributes;
using Sceelix.Core.IO;
using Sceelix.Core.Parameters;
using Sceelix.Core.Procedures;
using Sceelix.Extensions;
using Sceelix.Mathematics.Data;
using Sceelix.Mathematics.Parameters;
using Sceelix.Meshes.Data;
using Sceelix.Paths.Data;
namespace Sceelix.MyNewEngineLibrary
{
/// <summary>
/// Randomly distort the mesh's vertices
/// </summary>
[Procedure("8034DF9C-9104-453D-B249-B06D9073AC33", Label = "Mesh Distort")]
public class MeshDistort : SystemProcedure
{
/// <summary>
/// Input and Output
/// </summary>
private readonly SingleInput<MeshEntity> _input = new SingleInput<MeshEntity>("Input");
private readonly Output<MeshEntity> _output = new Output<MeshEntity>("Output");
/// <summary>
/// The direction (and amount) of the distortion
/// </summary>
private readonly Vector3DParameter _parameterAmount = new Vector3DParameter("Direction");
/// <summary>
/// The random seed
/// </summary>
private readonly IntParameter _parameterSeed = new IntParameter("Seed", 1000);
/// <summary>
/// What the direction is referring to.
/// </summary>
private readonly ChoiceParameter _parameterRelativeTo = new ChoiceParameter("Relative To", "Normals", new string[]
{
"Normals",
"World",
"Scope"
});
private class VertexEx
{
public Vertex Vertex = null;
public List<Face> adjacentFace = new List<Face>();
public Vector3D GetVertexNormal()
{
Vector3D v = new Vector3D();
foreach(Face face in adjacentFace)
v += face.Normal;
return v / adjacentFace.Count;
}
}
List<VertexEx> vertices = new List<VertexEx>();
private void CollateVertices(Face face, Vertex vertex)
{
var q = from foundvertex in vertices
where foundvertex.Vertex == vertex
select foundvertex;
VertexEx found = null;
if(q.Count() == 0)
{
found = new VertexEx() { Vertex = vertex };
vertices.Add(found);
}
else
found = q.First();
found.adjacentFace.Add(face);
}
protected override void Run()
{
MeshEntity meshEntity = _input.Read();
Vector3D actualTranslation;
actualTranslation = _parameterRelativeTo.Value == "World"
? _parameterAmount.Value
: meshEntity.BoxScope.ToWorldDirection(_parameterAmount.Value);
foreach(Face face0 in meshEntity.Faces)
foreach(Vertex vertex0 in face0.Vertices)
CollateVertices(face0, vertex0);
Random random = new Random(_parameterSeed.Value);
foreach(Face face1 in meshEntity.Faces)
foreach(Vertex vertex1 in face1.Vertices)
{
if(_parameterRelativeTo.Value == "Normals")
{
var q = from foundvertex in vertices
where foundvertex.Vertex == vertex1
select foundvertex;
actualTranslation = q.First().GetVertexNormal();
actualTranslation *= _parameterAmount.Value.Length;
}
float r = random.Float(-1, 1);
vertex1.Position = vertex1.Position + actualTranslation * r;
}
// Data was modified in-place, no need to re-construct.
MeshEntity output = meshEntity;
//meshEntity.Attributes.SetAttributesTo(output.Attributes);
//finally, return the newly create meshEntity
_output.Write(output);
}
}
}