Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152

    Problems with Video 265 (Session 9, pt. 37)

    Hello,
    I've encountered a problem while following along the video 265.

    It's all fine up untill the moment in which we change this line of code:
    from
    Code:
    var height = VertexCache.GetHeight(key);
    to
    Code:
    var height = VertexCache.Instance[key].Height;
    Inside GetVertexData() of the TerrainPatch.cs (basically starting to use the new Add() and AddNeighbors() function in the VertexCache.cs ).

    After this change, every time I try to run Unity, it crashes. Trying to open the debugger it says:

    Code:
    First-chance exception at 0x08276a7a in Unity.exe: 0xC0000005: Access violation in the reading of the path 0x00000014.
    I don't know how to fix this, I've rewatched the video (and the previous one) two times without spotting an error.

    Can you please help me figure this out? I can post the whole VertexCache.cs if it's allowed.

    Thank you
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

  2. #2
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152
    I guess it's a bit too hard to help me with this few informations and on a very old video. I'll post Vertex.cs and VertexCache.cs so you can maybe spot an error or two. I hope it's ok, if not please delete my post.

    Vertex.cs
    Code:
    using System;
    using UnityEngine;
    
    namespace Code.TerrainSystem
    {
    	public class Vertex
    	{
            public DateTime Touched { get; private set; }
    
    	    public Vector2 Key
    	    {
    	        get
    	        {
    	            Touched = DateTime.Now;
    	            return new Vector2(Position.x, Position.z);
    	        }
    	    }
    
    	    private Vector3 position;
            public Vector3 Position
            {
                get
                {
                    Touched = DateTime.Now;
                    return Position;
                }
    
                set
                {
                    Touched = DateTime.Now;
                    position = value;
                }
            }
    
    	    public float Height
    	    {
                get
                {
                    Touched = DateTime.Now;
                    return Position.y;
                }
    
                set
                {
                    Touched = DateTime.Now;
                    Position = new Vector3(Position.x, value, Position.z);
                }
    	    }
    
            private Vector3 normal;
    	    public Vector3 Normal
    	    {
    	        get
    	        {
    	            Touched = DateTime.Now;
                    return normal;
    	        }
    
    	        set
    	        {
    	            Touched = DateTime.Now;
    	            normal = value;
    	        }
    	    }
    
            private Vector4 tangent;
    	    public Vector4 Tangent
    	    {
    	        get
    	        {
    	            Touched = DateTime.Now;
                    return tangent;
    	        }
    
    	        set
    	        {
    	            Touched = DateTime.Now;
                    tangent = value;
    	        }
    	    }
    
            private Color color;
            public Color Color
            {
                get
                {
                    Touched = DateTime.Now;
                    return color;
                }
    
                set
                {
                    Touched = DateTime.Now;
                    color = value;
                }
            }
    
            public bool Hold { get; set; }
    
            public bool Modified { get; set; }
    
            public bool DestroyMe { get; set; }
    
    	    public Vertex(Vector3 position)
            {
                Touched = DateTime.Now;
    	        Position = position;
            }
    	}
    }
    VertexCache.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using Code.Features;
    using UnityEngine;
    
    namespace Code.TerrainSystem
    {
    	public class VertexCache//10.27 su 14.13 video 265
    	{
    	    public static readonly Dictionary<Vector2, Vertex> Vertices;
    
    	    private static readonly int PurgeInterval;
            private static readonly int PurgeLimit;
            private static readonly int VertexTTL; // Time To Live before we flag them for removal
            private static readonly int Size;
            private static readonly float Spacing;
            private static readonly int MaxHeight;
            private static readonly TerrainGenerator TerrainGenerator;
    
    	    private static Thread purgeThread;
            private static bool isQuitting;
    
            public static VertexCache Instance { get; private set; }
    	    
    	    public static int Count
    	    {
                get { return Vertices.Count; }
    	    }
    
    	    public Vertex this[Vector2 position]
    	    {
    	        get
    	        {
                    if (!Vertices.ContainsKey(position))
                        return Add(position, true);
    
    	            return Vertices[position];
    	        }
    	    }
    
            static VertexCache()
        	{
                Instance = new VertexCache();
    	        Vertices = new Dictionary<Vector2, Vertex>();  
                TerrainGenerator = new TerrainGenerator();
    
                PurgeInterval = Config.GetInt("PurgeInterval");
                PurgeLimit = Config.GetInt("PurgeLimit");
                VertexTTL = Config.GetInt("VertexTTL");
                Size = Config.GetInt("VertexCount");
                Spacing = Config.GetFloat("VertexSpacing");
                MaxHeight = Config.GetInt("MaxHeight");
        	}
    
            private VertexCache() {}
    
            public static Vertex Add(Vector2 position, bool hold, bool addNeighbors = true)
            {
                if (Vertices.ContainsKey(position))
                {
                    if (hold)
                        Vertices[position].Hold = true;
    
                    return Vertices[position];
                }
    
                var vertex = new Vertex(new Vector3(position.x, 0, position.y))
                {
                    Height = GetHeight(position)
                };
    
                lock (Vertices)
                {
                   Vertices.Add(position, vertex); 
                }
    
                if (hold)
                    vertex.Hold = true;
    
                if (addNeighbors)
                    AddNeighbors(position);
    
                return vertex;
            }
    
    	    private static void AddNeighbors(Vector2 position)
    	    {
    	        Add(position + new Vector2(-Spacing, Spacing), false, false);
                Add(position + new Vector2(0, Spacing), false, false);
                Add(position + new Vector2(Spacing, Spacing), false, false);
                Add(position + new Vector2(-Spacing, 0), false, false);
                Add(position + new Vector2(-Spacing, 0), false, false);
                Add(position + new Vector2(-Spacing, -Spacing), false, false);
                Add(position + new Vector2(0, -Spacing), false, false);
                Add(position + new Vector2(Spacing, -Spacing), false, false);
    	    }
    
    	    public static float GetHeight(Vector2 position)
            {
                return TerrainGenerator.GetSmoothHeight(position.x, position.y);
            }
    
            public static void Quit()
            {
                isQuitting = true;
            }
            
        }
    }
    Thank you for your help
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

  3. #3
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152
    Mmh maybe it's harder than I thought. Can I ask to some of you to send me your Vertex.cs and VertexCache.cs via PM? It doesn't matter if you're in a later video, I'll just double check the methods I've already in place.

    Thank you
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

  4. #4
    Join Date
    Apr 2010
    Location
    Alabama
    Posts
    100
    I don't see anything amiss with your vertex or vertex cache files, can I have a peek at TerrainPatch.cs?
    Get your facts first, and then you can distort them as much as you please. - Mark Twain

  5. #5
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152
    Thank you Moosaroo!

    Here it is:

    TerrainPatch.cs

    Code:
    using System;
    using Code.Features;
    using Code.Threading;
    using UnityEngine;
    
    namespace Code.TerrainSystem
    {
    	public class TerrainPatch
    	{
            public Vector3 Position { get; private set; }
            public TerrainMesh Mesh { get; private set; }
            public QueuePriority Priority { get; set; }
    
    	    public Vector3 WorldPosition
    	    {
                get { return Position * (Config.GetInt("VertexCount") - 1) * Config.GetFloat("VertexSpacing");  }
    	    }
    
    	    private bool enabled;
    	    public bool Enabled
    	    {
                get { return enabled; }
                set 
                { 
                    // Enable or Disable the mesh renderer
                    enabled = value;
                    if (Mesh != null)
                        Mesh.MeshRenderer.enabled = enabled;
                }
    	    }
    
            // Constructor
            public TerrainPatch(Vector3 position, QueuePriority priority)
            {
                Position = position;
                Priority = priority;
    
                // Execute the SetupMesh() within the Threading System
                PatchBuilder.QueuePatchDispatch(SetupMesh, this);
            }
    
            // Methods
    	    private void SetupMesh()
    	    {
                // If there's a Terrain mesh in the MyObjectPool take it,
                // if there isn't (null is returned) then create a new one
    	        Mesh = MyObjectPool.CheckOut<TerrainMesh>() as TerrainMesh ?? new TerrainMesh();
    
                // Parent it to the terrainobject.
               Mesh.MeshObject.transform.parent = TerrainManager.TerrainObject.transform;
    
    	        GetVertexData();
    	        SetMeshDetails();
                Mesh.Update(WorldPosition);
    
                // Enable the mesh renderer (since we're using the Property that sets it)
    	        Enabled = true;
    	    }
    
            private void GetVertexData()
            {
                // Get the Height information from the vertices
                for (int i = 0; i < Mesh.Vertices.Length; i++)
                {
                    // Get a key to look at information
                    var vertex = Mesh.Vertices[i];
                    var key = GetKey(vertex);
    
                    // THIS WORKS: var height = VertexCache.GetHeight(key); 
                    var height = VertexCache.Instance[key].Height;
    
                    Mesh.Vertices[i] = new Vector3(vertex.x, height, vertex.z);
                }
            }
    
    	    private void SetMeshDetails()
    	    {
    	        for (int i = 0; i < Mesh.Vertices.Length; i++)
    	        {
                    //Mesh.Normals[i] = Vector3.zero;
                    //Mesh.Tangents[i] = Vector4.zero;
                    //Mesh.Colors[i] = Color.white;
    	        }
    	    }
    
            public bool Recycle()
            {
                if (Mesh != null)
                {
                    Enabled = false;                            // Turn off the mesh
                    Mesh.MeshCollider.sharedMesh = null;        // Disable the collision
                    
                    // Parent it to this pool object
                    Mesh.MeshObject.transform.parent = MyObjectPool.PoolObject.transform;    
                    
                    MyObjectPool.CheckIn(Mesh);                 // Insert the mesh in the pool
                    return true;                                // All ok!
                }
    
                return false;                                   // Non ok!
            }
    
            public Vector2 GetKey(Vector2 position)
            {
                var x = (float)Math.Round( (position.x + WorldPosition.x ), 2);
                var y = (float)Math.Round((position.y + WorldPosition.z), 2);
    
                return new Vector2(x,y);
            }
    
            public Vector2 GetKey(Vector3 position)
            {
                return GetKey(new Vector2(position.x, position.z));
            }
    	}
    }
    Thank you again
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

  6. #6
    Join Date
    Apr 2010
    Location
    Alabama
    Posts
    100
    Ok i said nothing was amiss with Vertex or VertexCache. Im thinking now I might have been full of crap.

    In the Vertex Class:

    Code:
    private Vector3 position;
            public Vector3 Position
            {
                get
                {
                    Touched = DateTime.Now;
                    return position;  // Capitalizing this caused a nice glorious Unity crash
                }
                set
                {
                    Touched = DateTime.Now;
                    position = value;
                }
            }
    Check the capitalization on the return in your getter.
    Last edited by Moosaroo; 10-14-2011 at 07:28 AM.
    Get your facts first, and then you can distort them as much as you please. - Mark Twain

  7. #7
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152
    Thank you very much Moosaroo!
    I really missed that capitalization there.

    Thank you, you just saved me a lot of rewatching!

    Thank you again
    Last edited by Artanis; 10-14-2011 at 08:42 AM. Reason: typo
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

  8. #8
    Join Date
    Apr 2010
    Location
    Alabama
    Posts
    100
    Not a problem, those can be the tough to pick out.
    Get your facts first, and then you can distort them as much as you please. - Mark Twain

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •