Guest User

Untitled

a guest
Dec 12th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. using Assets.Blend_Warp.Script.BlendWarping;
  9. using System;
  10.  
  11. public class BlendWarp_Editor : MonoBehaviour
  12. {
  13. [Header("Blend-Warp Editor")]
  14. [Range(1, 8)]
  15. public int ID;
  16. public bool state = false;
  17. public bool exportsMesh;
  18.  
  19. [Header("Projector Display")]
  20. public Vector2Int projectorResolution = new Vector2Int(1920, 1080);
  21.  
  22. [Range(0.01f, 2)]
  23. public float scaleFactor;
  24.  
  25.  
  26. [Header("Quad")]
  27. public GameObject targetQuadObj;
  28.  
  29.  
  30. [Header("Warping Tools")]
  31. public Vector2Int gridSize = new Vector2Int(10, 10);
  32. public float gridWidth = 0.01f;
  33.  
  34.  
  35.  
  36. [Header("Blending Tools")]
  37. [Range(0, 100)]
  38. public float Brightness = 100;
  39.  
  40. [Range(0, 100)]
  41. public float leftBlending = 0;
  42. [Range(0, 100)]
  43. public float RightBlending = 0;
  44. [Range(0, 100)]
  45. public float TopBlending = 0;
  46. [Range(0, 100)]
  47. public float BottomBlending = 0;
  48.  
  49. /*
  50. * Following variable defines degree of function in shader
  51. * which applies blending
  52. *
  53. * Hotkey for editing: 'Y'
  54. * */
  55. [Range(0.1f, 5)]
  56. public float FunctionDegree = 1;
  57.  
  58.  
  59. public Material BlendingMaterial;
  60. public RenderTexture renderTexture;
  61.  
  62. [NonSerialized]
  63. public GameObject debugObj;
  64. private void Awake()
  65. {
  66.  
  67. if (BlendWarpManager.instance.shaderEffect != null)
  68. {
  69. BlendingMaterial = new Material(BlendWarpManager.instance.shaderEffect);
  70. }
  71. GetComponent<Camera>().targetTexture.width = projectorResolution.x;
  72. GetComponent<Camera>().targetTexture.height = projectorResolution.y;
  73. renderTexture = GetComponent<Camera>().targetTexture;
  74. // Debug.Log(GetComponent<Camera>().pixelWidth + " " + GetComponent<Camera>().pixelHeight);
  75. Rect viewPort = GetComponent<Camera>().rect;
  76. viewPort.width = viewPort.height = 1;
  77. GetComponent<Camera>().rect = viewPort;
  78. // Debug.Log(GetComponent<Camera>().pixelWidth + " " + GetComponent<Camera>().pixelHeight);
  79. // Debug.Log(GetComponent<Camera>().scaledPixelWidth + " " + GetComponent<Camera>().scaledPixelHeight);
  80.  
  81. }
  82.  
  83. List<float> rows = null;
  84. List<float> cols = null;
  85. List<List<Vector2>> grid = null;
  86.  
  87. Transform lookingPlane;
  88. void Start()
  89. {
  90. lookingPlane = GetComponent<Frustum>().lookPlane;
  91.  
  92. if (configFileManager.read == false)
  93. createNew_TargetQuad();
  94. else
  95. {
  96. load_TargetQuad();
  97. }
  98. }
  99. public BlendWarp_Data data;
  100. void load_TargetQuad()
  101. {
  102. if (targetQuadObj != null) DestroyImmediate(targetQuadObj);
  103. targetQuadObj = new GameObject("BlendWarp_Quad_" + ID, typeof(MeshRenderer), typeof(MeshFilter), typeof(BlendWarpQuad), typeof(BlendWarp_Grid));
  104. // targetQuadObj.transform.parent = transform;
  105. // quadPlane.AddComponent<BlendWarpQuad>().
  106. targetQuadObj.transform.position = new Vector3(1000 + ID * 200, 0, 0);
  107. //targetQuadObj.transform.rotation = Quaternion.Euler(0, 180, 0);
  108.  
  109. // data = new BlendWarp_Data();
  110. // data.ID = ID;
  111. // data.Rows = new List<float>(2) { 0, 10 };
  112. // data.Cols = new List<float>(2) { 0, 10 * transform.Find("Plane").localScale.z / (1.0f * transform.Find("Plane").localScale.x) };
  113. // data.RowCount = gridSize.x;
  114. // data.ColCount = gridSize.y;
  115. RightBlending = data.RightBlending;
  116. leftBlending = data.leftBlending;
  117. TopBlending = data.TopBlending;
  118. BottomBlending = data.BottomBlending;
  119. Brightness = data.Brightness;
  120. targetQuadObj.GetComponent<BlendWarpQuad>().startQuad(this, data);
  121. debugObj = Instantiate(BlendWarpManager.instance.Debugger, targetQuadObj.transform);
  122. }
  123.  
  124. public void createNew_TargetQuad()
  125. {
  126. if (targetQuadObj != null) DestroyImmediate(targetQuadObj);
  127. targetQuadObj = new GameObject("BlendWarp_Quad_" + ID, typeof(MeshRenderer), typeof(MeshFilter), typeof(BlendWarpQuad), typeof(BlendWarp_Grid));
  128. // targetQuadObj.transform.parent = transform;
  129. // quadPlane.AddComponent<BlendWarpQuad>().
  130. targetQuadObj.transform.position = new Vector3(1000 + ID * 200, 0, 0);
  131. //targetQuadObj.transform.rotation = Quaternion.Euler(0, 180, 0);
  132.  
  133. data = new BlendWarp_Data();
  134. data.ID = ID;
  135. data.Rows = new List<double>(2) { 0, 10 };
  136. // Debug.Log(transform.Find("Plane").localScale.z + " " + transform.Find("Plane").localScale.x);
  137. data.Cols = new List<double>(2) { 0, 10 * lookingPlane.localScale.z / (1.0 * lookingPlane.localScale.x) };
  138. data.RowCount = gridSize.x;
  139. data.ColCount = gridSize.y;
  140. targetQuadObj.GetComponent<BlendWarpQuad>().startQuad(this, data);
  141. debugObj = Instantiate(BlendWarpManager.instance.Debugger, targetQuadObj.transform);
  142. }
  143.  
  144.  
  145. void Update()
  146. {
  147. if (state == false) return;
  148. int amplifierSpeed = 10;
  149.  
  150. if (ControlKeySet.BrightnessEditingPermitted())
  151. {
  152. int amplify = Input.GetKey(ControlKeySet.GetControlKey(Controls.Amplify)) ? 1 : 0;
  153.  
  154. int increaseBrightness = Input.GetKey(ControlKeySet.GetControlKey(Controls.BrightnessIncrease)) ? 1 : 0;
  155. int decreaseBrightness = Input.GetKey(ControlKeySet.GetControlKey(Controls.BrightnessDecrease)) ? 1 : 0;
  156.  
  157. Brightness = Math.Max(0, Math.Min(100, Brightness
  158. + increaseBrightness * (amplify * (amplifierSpeed - 1) + 1) * BlendWarpManager.instance.blendMoveSpeed
  159. - decreaseBrightness * (amplify * (amplifierSpeed - 1) + 1) * BlendWarpManager.instance.blendMoveSpeed));
  160.  
  161. data.Brightness = Brightness;
  162. GUImessages.instance.showMessage("Brightness: " + (int)(data.Brightness) + "%", Color.green, true);
  163. }
  164.  
  165. if (ControlKeySet.BlendingEditingPermitted())
  166. {
  167. int amplify = Input.GetKey(ControlKeySet.GetControlKey(Controls.Amplify)) ? 1 : 0;
  168.  
  169. int blendingLeft = Input.GetKey(ControlKeySet.GetModeKey(ControllingModes.BlendingLeft)) ? 1 : 0;
  170. int blendingRight = Input.GetKey(ControlKeySet.GetModeKey(ControllingModes.BlendingRight)) ? 1 : 0;
  171. int increaseBlending = Input.GetKey(ControlKeySet.GetControlKey(Controls.BlendingIncrease)) ? 1 : 0;
  172. int decreaseBlending = Input.GetKey(ControlKeySet.GetControlKey(Controls.BlendingDecrease)) ? 1 : 0;
  173.  
  174. leftBlending = Math.Max(0, Math.Min(100, leftBlending + blendingLeft * (
  175. + increaseBlending * (amplify * (amplifierSpeed - 1) + 1) * BlendWarpManager.instance.blendMoveSpeed
  176. - decreaseBlending * (amplify * (amplifierSpeed - 1) + 1) * BlendWarpManager.instance.blendMoveSpeed)));
  177.  
  178. RightBlending = Math.Max(0, Math.Min(100, RightBlending + blendingRight * (
  179. + increaseBlending * (amplify * (amplifierSpeed - 1) + 1) * BlendWarpManager.instance.blendMoveSpeed
  180. - decreaseBlending * (amplify * (amplifierSpeed - 1) + 1) * BlendWarpManager.instance.blendMoveSpeed)));
  181.  
  182. data.leftBlending = leftBlending;
  183. data.RightBlending = RightBlending;
  184. GUImessages.instance.showMessage("leftBlending: " + (int)(data.leftBlending) + "%\nRightBlending: " + (int)(data.RightBlending) + "%", Color.green, true);
  185. }
  186.  
  187. if (ControlKeySet.BlendDegreeEditingPermitted())
  188. {
  189. int degreeIncrease = Input.GetKey(ControlKeySet.GetControlKey(Controls.BlendingDegreeIncrase)) ? 1 : 0;
  190. int degreeDecrease = Input.GetKey(ControlKeySet.GetControlKey(Controls.BlendingDegreeDecrease)) ? 1 : 0;
  191.  
  192. FunctionDegree = Math.Max(1, Math.Min(5, FunctionDegree
  193. + degreeDecrease
  194. - degreeDecrease));
  195.  
  196. GUImessages.instance.showMessage("Degree " + (int)FunctionDegree, Color.green, true);
  197. }
  198. }
  199.  
  200. void assignCamera(GameObject obj)
  201. {
  202. GameObject cam = new GameObject(transform.name + "_camera", typeof(Camera));
  203. cam.GetComponent<Camera>().orthographic = true;
  204. cam.GetComponent<Camera>().orthographicSize = 6;
  205. cam.GetComponent<Camera>().targetDisplay = ID;
  206. cam.GetComponent<Camera>().backgroundColor = new Color(0, 0, 0);
  207. cam.GetComponent<Camera>().clearFlags = CameraClearFlags.SolidColor;
  208. cam.GetComponent<Camera>().farClipPlane = 100;
  209. cam.transform.parent = obj.transform;
  210.  
  211. cam.transform.localPosition = new Vector3(0, 0, 1);
  212. cam.transform.localRotation = Quaternion.Euler(180, 0, 180);
  213. }
  214.  
  215.  
  216. public void turnEditor(bool on)
  217. {
  218. targetQuadObj.GetComponent<BlendWarp_Grid>().turnGrid(on);
  219. state = on;
  220. }
  221.  
  222. void OnRenderImage(RenderTexture source, RenderTexture destination)
  223. {
  224. BlendingMaterial.SetFloat("_BrightnessAmount", Brightness / 100.0f);
  225. BlendingMaterial.SetFloat("_BlendingLeft", leftBlending / 100.0f);
  226. BlendingMaterial.SetFloat("_BlendingRight", RightBlending / 100.0f);
  227. BlendingMaterial.SetFloat("_BlendingTop", TopBlending / 100.0f);
  228. BlendingMaterial.SetFloat("_BlendingBottom", BottomBlending / 100.0f);
  229. BlendingMaterial.SetFloat("_FunctionDegree", FunctionDegree);
  230. Graphics.Blit(source, destination, BlendingMaterial);
  231.  
  232. }
  233.  
  234.  
  235. }
Add Comment
Please, Sign In to add comment