Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Grid Framework [scripting and editor plugins]

Discussion in 'Assets and Asset Store' started by hiphish, Jul 24, 2012.

  1. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    No, I wasn't aware of that, please let me know which examples are throwing errors so I can fix them as soon as possible.
     
  2. Magikos

    Magikos

    Joined:
    Oct 16, 2013
    Posts:
    11
    Hi, I just bought the Grid Framework and Vectrosity, my question is if there was a way to render the grid under a sprite (Unity 4.3)? Thanks!
     
  3. Magikos

    Magikos

    Joined:
    Oct 16, 2013
    Posts:
    11
    Nevermind, I figured it out. I took examples of other scripts and created a simple script, dropped it on my grid and it works. The default shader code in one of your examples didn't work for me so I found another one.

    This works great, you should include it in the package. Thanks!

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class VectrosityRenderGrid : MonoBehaviour
    5. {
    6.     public Color GridColor = new Color(1f, 1f, 1f, .5f);
    7.     public float lineWidth = 5.0f;
    8.     public Material lineMaterial;
    9.     protected Material defaultRenderMaterial
    10.     {
    11.         get
    12.         {
    13.             return new Material(
    14. @"Shader ""Unlit Transparent Colored"" {
    15.    Properties {
    16.        // Adds Color field we can modify
    17.        _Color (""Main Color"", Color) = (1,1,1,1)
    18.        _MainTex (""Base (RGB)"", 2D) = ""white"" {}
    19.    }
    20.    
    21.    SubShader {
    22.        Tags {""Queue""=""Transparent"" ""IgnoreProjector""=""True"" ""RenderType""=""Transparent""}
    23.        LOD 100
    24.    
    25.        ZWrite Off
    26.        Blend SrcAlpha OneMinusSrcAlpha
    27.  
    28.        Pass {
    29.            Lighting Off
    30.            SetTexture [_MainTex] {
    31.                // Sets _Color as the 'constant' variable
    32.                constantColor[_Color]
    33.                
    34.                // Multiplies color (in constant) with texture
    35.                combine constant * texture
    36.            }
    37.        }
    38.    }
    39. }");
    40.         }
    41.     }
    42.  
    43.     private GFGrid _grid;
    44.     private Vectrosity.VectorLine _gridLine;
    45.  
    46.     // Use this for initialization
    47.     void Start()
    48.     {
    49.         if (lineMaterial == null)
    50.         {
    51.             lineMaterial = defaultRenderMaterial;
    52.             lineMaterial.SetColor("_Color", GridColor);
    53.         }
    54.  
    55.         _grid = GetComponent<GFGrid>();
    56.         _gridLine = new Vectrosity.VectorLine(gameObject.name, _grid.GetVectrosityPoints(), lineMaterial, lineWidth);
    57.         _gridLine.Draw3DAuto(transform);
    58.  
    59.     }
    60.  
    61. }
    62.  
    63.  
     
    Last edited: Nov 15, 2013
  4. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    The problem with shaders that are "found" on the the internet is that I can't just copy-paste something someone else has already copy-pasted. Could you please give me the link where you have it fron, then I'll take a look at it. I think having a set of "recommended shaders" could be a nice feature.
     
  5. Magikos

    Magikos

    Joined:
    Oct 16, 2013
    Posts:
    11
    I don't remeber the exact link, but it was on a Unity Answers page. Also, any chance you can add an event for when the grid needs to be redrawn? IE Resize event, etc
     
  6. Magikos

    Magikos

    Joined:
    Oct 16, 2013
    Posts:
    11
    I would like to calculate a move distance using the hex grid. How can I get a circular result showing only X number of move spaces?
     
  7. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I could add an event when the grid has been resized, but that wouldn't be enough. I would also need an even whenever something was done to the grid's Transform, because that affects the points as well. The way I'm doing it currently is that whenever the grid wants to draw itself it checks if its parameters have been changed and if its position and rotation is still the same; if yes, then it re-uses the points from the previous frame, if not it calculates them again. I could have the position and rotation checking running in the background every frame, but I don't think that's a good idea.
    Code (csharp):
    1.  
    2. void Update () {
    3.     if (lastPos != _transform.position || lastRot != _transform.rotation)
    4.         SendEvent ();
    5. }
    6.  
    What do you think?

    There is a couple of simple algorithms you can use, but Grid Framework doesn't have any built in; I want to do it, but for now I'm working on Playmaker support (there have been some design roadblocks because Grid Framework is more complex than you average action that just adds a few vectors, and the documentation for creating actions is pretty lackluster).

    Here is a very good site that explains all sorts of coordinate systems and algorithms.
    http://www.redblobgames.com/grids/hexagons/
    I have all those coordinate systems built in, and the algorithms are just a few lines of code. Make sure you read my documentation on coordinate systems, because some indices and coordinates are a bit different.
     
  8. Magikos

    Magikos

    Joined:
    Oct 16, 2013
    Posts:
    11
    Hi - thanks for the reply. I've seen that redblobgames website before and I have been looking at it. I will try and figure out how to get it all to work together.

    As for the event, do you have a function that you call when you redraw/recalculate the grid? Maybe you could put the send event at the end of that? GridChangedEvent? Unless you wanted to get more complex with all the events, Resized, Moved, etc etc - but a simple "Hey, something happened and you need to redraw me" would fit my needs. Right now I have something monitoring and passing messages around. It works, but I don't like it.

    Lastly, is there or could you add a way to get the Vectrosity data for a single grid cord? What I'd like to do is loop all my valid move posistions, even check their data if it is passable and if so, add it to an array and then lastly send that array over to VectorLine to display all my valid move points - it would also be valuable for other scenarios. You could highlight a grid with your current selection, enemies, valuable posistions, etc.

    Thanks again!
     
  9. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I could do that, but it would be only fired if there is actually a request to draw. Would that be good enough?

    I'm not sure i understand what you need. If you already have the coordinates you don't need anything else. You might have to run them through GridToWorld to get them into world space, but that's it.
     
  10. Magikos

    Magikos

    Joined:
    Oct 16, 2013
    Posts:
    11
    I've given more thought to what you orinially said with the update loop, sorry it was early for me. I think if you could raise the event any time the grid changed, example any time I would need to redraw the grid, that would be great. So the update code you did, plus at the end of the calculation method.

    What I mean is I have a grid cord, 5,12 - I would like to do something like _grid.GetVectrosityPointsFor(new Vector3(5, 12, 0))

    I would use it something like:
    Code (csharp):
    1.  
    2.  
    3. _line.Resize(_grid.GetVectrosityPoints()); //Draw basic grid lines
    4. _line.Draw3D(_grid.transform);
    5.  
    6. /* ... other code ... */
    7.  
    8. List<Vector3[]> points = new List<Vector3[]>();
    9.  
    10. foreach(Vector3 move in validMoves) //array of valid grid cords
    11.   points.Add(_grid.GetVectrosityPointsFor(move));  //get vectrosity points to draw this Hex at grid or world cords (which ever you think is best here)
    12.  
    13. _highlightLine.Resize(points.SelectMany(p => p).ToArray());  //highlight all valid move posistions (different material than _line)
    14. _highlightLine.Draw3D(_grid.transform);
    15.  
     
  11. yourpalmark

    yourpalmark

    Joined:
    May 10, 2013
    Posts:
    21
    First of all, great framework!

    We were using your framework for a quick proof of concept of a turned based strategy game. For a flat plane, it works wonderfully. We used it for the grid, grid selection, and unit movement. Our game however will not be 2D, so my question to you is if the grid could be used in a 3D environment, ie possibly projecting the grid and selection highlights on to the map, etc. The projection seems like it could cause performance issues.

    I'm thinking another possibility is to use the grid framework for logic, include a visible grid on the models themselves, but do the highlight as a flat grid space above the height of the model (selection of a grid space could be trickier here).

    Anyway, was wondering if you've tackled or seen anyone else tackle similar problems with your framework. And if so, any guidance would be much appreciated.

    Thanks again for the great asset!
     
  12. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm afraid I haven't come up with a proper solution for displaying a grid over uneven 3D terrain. The grid will always have straight lines, so if your game has dents and bumps it will look weird. Of course using the grid for game logic will be no problem.

    Yes, I think that's a good idea, I'll look into it

    I think you are misunderstanding the point of GetVectrosityPoints. Vectrosity needs an array of Vector3 coordinates in a certain order to draw its lines, so what the function does is take the points that are usually used to render the grid and create an array that's fit for use with Vectrosity. All that method does is take away the manual work of ordering the end points of lines from you. Running GetVectrosityPoints with just one coordinate would make no sense, you need at least two points for a line.
     
  13. Magikos

    Magikos

    Joined:
    Oct 16, 2013
    Posts:
    11
    Hmm, I guess I am either confused or not explaining myself. I figured GetVectrosityPoints returned an array of points to draw a Hex, so 6 points - if you had a grid of 10X10 - the method would return 600 points - the data to draw all the Hex grid lines.

    I would like a method or overload of GetVectrosityPoints to return the points for just 1 Hex (6 points). I would then call it over and over for all the different points I need ( all my valid move points ) and add them all into 1 big array that I would use to draw.

    Does that make sense? Is there a better way for me to "highlight" only a few Hex's that I determin as valid move locations?
     
  14. MikeTon

    MikeTon

    Joined:
    Jan 10, 2013
    Posts:
    57
    IndexOutOfRangeException: Array index is out of range.
    GFBoolVector3.get_Item (Int32 index) (at Assets/Plugins/Grid Framework/Vectors/GFBoolVector3.cs:60)
    GFHexGrid.DrawGridRect (Vector3 from, Vector3 to) (at Assets/Plugins/Grid Framework/GFHexGrid.cs:762)
    GFHexGrid.DrawGrid (Vector3 from, Vector3 to) (at Assets/Plugins/Grid Framework/GFHexGrid.cs:747)
    GFHexGrid.OnDrawGizmos () (at Assets/Plugins/Grid Framework/GFHexGrid.cs:813)

    Hi HipHish,

    I just bought Grid Framework and are liking it. It looks like all the example scenes exhibit this error:

    Design and Parsing
    Lights Off
    Movement
    Movement with Walls
    Rotary Dial
    Runtime Snapping
    Seemingly Endless Grid
    Sliding Puzzle
    Snake
    Terrain
    Vectrosity

    Any thoughts on what's happening?

    -Mike
     
  15. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I have seen it now as well, I'll issue an update soon. I have no idea what's going on, but resetting the grid components gets rid of the errors and it oly happened after updating to Unity 4.3. Maybe the problem is on Unity's end? Until the update gets approved you can simply click the little cog in every grid's inspector and choose reset. Some examples require you to set the values back to what they were meant to be. Tell me which examples you are interested in and I'll give you the proper values.

    That's not exactly how it works. When you create a new hex grid yo see red, green and blues lines, each of those is its own array and the array contains the horizontal and vertical zig-zagging lines in order, not the lines around each hex. What you want can be done simply like this:
    Code (csharp):
    1.  
    2. Vector3[] GetHexPoints (Vector3 hex) { // hex in grid coordinates
    3.     Vector3 points = new Vector[6];
    4.     points[0] = hex + new Vector3 (radius, 0, 0);
    5.     points[1] = hex + new Vector3 (0.5f * radius, Mathf.Sqrt(0.75f) * radius, 0);
    6.     points[2] = hex + new Vector3 (-0.5f * radius, Mathf.Sqrt(0.75f) * radius, 0);
    7.     points[3] = hex + new Vector3 (-radius, 0, 0);
    8.     points[4] = hex + new Vector3 (-0.5f * radius, -Mathf.Sqrt(0.75f) * radius, 0);
    9.     points[5] = hex + new Vector3 (0.5f * radius, -Mathf.Sqrt(0.75f) * radius, 0);
    10.     foreach (Vector3 point in points)
    11.         point = GridToWorld (point)
    12.     return points;
    13. }
    14.  
     
  16. Magikos

    Magikos

    Joined:
    Oct 16, 2013
    Posts:
    11
    Awesome! Thanks man! I'll let you know how it turns out.
     
  17. cranecam

    cranecam

    Joined:
    Nov 25, 2013
    Posts:
    25
    Hi,

    where is the manual?
     
  18. cranecam

    cranecam

    Joined:
    Nov 25, 2013
    Posts:
    25
    is it just me or is the sliding puzzle example messed up in the inspector of the rectangular grid component?

    Cannot get the grid to render in play mode on a fresh scene either.....
     
  19. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    In order to render a grid at runtime the camera needs a GFGridGrenderCamera component (under Components -> Grid Framework -> Camera). You might have to reset the grid component, to do so choose the "Rectangular Grid" object int he scene, click the little cog of the GFRectGrid component, choose Reset, then set its Custom Rendering Range (under Draw Render Settings) to from (0,0,0) to (6,6,0). Also, make sure that Render Grid is checked.

    In the menu bar under Help -> Grid Framework Documentation
     
  20. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    I will need some kind of simple PathFinding... It have something natively?
     
  21. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm sorry, but not yet, I'm still working on other features. Pathfinding will come at a later time, it will require a very good amount of both coding and, more importantly, design decisions. Once the API has been released I can't just change it later down the road, I have to make it versatile enough.
     
  22. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Grid Framework version 1.3.5 has been approved last week, here is the changelog:

    • Added a new event for when the grid changes in such a way that if would need to be redrawn.
    • Some of the exmples were broken when Unity updated to version 4.3, now they should be working again.
    • Overhauled the undo system for the grid align panel to remove the now obsolete Unity undo methods.

    I promised I would be working on Playmaker support, so what's going on here? I have been bogged down a lot with studies and real life stuff oder the last months (nothing bad though, don't worry), so things were going slowly, but they were moving. The current problem is Playmaker itself; from what I have used it is a fantastic system to crate game logic and totally worth it, however, when it comes to extending it things aren't that nice anymore.

    I am in contact with a staff member who is helping me out, so I'm not entirely reliant on the lackluster documentation, but there is a lot of ugly code redundancy that cannot be avoided. I'm still working on it and it will come out eventually, but it will take more time. How much? I don't really know, it depends on how rigid or flexible Playmaker turns out to be.
     
  23. stawberry

    stawberry

    Joined:
    Nov 12, 2012
    Posts:
    13
    I can't find manual in the latest package.
     
  24. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Is it not under Help -> Grid Framework documentation? The files are listed on he Asset Store page, so they should be included. Try importing again from the Asset Store, if that doesn't work PM me and I'll give you a link to an online version (I don't want my Dropbox links on public display).
     
  25. stawberry

    stawberry

    Joined:
    Nov 12, 2012
    Posts:
    13
    Found it. According to your video there is a PDF version of manual and I was searching for it.
     
  26. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Right, that's outdated. Originally I wrote the manual and API reference manually, but doing so became more and more unpractical, so I learned how to use Doxygen. Now I write the API reference right in the source files along with the code and Doxygen generates the HTML files.
     
  27. stawberry

    stawberry

    Joined:
    Nov 12, 2012
    Posts:
    13
    I am making an in-editor map editor. Basically there is a plane in the scene, and I created a hexagon grid overlapping it. When I click on the plane, a Cube should be instantiated in the center of nearest face. Very similar to how Grid Align Panel works. But the Cubes are all instantiated in the wrong position. My code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(MapEditorScript))]
    6. public class MapEditor : Editor {
    7.  
    8.     private MapEditorScript mapEditorScript;
    9.  
    10.     public override void OnInspectorGUI ()
    11.     {
    12.         mapEditorScript = (MapEditorScript)target;
    13.         mapEditorScript.grid = EditorGUILayout.ObjectField (mapEditorScript.grid, typeof(GFHexGrid), true) as GFHexGrid;
    14.     }
    15.  
    16.     public void OnSceneGUI()
    17.     {
    18.         if (Event.current.type == EventType.mouseUp)
    19.         {
    20.             Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
    21.             RaycastHit hitInfo;
    22.            
    23.             //ray hit something
    24.             if (Physics.Raycast(worldRay, out hitInfo))
    25.             {
    26.                 GameObject box = Resources.LoadAssetAtPath("Assets/Prefab/Cube.prefab", typeof(GameObject)) as GameObject;
    27.                 GameObject newBox = Instantiate(box) as GameObject;
    28.                 newBox.transform.position = mapEditorScript.grid.NearestFaceG(hitInfo.point, GFGrid.GridPlane.XZ);
    29.                 EditorUtility.SetDirty(newBox);
    30.             }
    31.         }
    32.  
    33.         Event.current.Use();
    34.     }
    35. }
     
  28. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Transform.position is always in world coordinates, so try using NearestFaceW instead of NearestFaceG. The suffix is the coordinate system and in hex grids G stands for grid and is the same as odd herringbone, whereas W stands for world. There als also rhombic (R) and cubic (C) coordinates available.
     
  29. Zennichimaro

    Zennichimaro

    Joined:
    Dec 28, 2013
    Posts:
    1
    Hi,

    I bought this a few weeks ago, but up until now I still can make it work on trivial stuff, it is so difficult to use... where can I get help? Is there any Stack Overflow tag or something?

    I have a lot of problems, but they are just so trivial... For first, I set the grid size, but the plane size never follow the size of the grid and there is no where to set the size of the plane or prevent the plane from being rendered. If I discard the Mesh Renderer attached to the Grid object, the triangle vertices are still rendered!! And worst, I cant move the object beyond the boundary of the plane...

    Any help is highly appreciated, preferable a Stack Overflow Q&A

    $Screen Shot 2013-12-31 at 7.29.40 AM.png
     
    Last edited: Dec 30, 2013
  30. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm sorry to hear you are having trouble. Grid Framework was written for people who already are familiar with Unity but don't want to do all the low-level math themselves. I'll try to explain things as clearly as I can, if you still have questions feel free to reply or PM me.

    First of all it's important to understand that Grid Framework grids are grids in a mathematical sense: they are infinitely large. What you see on screen is just a small visual representation and setting the size has no effect on the grid itself, just what you see. Each grid is a component that's attached to a GameObject and in Unity components are pretty much independent, you could for example have an object the looks like a cube but collides like a sphere. In a similar way the grid's size has no effect on the plane mesh, they are independent of each other.

    The plane mesh is a 3D model provided by Unity, I have nothing to do with it. Also, the triangles and vertices are not rendered, they are just drawn as gizmos when you have an object with a mesh collider selected. Again, this has nothing to do with Grid Framework, it's standard Unity behaviour that would happen even if you didn't have Grid Framework installed. the reason you can't move the object beyond the boundaries of the plane is because the plane's mesh also serves as the collider for handling mouse input. Again, standard Unity stuff.

    Since the plane mesh is provided by Unity you cannot change its size. If you want to have a mesh with the size of the grid the best way would be to generate the mesh from code. Create a new script and attach it to the object with the grid:
    Code (csharp):
    1.  
    2. [RequireComponent(typeof(GFGrid))] // the script won't work without a grid
    3. public class MyMeshBuilder : MonoBehaviour {
    4.     public Mesh mesh; // the mesh
    5.  
    6.     // internal variables for reference (various components that will use the mesh)
    7.     private MeshFilter mf;
    8.     private MeshCollider mc;
    9.     private MeshRenderer mr;
    10.     private GFGrid grid; // this is where we store the grid for reference
    11.  
    12.     public void Awake () {
    13.         // make sure the needed components exist and store them; CheckComponent<T>() is defined in this script near the bottom
    14.         mf = CheckComponent<MeshFilter>();
    15.         mc = CheckComponent<MeshCollider>();
    16.         mr = CheckComponent<MeshRenderer>();
    17.         grid = GetComponent<GFRectGrid>();
    18.        
    19.         // create the mesh
    20.         BuildMesh ();
    21.         // then assign it to the components that need it
    22.         AssignMesh ();
    23.     }
    24.  
    25.     void BuildMesh () {
    26.         // instantiate a new mesh and wipe it clean
    27.         mesh = new Mesh();
    28.         mesh.Clear();
    29.        
    30.         //this is where we will store the vertices
    31.         Vector3[] vertices = new Vector3[4] {
    32.             newVector3(-grid.size.x, -grid.size.y, 0), // lower left
    33.             newVector3(-grid.size.x, grid.size.y, 0), // upper left
    34.             newVector3(grid.size.x, grid.size.y, 0), // upper right
    35.             newVector3(grid.size.x, -grid.size.y, 0), // lower right
    36.         };
    37.         // assign the vertices to the mesh
    38.         mesh.vertices = vertices;
    39.        
    40.         /* Triangles like this:
    41.         1----2
    42.         |   /|
    43.         |  / |
    44.         | /  |
    45.         |/   |
    46.         0----3
    47.         */
    48.         mesh.triangles = triangles = new int[6] {0, 1, 2, 2, 3, 0};
    49.    
    50.         // add some dummy UVs to keep the shader happy or else it complains, but they are not used in this example
    51.         Vector2[] uvs = new Vector2[vertices.Length];
    52.             for (int k = 0; k < uvs.Length; k++) {
    53.             uvs[k] = new Vector2(vertices[k].x, vertices[k].y);
    54.         }
    55.         mesh.uv = uvs;
    56.        
    57.         // the usual cleanup
    58.         mesh.RecalculateNormals();
    59.         mesh.RecalculateBounds();
    60.         mesh.Optimize();
    61.     }
    62.  
    63.     void AssignMesh () {
    64.         mf.mesh = mesh;
    65.         mc.sharedMesh = mesh;
    66.     }
    67.  
    68.     // this handy method returns a component of given type and if there is none it creates one for you.
    69.     private T CheckComponent<T>() where T: Component {
    70.         T component = GetComponent<T>();
    71.         if (!component)
    72.             component = gameObject.AddComponent<T>();
    73.         return component;
    74.     }
    75. }
    76.  
    This is pretty much the same code used in the "terrain mesh" example, but a lot more simplified. We want to create a simple plane with just four vertices and three triangles. The vertices are taken straight from the grid's size, using the X and Y in this case, but you can change it to whatever you prefer. Then we assign the triangles, to this end we must list the indices of three vertices in a clockwise orientation. Finally we do some cleanup and assign everything to the mesh renderer, filter and collider.

    Changing the mesh's size means changing the position of its vertices, which requires you to do the calculation all over again, it's not something I would recommend doing each frame. If you are familiar with events in C# you can take a look at the user manual, there is an event you can subscribe to for when the grid is changed. if you wanted to see the mesh live in the editor you should use the event to make the mesh re-calculate only when needed, but it should be enough if the mesh i generated when the game starts.

    You can read more about generating grids from code here:
    http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/
    it is very well written and it's what helped me get started with the topic. All of this might look quite complicated, but that's because it is. The more you deviate from the standard 3D action game, the more ow-level work you will have to do, but the great thing bout Unity is that it lets you do it. Grid Framework will help you with the math, but you still need a firm grip on Unity itself to work with it.
     
  31. MikeTon

    MikeTon

    Joined:
    Jan 10, 2013
    Posts:
    57
    Hi HiPhish.

    I've been really enjoying the Grid Framework. And I'm looking to query what the current GridPlane of my grid object; I have setup logic that depends on it.

    Any advice on how to do that.

    The only documentation I've found is :

    * // UnityScrip:
    * var myPlane: GFGrid.GridPlane = GFGrid.GFGridPlane.XZ;
    * var planeIndex: int = (int)myPlane; // sets the variable to 1

    And I'm having issues running that.

    What I'd like to do is find out what my current GridPlane is set to.

    -Mike
     
  32. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi

    The GridPlane type is an enum, es documented here:
    http://msdn.microsoft.com/library/sbbt4032(v=vs.90).aspx

    Basically it means an enum is a list of specific types, in this case it's a YZ, XZ or XY plane. The exact type of a plane is
    Code (csharp):
    1. GFGrid.GridPlane.XY // or another type of plane
    You can also cast an enum into an int, each plane has a unique number associated with it (this is because in C an enum is really just a constant int), you can do it this way:
    Code (csharp):
    1. (int)GFGrid.GridPlane.XY // the number is 2
    The number behind each plane is the index of the missing coordinate, i.e. YZ = 0, XZ = 1 and XY = 2. Enums are a C# feature, but you can use them like any other type in unityScript as well:
    Code (csharp):
    1.  
    2. var myGrid : GFHexGrid; // the grid your are using
    3.  
    4. if (myGrid.gridPlane == GFGrid.GridPane.XY)
    5.     Debug.Log( "It's a plane of type XY." );
    6.  
    7. // or
    8.  
    9. Debug.Log( "Plane index is set to " +  (int)myGrid.gridPlane );
    10.  
    This I'm not sure what you get printed out if you don't cast to int, but I think that should work as well.
     
    Last edited: Jan 6, 2014
  33. B2F

    B2F

    Joined:
    Jul 10, 2013
    Posts:
    23
    Does this framework support the stacking of blocks? Say like Minecraft or like legos? If so I think this is exactly what i need.
     
  34. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    That depends on how exactly you want the stacking to work. One idea would be to snap the X- and Z coordinates to the grid and then use a raycast to determine the Y coordinate (assuming a 3D game like Minecraft:
    Code (csharp):
    1.  
    2. GFRectGrid myGrid; // the grid you will be using
    3.  
    4. Vector3 snappedPos = myGrid.NearestBoxW( transform.position ); // the world coordinates of the nearest box
    5.  
    6. // use regular Unity raycasting here
    7. RaycastHit hit; // this is where the cast information is stored
    8. float height = 0; // default the height to 0 if we don't hit anything below
    9. Physics.Raycast( transform.position, -Vector3.up, out hit ); // perform the raycast downwards
    10. if (hit) //make sure we actually hit something or we will get a null reference exception
    11.     float height = hit.point.y + 0.5f * transform.scale.y; // add half your block's height to the hit point
    12.  
    13. transform.position = new Vector3( snappedPos.x, height, snappedPos.z );
    14.  
    Snapping the block into place is simple, the tricky part is deciding on the height o your block doesn't float in space. There are other ways, you could for example store somewhere the size of each stack, but I like this approach better, it makes the decision on the fly and the block doesn't need to know anything about the rest of the scene. It will work for any size of playing field, since grids are infinitely large.
     
  35. lyndontroy

    lyndontroy

    Joined:
    Dec 12, 2012
    Posts:
    31
    Can you point me to a discussion or manual section, or help on:
    > How to warp a gridline based upon physics, like size and density of objects on grid coordinate and relative position of objects to one another.
    > Render the gridline warping, size, and color parameters based upon those physics characteristics.

    I want to create a solar system grid size, color, and bending depiction with real-world effects of planets and asteroids on one another. I also bought Vectrosity to help.

    Thanks
     
    Last edited: Jan 9, 2014
  36. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I read your post several times and I'm not entirely sure what you mean. Could you maybe provide a quick drawing or sketch? Are you perhaps trying to achieve something like this?

    Vectrosity uses meshes to render vector lines, so a first step would be to se GetVectoristyLines to passt the grid to Vectrosity. From there on you will need to manipulate the vertices of the mesh; I don't know how the meshes for Vectrosity are built, you will have to ask Eric5h5, the author. To get the elastic effect you will need to move the vertices within a certain radius of the planet downward. Here is a formula you could use:
    Code (csharp):
    1.  
    2. influence_factor = 1.25 // the multiple of the planet's radius that will influence the grid
    3. r = r_planet * influence_factor // only vertices within this radius will be influenced
    4. a // some magic number that makes the curve steeper of flatter
    5. foreach (vertex within the radius from the planet's axis) {
    6.    d = | planet_pos - vertex_pos | // distance of the vertex from the planet's axis
    7.     vertex_shift = a * d^2 - influence_factor
    8. }
    9.  
    This is a simple square function, it might not look that nice, but you can use any function you want. The idea is that the curvature should resemble a curve, and we can model a curve using any function we know from school. All we care about is the y-value to tell us how far to shift the vertex. The radius is there to give us a limit, otherwise the loops would try to lower each vertex in existence, which would be overkill if the user can't see the difference anymore.
     
  37. MikeTon

    MikeTon

    Joined:
    Jan 10, 2013
    Posts:
    57
    Here's what I get with Javascript/UnityScript when I try the following comparison:

    Code (csharp):
    1. if (myGrid.gridPlane == GFGrid.GridPane.XY) //errors with : BCE0051: Operator '==' cannot be used with a left hand side of type 'System.Type' and a right hand side of type 'GFGrid.GridPlane'.
    When I try to print/log myGrid.gridPlane I get:

    Code (csharp):
    1. print(myGrid.gridPlane); //GFGrid+GridPlane
    I think I'm misunderstanding how types resolve here. Do you have any advice to offer.

    Thanks for your time and attention so far.

    -Mike



     
    Last edited: Jan 12, 2014
  38. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Here is a simple test script, tell me if it works:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var grid : GFHexGrid;
    5. function Start () {
    6.     grid = GetComponent.<GFHexGrid>();
    7.     Debug.Log( grid.gridPlane );
    8.     if( grid.gridPlane == GFGrid.GridPlane.XY )
    9.         Debug.Log( "It's a plane in XY." );
    10. }
    11.  
    I get the following printed, as expected:
    If you are writing in UnityScript make sure your script is not in the plugins folder.
     
  39. lyndontroy

    lyndontroy

    Joined:
    Dec 12, 2012
    Posts:
    31
    hiphish, than you for reply. The image you provide of Earth on grid is the start to what I seek. Next, I need to add up to 40 additional planets, model their effects on one another, then show a line toeach planet with the effects shown as lines among their relationships with different color and sizes. Also, the planets would be sized based upon the strength of their relationships and the spin of each planet would also show the weight of their relationships. So:
    > Size on variable A that represents mass.
    > Spin on variable B that represents rate of change.
    > Line connector to each based upon relationship C.
    ? Line to each based upon relationship influence coefficient D.
    > Line color to each based upon relationship rate of change away from average E.

    I will draw an image and submit tonight.

    Thanks
     
  40. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm afraid I know next to nothing about astronomical physics, what you wrote goes over my head. I can't help you with the effects the planets have on each other and drawing lines between points is Vectrosity's domain. Grid Framework is a mathematical framework, the grid lines or vertices don't actually exist, they are calculated on the fly. Skewing the grid would first use a non-skewed grid to extract a set of vertices (can be done of of the box) and then those vertices would need to be moved depending on your physics model. I'm afraid I have nothing that would get that part done automatically, just the formula described above, so you will need to code that part manually.
     
  41. MrPixou

    MrPixou

    Joined:
    Dec 9, 2013
    Posts:
    1
    Last edited: Jan 21, 2014
  42. RandomProductYT

    RandomProductYT

    Joined:
    Oct 3, 2012
    Posts:
    7
    I apologize ahead of time if this question has already been answered.

    I'm very interested in using this asset for my project, but I need to be able to use the grid on curved surfaces (such as hills). Does this work with those yet? I haven't seen any note of it or any pictures.

    Thanks!
     
  43. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    The link you provided doesn't have any screenshots of the actual game, so I'll have to guess you want to be able to drag a time onto another tile and once you release they swap positions. The simplest and most flexible solution I can think of would be using events and delegates (C# only): When you pick up a tile store its grid position in world coordinates:
    Code (csharp):
    1.  
    2. GFGrid myGrid; //grid for reference
    3. Vector3 oldPos = myGrid.NearestFaceW( transform.position, GFGrid.GridPlane.XY ); //or whatever plane you are using
    Once you release the tile get the target position:
    Code (csharp):
    1. Vector3 targetPos = myGrid.NearestFaceW( transform.position, GFGrid.GridPlane.XY );
    Now we need to find the tile that is already occupying the target position and move it to the old position. This is where events and delegates come into play: you send an event and pass targetPos as an argument and each tile checks if it is close to that position:
    Code (csharp):
    1. if (Vector3.sqrMagnitude(transform.position - targetPos) < 0.001f )
    2.     transform.position = oldPosition;
    3.  
    We could have used if (transform.position == targetPos), but having some tolerance makes sure we don't miss anything due to rounding errors. The advantage of this approach is that we don't have to constantly keep track of the playing field, the tiles to it automatically.

    Hi, no need to apologize for asking something that isn't answered on the product page. Grids in Grid Framework are not what might pop up in one's head at first; they are not a collection of intersecting line segments, instead they are mathematical constructs. All grids are infinitely large and what you see is just a finite representation, just how a line segment is a representation of an infinitely large line.

    So to answer your question, no, I'm afraid I don't have such a solution built in. I am actually looking for such a solution myself, but the best I was able to find was using tricks like drawing the grid onto the texture or using Unity's projector.
     
  44. RandomProductYT

    RandomProductYT

    Joined:
    Oct 3, 2012
    Posts:
    7
    Thanks for the info!
     
  45. BGog

    BGog

    Joined:
    Feb 10, 2014
    Posts:
    10
    Hello! I'm loving the grid framework. However I'm having an issue that perhaps you could help with.

    When I have the grid render, I draws on top of all other objects in the scene. Is there a way to prevent this? I'm using it to place a grid down on the ground and I'd like objects sticking up from the ground to obscure what they cover.

    Here is a link to a screenshot.
    http://imgur.com/ZutBzsH
     
  46. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi,

    no, that shouldn't be the case. Here is the runtime snapping example, both in orthographic and in perspective mode:


    What version of Grid Framework are you using? I know I had such a bug in an older release but I thought I had fixed it since then. The current version is 1.3.5, you can check which one you have by going to Help -> Grid Framework documentation; if you don't see that entry you are definitely out of date. If you are up to date please tell me your camera settings and your grid settings so I can replicate your bug.
     
  47. BGog

    BGog

    Joined:
    Feb 10, 2014
    Posts:
    10
    Thanks much for the fast reply!

    I'm running 1.3.5. I figured out what the cause was but don't fully understand why.

    So the building in the picture has a transparent/diffuse shader. Other objects don't draw on top if it but the grid does.
    If I place a cube with a diffuse shader, the grid draws properly and if I place the cube behind the building, you cannot see the cube through the building.

    If I change the building to use a diffuse shader, then it works. Is there some issue with using a transparent shader?
     
  48. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm afraid I am no expert when it comes to shaders. You can drop in a custom material for your grids, so have you tried using the same shaders as for the objects in your scene? One thing I could see being the issue is that grids are rendered using OnPostRender, rendering them after the rest of the scene has been rendered, that's how the Unity documentation advises to use the the GL methods. Here is the default shader that's used:
    Code (csharp):
    1. "Shader \"Lines/Colored Blended\" {" +
    2.     "SubShader { Pass { " +
    3.     "   Blend SrcAlpha OneMinusSrcAlpha " +
    4.     "   ZWrite Off Cull Off Fog { Mode Off } " +
    5.     "   BindChannels {" +
    6.     "   Bind \"vertex\", vertex Bind \"color\", color }" +
    7.     "} } }";
    8.  
    I admit the rendering capabilities of Grid Framework are very rudimentary, it can bring the lines on the screen but it can't do anything fancy (which is why there is built-in support for Vectrosity). What you are experiencing is a technical limitation in the Unity engine, it is described here:
    http://answers.unity3d.com/questions/21691/drawing-semi-transparent-objects-with-glbeginglqua.html
    The third answer explains how to work around the issue by using a second camera. You should make your main camera and the second camera children of the same object, that way they will always have the same position. Don't worry about manually rendering, just place the GFGrdiRenderCamera script on that camera and it will get the lines on its own.
     
  49. petrucio

    petrucio

    Joined:
    Aug 30, 2011
    Posts:
    126
    I'm getting exceptions after upgrading to the latest version:

    This one while the game is running inside the editor:

    Code (csharp):
    1. IndexOutOfRangeException: Array index is out of range.
    2. GFBoolVector3.get_Item (Int32 index) (at Assets/Plugins/Grid Framework/Vectors/GFBoolVector3.cs:60)
    3. GFGrid.DrawGrid (Vector3 from, Vector3 to) (at Assets/Plugins/Grid Framework/Abstracts/GFGrid.cs:759)
    4. GFGrid.DrawGrid () (at Assets/Plugins/Grid Framework/Abstracts/GFGrid.cs:783)
    5. GFRectGrid.OnDrawGizmos () (at Assets/Plugins/Grid Framework/GFRectGrid.cs:305)
    6. UnityEditor.DockArea:OnGUI()
    7.  
    This one while viewing a grid in the inspector:

    Code (csharp):
    1. IndexOutOfRangeException: Array index is out of range.
    2. GFColorVector3.get_x () (at Assets/Plugins/Grid Framework/Vectors/GFColorVector3.cs:19)
    3. GFGridEditor.ColourFields () (at Assets/Editor/Grid Framework/Inspectors/GFGridEditor.cs:60)
    4. GFGridEditor.StandardFields () (at Assets/Editor/Grid Framework/Inspectors/GFGridEditor.cs:39)
    5. GFGridEditor.OnInspectorGUI () (at Assets/Editor/Grid Framework/Inspectors/GFGridEditor.cs:26)
    6. UnityEditor.InspectorWindow.DrawEditors (Boolean isRepaintEvent, UnityEditor.Editor[] editors, Boolean eyeDropperDirty) (at C:/BuildAgent/work/d3d49558e4d408f4/Editor/Mono/Inspector/InspectorWindow.cs:850)
    7. UnityEditor.DockArea:OnGUI()
    8.  
    Can't view the grid in the scene, can't edit axis colors in the inspector.
     
  50. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm not seeing any errors here; have you tried resetting your grids? In the inspector click the little cog of the grid component and choose Reset, this will reset the properties of the grid. Sometime Unity might not be able to cope with changes done to a script and you will have to force a reset.