-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransparencyManager.cs
371 lines (311 loc) · 16.7 KB
/
TransparencyManager.cs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*******************TRANSPARENCY_SCRIPT****************************************************
*
* Script implmented to make, unmake and more actions with transparent objects
*
*
* Creation Date: 26/03/2015
*
* UiW European Project
* Grupo DIANA - University of Malaga
***************************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TransparencyManager : MonoBehaviour {
//Variables
List<GameObject> gameObjectTransparentList; //List of transparent gameobjects
// public bool smartTransparencyState; //State of the smart transparency
//private GameObject goTurbine; //Link to the object that contain the scripts
//private GameObject mainCamera; //Link to the object that contains the main camera
WaitForSeconds capsuleCastWait = new WaitForSeconds(1.0f); //Smart transparency corutine wait time
//AdaptativeTransparency- RayCast
Vector3 mainCameraPos; //Main Camera Global position
List<Vector3> RayDestinationPoints_List; //List to store every destinition points (Vector3) to draw the ray. Ray comes from the camera to the destity points
//Constants
public const int RAYS_PER_AXIS = 5; //Num of rays to be casted to the selected Area per semi-axes
public const int MAX_RAYS_TOTAL = 100; //Maximum number of rays to be casted in total
///////////////////////////////////////////////////////////////////////////
/// <summary> Start this instance. Use this for initialization.</summary> //
///////////////////////////////////////////////////////////////////////////
private void Awake()
{
gameObjectTransparentList = new List<GameObject>(); //We create a new blank list
RayDestinationPoints_List = new List<Vector3>(); //Destination points for rays
// smartTransparencyState = false; //Initialize smart transperency state
}
///////////////////////////////////////////////////////////////
////////////////////// Others Methods ////////////////////////
///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/// <summary> Method to return the number of transparent objects.</summary>
/// <returns> The of tranparent game objects.</returns>
//////////////////////////////////////////////////////////////////////////////
public int NumberOfTranparentGameObjects()
{
return gameObjectTransparentList.Count;
}
/////////////////////////////////////////////////////////////
/// <summary> Check if an object is transparent.</summary> //
/////////////////////////////////////////////////////////////
public bool IsTransparentGameObject(GameObject obj)
{
return gameObjectTransparentList.Contains (obj);
}//END IsTransparentGameObject
/// <summary> Numbers the of transparent game objects. </summary>
/// <returns>The of transparent game objects.</returns>
public int NumberOfTransparentGameObjects()
{
return gameObjectTransparentList.Count;
}
/////////////////////////////////////////////////////
/// <summary> Make an object transparent.</summary> //
/////////////////////////////////////////////////////
public void TransparencyOn(GameObject obj)
{
if (!IsTransparentGameObject (obj))
{
//We store the object that we are removing into a list.
gameObjectTransparentList.Add (obj);
}
}//END GameObjectTransparencyON
////////////////////////////////////////////////////////////////
/// <summary> Remove the transparency of an object.</summary> //
////////////////////////////////////////////////////////////////
public void TransparencyOff(GameObject obj)
{
if (IsTransparentGameObject (obj))
{
//We store the object that we are removing into a list.
gameObjectTransparentList.Remove (obj);
//We change the material of the object to the transparent one
//obj.GetComponent<ObjectState_Script> ().GameObjectTransparencyOff ();
}
}//END GameObjectTransparencyOff
public bool IsTransparentGameObjectList(List<GameObject> listObj)
{
bool temp;
temp = true;
//Move in the list of objects and check if is transparent
foreach (GameObject obj in listObj) {
temp = temp & gameObjectTransparentList.Contains (obj);
}//End foreach
//temp is true only if all the objects are transparent (are in the list)
return temp;
}
///////////////////////////////////////////////////////////////////////
/// <summary> Remove the transparency of all the objects.</summary> //
///////////////////////////////////////////////////////////////////////
public void AllGameObjectTransparencyOff(bool _instanlly = false)
{
float duration;
// Get duration value
if (_instanlly) {
duration = 0.1f;
} else
{
duration = hom3r.quickLinks.scriptsObject.GetComponent<ConfigurationManager>().GetDurationTransparencyAnimation();
}
//Make not transparent one by one
List<GameObject> temp = new List<GameObject>(gameObjectTransparentList);
foreach (GameObject obj in temp)
{
if (obj != null)
{
obj.GetComponent<ObjectStateManager>().Do(new CObjectVisualStateCommand(TObjectVisualStateCommands.Transparency_Off, duration));
}
}
//Empty the list of hidden objects
gameObjectTransparentList.Clear ();
}
////////////////////////////////////////////////////////////////////////////
/// <summary> Make transparent a list of objets. </summary> //
////////////////////////////////////////////////////////////////////////////
public void GameObjectListTransparencyOn(List<GameObject> listObj, float duration)
{
//Move in the list of objects and make transparent
foreach (GameObject obj in listObj)
{
//obj.GetComponent<ObjectStateManager>().SendEvent(TObjectVisualStateCommands.Transparency_On, duration);
obj.GetComponent<ObjectStateManager>().Do(new CObjectVisualStateCommand(TObjectVisualStateCommands.Transparency_On, duration));
}
}
////////////////////////////////////////////////////////////////////////////
/// <summary> Make not transparent a list of objets. </summary> //
////////////////////////////////////////////////////////////////////////////
public void GameObjectListTransparencyOff(List<GameObject> listObj, float duration)
{
//Move in the list of objects and make transparent
foreach (GameObject obj in listObj) {
// obj.GetComponent<ObjectStateManager>().SendEvent(TObjectVisualStateCommands.Transparency_Off, duration);
obj.GetComponent<ObjectStateManager>().Do(new CObjectVisualStateCommand(TObjectVisualStateCommands.Transparency_Off, duration));
}
}
/////////////////////////////
/// ALPHA Level
/////////////////////////////
/// <summary>
///
/// </summary>
private void SetSmartTransparencyLevelToAllTransparentObjects()
{
foreach (GameObject obj in gameObjectTransparentList)
{
ObjectStateMaterialUtils.SetAlphaColorToMaterial(obj.GetComponent<Renderer>().material, hom3r.state.smartTransparencyAlphaLevel);
}
}
public void SetSmartTransparencyAlphaLevel(float value, THom3rCommandOrigin _origin)
{
hom3r.state.smartTransparencyAlphaLevel = value;
if (_origin == THom3rCommandOrigin.io)
{
hom3r.coreLink.EmitEvent(new CCoreEvent(TCoreEvent.Occlusion_SmartTransparency_AlphaLevelUpdated));
}
this.SetSmartTransparencyLevelToAllTransparentObjects();
}
/////////////////////////////////////////////////////////
/// <summary> Start the smart transparency.</summary>
/////////////////////////////////////////////////////////
public void SmartTransparencyStart()
{
if (!hom3r.state.smartTransparencyModeActive) {
//Change the boolean value that control when is on this mode
hom3r.state.smartTransparencyModeActive = true;
//Excute the smartTrsnaparency
StartCoroutine (SmartTransparency ());
}
}
/////////////////////////////////////////////////////
/// <summary> Stop the Smart transparency.</summary>
/////////////////////////////////////////////////////
public void SmartTransparencyStop()
{
hom3r.state.smartTransparencyModeActive = false;
}
/// <summary> Corutine to perform the smart transperency doing a multiple ray cast each 0.3 ms </summary>
/// <returns></returns>
IEnumerator SmartTransparency()
{
//Temp Var to store the list of objects to make transparent (distractors).
List<GameObject> distractorList;
distractorList = new List<GameObject>();
//Temporaly List of obj to make visible
List<GameObject> toMakeList;
toMakeList = new List<GameObject>();
//Calculate rays destination points and save them in a class parameter: RayDestinationPoints_List. This method will be also called from the Core when the area selectiopn change.
SetRayDestinationPoints_AllSelectedAreas();
// Set animation effect duration
//float duration = 0.5f;
float duration = hom3r.quickLinks.scriptsObject.GetComponent<ConfigurationManager>().GetDurationTransparencyAnimation();
//Check if the smartTransparency is activate
while (hom3r.state.smartTransparencyModeActive)
{
//Execute the "MultipleRaysCast" algorithm to determine the list of objects to make transparent (distractors)
distractorList = OcclusionDetection_RayCast();
if (distractorList != null)
{
//Move in the list of transparent objects and check with the list of distractors
//If a object is not a distractor we make it visible.
toMakeList = gameObjectTransparentList.FindAll(x => !distractorList.Contains(x));
//Make visible objects that are not a distractor now
if (hom3r.state.smartTransparencyModeActive) { GameObjectListTransparencyOff(toMakeList, duration); }
//Move in the list of distractor and check if it is already transparent
//If not we make it transparent
toMakeList = distractorList.FindAll(x => !gameObjectTransparentList.Contains(x));
if (hom3r.state.smartTransparencyModeActive) { GameObjectListTransparencyOn(toMakeList, duration); }
distractorList.Clear();
toMakeList.Clear();
}
//Execute just 3 time per second
yield return new WaitForSeconds(0.3f);
}//END while
distractorList.Clear();
toMakeList.Clear();
}
/// <summary> Algorithm to do the smart tranparency using raycast with multiple rays </summary>
/// <returns>gameObjectDistractorList list of distractors gameobject list</returns>
public List<GameObject> OcclusionDetection_RayCast()
{
List<GameObject> gameObjectDistractorList = new List<GameObject>(); //List to store every distractor game object
List<RaycastHit[]> rayCastHitsList = new List<RaycastHit[]>(); //List to store the RaycastHit[] arrays returned by the (Physics.RaycastAll method
GameObject temp_gameObjectHit; //GameObject to move through the Raycast list
Vector3 ray_direction; //variable to store each ray direction
// Get list of confirmed components, that include every area
List<GameObject> listConfirmedObjets = this.GetComponent<SelectionManager>().GetListOfComponentConfirmedObjects();
//Get the ID of the turbine layer
//int turbineLayer = 1 << LayerMask.NameToLayer( "go_father_layer");
int turbineLayer = 1 << LayerMask.NameToLayer(hom3r.state.productRootLayer);
//*** Ray cast Process
if (listConfirmedObjets != null)
{
//1. Update Camera position
mainCameraPos = GameObject.FindGameObjectWithTag("MainCamera").transform.position;
//2. Do the RayCast
for (int i = 0; i < RayDestinationPoints_List.Count/*numOfRays*/; i++) {
ray_direction = RayDestinationPoints_List[i] - mainCameraPos; //Calculate the direction of the ray
rayCastHitsList.Add (Physics.RaycastAll(mainCameraPos, ray_direction, ray_direction.magnitude, turbineLayer)); //RayCast only the turbine father layer
}
//3. Create the distractor gameobjects list with the rayCastHitsList
foreach (RaycastHit[] oneRay_GOList_Hit in rayCastHitsList)
{
foreach (RaycastHit oneRay_GO_Hit in oneRay_GOList_Hit)
{
temp_gameObjectHit = oneRay_GO_Hit.transform.gameObject;
//Check if the obj is ones of the selected object and if it's not in the distractor list (for not being included again)
if (temp_gameObjectHit != null && !listConfirmedObjets.Contains(temp_gameObjectHit) && !gameObjectDistractorList.Contains(temp_gameObjectHit))
{
//Store the object into a list.
gameObjectDistractorList.Add(temp_gameObjectHit);
}
}
}
return gameObjectDistractorList;
}
else {
Debug.Log("ERROR << No GameObject in the listConfirmedObjets");
return null;
}
}
/// <summary>
/// Calculate ray destination points list for every selected area. Save the list in a class parameter.
/// </summary>
public void SetRayDestinationPoints_AllSelectedAreas()
{
Bounds selectedGO_bounds;
Vector3 portion;
//Get list of confirmed objets (areas). Copy the list to allow the list access
List<GameObject> listConfirmedAreas = new List<GameObject>(this.GetComponent<SelectionManager>().GetListOfConfirmedObjects());
//Clear the list to construct new rays
RayDestinationPoints_List.Clear();
if (listConfirmedAreas.Count != 0)
{
//Limit the number of Rays (depending of the MAX_RAYS_TOTAL value and the number of selected areas. 6 is the number of semiaxes)
int raysPerSemiAxis;
if (RAYS_PER_AXIS <= ((MAX_RAYS_TOTAL / listConfirmedAreas.Count)-1)/6)
{
raysPerSemiAxis = RAYS_PER_AXIS;
}
else
{
raysPerSemiAxis = ((MAX_RAYS_TOTAL / listConfirmedAreas.Count) - 1) / 6;
}
//Update destiny points values and do the raycast for each selected area
foreach (GameObject selectedGO in listConfirmedAreas)
{
selectedGO_bounds = selectedGO.GetComponent<MeshRenderer>().bounds; //TODO: Any difference with GetComponent<Renderer>?
portion = new Vector3(selectedGO_bounds.extents.x / raysPerSemiAxis, selectedGO_bounds.extents.y / raysPerSemiAxis, selectedGO_bounds.extents.z / raysPerSemiAxis);
//Center ray
RayDestinationPoints_List.Add(new Vector3(selectedGO_bounds.center.x, selectedGO_bounds.center.y, selectedGO_bounds.center.z));
//Semiaxis rays
for (int i = 1; i <= raysPerSemiAxis; i++)
{
RayDestinationPoints_List.Add(new Vector3(selectedGO_bounds.center.x + portion.x * i, selectedGO_bounds.center.y, selectedGO_bounds.center.z));
RayDestinationPoints_List.Add(new Vector3(selectedGO_bounds.center.x - portion.x * i, selectedGO_bounds.center.y, selectedGO_bounds.center.z));
RayDestinationPoints_List.Add(new Vector3(selectedGO_bounds.center.x, selectedGO_bounds.center.y + portion.y * i, selectedGO_bounds.center.z));
RayDestinationPoints_List.Add(new Vector3(selectedGO_bounds.center.x, selectedGO_bounds.center.y - portion.y * i, selectedGO_bounds.center.z));
RayDestinationPoints_List.Add(new Vector3(selectedGO_bounds.center.x, selectedGO_bounds.center.y, selectedGO_bounds.center.z + portion.z * i));
RayDestinationPoints_List.Add(new Vector3(selectedGO_bounds.center.x, selectedGO_bounds.center.y, selectedGO_bounds.center.z - portion.z * i));
}
}// end foreach
}//end if (listConfirmedAreas != null)
}// end SetRayDestinationPoints_AllSelectedAreas
}//END Class