public void Initialize() { ClearCubes(); if (cubeCubes != null) { foreach (var cube in cubeCubes) { allCubes[cube.indexes.x, cube.indexes.y, cube.indexes.z] = cube; } } chunks = new Dictionary <string, Chunk>(); if (chunkVectors != null && chunkChunks != null) { for (var i = 0; i < chunkVectors.Count; ++i) { var chunk = chunkChunks[i]; chunk.cubeObject = this; chunks[(chunkVectors[i]).ToString()] = chunk; } } if (cubeLegend == null) { cubeLegend = new CubeLegend(); } cubeLegend.Initialize(); if (material == null) { material = new Material(Shader.Find("Diffuse")); } }
public CubeMesh Calculate(ref int visualVertexCount, ref int collisionVertexCount, Cube[,,] cubes, CubeLegend cubeLegend, ColliderType colliderType, string cubeTag) { // TODO: Put this back in when preprocessor directives are supported in Boo // Use UNITY_EDITOR //CubeGeneratorProgressEditor.ReportCube(chunk.gridPosition, gridPosition) if chunk // TODO: Vector3i.zero // TODO: Cached cube position of the chunk var position = (indexes - (chunk.dimensionsInCubes * chunk.gridPosition)).ToVector3() * cubeSize; var meshData = new CubeMesh(); if(!AdjacentCubeExistsInsideChunk(cubes, indexes.Down)) AddSide(Direction.Down, position, ref visualVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunk(cubes, indexes.Up)) AddSide(Direction.Up, position, ref visualVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunk(cubes, indexes.Right)) AddSide(Direction.Right, position, ref visualVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunk(cubes, indexes.Left)) AddSide(Direction.Left, position, ref visualVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunk(cubes, indexes.Front)) AddSide(Direction.Front, position, ref visualVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunk(cubes, indexes.Back)) AddSide(Direction.Back, position, ref visualVertexCount, cubeLegend, meshData); if (cubeLegend.cubeDefinitions[type].hasCollision) { if(colliderType == ColliderType.MeshColliderPerChunk) { if(!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Down)) AddCollisionSide(Direction.Down, position, ref collisionVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Up)) AddCollisionSide(Direction.Up, position, ref collisionVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Right)) AddCollisionSide(Direction.Right, position, ref collisionVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Left)) AddCollisionSide(Direction.Left, position, ref collisionVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Front)) AddCollisionSide(Direction.Front, position, ref collisionVertexCount, cubeLegend, meshData); if(!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Back)) AddCollisionSide(Direction.Back, position, ref collisionVertexCount, cubeLegend, meshData); } else if(colliderType == ColliderType.BoxColliderPerCube) { // TODO: Defer this until the game objects are being created for async compatibility // if(generateCollider) CreateCollision(cubeTag); } } return meshData; }
public CubeMesh AddSide(Direction side, Vector3 position, ref int vertexCount, CubeLegend cubeLegend, CubeMesh meshData) { var vertices = CalculateSideVertices(position, side); meshData.RenderableVertices.AddRange(vertices); meshData.RenderableUvs.AddRange(cubeLegend.UvsFor(type, side)); AddTriangles(cubeLegend, meshData, vertexCount); vertexCount += 4; return(meshData); }
/// <summary> /// Clones from another CubedObjectBehavior. This can be helpful for generators that need to run in threads and /// combine their results later. /// </summary> /// <param name='other'> /// The CubedObjectBehavior to clone from. /// </param> public void CloneFrom(CubedObjectBehaviour other) { allCubes = new Cube[other.TotalDimensions.x, other.TotalDimensions.y, other.TotalDimensions.z]; System.Array.Copy(other.allCubes, allCubes, other.allCubes.Length); material = other.material; chunkDimensions = other.chunkDimensions.Clone(); dimensionsInChunks = other.dimensionsInChunks.Clone(); colliderType = other.colliderType; cubeLegend = other.cubeLegend.Clone(); cubeSize = other.cubeSize; // TODO: Add more things to copy over. }
public void Initialize() { ClearCubes(); if (cubeCubes != null) { foreach (var cube in cubeCubes) { allCubes[cube.indexes.x, cube.indexes.y, cube.indexes.z] = cube; } } chunks = new Dictionary<string, Chunk>(); if (chunkVectors != null && chunkChunks != null) { for (var i = 0; i < chunkVectors.Count; ++i) { var chunk = chunkChunks[i]; chunk.cubeObject = this; chunks[(chunkVectors[i]).ToString()] = chunk; } } if(cubeLegend == null) cubeLegend = new CubeLegend(); cubeLegend.Initialize(); if(material == null) material = new Material(Shader.Find("Diffuse")); }
public CubeMesh AddCollisionSide(Direction side, Vector3 position, ref int vertexCount, CubeLegend cubeLegend, CubeMesh meshData) { var vertices = CalculateSideVertices(position, side); meshData.CollidableVertices.AddRange(vertices); AddCollisionTriangles(meshData, vertexCount); generateCollider = true; vertexCount += 4; return meshData; }
public CubeMesh AddSide(Direction side, Vector3 position, ref int vertexCount, CubeLegend cubeLegend, CubeMesh meshData) { var vertices = CalculateSideVertices(position, side); meshData.RenderableVertices.AddRange(vertices); meshData.RenderableUvs.AddRange(cubeLegend.UvsFor(type, side)); AddTriangles(cubeLegend, meshData, vertexCount); vertexCount += 4; return meshData; }
public bool AdjacentCubeExistsInsideChunkWithCollision(Cube[,,] cubes, CubeLegend legend, Vector3i adjacentPosition) { var cube = GetCubeInChunk(cubes, adjacentPosition); return cube != null && legend.cubeDefinitions[cube.type].hasCollision; }
void AddTriangles(CubeLegend cubeLegend, CubeMesh meshData, int vertexCount) { var newTriangles = baseTriangles.Select(i => i + vertexCount); meshData.RenderableTriangles.AddRange(newTriangles); }
public CubeMesh Calculate(ref int visualVertexCount, ref int collisionVertexCount, Cube[,,] cubes, CubeLegend cubeLegend, ColliderType colliderType, string cubeTag) { // TODO: Put this back in when preprocessor directives are supported in Boo // Use UNITY_EDITOR //CubeGeneratorProgressEditor.ReportCube(chunk.gridPosition, gridPosition) if chunk // TODO: Vector3i.zero // TODO: Cached cube position of the chunk var position = (indexes - (chunk.dimensionsInCubes * chunk.gridPosition)).ToVector3() * cubeSize; var meshData = new CubeMesh(); if (!AdjacentCubeExistsInsideChunk(cubes, indexes.Down)) { AddSide(Direction.Down, position, ref visualVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunk(cubes, indexes.Up)) { AddSide(Direction.Up, position, ref visualVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunk(cubes, indexes.Right)) { AddSide(Direction.Right, position, ref visualVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunk(cubes, indexes.Left)) { AddSide(Direction.Left, position, ref visualVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunk(cubes, indexes.Front)) { AddSide(Direction.Front, position, ref visualVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunk(cubes, indexes.Back)) { AddSide(Direction.Back, position, ref visualVertexCount, cubeLegend, meshData); } if (cubeLegend.cubeDefinitions[type].hasCollision) { if (colliderType == ColliderType.MeshColliderPerChunk) { if (!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Down)) { AddCollisionSide(Direction.Down, position, ref collisionVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Up)) { AddCollisionSide(Direction.Up, position, ref collisionVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Right)) { AddCollisionSide(Direction.Right, position, ref collisionVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Left)) { AddCollisionSide(Direction.Left, position, ref collisionVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Front)) { AddCollisionSide(Direction.Front, position, ref collisionVertexCount, cubeLegend, meshData); } if (!AdjacentCubeExistsInsideChunkWithCollision(cubes, cubeLegend, indexes.Back)) { AddCollisionSide(Direction.Back, position, ref collisionVertexCount, cubeLegend, meshData); } } else if (colliderType == ColliderType.BoxColliderPerCube) { // TODO: Defer this until the game objects are being created for async compatibility // if(generateCollider) CreateCollision(cubeTag); } } return(meshData); }
public bool AdjacentCubeExistsInsideChunkWithCollision(Cube[,,] cubes, CubeLegend legend, Vector3i adjacentPosition) { var cube = GetCubeInChunk(cubes, adjacentPosition); return(cube != null && legend.cubeDefinitions[cube.type].hasCollision); }
public CubeMesh AddCollisionSide(Direction side, Vector3 position, ref int vertexCount, CubeLegend cubeLegend, CubeMesh meshData) { var vertices = CalculateSideVertices(position, side); meshData.CollidableVertices.AddRange(vertices); AddCollisionTriangles(meshData, vertexCount); generateCollider = true; vertexCount += 4; return(meshData); }