Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
NormalMapConfigurable.cs
Go to the documentation of this file.
1 using System;
5 using UnityEngine;
6 using Random = UnityEngine.Random;
7 
8 namespace droid.Runtime.Prototyping.Configurables.Experimental {
12  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
13  + "NormalMap"
14  + ConfigurableComponentMenuPath._Postfix)]
15  [RequireComponent(typeof(Renderer))]
17  [SerializeField] Texture[] _textures = null;
18  [SerializeField] bool load_from_resources_if_empty = true;
19  [SerializeField] Texture _texture = null;
20  [SerializeField] Renderer _renderer = null;
21  [SerializeField] bool use_shared = false;
22  [SerializeField] Material _mat;
23  [SerializeField] int _last_sample;
24  static readonly Int32 _main_tex = Shader.PropertyToID("_BumpMap");
25 
29  protected override void PreSetup() {
30  this._renderer = this.GetComponent<Renderer>();
31  if (Application.isPlaying) {
32  if (this.use_shared) {
33  this._mat = this._renderer?.sharedMaterial;
34  } else {
35  this._mat = this._renderer?.material;
36  }
37  }
38 
39  if (this.load_from_resources_if_empty) {
40  if (this._textures == null || this._textures.Length == 0) {
41  this._textures = Resources.LoadAll<Texture>("Textures");
42  }
43  }
44  }
45 
46  public override ISpace ConfigurableValueSpace { get; }
47 
51  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
52  #if NEODROID_DEBUG
53  if (this.Debugging) {
54  DebugPrinting.ApplyPrint(this.Debugging, configuration, this.Identifier);
55  }
56  #endif
57 
58  this._texture = this._textures[(int)configuration.ConfigurableValue];
59 
60  this._mat.SetTexture(_main_tex, this._texture);
61  }
62 
67  public override Configuration[] SampleConfigurations() {
68  this._last_sample = int.Parse(Random.Range(0, this._textures.Length).ToString());
69 
70  return new[] {new Configuration(this.Identifier, this._last_sample)};
71  }
72  }
73 }
override void ApplyConfiguration(IConfigurableConfiguration configuration)