private void Clean(Context context) { if (_dirty) { if (_va != null) { _va.Dispose(); _va = null; _drawState.VertexArray = null; _drawStateSolid.VertexArray = null; } Mesh mesh = BoxTessellator.Compute(2 * _shape.Radii); _va = context.CreateVertexArray(mesh, _drawState.ShaderProgram.VertexAttributes, BufferHint.StaticDraw); _drawState.VertexArray = _va; _drawStateSolid.VertexArray = _va; _primitiveType = mesh.PrimitiveType; _renderState.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder; ((Uniform <Vector3F>)_drawState.ShaderProgram.Uniforms["u_globeOneOverRadiiSquared"]).Value = _shape.OneOverRadiiSquared.ToVector3F(); ((Uniform <Vector3F>)_drawStateSolid.ShaderProgram.Uniforms["u_globeOneOverRadiiSquared"]).Value = _shape.OneOverRadiiSquared.ToVector3F(); if (_wireframe != null) { _wireframe.Dispose(); _wireframe = null; } _wireframe = new Wireframe(context, mesh); _wireframe.FacetCullingFace = CullFace.Front; _wireframe.Width = 3; _dirty = false; } }
private void Update(Context context) { if (_dirtyVA) { Vector3D radii = new Vector3D(_tileResolution.X, _tileResolution.Y, (_tileMaximumHeight - _tileMinimumHeight) * _heightExaggeration.Value); Vector3D halfRadii = 0.5 * radii; Mesh mesh = BoxTessellator.Compute(radii); // // TEXEL_SPACE_TODO: Translate box so it is not centered at // the origin - world space and texel space will match up. // IList <Vector3D> positions = ((VertexAttributeDoubleVector3)mesh.Attributes["position"]).Values; for (int i = 0; i < positions.Count; ++i) { positions[i] = positions[i] + halfRadii; } if (_drawState.VertexArray != null) { _drawState.VertexArray.Dispose(); _drawState.VertexArray = null; } _drawState.VertexArray = context.CreateVertexArray(mesh, _drawState.ShaderProgram.VertexAttributes, BufferHint.StaticDraw); _primitiveType = mesh.PrimitiveType; _drawState.RenderState.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder; if (_wireframe != null) { _wireframe.Dispose(); } _wireframe = new Wireframe(context, mesh); _wireframe.FacetCullingFace = CullFace.Front; _wireframe.Width = 3; _dirtyVA = false; } }
protected unsafe override void CreateResources(ResourceFactory factory) { //创建一个最大半径的Cube var boxMesh = BoxTessellator.Compute(Shape.Radii * 2f); //创建此mesh的相关资源 _mesh = boxMesh; //创建texture和textureview var inPath = @"E:\swyy\Lib\veldrid-samples\assets\Earth.png"; ImageSharpTexture inputImage = new ImageSharpTexture(inPath, false); _surfaceTexture = inputImage.CreateDeviceTexture(GraphicsDevice, factory); _surfaceTextureView = factory.CreateTextureView(_surfaceTexture); //创建一个顶点缓冲数据 _vertexBuffer = factory.CreateBuffer(new BufferDescription((uint)boxMesh.Positions.Length * VertexPosition.SizeInBytes, BufferUsage.VertexBuffer)); _indexBuffer = factory.CreateBuffer(new BufferDescription((uint)(sizeof(ushort) * boxMesh.Indices.Length), BufferUsage.IndexBuffer)); _uboBuffer = factory.CreateBuffer(new BufferDescription(144, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); //提前更新raddisquard参数 _uboBase = new BaseUBO(); //提前更新关照模型等固定参数 //更新光照模型 var DiffuseIntensity = 0.65f; var SpecularIntensity = 0.25f; var AmbientIntensity = 0.10f; var Shininess = 12; var lightModel = new Vector4( DiffuseIntensity, SpecularIntensity, AmbientIntensity, Shininess); _uboBase.DiffuseSpecularAmbientShininess = lightModel; _uboBase.GlobeOneOverRadiiSquared = Shape.OneOverRadiiSquared; _uboBase.UseAverageDepth = false; //提前更新参数 GraphicsDevice.UpdateBuffer(_uboBuffer, 0, _uboBase); //创建prj的资源布局作为地球的默认资源布局 ResourceLayout projViewLayout = factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("UBO", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment) )); //创建prj的资源 _projViewSet = factory.CreateResourceSet(new ResourceSetDescription( projViewLayout, _uboBuffer )); GraphicsDevice.UpdateBuffer(_vertexBuffer, 0, _mesh.Positions); GraphicsDevice.UpdateBuffer(_indexBuffer, 0, _mesh.Indices); //创建texture和sampler资源布局 ResourceLayout worldTextureLayout = factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("SurfaceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); //创建textureset _textureSet = factory.CreateResourceSet(new ResourceSetDescription( worldTextureLayout, _surfaceTextureView, GraphicsDevice.Aniso4xSampler )); //创建shaderprograme,仅传入inWorldPosition ShaderSetDescription shaderSet = new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("inPosition", VertexElementSemantic.Position, VertexElementFormat.Float3)) }, new Shader[] { LoadEmbbedShader(ShaderStages.Vertex, "GlobeVS.spv"), LoadEmbbedShader(ShaderStages.Fragment, "GlobeFS.spv") }); //创建渲染管线 _pipeline = factory.CreateGraphicsPipeline(new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, shaderSet, new[] { projViewLayout, worldTextureLayout }, MainSwapchain.Framebuffer.OutputDescription)); //创建一个命令队列 _cl = factory.CreateCommandList(); }