Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
MeshConfigurable.cs
Go to the documentation of this file.
6 using UnityEngine;
7 
8 namespace droid.Runtime.Prototyping.Configurables.Experimental {
12  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
13  + "Mesh"
14  + ConfigurableComponentMenuPath._Postfix)]
15  [RequireComponent(typeof(MeshFilter))]
16  public class MeshConfigurable : Configurable {
17  string _mesh_str;
18 
19  Mesh _deforming_mesh = null;
20  Vector3[] _original_vertices = null, _displaced_vertices = null;
21  Perlin _noise = null;
22  float _speed = 1.0f;
23 
24  [SerializeField] Mesh[] _meshes = null;
25  [SerializeField] MeshFilter _mesh_filter = null;
26  [SerializeField] bool _displace_mesh = false;
27  [SerializeField] Space1 _deformation_space = new Space1 {_Min_Value = 1f, _Max_Value = 5f};
28 
32  protected override void PreSetup() {
33  this._mesh_str = this.Identifier + "Mesh";
34  this._mesh_filter = this.GetComponent<MeshFilter>();
35  if (Application.isPlaying) {
36  this._deforming_mesh = this._mesh_filter.mesh;
37  this._original_vertices = this._deforming_mesh.vertices;
38  this._displaced_vertices = new Vector3[this._original_vertices.Length];
39  for (var i = 0; i < this._original_vertices.Length; i++) {
40  this._displaced_vertices[i] = this._original_vertices[i];
41  }
42  }
43 
44  this._noise = new Perlin();
45  }
46 
50  protected override void RegisterComponent() {
51  this.ParentEnvironment =
52  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._mesh_str);
53  }
54 
58  protected override void UnRegisterComponent() {
59  this.ParentEnvironment?.UnRegister(this, this._mesh_str);
60  }
61 
62  public override ISpace ConfigurableValueSpace { get; }
63 
67  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
68  #if NEODROID_DEBUG
69  DebugPrinting.ApplyPrint(this.Debugging, configuration, this.Identifier);
70  #endif
71 
72  if (configuration.ConfigurableName == this._mesh_str) {
73  if (this._displace_mesh) {
74  if (this._deforming_mesh) {
75  var time_x = Time.time * this._speed + 0.1365143f;
76  var time_y = Time.time * this._speed + 1.21688f;
77  var time_z = Time.time * this._speed + 2.5564f;
78 
79  for (var i = 0; i < this._displaced_vertices.Length; i++) {
80  var orig = this._original_vertices[i];
81  //orig.y = orig.y * (1+(float)Math.Cos(Time.deltaTime))*(configuration.ConfigurableValue);
82  //orig.x = orig.x * (1+(float)Math.Sin(Time.deltaTime))*(configuration.ConfigurableValue);
83 
84  orig.x += this._noise.Noise(time_x + orig.x, time_x + orig.y, time_x + orig.z)
85  * configuration.ConfigurableValue;
86  orig.y += this._noise.Noise(time_y + orig.x, time_y + orig.y, time_y + orig.z)
87  * configuration.ConfigurableValue;
88  orig.z += this._noise.Noise(time_z + orig.x, time_z + orig.y, time_z + orig.z)
89  * configuration.ConfigurableValue;
90 
91  this._displaced_vertices[i] = orig;
92  }
93 
94  this._deforming_mesh.vertices = this._displaced_vertices;
95 
96  this._deforming_mesh.RecalculateNormals();
97  }
98  } else if (this._meshes.Length > 0) {
99  var idx = (int)(configuration.ConfigurableValue * this._meshes.Length);
100  this._mesh_filter.mesh = this._meshes[idx];
101  }
102  }
103  }
104 
109  public override Configuration[] SampleConfigurations() {
110  return new[] {new Configuration(this._mesh_str, this._deformation_space.Sample())};
111  }
112  }
113 }
override void ApplyConfiguration(IConfigurableConfiguration configuration)