Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Array Shifting / Wrong Index / i = [x+y*size+z*size*size]

Discussion in 'Scripting' started by Shiikarii, Aug 11, 2016.

  1. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Greetings, I have a simple question.
    How can I shift a linear array in 3 dimensions?​

    X.gif Y.gif

    Z-axis working + XYZ

    Z.gif XYZ.gif
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shifting : MonoBehaviour
    5. {
    6.     public int size;
    7.     public float speed;
    8.  
    9.     private byte[] volume;
    10.     private byte[] shifted;
    11.  
    12.     public bool widthShift, heightShift, depthShift;
    13.  
    14.     private int widthOffset = 0;
    15.     private int heightOffset = 0;
    16.     private int depthOffset = 0;
    17.  
    18.     private float time = 0;
    19.     private int cube;
    20.  
    21.     void Start()
    22.     {
    23.         volume = new byte[size * size * size];
    24.         shifted = new byte[size * size * size];
    25.  
    26.         cube = size * size * size;
    27.  
    28.         for (int x = 0; x < size; x++)
    29.             for (int y = 0; y < size; y++)
    30.                 for(int z = 0; z < size; z++)
    31.                     volume[x + y * size + z * size * size] = (x == 0 || y == 0 || z == 0) ? (byte)1 : (byte)0;
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         time += Time.fixedDeltaTime * speed;
    37.  
    38.         if (time > 1)
    39.         {
    40.             time = 0;
    41.  
    42.             widthOffset = (widthOffset >= size) ? 0 : widthOffset;
    43.             heightOffset = (heightOffset >= size) ? 0 : heightOffset;
    44.             depthOffset = (depthOffset >= size) ? 0 : depthOffset;
    45.  
    46.             if (widthShift)
    47.                 widthOffset++;
    48.             else
    49.                 widthOffset = 0;
    50.  
    51.             if (heightShift)
    52.                 heightOffset++;
    53.             else
    54.                 heightOffset = 0;
    55.  
    56.             if (depthShift)
    57.                 depthOffset++;
    58.             else
    59.                 depthOffset = 0;
    60.  
    61.             Shift(widthOffset, heightOffset, depthOffset);
    62.         }
    63.     }
    64.  
    65.     void Shift(int xOff, int yOff, int zOff)
    66.     {
    67.         for (int x = 0; x < size; x++)
    68.             for (int y = 0; y < size; y++)
    69.                 for(int z = 0; z < size; z++)
    70.                 {
    71.                     int i = ((x + xOff) + (y + yOff) * size + (z + zOff) * size * size);
    72.                     i = (i >= cube) ? (i - cube) : i;
    73.  
    74.                     shifted[x + y * size + z * size * size] = volume[i];
    75.                 }
    76.     }
    77.  
    78.     void OnDrawGizmos()
    79.     {
    80.         if(Application.isPlaying)
    81.             for(int x = 0; x < size; x++)
    82.                 for(int y = 0; y < size; y++)
    83.                     for(int z = 0; z < size; z++)
    84.                     {
    85.                         Gizmos.color = (shifted[x + y * size + z * size * size] == 1) ? new Color32(0, 255, 0, 255) : new Color32(255, 0, 0, 4);
    86.                         Gizmos.DrawWireCube(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f), new Vector3(0.95f, 0.95f, 0.95f));
    87.                     }
    88.     }
    89. }
     
    Last edited: Aug 11, 2016
  2. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Does nobody have an idea what the problem could be? :/