private static StaticBuffer <ushort> CreateIndexBuffer(GraphicsDevice graphicsDevice, int maxParticles) { var uploadBatch = new ResourceUploadBatch(graphicsDevice); uploadBatch.Begin(); var indices = new ushort[maxParticles * 2 * 3]; // Two triangles per particle. var indexCounter = 0; for (ushort i = 0; i < maxParticles * 4; i += 4) { indices[indexCounter++] = (ushort)(i + 0); indices[indexCounter++] = (ushort)(i + 2); indices[indexCounter++] = (ushort)(i + 1); indices[indexCounter++] = (ushort)(i + 1); indices[indexCounter++] = (ushort)(i + 2); indices[indexCounter++] = (ushort)(i + 3); } var result = StaticBuffer.Create( graphicsDevice, uploadBatch, indices); uploadBatch.End(); return(result); }
private static StaticBuffer <CliffInfo> CreateCliffDetails( GraphicsDevice graphicsDevice, ResourceUploadBatch uploadBatch, MapFile mapFile) { var cliffDetails = new CliffInfo[mapFile.BlendTileData.CliffTextureMappings.Length]; const int cliffScalingFactor = 64; for (var i = 0; i < cliffDetails.Length; i++) { var cliffMapping = mapFile.BlendTileData.CliffTextureMappings[i]; cliffDetails[i] = new CliffInfo { BottomLeftUV = cliffMapping.BottomLeftCoords * cliffScalingFactor, BottomRightUV = cliffMapping.BottomRightCoords * cliffScalingFactor, TopLeftUV = cliffMapping.TopLeftCoords * cliffScalingFactor, TopRightUV = cliffMapping.TopRightCoords * cliffScalingFactor }; } return(cliffDetails.Length > 0 ? StaticBuffer.Create( graphicsDevice, uploadBatch, cliffDetails) : null); }
public void AddObject(StaticBuffer buffer) { lock (staticBufferList) { staticBufferList.Add(buffer); } }
internal ModelMeshMaterialPass( GraphicsDevice graphicsDevice, ResourceUploadBatch uploadBatch, uint numTextureStages, MeshTexCoords[] texCoords, MeshTextureIndex[] textureIndices, uint[] materialIndices, IReadOnlyList <ModelMeshPart> meshParts) { NumTextureStages = numTextureStages; TexCoordVertexBuffer = AddDisposable(StaticBuffer.Create( graphicsDevice, uploadBatch, texCoords)); TextureIndicesBuffer = AddDisposable(StaticBuffer.Create( graphicsDevice, uploadBatch, textureIndices)); MaterialIndicesBuffer = AddDisposable(StaticBuffer.Create( graphicsDevice, uploadBatch, materialIndices)); MeshParts = meshParts; }
private StaticBuffer <ushort> CreateIndexBuffer( ResourceUploadBatch uploadBatch, TerrainPatchSize size, out ushort[] indices) { // TODO: Could use triangle strip var numIndices = CalculateNumIndices(size.Width, size.Height); indices = new ushort[numIndices]; for (int y = 0, indexIndex = 0; y < size.Height - 1; y++) { var yThis = y * size.Width; var yNext = (y + 1) * size.Width; for (var x = 0; x < size.Width - 1; x++) { // Triangle 1 indices[indexIndex++] = (ushort)(yThis + x); indices[indexIndex++] = (ushort)(yThis + x + 1); indices[indexIndex++] = (ushort)(yNext + x); // Triangle 2 indices[indexIndex++] = (ushort)(yNext + x); indices[indexIndex++] = (ushort)(yThis + x + 1); indices[indexIndex++] = (ushort)(yNext + x + 1); } } return(StaticBuffer.Create( _graphicsDevice, uploadBatch, indices)); }
public void SetStaticBuffer <T>(int index, StaticBuffer <T> buffer) where T : struct { // TODO: Validation. PlatformSetShaderResourceView(index, buffer?.ShaderResourceView ?? GraphicsDevice.NullBufferShaderResourceView); }
public static ShaderResourceView Create <T>( GraphicsDevice graphicsDevice, StaticBuffer <T> buffer) where T : struct { var result = new ShaderResourceView(graphicsDevice, 1); result.PlatformSetStructuredBuffer(0, buffer); return(result); }
public void DrawIndexed( PrimitiveType primitiveType, uint indexCount, StaticBuffer <ushort> indexBuffer, uint indexBufferOffset) { PlatformDrawIndexed( primitiveType, indexCount, indexBuffer, indexBufferOffset); }
public ModelMesh( GraphicsDevice graphicsDevice, ResourceUploadBatch uploadBatch, string name, MeshVertex[] vertices, ushort[] indices, VertexMaterial[] vertexMaterials, Texture[] textures, ModelMeshMaterialPass[] materialPasses, bool isSkinned, ModelBone parentBone, uint numBones, BoundingBox boundingBox) { Name = name; ParentBone = parentBone; NumBones = numBones; BoundingBox = boundingBox; Skinned = isSkinned; _vertexBuffer = AddDisposable(StaticBuffer.Create( graphicsDevice, uploadBatch, vertices)); _indexBuffer = AddDisposable(StaticBuffer.Create( graphicsDevice, uploadBatch, indices)); _materialsBuffer = AddDisposable(StaticBuffer.Create( graphicsDevice, uploadBatch, vertexMaterials)); _textures = AddDisposable(new TextureSet(graphicsDevice, textures)); foreach (var materialPass in materialPasses) { AddDisposable(materialPass); } MaterialPasses = materialPasses; }
internal TerrainPatchComponent( TerrainEffect terrainEffect, EffectPipelineStateHandle pipelineStateHandle, Int32Rect patchBounds, StaticBuffer <TerrainVertex> vertexBuffer, StaticBuffer <ushort> indexBuffer, Triangle[] triangles, BoundingBox boundingBox) { _terrainEffect = terrainEffect; _pipelineStateHandle = pipelineStateHandle; Bounds = patchBounds; _vertexBuffer = vertexBuffer; _indexBuffer = indexBuffer; LocalBoundingBox = boundingBox; BoundingBox = boundingBox; Triangles = triangles; }
protected override void Start() { base.Start(); _particleEffect = ContentManager.GetEffect <ParticleEffect>(); _velocityType = VelocityTypeUtility.GetImplementation(Definition.VelocityType); _volumeType = VolumeTypeUtility.GetImplementation(Definition.VolumeType); var texturePath = Path.Combine("Art", "Textures", Definition.ParticleName); _texture = ContentManager.Load <Texture>(texturePath, uploadBatch: null); var blendState = GetBlendState(Definition.Shader); _pipelineStateHandle = new EffectPipelineState( RasterizerStateDescription.CullBackSolid, DepthStencilStateDescription.DepthRead, blendState) .GetHandle(); _initialDelay = Definition.InitialDelay.GetRandomInt(); _startSizeRate = Definition.StartSizeRate.GetRandomFloat(); _startSize = 0; _colorKeyframes = new List <ParticleColorKeyframe>(); if (Definition.Color1 != null) { _colorKeyframes.Add(new ParticleColorKeyframe(Definition.Color1)); } void addColorKeyframe(RgbColorKeyframe keyframe, RgbColorKeyframe previous) { if (keyframe != null && keyframe.Time > previous.Time) { _colorKeyframes.Add(new ParticleColorKeyframe(keyframe)); } } addColorKeyframe(Definition.Color2, Definition.Color1); addColorKeyframe(Definition.Color3, Definition.Color2); addColorKeyframe(Definition.Color4, Definition.Color3); addColorKeyframe(Definition.Color5, Definition.Color4); addColorKeyframe(Definition.Color6, Definition.Color5); addColorKeyframe(Definition.Color7, Definition.Color6); addColorKeyframe(Definition.Color8, Definition.Color7); var maxParticles = CalculateMaxParticles(); _particles = new Particle[maxParticles]; for (var i = 0; i < _particles.Length; i++) { _particles[i].Dead = true; } _deadList = new List <int>(); _deadList.AddRange(Enumerable.Range(0, maxParticles)); _vertexBuffer = DynamicBuffer <ParticleVertex> .CreateArray( GraphicsDevice, maxParticles * 4, BufferUsageFlags.None); _vertices = new ParticleVertex[_vertexBuffer.ElementCount]; _indexBuffer = CreateIndexBuffer(GraphicsDevice, maxParticles); State = ParticleSystemState.Active; }
public void SetCliffDetails(StaticBuffer <CliffInfo> cliffDetailsBuffer) { _cliffDetailsBuffer = cliffDetailsBuffer; _dirtyFlags |= TerrainEffectDirtyFlags.CliffDetailsBuffer; }
public void SetTextureIndices(StaticBuffer <MeshTextureIndex> textureIndicesBuffer) { _textureIndicesBuffer = textureIndicesBuffer; _dirtyFlags |= MeshEffectDirtyFlags.TextureIndicesBuffer; }
private void PlatformSetTypedBuffer(int index, StaticBuffer buffer, PixelFormat format) { }
private void PlatformSetStructuredBuffer(int index, StaticBuffer buffer) { }
public void SetMaterials(StaticBuffer <VertexMaterial> materialsBuffer) { _materialsBuffer = materialsBuffer; _dirtyFlags |= MeshEffectDirtyFlags.MaterialsBuffer; }
private void PlatformSetConstantBuffer(int index, StaticBuffer buffer) { }
public void SetMaterialIndices(StaticBuffer <uint> materialIndicesBuffer) { _materialIndicesBuffer = materialIndicesBuffer; _dirtyFlags |= MeshEffectDirtyFlags.MaterialIndicesBuffer; }
protected override Scene LoadEntry(FileSystemEntry entry, ContentManager contentManager, ResourceUploadBatch uploadBatch) { contentManager.IniDataContext.LoadIniFile(@"Data\INI\Terrain.ini"); var mapFile = MapFile.FromFileSystemEntry(entry); var result = new Scene(); result.Settings.LightingConfigurations = mapFile.GlobalLighting.LightingConfigurations.ToLightSettingsDictionary(); result.Settings.TimeOfDay = mapFile.GlobalLighting.Time; var heightMap = new HeightMap(mapFile.HeightMapData); result.HeightMap = heightMap; var terrainEntity = new Entity(); result.Entities.Add(terrainEntity); terrainEntity.Components.Add(new TerrainComponent { HeightMap = heightMap }); var terrainEffect = AddDisposable(new TerrainEffect( contentManager.GraphicsDevice, mapFile.BlendTileData.Textures.Length)); var pipelineStateSolid = new EffectPipelineState( RasterizerStateDescription.CullBackSolid, DepthStencilStateDescription.Default, BlendStateDescription.Opaque) .GetHandle(); var indexBufferCache = AddDisposable(new TerrainPatchIndexBufferCache(contentManager.GraphicsDevice)); CreatePatches( contentManager.GraphicsDevice, uploadBatch, terrainEntity, heightMap, mapFile.BlendTileData, terrainEffect, pipelineStateSolid, indexBufferCache); var tileDataTexture = AddDisposable(CreateTileDataTexture( contentManager.GraphicsDevice, uploadBatch, mapFile, heightMap)); var cliffDetailsBuffer = AddDisposable(CreateCliffDetails( contentManager.GraphicsDevice, uploadBatch, mapFile)); CreateTextures( contentManager, uploadBatch, mapFile.BlendTileData, out var textures, out var textureDetails); var textureDetailsBuffer = AddDisposable(StaticBuffer.Create( contentManager.GraphicsDevice, uploadBatch, textureDetails)); var textureSet = AddDisposable(new TextureSet( contentManager.GraphicsDevice, textures)); terrainEffect.SetTileData(tileDataTexture); terrainEffect.SetCliffDetails(cliffDetailsBuffer); terrainEffect.SetTextureDetails(textureDetailsBuffer); terrainEffect.SetTextures(textureSet); var objectsEntity = new Entity(); result.Entities.Add(objectsEntity); LoadObjects( contentManager, objectsEntity, heightMap, mapFile.ObjectsList.Objects, result.Settings); foreach (var team in mapFile.SidesList.Teams) { var name = (string)team.Properties["teamName"].Value; var owner = (string)team.Properties["teamOwner"].Value; var isSingleton = (bool)team.Properties["teamIsSingleton"].Value; } foreach (var waypointPath in mapFile.WaypointsList.WaypointPaths) { var start = result.Settings.Waypoints[waypointPath.StartWaypointID]; var end = result.Settings.Waypoints[waypointPath.EndWaypointID]; result.Settings.WaypointPaths[start.Name] = new Settings.WaypointPath( start, end); } var scriptsEntity = new Entity(); result.Entities.Add(scriptsEntity); // TODO: Don't hardcode this. // Perhaps add one ScriptComponent for the neutral player, // and one for the active player. var scriptList = mapFile.SidesList.PlayerScripts.ScriptLists[0]; AddScripts(scriptsEntity, scriptList, result.Settings); return(result); }
public void SetTextureDetails(StaticBuffer <TextureInfo> textureDetailsBuffer) { _textureDetailsBuffer = textureDetailsBuffer; _dirtyFlags |= TerrainEffectDirtyFlags.TextureDetailsBuffer; }
private static StaticBuffer <TerrainVertex> CreateVertexBuffer( GraphicsDevice graphicsDevice, ResourceUploadBatch uploadBatch, HeightMap heightMap, Int32Rect patchBounds, ushort[] indices, out BoundingBox boundingBox, out Triangle[] triangles) { var numVertices = patchBounds.Width * patchBounds.Height; var vertices = new TerrainVertex[numVertices]; var points = new Vector3[numVertices]; var vertexIndex = 0; for (var y = patchBounds.Y; y < patchBounds.Y + patchBounds.Height; y++) { for (var x = patchBounds.X; x < patchBounds.X + patchBounds.Width; x++) { var position = heightMap.GetPosition(x, y); points[vertexIndex] = position; vertices[vertexIndex++] = new TerrainVertex { Position = position, Normal = heightMap.Normals[x, y], UV = new Vector2(x, y) }; } } boundingBox = BoundingBox.CreateFromPoints(points); triangles = new Triangle[(patchBounds.Width - 1) * (patchBounds.Height) * 2]; var triangleIndex = 0; var indexIndex = 0; for (var y = 0; y < patchBounds.Height - 1; y++) { for (var x = 0; x < patchBounds.Width - 1; x++) { // Triangle 1 triangles[triangleIndex++] = new Triangle { V0 = points[indices[indexIndex++]], V1 = points[indices[indexIndex++]], V2 = points[indices[indexIndex++]] }; // Triangle 2 triangles[triangleIndex++] = new Triangle { V0 = points[indices[indexIndex++]], V1 = points[indices[indexIndex++]], V2 = points[indices[indexIndex++]] }; } } return(StaticBuffer.Create( graphicsDevice, uploadBatch, vertices)); }