void BuildVoxelWorld() { var baseHeight = (int)MapboxConvenience.GetRelativeHeightFromColor((_elevationTexture.GetPixel(_elevationTexture.width / 2, _elevationTexture.height / 2)), 1 / _elevationMultiplier); for (int x = 0; x < _rasterTexture.width; x++) { for (int z = 0; z < _rasterTexture.height; z++) { var height = (int)MapboxConvenience.GetRelativeHeightFromColor(_elevationTexture.GetPixel(x, z), 1 / _elevationMultiplier) - baseHeight; height = (int)(height * _tileScale); var startHeight = height - _voxelDepthPadding - 1; var color = _rasterTexture.GetPixel(x, z); for (int y = startHeight; y < height; y++) { _voxels.Add(new VoxelData() { Position = new Vector3(x, y, z), Prefab = _voxelFetcher.GetVoxelFromColor(color) }); } } } // TODO: decouple me! _player.transform.position = new Vector3(_tileWidthInVoxels * .5f, 2f, _tileWidthInVoxels * .5f); StartCoroutine(BuildRoutine()); var cc = _player.GetComponent <CharacterController>(); if (cc) { cc.enabled = true; } }
void ExtrudeHeight() { // FIXME: what does this number represent? var relativeScale = TileScaleInMeters / 256 * MapboxConvenience.Instance.TileScale / transform.localScale.x; var elevationTexture = new Texture2D(0, 0); elevationTexture.LoadImage(_elevation.Data); // HACK: UV's are "flipped" on Unity plane. Undo that flip. var flip = Quaternion.Euler(0, 180, 0); var pixelsPerMeter = 256f / MapboxConvenience.Instance.TileScale; var newVertices = new List <Vector3>(); // TODO: optimize with GetPixels() foreach (var vert in _vertices) { var localVert = flip * vert; // Account for local scale. localVert *= transform.localScale.x; var x = (int)Mathf.Clamp((localVert.x * pixelsPerMeter + 128), 0, 255); var y = (int)Mathf.Clamp((localVert.z * pixelsPerMeter + 128), 0, 255); var color = elevationTexture.GetPixel(x, y); var newVert = vert; newVert.y = MapboxConvenience.GetRelativeHeightFromColor(color, relativeScale); newVertices.Add(newVert); } _meshFilter.mesh.SetVertices(newVertices); var collider = GetComponent <MeshCollider>(); if (collider) { collider.sharedMesh = _meshFilter.mesh; } // This fixes rendering bugs if vertices are too far offset from their original locations. _meshFilter.mesh.RecalculateBounds(); }