/// <summary> /// Creates an Entity that contains our dynamic Vertex and Index buffers. /// This Entity will be rendered by the model renderer. /// </summary> /// <param name="verticesCount"></param> /// <param name="indicesCount"></param> private void CreateTerrainModelEntity(int verticesCount, int indicesCount) { // Compute sizes var vertexDeclaration = VertexNormalTexture.VertexDeclaration; var vertexBufferSize = verticesCount * vertexDeclaration.CalculateSize(); var indexBufferSize = indicesCount * sizeof(int); // Create Vertex and Index buffers terrainVertexBuffer = Buffer.Vertex.New(GraphicsDevice, vertexBufferSize, GraphicsResourceUsage.Dynamic); terrainIndexBuffer = Buffer.New(GraphicsDevice, indexBufferSize, BufferFlags.IndexBuffer, GraphicsResourceUsage.Dynamic); // Prepare mesh and entity var meshDraw = new MeshDraw { PrimitiveType = PrimitiveType.TriangleStrip, VertexBuffers = new[] { new VertexBufferBinding(terrainVertexBuffer, vertexDeclaration, verticesCount) }, IndexBuffer = new IndexBufferBinding(terrainIndexBuffer, true, indicesCount), }; // var effect1 = EffectSystem.LoadEffect("SimpleTerrainEffectMain").WaitForResult(); // var effect2 = EffectSystem.LoadEffect("VertexTextureTerrain"); var effectMaterial = LoadAsset <Material>("mt_rock"); Texture tex0 = (Texture)effectMaterial.Parameters[MaterialKeys.DiffuseMap]; // rock Texture tex1 = (Texture)effectMaterial.Parameters[DiffuseMap1]; // grass Texture tex2 = (Texture)effectMaterial.Parameters[DiffuseMap2]; // water effectMaterial.Parameters.Add(VertexTextureTerrainKeys.MeshTexture0, tex1); effectMaterial.Parameters.Add(VertexTextureTerrainKeys.MeshTexture1, tex0); effectMaterial.Parameters.Add(VertexTextureTerrainKeys.MeshTexture2, tex2); effectMaterial.Parameters.Add(MaterialKeys.DiffuseMap, tex1); //terrainMesh = new Mesh { Draw = meshDraw /*, Material = LoadAsset<Material>("TerrainMaterial")*/ }; terrainMesh = new Mesh { Draw = meshDraw, MaterialIndex = 0 }; //terrainEntity = new Entity { { ModelComponent.Key, new ModelComponent { Model = new Model { terrainMesh } } } }; //TerrainEntity.Add<ModelComponent>( ModelComponent.Key, new ModelComponent { Model = new Model { terrainMesh } } ); TerrainEntity.Add <ModelComponent>(ModelComponent.Key, new ModelComponent { Model = new Model { terrainMesh, effectMaterial } }); }
/// <summary> /// Creates a new StructuredCounter buffer accessible as a <see cref="ShaderResourceView" /> and optionaly as a <see cref="UnorderedAccessView" />. /// </summary> /// <typeparam name="T">Type of the element in the structured buffer</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="count">The number of element in this buffer.</param> /// <returns>A Structured buffer</returns> public static Buffer <T> New <T>(GraphicsDevice device, int count) where T : struct { const BufferFlags BufferFlags = BufferFlags.StructuredCounterBuffer | BufferFlags.ShaderResource | BufferFlags.UnorderedAccess; return(Buffer.New <T>(device, count, BufferFlags)); }
/// <summary> /// Creates a new StructuredAppend buffer accessible as a <see cref="ShaderResourceView" /> and as a <see cref="UnorderedAccessView" />. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="count">The number of element in this buffer.</param> /// <param name="elementSize">Size of the struct.</param> /// <returns>A StructuredAppend buffer</returns> public static Buffer New(GraphicsDevice device, int count, int elementSize) { const BufferFlags BufferFlags = BufferFlags.StructuredAppendBuffer | BufferFlags.ShaderResource | BufferFlags.UnorderedAccess; return(Buffer.New(device, count * elementSize, elementSize, BufferFlags)); }
/// <summary> /// Creates a new Typed buffer <see cref="GraphicsResourceUsage.Default" /> uasge. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="count">The number of data with the following viewFormat.</param> /// <param name="viewFormat">The view format of the buffer.</param> /// <param name="isUnorderedAccess">if set to <c>true</c> this buffer supports unordered access (RW in HLSL).</param> /// <param name="usage">The usage.</param> /// <returns>A Typed buffer</returns> public static Buffer New(GraphicsDevice device, int count, PixelFormat viewFormat, bool isUnorderedAccess = false, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(Buffer.New(device, count * viewFormat.SizeInBytes(), BufferFlags.ShaderResource | (isUnorderedAccess ? BufferFlags.UnorderedAccess : BufferFlags.None), viewFormat, usage)); }
/// <summary> /// Creates a new index buffer with <see cref="GraphicsResourceUsage.Default"/> uasge by default. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="size">The size in bytes.</param> /// <param name="usage">The usage.</param> /// <returns>A index buffer</returns> public static Buffer New(GraphicsDevice device, int size, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(Buffer.New(device, size, BufferFlags.IndexBuffer, usage)); }
/// <summary> /// Creates a new index buffer with <see cref="GraphicsResourceUsage.Immutable"/> uasge by default. /// </summary> /// <typeparam name="T">Type of the index buffer to get the sizeof from</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the index buffer.</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A index buffer</returns> public static Buffer <T> New <T>(GraphicsDevice device, T[] value, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable) where T : struct { return(Buffer.New(device, value, BufferFlags.IndexBuffer, usage)); }
public void Generate(IServiceRegistry services, Model model) { if (services == null) { throw new ArgumentNullException("services"); } if (model == null) { throw new ArgumentNullException("model"); } var graphicsDevice = services.GetSafeServiceAs <IGraphicsDeviceService>().GraphicsDevice; var data = this.CreatePrimitiveMeshData(); if (data.Vertices.Length == 0) { throw new InvalidOperationException("Invalid GeometricPrimitive [{0}]. Expecting non-zero Vertices array"); } var boundingBox = BoundingBox.Empty; for (int i = 0; i < data.Vertices.Length; i++) { BoundingBox.Merge(ref boundingBox, ref data.Vertices[i].Position, out boundingBox); } BoundingSphere boundingSphere; unsafe { fixed(void *verticesPtr = data.Vertices) BoundingSphere.FromPoints((IntPtr)verticesPtr, 0, data.Vertices.Length, VertexPositionNormalTexture.Size, out boundingSphere); } var originalLayout = data.Vertices[0].GetLayout(); // Generate Tangent/BiNormal vectors var resultWithTangentBiNormal = VertexHelper.GenerateTangentBinormal(originalLayout, data.Vertices, data.Indices); // Generate Multitexcoords var result = VertexHelper.GenerateMultiTextureCoordinates(resultWithTangentBiNormal); var meshDraw = new MeshDraw(); var layout = result.Layout; var vertexBuffer = result.VertexBuffer; var indices = data.Indices; if (indices.Length < 0xFFFF) { var indicesShort = new ushort[indices.Length]; for (int i = 0; i < indicesShort.Length; i++) { indicesShort[i] = (ushort)indices[i]; } meshDraw.IndexBuffer = new IndexBufferBinding(Buffer.Index.New(graphicsDevice, indicesShort).RecreateWith(indicesShort), false, indices.Length); } else { if (graphicsDevice.Features.Profile <= GraphicsProfile.Level_9_3) { throw new InvalidOperationException("Cannot generate more than 65535 indices on feature level HW <= 9.3"); } meshDraw.IndexBuffer = new IndexBufferBinding(Buffer.Index.New(graphicsDevice, indices).RecreateWith(indices), true, indices.Length); } meshDraw.VertexBuffers = new[] { new VertexBufferBinding(Buffer.New(graphicsDevice, vertexBuffer, BufferFlags.VertexBuffer).RecreateWith(vertexBuffer), layout, data.Vertices.Length) }; meshDraw.DrawCount = indices.Length; meshDraw.PrimitiveType = PrimitiveType.TriangleList; var mesh = new Mesh { Draw = meshDraw, BoundingBox = boundingBox, BoundingSphere = boundingSphere }; model.BoundingBox = boundingBox; model.BoundingSphere = boundingSphere; model.Add(mesh); if (MaterialInstance != null && MaterialInstance.Material != null) { model.Materials.Add(MaterialInstance); } }
/// <summary> /// Creates a temporary buffer. /// </summary> /// <param name="description">The description.</param> /// <param name="viewFormat">The shader view format on the buffer</param> /// <returns>Buffer.</returns> protected virtual Buffer CreateBuffer(BufferDescription description, PixelFormat viewFormat) { return(Buffer.New(GraphicsDevice, description, viewFormat)); }
/// <summary> /// Creates a new Raw buffer with <see cref="GraphicsResourceUsage.Default"/> uasge by default. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the Raw buffer.</param> /// <param name="additionalBindings">The additional bindings (for example, to create a combined raw/index buffer, pass <see cref="BufferFlags.IndexBuffer" />)</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A Raw buffer</returns> public static Buffer New(GraphicsDevice device, DataPointer value, BufferFlags additionalBindings = BufferFlags.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(Buffer.New(device, value, 0, BufferFlags.RawBuffer | additionalBindings, usage)); }
/// <summary> /// Creates a new Vertex buffer with <see cref="GraphicsResourceUsage.Default"/> usage by default. /// </summary> /// <typeparam name="T">Type of the Vertex buffer to get the sizeof from</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="vertexBufferCount">Number of vertex in this buffer with the sizeof(T).</param> /// <param name="usage">The usage.</param> /// <returns>A Vertex buffer</returns> public static Buffer <T> New <T>(GraphicsDevice device, int vertexBufferCount, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : struct { return(Buffer.New <T>(device, vertexBufferCount, BufferFlags.VertexBuffer, usage)); }
/// <summary> /// Creates a new Raw buffer with <see cref="GraphicsResourceUsage.Default"/> uasge by default. /// </summary> /// <typeparam name="T">Type of the Raw buffer to get the sizeof from</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the Raw buffer.</param> /// <param name="additionalBindings">The additional bindings (for example, to create a combined raw/index buffer, pass <see cref="BufferFlags.IndexBuffer" />)</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A Raw buffer</returns> public static Buffer <T> New <T>(GraphicsDevice device, T[] value, BufferFlags additionalBindings = BufferFlags.None, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : struct { return(Buffer.New(device, value, BufferFlags.RawBuffer | additionalBindings, usage)); }
/// <summary> /// Creates a new Typed buffer <see cref="GraphicsResourceUsage.Default" /> uasge. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the Typed buffer.</param> /// <param name="viewFormat">The view format of the buffer.</param> /// <param name="isUnorderedAccess">if set to <c>true</c> this buffer supports unordered access (RW in HLSL).</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A Typed buffer</returns> public static Buffer New(GraphicsDevice device, DataPointer value, PixelFormat viewFormat, bool isUnorderedAccess = false, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(Buffer.New(device, value, 0, BufferFlags.ShaderResource | (isUnorderedAccess?BufferFlags.UnorderedAccess : BufferFlags.None), viewFormat, usage)); }
/// <summary> /// Creates a new Typed buffer <see cref="GraphicsResourceUsage.Default" /> uasge. /// </summary> /// <typeparam name="T">Type of the Typed buffer to get the sizeof from</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the Typed buffer.</param> /// <param name="viewFormat">The view format of the buffer.</param> /// <param name="isUnorderedAccess">if set to <c>true</c> this buffer supports unordered access (RW in HLSL).</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A Typed buffer</returns> public static Buffer <T> New <T>(GraphicsDevice device, T[] value, PixelFormat viewFormat, bool isUnorderedAccess = false, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : struct { return(Buffer.New(device, value, BufferFlags.ShaderResource | (isUnorderedAccess ? BufferFlags.UnorderedAccess : BufferFlags.None), viewFormat, usage)); }
/// <summary> /// Creates a new StructuredCounter buffer <see cref="GraphicsResourceUsage.Default" /> uasge. /// </summary> /// <typeparam name="T">Type of the StructuredCounter buffer to get the sizeof from</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the StructuredCounter buffer.</param> /// <returns>A StructuredCounter buffer</returns> public static Buffer New <T>(GraphicsDevice device, T[] value) where T : struct { const BufferFlags BufferFlags = BufferFlags.StructuredCounterBuffer | BufferFlags.ShaderResource | BufferFlags.UnorderedAccess; return(Buffer.New(device, value, BufferFlags)); }
/// <summary> /// Creates a new constant buffer with <see cref="GraphicsResourceUsage.Dynamic"/> usage. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="size">The size in bytes.</param> /// <returns>A constant buffer</returns> public static Buffer New(GraphicsDevice device, int size) { return(Buffer.New(device, size, BufferFlags.ConstantBuffer, GraphicsResourceUsage.Dynamic)); }
/// <summary> /// Creates a new StructuredCounter buffer <see cref="GraphicsResourceUsage.Default" /> uasge. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the StructuredCounter buffer.</param> /// <param name="elementSize">Size of the element.</param> /// <returns>A StructuredCounter buffer</returns> public static Buffer New(GraphicsDevice device, DataPointer value, int elementSize) { const BufferFlags BufferFlags = BufferFlags.StructuredCounterBuffer | BufferFlags.ShaderResource | BufferFlags.UnorderedAccess; return(Buffer.New(device, value, elementSize, BufferFlags)); }
/// <summary> /// Creates a new constant buffer with <see cref="GraphicsResourceUsage.Dynamic"/> usage. /// </summary> /// <typeparam name="T">Type of the constant buffer to get the sizeof from</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <returns>A constant buffer</returns> public static Buffer <T> New <T>(GraphicsDevice device) where T : struct { return(Buffer.New <T>(device, 1, BufferFlags.ConstantBuffer, GraphicsResourceUsage.Dynamic)); }
/// <summary> /// Creates a new index buffer with <see cref="GraphicsResourceUsage.Immutable"/> uasge by default. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the index buffer.</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A index buffer</returns> public static Buffer New(GraphicsDevice device, DataPointer value, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable) { return(Buffer.New(device, value, 0, BufferFlags.IndexBuffer, usage)); }
/// <summary> /// Creates a new constant buffer with <see cref="GraphicsResourceUsage.Dynamic"/> usage. /// </summary> /// <typeparam name="T">Type of the constant buffer to get the sizeof from</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the constant buffer.</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A constant buffer</returns> public static Buffer <T> New <T>(GraphicsDevice device, T[] value, GraphicsResourceUsage usage = GraphicsResourceUsage.Dynamic) where T : struct { return(Buffer.New(device, value, BufferFlags.ConstantBuffer, usage)); }
/// <summary> /// Creates a new index buffer with <see cref="GraphicsResourceUsage.Default"/> uasge by default. /// </summary> /// <typeparam name="T">Type of the index buffer to get the sizeof from</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="usage">The usage.</param> /// <returns>A index buffer</returns> public static Buffer <T> New <T>(GraphicsDevice device, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) where T : struct { return(Buffer.New <T>(device, 1, BufferFlags.IndexBuffer, usage)); }
/// <summary> /// Creates a new constant buffer with <see cref="GraphicsResourceUsage.Dynamic"/> usage. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the constant buffer.</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A constant buffer</returns> public static Buffer New(GraphicsDevice device, DataPointer value, GraphicsResourceUsage usage = GraphicsResourceUsage.Dynamic) { return(Buffer.New(device, value, 0, BufferFlags.ConstantBuffer, usage)); }
/// <summary> /// Creates a new index buffer with <see cref="GraphicsResourceUsage.Immutable"/> uasge by default. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="value">The value to initialize the index buffer.</param> /// <param name="is32BitIndex">Set to true if the buffer is using a 32 bit index or false for 16 bit index.</param> /// <param name="usage">The usage of this resource.</param> /// <returns>A index buffer</returns> public static Buffer New(GraphicsDevice device, byte[] value, bool is32BitIndex, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable) { return(Buffer.New(device, value, is32BitIndex ? 4 : 2, BufferFlags.IndexBuffer, PixelFormat.None, usage)); }
/// <summary> /// Creates a tempoary buffer. /// </summary> /// <param name="description">The description.</param> /// <returns>Buffer.</returns> protected virtual Buffer CreateBuffer(BufferDescription description) { return(Buffer.New(GraphicsDevice, description)); }