Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ChangeMaterialOnRenderByInstance.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
4 using UnityEngine;
5 using Random = UnityEngine.Random;
6 
7 namespace droid.Runtime.Utilities.GameObjects.NeodroidCamera.Segmentation.Obsolete {
11  [ExecuteInEditMode]
15  Renderer[] _all_renders;
16 
19  MaterialPropertyBlock _block;
20 
23  LinkedList<Color>[] _original_colors;
24 
25  [SerializeField] ColorByInstance[] instanceColorArray;
26 
29  public Dictionary<GameObject, Color> ColorsDictGameObject { get; } = new Dictionary<GameObject, Color>();
30 
33  public override Dictionary<String, Color> ColorsDict {
34  get {
35  var colors = new Dictionary<String, Color>();
36  foreach (var key_val in this.ColorsDictGameObject) {
37  colors.Add(key_val.Key.GetInstanceID().ToString(), key_val.Value);
38  }
39 
40  return colors;
41  }
42  }
43 
46  public ColorByInstance[] InstanceColors {
47  get {
48  if (this.ColorsDictGameObject != null) {
49  this.instanceColorArray = new ColorByInstance[this.ColorsDictGameObject.Keys.Count];
50  var i = 0;
51  foreach (var key in this.ColorsDictGameObject.Keys) {
52  var seg = new ColorByInstance {_Game_Object = key, _Color = this.ColorsDictGameObject[key]};
53  this.instanceColorArray[i] = seg;
54  i++;
55  }
56 
57  return this.instanceColorArray;
58  }
59 
60  return null;
61  }
62  set {
63  foreach (var seg in value) {
64  this.ColorsDictGameObject[seg._Game_Object] = seg._Color;
65  }
66  }
67  }
68 
69  // Use this for initialization
72  void Start() { this.Setup(); }
73 
76  void Awake() {
77  this._all_renders = FindObjectsOfType<Renderer>();
78  this._block = new MaterialPropertyBlock();
79  this.Setup();
80  }
81 
82  // Update is called once per frame
85  void Update() {
86  var renderers = FindObjectsOfType<Renderer>();
87  if (this.ColorsDictGameObject == null) {
88  this.Setup();
89  } else if (this.ColorsDictGameObject.Keys.Count != renderers.Length) {
90  this._all_renders = renderers;
91  this.Setup();
92  }
93  }
94 
95  void CheckBlock() {
96  if (this._block == null) {
97  this._block = new MaterialPropertyBlock();
98  }
99  }
100 
103  void Setup() {
104  this.CheckBlock();
105 
106  this.ColorsDictGameObject.Clear();
107  foreach (var rend in this._all_renders) {
108  if (rend) {
109  this.ColorsDictGameObject.Add(rend.gameObject, Random.ColorHSV());
110  }
111  }
112  }
113 
116  protected override void Change() {
117  this.CheckBlock();
118  this._original_colors = new LinkedList<Color>[this._all_renders.Length];
119 
120  for (var i = 0; i < this._original_colors.Length; i++) {
121  this._original_colors[i] = new LinkedList<Color>();
122  }
123 
124  for (var i = 0; i < this._all_renders.Length; i++) {
125  var c_renderer = this._all_renders[i];
126  if (c_renderer) {
127  foreach (var mat in c_renderer.sharedMaterials) {
128  if (mat != null && mat.HasProperty(this._Default_Color_Tag)) {
129  this._original_colors[i].AddFirst(mat.color);
130  }
131 
132  if (this.ColorsDictGameObject.ContainsKey(c_renderer.gameObject)) {
133  var val = this.ColorsDictGameObject[c_renderer.gameObject];
134  this._block.SetColor(this._Segmentation_Color_Tag, val);
135  this._block.SetColor(this._Outline_Color_Tag, this._Outline_Color);
136  this._block.SetFloat(this._Outline_Width_Factor_Tag, this._Outline_Width_Factor);
137  }
138 
139  c_renderer.SetPropertyBlock(this._block);
140  }
141  }
142  }
143  }
144 
147  protected override void Restore() {
148  this.CheckBlock();
149  for (var i = 0; i < this._all_renders.Length; i++) {
150  var c_renderer = this._all_renders[i];
151  if (c_renderer) {
152  foreach (var mat in c_renderer.sharedMaterials) {
153  if (mat != null && this._original_colors != null && i < this._original_colors.Length) {
154  var c_original_color = this._original_colors[i];
155  if (c_original_color != null) {
156  var c = this._original_colors[i];
157  var last = c?.Last;
158  if (last != null) {
159  var last_val = last.Value;
160  this._block.SetColor(this._Default_Color_Tag, last_val);
161  c_original_color.RemoveLast();
162  c_renderer.SetPropertyBlock(this._block);
163  }
164  }
165  }
166  }
167  }
168  }
169  }
170  }
171 }