Search Unity

GL Commands within a system?

Discussion in 'Entity Component System' started by TheGabelle, Aug 2, 2020.

  1. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    I want to convert this code to ECS:
    https://docs.unity3d.com/ScriptReference/GL.LINES.html

    Here's my quick attempt that doesn't work:
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using Unity.Rendering;
    4.  
    5.  
    6. [AlwaysUpdateSystem]
    7. [UpdateInGroup(typeof( PresentationSystemGroup ))]
    8. [UpdateAfter(typeof(MatrixPreviousSystem))]
    9. public class GLCommandsSystem : SystemBase
    10. {
    11.  
    12.     // Draws a line from "startVertex" var to the curent mouse position.
    13.     Material mat;
    14.     Vector3 startVertex;
    15.     Vector3 mousePos;
    16.  
    17.     protected override void OnCreate()
    18.     {
    19.         startVertex = Vector3.zero;
    20.         mousePos = Vector3.one;
    21.         mat = Resources.Load<Material>("Line"); // Default Sprite Renderer
    22.     }
    23.  
    24.     protected override void OnUpdate()
    25.     {
    26.        
    27.         mousePos = Input.mousePosition;
    28.         // Press space to update startVertex
    29.         if (Input.GetKeyDown(KeyCode.Space))
    30.         {
    31.             startVertex = new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0);
    32.         }
    33.  
    34.  
    35.         if (!mat)
    36.         {
    37.             Debug.LogError("Please Assign a material on the inspector");
    38.             return;
    39.         }
    40.         GL.PushMatrix();
    41.         mat.SetPass(0);
    42.         GL.LoadOrtho();
    43.  
    44.         GL.Begin(GL.LINES);
    45.         GL.Color(Color.red);
    46.         GL.Vertex(startVertex);
    47.         GL.Vertex(new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0));
    48.         GL.End();
    49.  
    50.         GL.PopMatrix();
    51.  
    52.         Debug.Log("mouse: " + mousePos.ToString());
    53.  
    54.     }
    55.    
    56. }
    I am using HDRP so maybe that's the issue?