Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
TextureConfigurable.cs
Go to the documentation of this file.
4 using UnityEngine;
5 
6 namespace droid.Runtime.Prototyping.Configurables {
10  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
11  + "Texture"
12  + ConfigurableComponentMenuPath._Postfix)]
13  [RequireComponent(typeof(Renderer))]
15  [SerializeField] Texture[] _textures = null;
16  [SerializeField] bool load_from_resources_if_empty = true;
17  [SerializeField] Texture _texture = null;
18  [SerializeField] Renderer _renderer = null;
19  [SerializeField] bool use_shared = false;
20  Material _mat;
21  [SerializeField] int _last_sample;
22  [SerializeField] string load_path = "Textures";
23  static readonly int _main_tex = Shader.PropertyToID("_MainTex");
24 
28  protected override void PreSetup() {
29  this._renderer = this.GetComponent<Renderer>();
30  if (Application.isPlaying) {
31  if (this.use_shared) {
32  this._mat = this._renderer?.sharedMaterial;
33  } else {
34  this._mat = this._renderer?.material;
35  }
36  }
37 
38  if (this.load_from_resources_if_empty) {
39  if (this._textures == null || this._textures.Length == 0) {
40  this._textures = Resources.LoadAll<Texture>(this.load_path);
41  }
42  }
43  }
44 
45  public override ISpace ConfigurableValueSpace { get; }
46 
50  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
51  #if NEODROID_DEBUG
52  if (this.Debugging) {
53  DebugPrinting.ApplyPrint(this.Debugging, configuration, this.Identifier);
54  }
55  #endif
56 
57  this._texture = this._textures[(int)configuration.ConfigurableValue];
58 
59  this._mat.SetTexture(_main_tex, this._texture);
60  }
61 
65  protected override void Randomise() {
66  this._texture = this._textures[Random.Range(0, this._textures.Length)];
67 
68  this._mat.SetTexture(_main_tex, this._texture);
69  }
70 
75  public override Configuration[] SampleConfigurations() {
76  this._last_sample = Random.Range(0, this._textures.Length);
77 
78  return new[] {new Configuration(this.Identifier, this._last_sample)};
79  }
80  }
81 }
override void ApplyConfiguration(IConfigurableConfiguration configuration)