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. Dismiss Notice

How can I Get A Vertices Vector In World Space

Discussion in 'Scripting' started by keenanwoodall, Jul 23, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    If you know how to get each vertices position (obviously through a for loop) and modify their position by getting their coordinates in world space
     
    Last edited: Jul 23, 2014
  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I thought I figured it out...
    NOPE I haven't figured it out. All of the Vectors in the array are (1.5, 0.8, 1.5). Again, how do get a vertices Vector in world space
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  4. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
  5. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    That worked well. Now I'm trying to move each vertice based on perlin noise. I made another script that works wll, but instead of modifying vertices, it creates and moves cubes.
    Current VertexBasedDisplacementScript
    Code (js):
    1.  
    2. var seed : int = 0.0;
    3. var roundHeightToInt : boolean;
    4. var noiseScale : float = 6.5;
    5. var waveHeight : int = 3;
    6. var animateSpeed : float = 1.0;
    7.  
    8. private var mesh : Mesh;
    9. private var vertices : Vector3[];
    10. private var perlinHeight : float;
    11.  
    12. function Start ()
    13. {
    14.     mesh = this.gameObject.GetComponent(MeshFilter).mesh;
    15. }
    16.  
    17. function Update ()
    18. {
    19.     mesh = this.gameObject.GetComponent(MeshFilter).mesh;
    20.     vertices = mesh.vertices;
    21.     for (var vert = 0; vert < vertices.Length; vert++)
    22.     {
    23.         var worldVert : Vector3 = vertices[vert];
    24.         perlinHeight = Mathf.PerlinNoise (worldVert.x,worldVert.z);
    25.         worldVert.y = perlinHeight * waveHeight;
    26.         Debug.Log (worldVert);
    27.     }
    28.     //mesh.RecalculateBounds();
    29.     //mesh.RecalculateNormals();
    30. }
    31.  
    CubeBasedDisplacementScript:
    Code (js):
    1.  
    2. var cube : GameObject;
    3. var colorSetsScale : boolean;
    4. var heightSetsScale : boolean;
    5. var animateColor : boolean;
    6. var roundHeightToInt : boolean;
    7. var noiseScale : float = 6.5;
    8. var cubeHeight : int = 3;
    9. var animateSpeed : float = 1.0;
    10.  
    11. function Start ()
    12. {
    13.     for (var spawnPositionX = 0; spawnPositionX < cubeAmountX; spawnPositionX++)
    14.     {
    15.         for (var spawnPositionZ = 0; spawnPositionZ < cubeAmountZ; spawnPositionZ++)
    16.         {  
    17.             var makeCube = Instantiate(cube, Vector3(spawnPositionX,0,spawnPositionZ), Quaternion.identity);
    18.             makeCube.transform.parent = this.transform;
    19.         }
    20.     }
    21.     for (var child : Transform in this.transform)
    22.     {
    23.         var height = Mathf.PerlinNoise(child.transform.position.x/noiseScale + (seed * 10), child.transform.position.z/noiseScale + (seed *  10));
    24.         child.renderer.material.color = Color(height,height,height,height);
    25.         if (colorSetsScale)
    26.         {
    27.             child.transform.localScale = Vector3 (height,height,height);
    28.         }
    29.     }
    30.     if (noiseScale <= 0.001)
    31.     {
    32.         noiseScale = 0.0011;
    33.     }
    34. }
    35.  
    36. function Update ()
    37. {
    38.     for (var child : Transform in this.transform)
    39.     {
    40.         var height = Mathf.PerlinNoise(child.transform.position.x/noiseScale, child.transform.position.z/noiseScale);
    41.         if (animateColor)
    42.         {
    43.             child.renderer.material.color = Color(height,height,height,height);
    44.         }
    45.     }
    46.     for (var child : Transform in this.transform)
    47.     {
    48.         height = Mathf.PerlinNoise(Time.time * animateSpeed + (child.transform.position.x/noiseScale) + (seed * 10), Time.time * animateSpeed + (child.transform.position.z/noiseScale) + (seed * 10));
    49.         if (!roundHeightToInt)
    50.         {
    51.             child.transform.position.y = height * cubeHeight + transform.position.y;
    52.         }
    53.         else if (roundHeightToInt)
    54.         {
    55.             child.transform.position.y = Mathf.RoundToInt(height * cubeHeight + transform.position.y);
    56.         }
    57.         if (animateColor)
    58.         {
    59.             child.renderer.material.color = Color(height,height,height,height);
    60.         }
    61.         if (colorSetsScale)
    62.         {
    63.             child.transform.localScale = Vector3 (height,height,height);
    64.             heightSetsScale = false;
    65.         }
    66.         if (heightSetsScale)
    67.         {
    68.             var cubeHeight : float = child.transform.localPosition.y;
    69.             child.transform.localScale = Vector3 (cubeHeight, cubeHeight, cubeHeight);
    70.             colorSetsScale = false;
    71.         }
    72.     }
    73. }