Element3D.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #if false
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. using UnityEditor;
  5. namespace UnityEditor.VFX.UI
  6. {
  7. public class Element3D : VisualElement
  8. {
  9. Mesh m_Mesh;
  10. Material m_Material;
  11. Material m_LineMaterial;
  12. public Vector3 position { get; set; }
  13. public Quaternion rotation { get; set; }
  14. public Element3D()
  15. {
  16. GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
  17. m_Mesh = go.GetComponent<MeshFilter>().sharedMesh;
  18. m_Material = go.GetComponent<MeshRenderer>().sharedMaterial;
  19. GameObject.DestroyImmediate(go);
  20. position = new Vector3(0, 0, -5);
  21. rotation = Quaternion.identity;
  22. m_LineMaterial = new Material(Shader.Find("Unlit/Element3DGridShader"));
  23. m_LineMaterial.color = Color.gray;
  24. }
  25. RenderTexture m_RenderTexture;
  26. #if ELEMENT3D_USE_BLIT_TEXTURE
  27. Texture2D m_BlitTexture;
  28. #endif
  29. public override void DoRepaint()
  30. {
  31. Rect panelRect = this.panel.visualTree.layout;
  32. Rect viewPort = this.parent.ChangeCoordinatesTo(this, layout);
  33. if (m_RenderTexture == null)
  34. {
  35. m_RenderTexture = new RenderTexture(Mathf.CeilToInt(viewPort.width), Mathf.CeilToInt(viewPort.height), 32, RenderTextureFormat.Default, RenderTextureReadWrite.sRGB);
  36. }
  37. if (m_RenderTexture.width != Mathf.CeilToInt(viewPort.width))
  38. {
  39. m_RenderTexture.Release();
  40. m_RenderTexture.width = Mathf.CeilToInt(viewPort.width);
  41. }
  42. if (m_RenderTexture.height != Mathf.CeilToInt(viewPort.height))
  43. {
  44. m_RenderTexture.Release();
  45. m_RenderTexture.height = Mathf.CeilToInt(viewPort.height);
  46. }
  47. #if ELEMENT3D_USE_BLIT_TEXTURE
  48. if (m_BlitTexture == null || m_BlitTexture.height != m_RenderTexture.height || m_BlitTexture.width != m_RenderTexture.width)
  49. {
  50. if (m_BlitTexture != null)
  51. m_BlitTexture.Resize(m_RenderTexture.width, m_BlitTexture.height);
  52. else
  53. {
  54. m_BlitTexture = new Texture2D(m_RenderTexture.width, m_RenderTexture.height, TextureFormat.ARGB32, false);
  55. style.backgroundImage = m_BlitTexture;
  56. }
  57. }
  58. #endif
  59. //EditorGUIUtility.SetRenderTextureNoViewport(m_RenderTexture);
  60. RenderTexture.active = m_RenderTexture;
  61. GL.PushMatrix();
  62. //GL.Viewport(viewPort);
  63. GL.Clear(true, true, new Color(0.8f, 0.8f, 0.8f, 1));
  64. #if true
  65. GL.LoadProjectionMatrix(Matrix4x4.Perspective(60, viewPort.width / viewPort.height, 0.01f, 100));
  66. GL.modelview = Matrix4x4.Translate(position) * Matrix4x4.Rotate(rotation);
  67. m_LineMaterial.SetPass(0);
  68. float count = 20;
  69. GL.Begin(GL.LINES);
  70. for (float x = -count; x <= count; x++)
  71. {
  72. GL.Vertex3(x, 0, -count);
  73. GL.Vertex3(x, 0, count);
  74. }
  75. GL.End();
  76. GL.Begin(GL.LINES);
  77. for (float x = -count; x <= count; x++)
  78. {
  79. GL.Vertex3(-count, 0, x);
  80. GL.Vertex3(count, 0, x);
  81. }
  82. GL.End();
  83. GL.invertCulling = true;
  84. m_Material.SetPass(0);
  85. UnityEngine.Graphics.DrawMeshNow(m_Mesh, Matrix4x4.identity);
  86. GL.invertCulling = false;
  87. //Graphics.DrawMesh(m_Mesh, Matrix4x4.identity, m_Material, 1);
  88. #endif
  89. GL.PopMatrix();
  90. #if ELEMENT3D_USE_BLIT_TEXTURE
  91. m_BlitTexture.ReadPixels(viewPort, 0, 0);
  92. RenderTexture.active = null;
  93. m_BlitTexture.Apply();
  94. base.DoRepaint();
  95. #else
  96. RenderTexture.active = null;
  97. var painter = elementPanel.stylePainter;
  98. var painterParams = painter.GetDefaultTextureParameters(this);
  99. painterParams.texture = m_RenderTexture;
  100. painter.DrawTexture(painterParams);
  101. #endif
  102. }
  103. }
  104. }
  105. #endif