Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Gizmos InGame [Help Needed]

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Nov 7, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I have a set of GL lines, which I am using to represent transform handles in an editor.
    I have got them to display correctly and distinguish themselves from the floor grid, but cannot work out how to get the movement functionality or rotation which will be needed for the editor.

    To use what I have posted below, place the TransformGizmo scrpt on the camera, and put an object in the Target slot, then place the material in the Line Material slot and press play.
    The lines will be rendered similarly to the Unity handles (just without the cones on the end as I don't know how to render those).

    The Script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class TransformGizmo : MonoBehaviour
    6. {
    7.     public Material lineMaterial;
    8.     public enum TransformSpace
    9.     {
    10.         Global,
    11.         Local
    12.     }
    13.  
    14.  
    15.     public Transform target;
    16.     public TransformSpace space = TransformSpace.Global;
    17.     public float handleLength = 5.0f;
    18.  
    19.     void OnPostRender()
    20.     {
    21.         Vector3 xAxisEnd;
    22.         Vector3 yAxisEnd;
    23.         Vector3 zAxisEnd;
    24.         if (space == TransformSpace.Global)
    25.         {
    26.             xAxisEnd = target.transform.position + Vector3.right * handleLength;
    27.             yAxisEnd = target.transform.position + Vector3.up * handleLength;
    28.             zAxisEnd = target.transform.position + Vector3.forward * handleLength;
    29.         }
    30.         else
    31.         {
    32.             xAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.right * handleLength);
    33.             yAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.up * handleLength);
    34.             zAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.forward * handleLength);
    35.         }
    36.         lineMaterial.SetPass(0);
    37.         GL.Begin(GL.LINES);
    38.         // X line
    39.         GL.Color(new Color(1, 0, 0, 1f));
    40.         GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
    41.         GL.Vertex3(xAxisEnd.x, xAxisEnd.y, xAxisEnd.z);
    42.         // Y line
    43.         GL.Color(new Color(0, 1, 0, 1f));
    44.         GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
    45.         GL.Vertex3(yAxisEnd.x, yAxisEnd.y, yAxisEnd.z);
    46.         // Z line
    47.         GL.Color(new Color(0, 0, 1, 1f));
    48.         GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
    49.         GL.Vertex3(zAxisEnd.x, zAxisEnd.y, zAxisEnd.z);
    50.         GL.End();
    51.     }
    52.  
    53. }
    The Material Shader (Keep it white)
    Code (CSharp):
    1. Shader "Unlit/GLMat"{
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.     }
    5.     SubShader {
    6.         Lighting Off
    7.         Cull Off
    8.         ZWrite Off
    9.         ZTest always
    10.         Fog { Mode Off }
    11.      
    12.         Tags { "Queue"="Transparent+1" }
    13.         Pass {
    14.         ZWrite On
    15.         ZTest LEqual
    16.         Cull off
    17.         Fog {Mode Off}
    18.             Blend SrcAlpha OneMinusSrcAlpha
    19.             CGPROGRAM
    20.                 #pragma vertex vert
    21.                 #pragma fragment frag
    22.                 #include "UnityCG.cginc"
    23.      
    24.                 float4 _Color;
    25.      
    26.                 struct v2f {
    27.                     float4 pos : SV_POSITION;
    28.                     float4 col : COLOR;
    29.                 };
    30.              
    31.                 v2f vert (appdata_full vInput) {
    32.                     v2f OUT;
    33.                     OUT.pos = mul (UNITY_MATRIX_MVP, vInput.vertex);
    34.                     OUT.col = vInput.color;
    35.                     return OUT;
    36.                 }
    37.              
    38.                 half4 frag (v2f fInput) : COLOR {
    39.                     return _Color * fInput.col;
    40.                 }
    41.             ENDCG
    42.         }
    43.     }
    44.     FallBack "Diffuse"
    45. }
     
  2. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    I think its a known thing that gizmos don't rotate. There may have been a solution, but I forget. Google may dig something up.
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Sorry, not talking about the gizmo rotation, but the object.
    For now thought I am just trying to work out how to MOVE the object using the handles.