Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<Quad> quads = new List<Quad>();
- //pseudocode:
- foreach (Face Direction of block)
- {
- quads.Add(CreateFace(position, Face Direction));
- }
- PlanetBlockGeometry newBlock = new PlanetBlockGeometry(position, quads, type, GetCenterOfBlock(position));
- //CreateFace method
- private Quad CreateFace(Coordinate position, FACING_DIRECTION direction)
- {
- #region get offset from face direction
- Coordinate[] offsets = new Coordinate[4];
- switch (direction)
- {
- case FACING_DIRECTION.BOTTOM:
- {
- offsets[0] = new Coordinate(0, 0, 0, 1);
- offsets[1] = new Coordinate(0, 0, 0, 0);
- offsets[2] = new Coordinate(0, 1, 0, 0);
- offsets[3] = new Coordinate(0, 1, 0, 1);
- break;
- }
- case FACING_DIRECTION.LEFT:
- {
- offsets[0] = new Coordinate(0, 0, 1, 0);
- offsets[1] = new Coordinate(0, 0, 1, 1);
- offsets[2] = new Coordinate(0, 0, 0, 1);
- offsets[3] = new Coordinate(0, 0, 0, 0);
- break;
- }
- case FACING_DIRECTION.TOP:
- {
- offsets[0] = new Coordinate(0, 0, 1, 0);
- offsets[1] = new Coordinate(0, 1, 1, 0);
- offsets[2] = new Coordinate(0, 1, 1, 1);
- offsets[3] = new Coordinate(0, 0, 1, 1);
- break;
- }
- case FACING_DIRECTION.BACK:
- {
- offsets[0] = new Coordinate(0, 0, 1, 0);
- offsets[1] = new Coordinate(0, 1, 1, 0);
- offsets[2] = new Coordinate(0, 1, 0, 0);
- offsets[3] = new Coordinate(0, 0, 0, 0);
- break;
- }
- case FACING_DIRECTION.FRONT:
- {
- offsets[0] = new Coordinate(0, 0, 1, 1);
- offsets[1] = new Coordinate(0, 1, 1, 1);
- offsets[2] = new Coordinate(0, 1, 0, 1);
- offsets[3] = new Coordinate(0, 0, 0, 1);
- break;
- }
- case FACING_DIRECTION.RIGHT:
- {
- offsets[0] = new Coordinate(0, 1, 1, 1);
- offsets[1] = new Coordinate(0, 1, 1, 0);
- offsets[2] = new Coordinate(0, 1, 0, 0);
- offsets[3] = new Coordinate(0, 1, 0, 1);
- break;
- }
- }
- #endregion
- Vector3 correctedTopLeftPosition = GetCorrectedCubePosition(position + offsets[0]);
- Vector3 topLeftPosition = MapToProjected(correctedTopLeftPosition, (position.Shell + offsets[0].Shell));
- Vector3 correctedTopRightPosition = GetCorrectedCubePosition(position + offsets[1]);
- Vector3 topRightPosition = MapToProjected(correctedTopRightPosition, (position.Shell + offsets[1].Shell));
- Vector3 correctedBottomRightPosition = GetCorrectedCubePosition(position + offsets[2]);
- Vector3 bottomRightPosition = MapToProjected(correctedBottomRightPosition, (position.Shell + offsets[2].Shell));
- Vector3 correctedBottomLeftPosition = GetCorrectedCubePosition(position + offsets[3]);
- Vector3 bottomLeftPosition = MapToProjected(correctedBottomLeftPosition, (position.Shell + offsets[3].Shell));
- VertexPositionNormalTexture[] corners = new VertexPositionNormalTexture[4]
- {
- new VertexPositionNormalTexture(topLeftPosition, Vector3.Zero, Vector2.Zero),
- new VertexPositionNormalTexture(topRightPosition, Vector3.Zero, Vector2.Zero),
- new VertexPositionNormalTexture(bottomRightPosition, Vector3.Zero, Vector2.Zero),
- new VertexPositionNormalTexture(bottomLeftPosition, Vector3.Zero, Vector2.Zero)
- };
- Quad quad = new Quad(corners, direction);
- return quad;
- }
- //GetCorrectedCubePosition method
- private Vector3 GetCorrectedCubePosition(Coordinate position)
- {
- Vector3 positionOnCube = new Vector3(position.X, position.Y, 0);
- positionOnCube -= new Vector3(planet.BlockMap.FaceWidth / 2, planet.BlockMap.FaceWidth / 2, planet.BlockMap.FaceWidth / 2);
- //adjust for positive and negative values
- if (position.Face == PlanetBlockMap.SOUTH | position.Face == PlanetBlockMap.BACK | position.Face == PlanetBlockMap.WEST) //negative Y
- {
- positionOnCube.Z *= -1;
- }
- //rotate the whole vector to suit the direction this face is facing
- Vector3 rotatedPosition = Vector3.Zero;
- if (position.Face == 0 | position.Face == 1)
- {
- rotatedPosition.X = positionOnCube.X;
- rotatedPosition.Y = positionOnCube.Z;
- rotatedPosition.Z = positionOnCube.Y;
- }
- else if (position.Face == 2 | position.Face == 3)
- {
- rotatedPosition = positionOnCube;
- }
- else if (position.Face == 4 | position.Face == 5)
- {
- rotatedPosition.X = positionOnCube.Z;
- rotatedPosition.Y = positionOnCube.Y;
- rotatedPosition.Z = positionOnCube.X;
- }
- rotatedPosition *= (1f / planet.BlockMap.FaceWidth);
- return rotatedPosition;
- }
- //MapToProjected method
- private Vector3 MapToProjected(Vector3 positionOnCube, float radiusOfSphere)
- {
- Vector3 normal = positionOnCube;
- normal.Normalize();
- normal *= radiusOfSphere;
- return normal;
- }
- //GetCenterOfBlock Method
- private Vector3 GetCenterOfBlock(Coordinate position)
- {
- Vector3[] corners = new Vector3[8];
- corners[0] = ProjectFromCoordinate(position);
- corners[1] = ProjectFromCoordinate(position + new Coordinate(0, 0, 0, 1));
- corners[2] = ProjectFromCoordinate(position + new Coordinate(0, 0, 1, 0));
- corners[3] = ProjectFromCoordinate(position + new Coordinate(0, 1, 0, 0));
- corners[4] = ProjectFromCoordinate(position + new Coordinate(0, 1, 1, 0));
- corners[5] = ProjectFromCoordinate(position + new Coordinate(0, 1, 0, 1));
- corners[6] = ProjectFromCoordinate(position + new Coordinate(0, 0, 1, 1));
- corners[7] = ProjectFromCoordinate(position + new Coordinate(0, 1, 1, 1));
- return (AverageVector3(corners));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement