public static bool isSolid(Vector3[,,] voxels, int x, int y, int z) { if (x < 0 || x >= voxels.GetLength(0) || y < 0 || y >= voxels.GetLength(1) || z < 0 || z >= voxels.GetLength(2)) { return(false); } return(voxels[x, y, z] != negative); }
private static float Make3DPerlinNoise(Vector3[,,] gradientMap, float x, float y, float z, Continum continum) { int length = gradientMap.GetLength(0); if (length < 2) { throw new ArgumentException("[NoiseGenerator] Degraded gradient map (x dimension)"); } int width = gradientMap.GetLength(1); if (width < 2) { throw new ArgumentException("[NoiseGenerator] Degraded gradient map (y dimension)"); } int height = gradientMap.GetLength(2); if (height < 2) { throw new ArgumentException("[NoiseGenerator] Degraded gradient map (z dimension)"); } bool isContinuousOnX = ((int)continum & 1) == 1; bool isContinuousOnY = ((int)continum & 2) == 1; bool isContinuousOnZ = ((int)continum & 4) == 1; x *= isContinuousOnX ? length : length - 1; y *= isContinuousOnY ? width : width - 1; z *= isContinuousOnZ ? height : height - 1; int xi = Mathf.FloorToInt(x); int yi = Mathf.FloorToInt(y); int zi = Mathf.FloorToInt(z); float xf = x - xi; float yf = y - yi; float zf = z - zi; Vector3 point = new Vector3(x, y, z); float[] dotProducts = new float[8]; for (int i = 0; i < 8; i++) { int xu = xi + (i & 1); int yu = yi + (i >> 1); int zu = zi + (i >> 2); dotProducts[i] = Vector3.Dot(gradientMap[isContinuousOnX ? xu % length : xu, isContinuousOnY ? yu % width : yu, isContinuousOnZ ? zu % height : zu], new Vector3(xu, yu, zu) - point); } float u = Fade(xf); float v = Fade(yf); float w = Fade(zf); return(Mathf.Lerp(Mathf.Lerp(Mathf.Lerp(dotProducts[0], dotProducts[1], u), Mathf.Lerp(dotProducts[2], dotProducts[3], u), v), Mathf.Lerp(Mathf.Lerp(dotProducts[4], dotProducts[5], u), Mathf.Lerp(dotProducts[6], dotProducts[7], u), v), w)); }
public float at(Vector3 point) { int sx = (int) Math.Floor(point.x); int sy = (int) Math.Floor(point.y); int sz = (int) Math.Floor(point.z); var qp = point - new Vector3(sx, sy, sz); var w = data.GetLength(0); var h = data.GetLength(1); var d = data.GetLength(2); var sx1 = sx + 1; var sy1 = sy + 1; var sz1 = sz + 1; var noise = Mathf.Lerp( Mathf.Lerp( Mathf.Lerp( Vector3.Dot(data[sx % w, sy % h, sz % d], point - new Vector3(sx, sy, sz)), Vector3.Dot(data[sx1 % w, sy % h, sz % d], point - new Vector3(sx1, sy, sz)), smoother(qp.x) ), Mathf.Lerp( Vector3.Dot(data[sx % w, sy1 % h, sz % d], point - new Vector3(sx, sy1, sz)), Vector3.Dot(data[sx1 % w, sy1 % h, sz % d], point - new Vector3(sx1, sy1, sz)), smoother(qp.x) ), smoother(qp.y) ), Mathf.Lerp( Mathf.Lerp( Vector3.Dot(data[sx % w, sy % h, sz1 % d], point - new Vector3(sx, sy, sz1)), Vector3.Dot(data[sx1 % w, sy % h, sz1 % d], point - new Vector3(sx1, sy, sz1)), smoother(qp.x) ), Mathf.Lerp( Vector3.Dot(data[sx % w, sy1 % h, sz1 % d], point - new Vector3(sx, sy1, sz1)), Vector3.Dot(data[sx1 % w, sy1 % h, sz1 % d], point - new Vector3(sx1, sy1, sz1)), smoother(qp.x) ), smoother(qp.y) ), smoother(qp.z) ); if (p != null) { noise += p.at(point * 2) / 2; } return noise; }
/// Create a sampler with the following parameters: /// /// width: each sample's x coordinate will be between [0, width] /// height: each sample's y coordinate will be between [0, height] /// depth: each sample's z coordinate will be between [0. depth] /// radius: each sample will be at least `radius` units away from any other sample, and at most 2 * `radius`. public PoissonfromGithub(float width, float height, float depth, float radius) { cube = new Vector3(width, height, depth); radius2 = radius * radius; cellSize = radius / Mathf.Sqrt(3); grid = new Vector3[Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize), Mathf.CeilToInt(depth / cellSize)]; Debug.Log(grid.GetLength(0)); Debug.Log(grid.GetLength(1)); Debug.Log(grid.GetLength(2)); }
public void CreateFoundation() { Vector3[,,] mapMeshPoints = Vector3[houseData.houseConfig.houseWidth, houseData.houseConfig.houseLength, 4]; for (int x = 0; x < mapMeshPoints.GetLength(0); x++) { for (int z = 0; z < mapMeshPoints.GetLength(1); z++) { if (houseData.floorPlan.area [x, z] == UnitType.INNER_GROUND) { } } } }
void InitVectorGrid(Vector3[,,] grid, Vector3 value) { int i, j, k, iMax = grid.GetLength(0), jMax = grid.GetLength(1), kMax = grid.GetLength(2); for (i = 0; i < iMax; i++) { for (j = 0; j < jMax; j++) { for (k = 0; k < kMax; k++) { grid [i, j, k] = new Vector3(value.x, value.y, value.z); } } } }
private void Draw(Vector3[,,] value) { for (int z = 0; z < value.GetLength(2); ++z) { for (int y = 0; y < value.GetLength(1); ++y) { for (int x = 0; x < value.GetLength(0); ++x) { Vector3 p = ComputePoint(x, y, z); Color c = ComputeColor(p, ComputePoint(0, 0, m_gridExtent - 1), ComputePoint(m_gridExtent - 1, m_gridExtent - 1, 0), 0.1f); DebugUtil.DrawArrow(p, p + m_elementSize * value[z, y, x], 0.05f, c, true, DebugUtil.Style.FlatShaded); } } } }
public LoopableNoise3D(int seed, float period, Point3 periodLoop) { this.seed = seed; this.period = period; this.periodLoop = periodLoop; Random rdm = new Random(seed); gradientVectors = new Vector3[periodLoop.X, periodLoop.Y, periodLoop.Z]; for(int i=0;i<gradientVectors.GetLength(0);i++) for(int j=0;j<gradientVectors.GetLength(1);j++) for (int k = 0; k < gradientVectors.GetLength(2); k++) { gradientVectors[i, j, k] = randVect(rdm); } }
// Calculate magnitude of gradient volume public static float[,,] GradientMagnitude(Vector3[, ,] data) { int sizeX = data.GetLength(0); int sizeY = data.GetLength(1); int sizeZ = data.GetLength(2); float[,,] magnitude = new float[sizeX, sizeY, sizeZ]; for (int k = 1; k < sizeZ - 1; k++) { for (int j = 1; j < sizeY - 1; j++) { for (int i = 1; i < sizeX - 1; i++) { Vector3 gradValue = data[i, j, k]; magnitude[i, j, k] = gradValue.Length(); } } } return(magnitude); }
public LoopableNoise3D(int seed, float period, Point3 periodLoop) { this.seed = seed; this.period = period; this.periodLoop = periodLoop; Random rdm = new Random(seed); gradientVectors = new Vector3[periodLoop.X, periodLoop.Y, periodLoop.Z]; for (int i = 0; i < gradientVectors.GetLength(0); i++) { for (int j = 0; j < gradientVectors.GetLength(1); j++) { for (int k = 0; k < gradientVectors.GetLength(2); k++) { gradientVectors[i, j, k] = randVect(rdm); } } } }
//TODO: Do this in a rendertexture too somehow? //Fill in UAV? public void GenerateTexture(Vector3[,,] values, bool newTexture = true) { int xres = values.GetLength(0); int yres = values.GetLength(1); int zres = values.GetLength(2); if (!IsPowerOfTwo(xres) || !IsPowerOfTwo(yres) || !IsPowerOfTwo(zres)) { Debug.Log("Can only generate a power of two sized turbulence texture!"); return; } Color[] colours = new Color[xres * yres * zres]; int id = 0; for (int z = 0; z < zres; ++z) { for (int y = 0; y < yres; ++y) { for (int x = 0; x < xres; ++x) { colours[id] = EncodeVector(values[x, y, z]); ++id; } } } if (forceTexture == null || newTexture) { forceTexture = new Texture3D(xres, yres, zres, TextureFormat.RGB24, false) { anisoLevel = 0, wrapMode = TextureWrapMode.Repeat }; } forceTexture.SetPixels(colours); forceTexture.Apply(); }
public static List <VoxelData> CalculateOctree(Vector3[,,] voxels) { List <VoxelData> voxelData = new List <VoxelData>(); int size = voxels.GetLength(0); size = NextPowerOf2(size); VoxelData root = new VoxelData(new Vector3(0, 0, 0), size, 0, 0, 1, new Vector3(1, 0.5f, 1)); voxelData.Add(root); SplitOctree(voxels, voxelData, 0); return(voxelData); }
private static float Make2DPerlinNoise(Vector3[,,] gradientMap, float x, float y) { int length = gradientMap.GetLength(0); if (length < 2) { throw new ArgumentException("[NoiseGenerator] Degraded gradient map (x dimension)"); } int width = gradientMap.GetLength(1); if (width < 2) { throw new ArgumentException("[NoiseGenerator] Degraded gradient map (y dimension)"); } x *= length - 1; y *= width - 1; int xi = Mathf.FloorToInt(x); int yi = Mathf.FloorToInt(y); float[] dotProducts = new float[4]; for (int i = 0; i < 4; i++) { int xu = xi + (i & 1); int yu = yi + (i >> 1); dotProducts[i] = Vector2.Dot(gradientMap[xu, yu, 0], new Vector2(x - xu, y - yu)); } float u = Fade(x - xi); float v = Fade(y - yi); return(Mathf.Lerp(Mathf.Lerp(dotProducts[0], dotProducts[1], u), Mathf.Lerp(dotProducts[2], dotProducts[3], u), v) * SQRT2); }
public T[,,] SpawnElements <T>(Vector3[,,] positions) { T[,,] elements = new T[positions.GetLength(0), positions.GetLength(1), positions.GetLength(2)]; for (int k = 0; k < positions.GetLength(2); k++) { for (int j = 0; j < positions.GetLength(1); j++) { for (int i = 0; i < positions.GetLength(0); i++) { elements[i, j, k] = SpawnElementAt <T>(positions[i, j, k]); } } } return(elements); }
void Start() { field = new Vector3[resolution, resolution, resolution]; for (int i = 0; i < field.GetLength(0); i++) { for (int j = 0; j < field.GetLength(1); j++) { for (int k = 0; k < field.GetLength(2); k++) { Vector3 coordinate = new Vector3( dimension.x * ((float)i / ((float)field.GetLength(0) - 1)), dimension.y * ((float)j / ((float)field.GetLength(1) - 1)), dimension.z * ((float)k / ((float)field.GetLength(2) - 1))); Vector3 orthogonal = Vector3.Cross(Vector3.up, coordinate - (Vector3.one * 0.5f)); field[i, j, k] = (((orthogonal.normalized) * (1f / orthogonal.magnitude)) * 0.001f) - ((coordinate - (Vector3.one * 0.5f)) * 0.1f); } } } }
public void init(string vecFieldName) { StreamReader sr = null; #if UNITY_WEBPLAYER HttpWebRequest request = (HttpWebRequest)WebRequest.Create(vecFieldName); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. sr = new StreamReader(dataStream); #else FileInfo file = new FileInfo(vecFieldName); sr = file.OpenText(); #endif string line = " "; // int ind = 0; for (int i = 0; i < _grid.GetLength(0); i++) { for (int j = 0; j < _grid.GetLength(1); j++) { for (int k = 0; k < _grid.GetLength(2); k++) { line = sr.ReadLine(); string[] vals = line.Split(' '); _grid[i, j, k] = new Vector3(float.Parse(vals[0]), float.Parse(vals[1]), float.Parse(vals[2])); // _lines[ind] = new GameObject(); //Debug.Log(_grid[i,j,k]); // ind ++; } } } }
public Vector3 Sample(Vector3 location) { var sampleLocation = location + _center; var sample = _field[ (int)Utilities.Clamp(sampleLocation.X, 0, _field.GetLength(0) - 1), (int)Utilities.Clamp(sampleLocation.Y, 0, _field.GetLength(1) - 1), (int)Utilities.Clamp(sampleLocation.Z, 0, _field.GetLength(2) - 1) ]; return(sample); }
// Exports the volume in raw data, can export the gradient as well public void Export(string path, bool includeGradient = true) { // get the max mand min values used for mapping values to fit in a single byte float max = Max; float min = Min; int reduced = Size - 2; int outputSize = reduced * reduced * reduced; Vector3[,,] gradient = null; float gradientDominant = 0; // calculate the gradient and get the maximum absolute value for mapping purposes if (includeGradient) { outputSize *= 4; gradient = Utils.SobelGradient(Data); float gradientMin = gradient[0, 0, 0].X; float gradientMax = gradient[0, 0, 0].X; for (int k = 0; k < gradient.GetLength(2); k++) { for (int j = 0; j < gradient.GetLength(1); j++) { for (int i = 0; i < gradient.GetLength(0); i++) { Vector3 direction = gradient[i, j, k]; if (direction.X < gradientMin) { gradientMin = direction.X; } if (direction.X > gradientMax) { gradientMax = direction.X; } if (direction.Y < gradientMin) { gradientMin = direction.Y; } if (direction.Y > gradientMax) { gradientMax = direction.Y; } if (direction.Z < gradientMin) { gradientMin = direction.Z; } if (direction.Z > gradientMax) { gradientMax = direction.Z; } } } } gradientDominant = Math.Max(Math.Abs(gradientMin), Math.Abs(gradientMax)); } // sequentially map the volume and gradient data to fit into a singel byte byte[] output = new byte[outputSize]; for (int k = 1; k < Size - 1; k++) { for (int j = 1; j < Size - 1; j++) { for (int i = 1; i < Size - 1; i++) { int outputIndex = (i - 1) + reduced * ((j - 1) + reduced * (k - 1)); if (includeGradient) { outputIndex *= 4; output[outputIndex + 1] = (byte)(int)Math.Round(Utils.Map(gradient[i - 1, j - 1, k - 1].X, -gradientDominant, gradientDominant, 0, 255)); output[outputIndex + 2] = (byte)(int)Math.Round(Utils.Map(gradient[i - 1, j - 1, k - 1].Y, -gradientDominant, gradientDominant, 0, 255)); output[outputIndex + 3] = (byte)(int)Math.Round(Utils.Map(gradient[i - 1, j - 1, k - 1].Z, -gradientDominant, gradientDominant, 0, 255)); } output[outputIndex] = (byte)(int)Math.Round(Utils.Map(Data[i, j, k], min, max, 0, 255)); } } } // weite byte array to file File.WriteAllBytes(path, output); }
public void OnDrawGizmos() { if (field == null) { Start(); } for (int i = 0; i < field.GetLength(0); i++) { for (int j = 0; j < field.GetLength(1); j++) { for (int k = 0; k < field.GetLength(2); k++) { Vector3 coordinate = new Vector3( dimension.x * ((float)i / ((float)field.GetLength(0) - 1)), dimension.y * ((float)j / ((float)field.GetLength(1) - 1)), dimension.z * ((float)k / ((float)field.GetLength(2) - 1))); Gizmos.DrawLine(transform.TransformPoint(coordinate), transform.TransformPoint(coordinate + field[i, j, k])); } } } }
public void init(int camera_count, int dp_count, int bone_count, int camera_width_pixel, int camera_height_pixel) { last_diff_frames = new Texture2D[camera_count]; current_diff_frames = new Texture2D[camera_count]; movement = new Vector3[camera_count, camera_width_pixel, camera_height_pixel]; for (int i = 0; i < movement.GetLength(0); i++) { for (int j = 0; j < movement.GetLength(1); j++) { for (int k = 0; k < movement.GetLength(2); k++) { movement[i, j, k] = new Vector3(); } } } dp3D = new Vector3[dp_count]; for (int i = 0; i < dp3D.GetLength(0); i++) { dp3D[i] = new Vector3(); } dp_on_project_plane = new Vector3[camera_count, dp_count]; for (int i = 0; i < dp_on_project_plane.GetLength(0); i++) { for (int j = 0; j < dp_on_project_plane.GetLength(1); j++) { dp_on_project_plane[i, j] = new Vector3(); } } dp_normalized = new Vector3[camera_count, dp_count]; for (int i = 0; i < dp_normalized.GetLength(0); i++) { for (int j = 0; j < dp_normalized.GetLength(1); j++) { dp_normalized[i, j] = new Vector3(); } } dp_on_texture = new Vector3[camera_count, dp_count]; for (int i = 0; i < dp_on_texture.GetLength(0); i++) { for (int j = 0; j < dp_on_texture.GetLength(1); j++) { dp_on_texture[i, j] = new Vector3(); } } //====================== dp_radius = new float[dp_count]; dp_radius_on_plane = new float[camera_count, dp_count]; dp_radius_on_texture = new int[camera_count, dp_count]; //====================== dp_2D = new DetectingPoint2D[camera_count, dp_count]; for (int i = 0; i < dp_2D.GetLength(0); i++) { for (int j = 0; j < dp_2D.GetLength(1); j++) { dp_2D[i, j] = new DetectingPoint2D(); } } dp_movement = new Vector3[camera_count, dp_count]; for (int i = 0; i < dp_movement.GetLength(0); i++) { for (int j = 0; j < dp_movement.GetLength(1); j++) { dp_movement[i, j] = new Vector3(); } } dp_movement_3D = new Vector3[dp_count]; for (int i = 0; i < dp_movement.GetLength(0); i++) { dp_movement_3D[i] = new Vector3(); } //========================= bone_movement = new Vector3[bone_count]; for (int i = 0; i < dp_movement.GetLength(0); i++) { dp_movement_3D[i] = new Vector3(); } }
//raycasting and information on visual gathering public void FillVisibilityGrid() //fills the visibility grid with the qualities of view from this camera { //getting coordinates of all ground projections - Floor must have boxcollider Vector3[,,] projectedRays = ProjectRaysFromCamera(); Bounds map = GameObject.Find("Floor").GetComponent <BoxCollider>().bounds; Bounds mapVolume = GameObject.Find("Map").GetComponent <MapController>().mapBounds; Vector3[,] onTheGroundProjections = new Vector3[projectedRays.GetLength(0), projectedRays.GetLength(1)]; for (int i = 0; i < onTheGroundProjections.GetLength(0); i++) { for (int j = 0; j < onTheGroundProjections.GetLength(1); j++) { if (mapVolume.Contains(projectedRays[i, j, 0])) { //Debug.Log(projectedRays[i, j, 0]); onTheGroundProjections[i, j] = map.ClosestPoint(projectedRays[i, j, 0]); } else { onTheGroundProjections[i, j] = Vector3.zero; } } } Cell[,] grid = GameObject.Find("Map").GetComponent <GridController>().observationGrid; //declaring a fictious grid and initializing it, for quality of view of this camera float[,] proposedGrid = new float[grid.GetLength(0), grid.GetLength(1)]; for (int i = 0; i < proposedGrid.GetLength(0); i++) { for (int j = 0; j < proposedGrid.GetLength(1); j++) { proposedGrid[i, j] = 0f; } } GridController pointer = GameObject.Find("Map").GetComponent <GridController>(); //substituting each cell which has a higher QoV from the fictious grid to the actual grid for (int p1 = 0; p1 < onTheGroundProjections.GetLength(0); p1++) { for (int p2 = 0; p2 < onTheGroundProjections.GetLength(1); p2++) { for (int i = 0; i < proposedGrid.GetLength(0); i++) { for (int j = 0; j < proposedGrid.GetLength(1); j++) { if (onTheGroundProjections[p1, p2] != Vector3.zero & grid[i, j].Contains(onTheGroundProjections[p1, p2])) { //proposedGrid[i, j] += SpatialConfidence(projectedRays[p1, p2, 0]); //update the timeConfidenceGridNewObs pointer.UpdateTimeConfidenceGridNewObs(i, j); pointer.UpdateSpatialConfidenceGridNewObs(i, j, SpatialConfidence(projectedRays[p1, p2, 0])); //Debug.Log(onTheGroundProjections[p1, p2]); //Debug.Log(projectedRays[i, j, 1]); if (projectedRays[p1, p2, 1].magnitude > 1) { pointer.CountObservationGridNewObs(i, j); //Debug.Log(i + "," + j); //Debug.Log(projectedRays[i, j, 1].z); } } if (p1 == (onTheGroundProjections.GetLength(0) - 1) && p2 == (onTheGroundProjections.GetLength(1) - 1)) //when I finished projecting every possible projection in this cell set the qualityofview of the grid to be the highest { //Debug.Log(onTheGroundProjections[p1, p2]); if (grid[i, j].value < proposedGrid[i, j]) { grid[i, j].value = proposedGrid[i, j]; } } } } } } }