private GeometryData CreateTilemapData() { var tilesX = GetData<int>("tilesX"); var tilesY = GetData<int>("tilesY"); var tileElementSize = GetData<int>("tileElementSize"); var diffuseTexture = _drawableElement.Material.DiffuseTexture; var tilesetElementsX = diffuseTexture.Width / tileElementSize; var geometryData = new GeometryData { Vertices = new VertexPositionNormalTexture[tilesX*tilesY*4], Indices = new ushort[tilesX*tilesY*6] }; var tiles = GetData<string>("map") .Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse).ToArray(); var texturePartSize = diffuseTexture.GetTexcoordsFromPixelCoords(tileElementSize, tileElementSize); var vertexBaseIndex = 0; var indexBaseIndex = 0; for (var i = 0; i < Math.Min(tiles.Length, tilesX*tilesY); i++) { var element = tiles[i]; if (element == 0) continue; var tilesetX = (element-1)%tilesetElementsX; var tilesetY = (element - 1) / tilesetElementsX; var mapX = i%tilesX; var mapY = i/tilesX; var position = new Vector3(mapX, 0.0f, mapY); var texcoords = diffuseTexture.GetTexcoordsFromPixelCoords(tilesetX* tileElementSize, tilesetY* tileElementSize); var normal = Vector3.UnitY; geometryData.Vertices[vertexBaseIndex+0] = new VertexPositionNormalTexture(position + new Vector3(0,0,-1), normal, texcoords + new Vector2(0, texturePartSize.Y)); geometryData.Vertices[vertexBaseIndex+1] = new VertexPositionNormalTexture(position + new Vector3(0,0, 0), normal, texcoords + new Vector2(0,0)); geometryData.Vertices[vertexBaseIndex+2] = new VertexPositionNormalTexture(position + new Vector3(1,0, -1), normal, texcoords + new Vector2(texturePartSize.X, texturePartSize.Y)); geometryData.Vertices[vertexBaseIndex+3] = new VertexPositionNormalTexture(position + new Vector3(1, 0, 0), normal, texcoords + new Vector2(texturePartSize.X, 0)); geometryData.Indices[indexBaseIndex + 0] = (ushort) vertexBaseIndex; geometryData.Indices[indexBaseIndex + 1] = (ushort) (vertexBaseIndex+2); geometryData.Indices[indexBaseIndex + 2] = (ushort) (vertexBaseIndex+1); geometryData.Indices[indexBaseIndex + 3] = (ushort) (vertexBaseIndex+1); geometryData.Indices[indexBaseIndex + 4] = (ushort) (vertexBaseIndex+2); geometryData.Indices[indexBaseIndex + 5] = (ushort) (vertexBaseIndex+3); vertexBaseIndex += 4; indexBaseIndex += 6; } return geometryData; }
internal Mesh Clone() { var geometryData = new GeometryData { Vertices = new VertexPositionNormalTexture[VertexCount], Indices = new ushort[IndexCount], }; _vertexBuffer.GetData(geometryData.Vertices); _indexBuffer.GetData(geometryData.Indices); return(new Mesh(_vertexBuffer.GraphicsDevice, geometryData)); }
public Mesh(GraphicsDevice device, GeometryData data, PrimitiveType primitiveType = PrimitiveType.TriangleList, bool holdGeometryData = true) { _primitiveType = primitiveType; GeometryData = holdGeometryData ? data : null; VertexCount = data.Vertices.Length; IndexCount = data.Indices.Length; _vertexBuffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, VertexCount, BufferUsage.WriteOnly); _indexBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, IndexCount, BufferUsage.WriteOnly); _vertexBuffer.SetData(data.Vertices); _indexBuffer.SetData(data.Indices); var min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); var max = new Vector3(float.MinValue, float.MinValue, float.MinValue); foreach (var vertex in data.Vertices) { min.X = MathHelper.Min(min.X, vertex.Position.X); min.Y = MathHelper.Min(min.Y, vertex.Position.Y); min.Z = MathHelper.Min(min.Z, vertex.Position.Z); max.X = MathHelper.Max(max.X, vertex.Position.X); max.Y = MathHelper.Max(max.Y, vertex.Position.Y); max.Z = MathHelper.Max(max.Z, vertex.Position.Z); } LocalBounds = new BoundingBox(min, max); switch (primitiveType) { case PrimitiveType.TriangleList: _primitiveCount = IndexCount / 3; break; case PrimitiveType.TriangleStrip: break; case PrimitiveType.LineList: _primitiveCount = IndexCount / 2; break; case PrimitiveType.LineStrip: break; default: throw new ArgumentOutOfRangeException(nameof(primitiveType), primitiveType, null); } InstanceCount++; }
private static ModelMesh LoadPMesh(GameMode gameMode, string filePath) { using (var fileStream = new FileStream(filePath, FileMode.Open)) { using (var binaryReader = new BinaryReader(fileStream)) { var vertexCount = binaryReader.ReadInt32(); var indicesCount = binaryReader.ReadInt32(); var textureName = binaryReader.ReadString(); var textureFilePath = Path.Combine(Path.GetDirectoryName(filePath) ?? "", Path.GetFileName(textureName) ?? ""); Texture2D texture = null; if (File.Exists(textureFilePath)) { texture = gameMode.GetTextureFromRawFolder(textureFilePath); } var geometryData = new GeometryData { Vertices = new VertexPositionNormalTexture[vertexCount], Indices = new ushort[indicesCount] }; for (var i = 0; i < vertexCount; i++) { var position = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()); var normal = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()); var uv = new Vector2(binaryReader.ReadSingle(), binaryReader.ReadSingle()); geometryData.Vertices[i] = new VertexPositionNormalTexture(position, normal, uv); } for(var i = 0; i < indicesCount; i++) { geometryData.Indices[i] = (ushort) binaryReader.ReadInt32(); } return new ModelMesh { Mesh = new Mesh(gameMode.GraphicsDevice, geometryData, PrimitiveType.TriangleList, false), Material = new Material { DiffuseTexture = texture } }; } } }
public Mesh(GraphicsDevice device, GeometryData data, PrimitiveType primitiveType = PrimitiveType.TriangleList, bool holdGeometryData = true) { _primitiveType = primitiveType; GeometryData = holdGeometryData ? data : null; VertexCount = data.Vertices.Length; IndexCount = data.Indices.Length; _vertexBuffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, VertexCount, BufferUsage.WriteOnly); _indexBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, IndexCount, BufferUsage.WriteOnly); _vertexBuffer.SetData(data.Vertices); _indexBuffer.SetData(data.Indices); var min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); var max = new Vector3(float.MinValue, float.MinValue, float.MinValue); foreach (var vertex in data.Vertices) { min.X = MathHelper.Min(min.X, vertex.Position.X); min.Y = MathHelper.Min(min.Y, vertex.Position.Y); min.Z = MathHelper.Min(min.Z, vertex.Position.Z); max.X = MathHelper.Max(max.X, vertex.Position.X); max.Y = MathHelper.Max(max.Y, vertex.Position.Y); max.Z = MathHelper.Max(max.Z, vertex.Position.Z); } LocalBounds = new BoundingBox(min, max); switch (primitiveType) { case PrimitiveType.TriangleList: _primitiveCount = IndexCount/3; break; case PrimitiveType.TriangleStrip: break; case PrimitiveType.LineList: _primitiveCount = IndexCount/2; break; case PrimitiveType.LineStrip: break; default: throw new ArgumentOutOfRangeException(nameof(primitiveType), primitiveType, null); } InstanceCount++; }
public CollisionManager() { _allColliders = new List<Collider>(); _allTriggers = new List<Collider>(); _allTriggersAndColliders = new List<Collider>(); var geometryBox = new GeometryData { Vertices = new[] { new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), Vector3.Zero, Vector2.Zero), new VertexPositionNormalTexture(new Vector3( 0.5f, -0.5f, -0.5f), Vector3.Zero, Vector2.Zero), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), Vector3.Zero, Vector2.Zero), new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), Vector3.Zero, Vector2.Zero), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Zero, Vector2.Zero), new VertexPositionNormalTexture(new Vector3( 0.5f, 0.5f, -0.5f), Vector3.Zero, Vector2.Zero), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), Vector3.Zero, Vector2.Zero), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), Vector3.Zero, Vector2.Zero), }, Indices = new ushort[] { 0,1,1,2,2,3,3,0, 4,5,5,6,6,7,7,4, 0,4,1,5,2,6,3,7 } }; _boundingBoxMesh = new Mesh(GameInstance.GraphicsDevice, geometryBox, PrimitiveType.LineList, false) { PreventDrawCallCount = true }; _lineDrawEffect = GameInstance.Content.Load<Effect>(ResourceNames.Effects.DebugShadowMap); _worldViewProjection = _lineDrawEffect.Parameters["WorldViewProjection"]; _modulateColor = _lineDrawEffect.Parameters["Color"]; _lineTechnique = _lineDrawEffect.Techniques["LineDraw"]; }
internal Mesh Clone() { var geometryData = new GeometryData { Vertices = new VertexPositionNormalTexture[VertexCount], Indices = new ushort[IndexCount], }; _vertexBuffer.GetData(geometryData.Vertices); _indexBuffer.GetData(geometryData.Indices); return new Mesh(_vertexBuffer.GraphicsDevice, geometryData); }
private static GeometryData GenerateGeometryDataFromAssimpMesh(Assimp.Mesh mesh) { var geometryData = new GeometryData { Vertices = new VertexPositionNormalTexture[mesh.VertexCount], Indices = new ushort[mesh.FaceCount * 3] }; geometryData.Vertices = new VertexPositionNormalTexture[mesh.VertexCount]; for (var i = 0; i < mesh.VertexCount; i++) { var vertex = mesh.Vertices[i]; geometryData.Vertices[i].Position = new Vector3(vertex.X, vertex.Y, vertex.Z); var normal = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); geometryData.Vertices[i].Normal = new Vector3(normal.X, normal.Y, normal.Z); var texcoord = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0][i] : new Vector3D(); geometryData.Vertices[i].TextureCoordinate = new Vector2(texcoord.X, texcoord.Y); } for (var i = 0; i < mesh.FaceCount; i++) { geometryData.Indices[i * 3 + 0] = (ushort)mesh.Faces[i].Indices[0]; geometryData.Indices[i * 3 + 1] = (ushort)mesh.Faces[i].Indices[1]; geometryData.Indices[i * 3 + 2] = (ushort)mesh.Faces[i].Indices[2]; } return geometryData; }
/// <summary> /// Generates Cube with optional texcoord offset. /// </summary> /// <param name="offsetAndScaleSides">Offset then scale for each side in order: front, back, right, left, top, bottom.</param> /// <returns>Cube</returns> public static GeometryData GenerateCubeData(Vector2[] offsetAndScaleSides = null) { var t = offsetAndScaleSides == null || offsetAndScaleSides.Length < 12 ? DefaultOffsetAndScaleSides : offsetAndScaleSides; var data = new GeometryData { Vertices = new[] { //front new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), Vector3.Backward, t[0] + new Vector2(0.0f, 1.0f) * t[1]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), Vector3.Backward, t[0] + new Vector2(0.0f, 0.0f) * t[1]), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), Vector3.Backward, t[0] + new Vector2(1.0f, 1.0f) * t[1]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), Vector3.Backward, t[0] + new Vector2(1.0f, 0.0f) * t[1]), //back new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), Vector3.Forward, t[2] + new Vector2(0.0f, 1.0f) * t[3]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), Vector3.Forward, t[2] + new Vector2(0.0f, 0.0f) * t[3]), new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), Vector3.Forward, t[2] + new Vector2(1.0f, 1.0f) * t[3]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Forward, t[2] + new Vector2(1.0f, 0.0f) * t[3]), //right new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), Vector3.Right, t[4] + new Vector2(0.0f, 1.0f) * t[5]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), Vector3.Right, t[4] + new Vector2(0.0f, 0.0f) * t[5]), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), Vector3.Right, t[4] + new Vector2(1.0f, 1.0f) * t[5]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), Vector3.Right, t[4] + new Vector2(1.0f, 0.0f) * t[5]), //left new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), Vector3.Left, t[6] + new Vector2(0.0f, 1.0f) * t[7]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Left, t[6] + new Vector2(0.0f, 0.0f) * t[7]), new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), Vector3.Left, t[6] + new Vector2(1.0f, 1.0f) * t[7]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), Vector3.Left, t[6] + new Vector2(1.0f, 0.0f) * t[7]), //top new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), Vector3.Up, t[8] + new Vector2(0.0f, 1.0f) * t[9]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Up, t[8] + new Vector2(0.0f, 0.0f) * t[9]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), Vector3.Up, t[8] + new Vector2(1.0f, 1.0f) * t[9]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), Vector3.Up, t[8] + new Vector2(1.0f, 0.0f) * t[9]), //bottom new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), Vector3.Down, t[10] + new Vector2(0.0f, 1.0f) * t[11]), new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), Vector3.Down, t[10] + new Vector2(0.0f, 0.0f) * t[11]), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), Vector3.Down, t[10] + new Vector2(1.0f, 1.0f) * t[11]), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), Vector3.Down, t[10] + new Vector2(1.0f, 0.0f) * t[11]), }, Indices = new ushort[36] }; for (var i = 0; i < 6; i++) { data.Indices[i * 6 + 0] = (ushort)(i * 4 + 0); data.Indices[i * 6 + 1] = (ushort)(i * 4 + 1); data.Indices[i * 6 + 2] = (ushort)(i * 4 + 2); data.Indices[i * 6 + 3] = (ushort)(i * 4 + 1); data.Indices[i * 6 + 4] = (ushort)(i * 4 + 3); data.Indices[i * 6 + 5] = (ushort)(i * 4 + 2); } return data; }
/// <summary> /// Generates Cube with optional texcoord offset. /// </summary> /// <param name="offsetAndScaleSides">Offset then scale for each side in order: front, back, right, left, top, bottom.</param> /// <returns>Cube</returns> public static GeometryData GenerateCubeData(Vector2[] offsetAndScaleSides = null) { var t = offsetAndScaleSides == null || offsetAndScaleSides.Length < 12 ? DefaultOffsetAndScaleSides : offsetAndScaleSides; var data = new GeometryData { Vertices = new[] { //front new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), Vector3.Backward, t[0] + new Vector2(0.0f, 1.0f) * t[1]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), Vector3.Backward, t[0] + new Vector2(0.0f, 0.0f) * t[1]), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), Vector3.Backward, t[0] + new Vector2(1.0f, 1.0f) * t[1]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), Vector3.Backward, t[0] + new Vector2(1.0f, 0.0f) * t[1]), //back new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), Vector3.Forward, t[2] + new Vector2(0.0f, 1.0f) * t[3]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), Vector3.Forward, t[2] + new Vector2(0.0f, 0.0f) * t[3]), new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), Vector3.Forward, t[2] + new Vector2(1.0f, 1.0f) * t[3]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Forward, t[2] + new Vector2(1.0f, 0.0f) * t[3]), //right new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), Vector3.Right, t[4] + new Vector2(0.0f, 1.0f) * t[5]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), Vector3.Right, t[4] + new Vector2(0.0f, 0.0f) * t[5]), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), Vector3.Right, t[4] + new Vector2(1.0f, 1.0f) * t[5]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), Vector3.Right, t[4] + new Vector2(1.0f, 0.0f) * t[5]), //left new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), Vector3.Left, t[6] + new Vector2(0.0f, 1.0f) * t[7]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Left, t[6] + new Vector2(0.0f, 0.0f) * t[7]), new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), Vector3.Left, t[6] + new Vector2(1.0f, 1.0f) * t[7]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), Vector3.Left, t[6] + new Vector2(1.0f, 0.0f) * t[7]), //top new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), Vector3.Up, t[8] + new Vector2(0.0f, 1.0f) * t[9]), new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Up, t[8] + new Vector2(0.0f, 0.0f) * t[9]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), Vector3.Up, t[8] + new Vector2(1.0f, 1.0f) * t[9]), new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), Vector3.Up, t[8] + new Vector2(1.0f, 0.0f) * t[9]), //bottom new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), Vector3.Down, t[10] + new Vector2(0.0f, 1.0f) * t[11]), new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), Vector3.Down, t[10] + new Vector2(0.0f, 0.0f) * t[11]), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), Vector3.Down, t[10] + new Vector2(1.0f, 1.0f) * t[11]), new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), Vector3.Down, t[10] + new Vector2(1.0f, 0.0f) * t[11]), }, Indices = new ushort[36] }; for (var i = 0; i < 6; i++) { data.Indices[i * 6 + 0] = (ushort)(i * 4 + 0); data.Indices[i * 6 + 1] = (ushort)(i * 4 + 1); data.Indices[i * 6 + 2] = (ushort)(i * 4 + 2); data.Indices[i * 6 + 3] = (ushort)(i * 4 + 1); data.Indices[i * 6 + 4] = (ushort)(i * 4 + 3); data.Indices[i * 6 + 5] = (ushort)(i * 4 + 2); } return(data); }
public void Preload() { var data = FileLoader.GetFiles(new[] { PrimitivesFilePath, NaturesFilePath, TypesFilePath, PokedexesFilePath }); _primitiveModels = DataModel<PrimitiveModel[]>.FromByteArray(data[0].Data); foreach (var primitiveModel in _primitiveModels) { var geometryData = new GeometryData { Vertices = primitiveModel.Vertices.Select(v => new VertexPositionNormalTexture { Position = v.Position.GetVector3(), TextureCoordinate = v.TexCoord.GetVector2(), Normal = v.Normal.GetVector3() }).ToArray(), Indices = primitiveModel.Indices.Select(i => (ushort)i).ToArray() }; Mesh mesh = null; GameContext.EnsureExecutedInMainThread(() => mesh = new Mesh(GraphicsDevice, geometryData)); _meshPrimitivesByName.Add(primitiveModel.Id, mesh); } _natureModels = DataModel<NatureModel[]>.FromByteArray(data[1].Data); _typeModels = DataModel<TypeModel[]>.FromByteArray(data[2].Data); _pokedexModels = DataModel<PokedexModel[]>.FromByteArray(data[3].Data); var movesFilePaths = FileLoader.GetFilesOfFolder(MoveFilesPath); _moveModels = movesFilePaths.Select(d => DataModel<MoveModel>.FromByteArray(d.Data)).ToArray(); var itemsFiles = FileLoader.GetFilesOfFolder(ItemFilesPath); _itemModels = itemsFiles.Select(d => DataModel<ItemModel>.FromByteArray(d.Data)).ToArray(); var abilityFiles = FileLoader.GetFilesOfFolder(AbilityFilesPath); _abilityModels = abilityFiles.Select(d => DataModel<AbilityModel>.FromByteArray(d.Data)).ToArray(); }