internal TerrainPatch( HeightMap heightMap, Rectangle patchBounds, GraphicsDevice graphicsDevice, TerrainPatchIndexBufferCache indexBufferCache, Material material) { Bounds = patchBounds; _indexBuffer = indexBufferCache.GetIndexBuffer( patchBounds.Width, patchBounds.Height, out var indices); _numIndices = (uint)indices.Length; _vertexBuffer = AddDisposable(CreateVertexBuffer( graphicsDevice, heightMap, patchBounds, indices, out var boundingBox, out var triangles)); BoundingBox = boundingBox; Triangles = triangles; _material = material; _beforeRender = (CommandList cl, Graphics.Rendering.RenderContext context, in RenderItem renderItem) => { cl.SetVertexBuffer(0, _vertexBuffer); }; }
internal TerrainPatch( HeightMap heightMap, Rectangle patchBounds, GraphicsDevice graphicsDevice, TerrainPatchIndexBufferCache indexBufferCache, ResourceSet materialResourceSet) { Bounds = patchBounds; _indexBuffer = indexBufferCache.GetIndexBuffer( patchBounds.Width, patchBounds.Height, out var indices); _numIndices = (uint)indices.Length; _vertexBuffer = AddDisposable(CreateVertexBuffer( graphicsDevice, heightMap, patchBounds, indices, out var boundingBox, out var triangles)); BoundingBox = boundingBox; Triangles = triangles; _beforeRender = (cl, context) => { cl.SetGraphicsResourceSet(4, materialResourceSet); cl.SetVertexBuffer(0, _vertexBuffer); }; }
public ModelMeshPartInstance( ModelMeshPart modelMeshPart, ModelMeshInstance modelMeshInstance, AssetLoadContext loadContext) { ModelMeshPart = modelMeshPart; MeshInstance = modelMeshInstance; _renderItemConstantsResourceSet = AddDisposable( loadContext.ShaderResources.Mesh.CreateRenderItemConstantsResourceSet( modelMeshPart.ModelMesh.MeshConstantsBuffer, modelMeshInstance.RenderItemConstantsBufferVS, modelMeshInstance.ModelInstance.SkinningBuffer, modelMeshInstance.ModelInstance.RenderItemConstantsBufferPS)); BeforeRenderCallback = (CommandList cl, in RenderItem renderItem) => { OnBeforeRender(cl, renderItem); }; BeforeRenderCallbackDepth = (CommandList cl, in RenderItem renderItem) => { OnBeforeRender(cl, renderItem); }; }
private WaterArea(AssetLoadContext loadContext, string bumpTexName = null) { _shaderSet = loadContext.ShaderResources.Water.ShaderSet; _pipeline = loadContext.ShaderResources.Water.Pipeline; _resourceSets = new Dictionary <TimeOfDay, ResourceSet>(); Texture bumpTexture = null; if (bumpTexName != null) { bumpTexture = loadContext.AssetStore.Textures.GetByName(bumpTexName); } else { bumpTexture = loadContext.StandardGraphicsResources.SolidWhiteTexture; } foreach (var waterSet in loadContext.AssetStore.WaterSets) { // TODO: Cache these resource sets in some sort of scoped data context. var resourceSet = AddDisposable(loadContext.ShaderResources.Water.CreateMaterialResourceSet(waterSet.WaterTexture.Value, bumpTexture)); _resourceSets.Add(waterSet.TimeOfDay, resourceSet); } _beforeRender = (cl, context) => { cl.SetGraphicsResourceSet(4, _resourceSets[context.Scene3D.Lighting.TimeOfDay]); cl.SetVertexBuffer(0, _vertexBuffer); }; }
private WaterArea(AssetLoadContext loadContext) { _shaderSet = loadContext.ShaderResources.Water.ShaderSet; _pipeline = loadContext.ShaderResources.Water.Pipeline; _beforeRender = (cl, context) => { cl.SetVertexBuffer(0, _vertexBuffer); }; }
private WaterArea( ContentManager contentManager, PolygonTrigger trigger) { var triggerPoints = trigger.Points .Select(x => new Vector2(x.X, x.Y)) .ToArray(); Triangulator.Triangulate( triggerPoints, WindingOrder.CounterClockwise, out var trianglePoints, out var triangleIndices); var vertices = trianglePoints .Select(x => new WaterShaderResources.WaterVertex { Position = new Vector3(x.X, x.Y, trigger.Points[0].Z) }) .ToArray(); _boundingBox = BoundingBox.CreateFromPoints(vertices.Select(x => x.Position)); _vertexBuffer = AddDisposable(contentManager.GraphicsDevice.CreateStaticBuffer( vertices, BufferUsage.VertexBuffer)); _numIndices = (uint)triangleIndices.Length; _indexBuffer = AddDisposable(contentManager.GraphicsDevice.CreateStaticBuffer( triangleIndices, BufferUsage.IndexBuffer)); _shaderSet = contentManager.ShaderResources.Water.ShaderSet; _pipeline = contentManager.ShaderResources.Water.Pipeline; _resourceSets = new Dictionary <TimeOfDay, ResourceSet>(); foreach (var waterSet in contentManager.IniDataContext.WaterSets) { var waterTexture = contentManager.Load <Texture>(Path.Combine("Art", "Textures", waterSet.WaterTexture)); // TODO: Cache these resource sets in some sort of scoped data context. var resourceSet = AddDisposable(contentManager.ShaderResources.Water.CreateMaterialResourceSet(waterTexture)); _resourceSets.Add(waterSet.TimeOfDay, resourceSet); } _beforeRender = (cl, context) => { cl.SetGraphicsResourceSet(4, _resourceSets[context.Scene3D.Lighting.TimeOfDay]); cl.SetVertexBuffer(0, _vertexBuffer); }; }
internal TerrainPatch( HeightMap heightMap, Rectangle patchBounds, GraphicsDevice graphicsDevice, TerrainPatchIndexBufferCache indexBufferCache, ResourceSet materialResourceSet, ResourceSet radiusCursorDecalsResourceSet, Func <ResourceSet> causticsRendererCallback) { Bounds = patchBounds; _indexBuffer = indexBufferCache.GetIndexBuffer( patchBounds.Width, patchBounds.Height, out var indices); _numIndices = (uint)indices.Length; _vertexBuffer = AddDisposable(CreateVertexBuffer( graphicsDevice, heightMap, patchBounds, indices, out var boundingBox, out var triangles)); BoundingBox = boundingBox; Triangles = triangles; _beforeRender = (cl, context) => { var isRender = context.Scene3D.Waters.IsRenderCaustics; if (isRender) { var causticsResourceSet = causticsRendererCallback.Invoke(); cl.SetGraphicsResourceSet(4, causticsResourceSet); } else { cl.SetGraphicsResourceSet(4, materialResourceSet); } cl.SetGraphicsResourceSet(5, radiusCursorDecalsResourceSet); cl.SetVertexBuffer(0, _vertexBuffer); }; }
private WaterArea(AssetLoadContext loadContext, string debugName) { _shaderSet = loadContext.ShaderResources.Water; _pipeline = loadContext.ShaderResources.Water.Pipeline; _material = AddDisposable( new Material( _shaderSet, _pipeline, null)); // TODO: MaterialResourceSet _debugName = debugName; _beforeRender = (CommandList cl, RenderContext context, in RenderItem renderItem) => { cl.SetVertexBuffer(0, _vertexBuffer); }; }
private void PostInitialize(AssetLoadContext loadContext) { _samplerResourceSet = loadContext.ShaderResources.Mesh.SamplerResourceSet; _depthShaderSet = loadContext.ShaderResources.MeshDepth.ShaderSet; BeforeRenderDelegates = new BeforeRenderDelegate[MeshParts.Count]; BeforeRenderDelegatesDepth = new BeforeRenderDelegate[MeshParts.Count]; for (var i = 0; i < BeforeRenderDelegates.Length; i++) { var meshPart = MeshParts[i]; BeforeRenderDelegates[i] = (cl, context) => { cl.SetGraphicsResourceSet(4, _meshConstantsResourceSet); cl.SetGraphicsResourceSet(5, meshPart.MaterialResourceSet); cl.SetGraphicsResourceSet(6, _samplerResourceSet); cl.SetVertexBuffer(0, _vertexBuffer); if (meshPart.TexCoordVertexBuffer != null) { cl.SetVertexBuffer(1, meshPart.TexCoordVertexBuffer); } }; BeforeRenderDelegatesDepth[i] = (cl, context) => { cl.SetGraphicsResourceSet(1, _meshConstantsResourceSet); cl.SetVertexBuffer(0, _vertexBuffer); if (meshPart.TexCoordVertexBuffer != null) { cl.SetVertexBuffer(1, meshPart.TexCoordVertexBuffer); } }; } }
internal Road( AssetLoadContext loadContext, HeightMap heightMap, RoadNetwork network) { var vertices = new List <RoadShaderResources.RoadVertex>(); var indices = new List <ushort>(); foreach (var segment in network.Segments) { segment.GenerateMesh(network.Template, heightMap, vertices, indices); } _boundingBox = BoundingBox.CreateFromPoints(vertices.Select(x => x.Position)); _vertexBuffer = AddDisposable(loadContext.GraphicsDevice.CreateStaticBuffer( vertices.ToArray(), BufferUsage.VertexBuffer)); _numIndices = (uint)indices.Count; _indexBuffer = AddDisposable(loadContext.GraphicsDevice.CreateStaticBuffer( indices.ToArray(), BufferUsage.IndexBuffer)); _shaderSet = loadContext.ShaderResources.Road.ShaderSet; _pipeline = loadContext.ShaderResources.Road.Pipeline; // TODO: Cache these resource sets in some sort of scoped data context. _resourceSet = AddDisposable(loadContext.ShaderResources.Road.CreateMaterialResourceSet(network.Template.Texture.Value)); _beforeRender = (cl, context) => { cl.SetGraphicsResourceSet(4, _resourceSet); cl.SetVertexBuffer(0, _vertexBuffer); }; }
internal ModelInstance(Model model, AssetLoadContext loadContext) { Model = model; _graphicsDevice = loadContext.GraphicsDevice; ModelBoneInstances = new ModelBoneInstance[model.BoneHierarchy.Bones.Length]; for (var i = 0; i < model.BoneHierarchy.Bones.Length; i++) { ModelBoneInstances[i] = new ModelBoneInstance(model.BoneHierarchy.Bones[i]); } RelativeBoneTransforms = new Matrix4x4[model.BoneHierarchy.Bones.Length]; AbsoluteBoneTransforms = new Matrix4x4[model.BoneHierarchy.Bones.Length]; BoneVisibilities = new bool[model.BoneHierarchy.Bones.Length]; BoneFrameVisibilities = new bool[model.BoneHierarchy.Bones.Length]; for (var i = 0; i < model.BoneHierarchy.Bones.Length; i++) { BoneVisibilities[i] = true; BoneFrameVisibilities[i] = true; } _hasSkinnedMeshes = model.SubObjects.Any(x => x.RenderObject.Skinned); DeviceBuffer skinningBuffer; if (_hasSkinnedMeshes) { _skinningBuffer = skinningBuffer = AddDisposable(_graphicsDevice.ResourceFactory.CreateBuffer( new BufferDescription( (uint)(64 * model.BoneHierarchy.Bones.Length), BufferUsage.StructuredBufferReadOnly | BufferUsage.Dynamic, 64, true))); _skinningBones = new Matrix4x4[model.BoneHierarchy.Bones.Length]; } else { skinningBuffer = loadContext.StandardGraphicsResources.GetNullStructuredBuffer(64); } SkinningBufferResourceSet = AddDisposable(loadContext.ShaderResources.Mesh.CreateSkinningResourceSet(skinningBuffer)); AnimationInstances = new List <AnimationInstance>(); BeforeRenderDelegates = new BeforeRenderDelegate[model.SubObjects.Length][]; BeforeRenderDelegatesDepth = new BeforeRenderDelegate[model.SubObjects.Length][]; for (var i = 0; i < model.SubObjects.Length; i++) { var mesh = model.SubObjects[i].RenderObject; BeforeRenderDelegates[i] = new BeforeRenderDelegate[mesh.MeshParts.Count]; BeforeRenderDelegatesDepth[i] = new BeforeRenderDelegate[mesh.MeshParts.Count]; for (var j = 0; j < mesh.MeshParts.Count; j++) { var meshBeforeRender = mesh.BeforeRenderDelegates[j]; var meshBeforeRenderDepth = mesh.BeforeRenderDelegatesDepth[j]; BeforeRenderDelegates[i][j] = (cl, context) => { cl.SetGraphicsResourceSet(8, SkinningBufferResourceSet); meshBeforeRender(cl, context); }; BeforeRenderDelegatesDepth[i][j] = (cl, context) => { cl.SetGraphicsResourceSet(3, SkinningBufferResourceSet); meshBeforeRenderDepth(cl, context); }; } } }
public ParticleSystem( ContentManager contentManager, FXParticleSystemTemplate template, GetMatrixReferenceDelegate getWorldMatrix) { Template = template; _getWorldMatrix = getWorldMatrix; var maxParticles = CalculateMaxParticles(); // If this system never emits any particles, there's no reason to fully initialise it. if (maxParticles == 0) { return; } _graphicsDevice = contentManager.GraphicsDevice; _renderItemConstantsBufferVS = AddDisposable(new ConstantBuffer <MeshShaderResources.RenderItemConstantsVS>(_graphicsDevice)); _velocityType = Template.EmissionVelocity; _volumeType = Template.EmissionVolume; var texturePath = Path.Combine("Art", "Textures", Template.ParticleName); var texture = contentManager.Load <Texture>(texturePath); _particleResourceSet = AddDisposable(contentManager.ShaderResources.Particle.CreateParticleResoureSet( _renderItemConstantsBufferVS.Buffer, texture)); _shaderSet = contentManager.ShaderResources.Particle.ShaderSet; _pipeline = contentManager.ShaderResources.Particle.GetCachedPipeline(Template.Shader); _initialDelay = Template.InitialDelay.GetRandomInt(); _startSizeRate = Template.StartSizeRate.GetRandomFloat(); _startSize = 0; _colorKeyframes = new List <ParticleColorKeyframe>(); var colors = Template.Colors; if (colors.Color1 != null) { _colorKeyframes.Add(new ParticleColorKeyframe(colors.Color1)); } void addColorKeyframe(RgbColorKeyframe keyframe, RgbColorKeyframe previous) { if (keyframe != null && keyframe.Time > previous.Time) { _colorKeyframes.Add(new ParticleColorKeyframe(keyframe)); } } addColorKeyframe(colors.Color2, colors.Color1); addColorKeyframe(colors.Color3, colors.Color2); addColorKeyframe(colors.Color4, colors.Color3); addColorKeyframe(colors.Color5, colors.Color4); addColorKeyframe(colors.Color6, colors.Color5); addColorKeyframe(colors.Color7, colors.Color6); addColorKeyframe(colors.Color8, colors.Color7); _particles = new Particle[maxParticles]; for (var i = 0; i < _particles.Length; i++) { _particles[i].AlphaKeyframes = new List <ParticleAlphaKeyframe>(); _particles[i].Dead = true; } _deadList = new List <int>(); _deadList.AddRange(Enumerable.Range(0, maxParticles)); var numVertices = maxParticles * 4; _vertexBuffer = AddDisposable(contentManager.GraphicsDevice.ResourceFactory.CreateBuffer( new BufferDescription( (uint)(ParticleShaderResources.ParticleVertex.VertexDescriptor.Stride * maxParticles * 4), BufferUsage.VertexBuffer | BufferUsage.Dynamic))); _vertices = new ParticleShaderResources.ParticleVertex[numVertices]; _indexBuffer = AddDisposable(CreateIndexBuffer( contentManager.GraphicsDevice, maxParticles, out _numIndices)); State = ParticleSystemState.Active; _beforeRender = (cl, context) => { // Only update once we know this particle system is visible on screen. Update(cl, context.GameTime); if (_worldMatrixChanged) { _renderItemConstantsBufferVS.Update(cl); } cl.SetGraphicsResourceSet(1, _particleResourceSet); cl.SetVertexBuffer(0, _vertexBuffer); }; }