protected void SetSceneConstants() { sceneConstants.View = Freelook.View; sceneConstants.Projection = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, NearPlane, FarPlane); sceneConstants.ViewInverse = Matrix.Invert(Freelook.View); Vector3 light = new Vector3(20, 30, 10); sceneConstants.LightPosition = new Vector4(light, 1); Texture2DDescription depthBuffer = lightDepthTexture.Description; Matrix lightView = Matrix.LookAtLH(light, Vector3.Zero, Freelook.Up); Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8, depthBuffer.Height / 8, NearPlane, FarPlane); sceneConstants.LightViewProjection = lightView * lightProjection; using (var data = sceneConstantsBuffer.Map(MapMode.WriteDiscard)) { Marshal.StructureToPtr(sceneConstants, data.DataPointer, false); sceneConstantsBuffer.Unmap(); } effect2.GetVariableByName("InverseProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.View * sceneConstants.Projection)); effect2.GetVariableByName("InverseView").AsMatrix().SetMatrix(sceneConstants.ViewInverse); effect2.GetVariableByName("LightInverseViewProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.LightViewProjection)); effect2.GetVariableByName("LightPosition").AsVector().Set(sceneConstants.LightPosition); }
// Used with soft bodies public void SetDynamicVertexBuffer(Device device, Vector3[] vectors) { if (VertexBuffer != null && VertexCount * 2 == vectors.Length) { // Update existing buffer using (var data = VertexBuffer.Map(MapMode.WriteDiscard)) { data.WriteRange(vectors, 0, vectors.Length); VertexBuffer.Unmap(); } } else { // Create new buffer if (VertexBuffer != null) VertexBuffer.Dispose(); BufferDescription vertexBufferDesc = new BufferDescription() { SizeInBytes = Marshal.SizeOf(typeof(Vector3)) * vectors.Length, Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write }; using (var data = new DataStream(vertexBufferDesc.SizeInBytes, false, true)) { data.WriteRange(vectors); data.Position = 0; VertexBuffer = new Buffer(device, data, vertexBufferDesc); } BufferBindings[0] = new VertexBufferBinding(VertexBuffer, 24, 0); } }
void setVertices(Vertex[] arr) { //頂点バッファの作成 var size = Marshal.SizeOf(typeof(Vertex)); if (_vertexBuffer == null || _vertexCount < arr.Length) { if (null != _vertexBuffer) { _vertexBuffer.Dispose(); } _vertexBuffer = new Buffer(_device, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = size * arr.Length, Usage = ResourceUsage.Dynamic }); } var ds = _vertexBuffer.Map(MapMode.WriteDiscard); ds.WriteRange(arr); _vertexBuffer.Unmap(); }
void SetSceneConstants() { FreeLook freelook = Demo.Freelook; Vector3 up = MathHelper.Convert(freelook.Up); sceneConstants.View = Matrix.LookAtLH(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), up); sceneConstants.Projection = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane); sceneConstants.ViewInverse = Matrix.Invert(sceneConstants.View); Vector3 light = new Vector3(20, 30, 10); Texture2DDescription depthBuffer = lightDepthTexture.Description; Matrix lightView = Matrix.LookAtLH(light, Vector3.Zero, up); Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _nearPlane, FarPlane); sceneConstants.LightViewProjection = lightView * lightProjection; using (var data = sceneConstantsBuffer.Map(MapMode.WriteDiscard)) { Marshal.StructureToPtr(sceneConstants, data.DataPointer, false); sceneConstantsBuffer.Unmap(); } effect2.GetVariableByName("InverseProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.Projection)); effect2.GetVariableByName("InverseView").AsMatrix().SetMatrix(sceneConstants.ViewInverse); effect2.GetVariableByName("LightInverseViewProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.LightViewProjection)); effect2.GetVariableByName("LightPosition").AsVector().Set(new Vector4(light, 1)); effect2.GetVariableByName("EyePosition").AsVector().Set(new Vector4(MathHelper.Convert(freelook.Eye), 1)); effect2.GetVariableByName("EyeZAxis").AsVector().Set(new Vector4(Vector3.Normalize(MathHelper.Convert(freelook.Target - freelook.Eye)), 1)); float tanHalfFOVY = (float)Math.Tan(FieldOfView * 0.5f); effect2.GetVariableByName("TanHalfFOVX").AsScalar().Set(tanHalfFOVY * AspectRatio); effect2.GetVariableByName("TanHalfFOVY").AsScalar().Set(tanHalfFOVY); float projectionA = FarPlane / (FarPlane - _nearPlane); float projectionB = -projectionA * _nearPlane; effect2.GetVariableByName("ProjectionA").AsScalar().Set(projectionA); effect2.GetVariableByName("ProjectionB").AsScalar().Set(projectionB); Matrix overlayMatrix = Matrix.Scaling(2 * info.Width / _width, 2 * info.Height / _height, 1.0f); overlayMatrix *= Matrix.Translation(-(_width - info.Width) / _width, (_height - info.Height) / _height, 0.0f); effect2.GetVariableByName("OverlayViewProjection").AsMatrix().SetMatrix(overlayMatrix); }
public void SetVertexBuffer(Device device, Vector3[] vertices) { BufferDescription vertexBufferDesc = new BufferDescription() { SizeInBytes = Marshal.SizeOf(typeof(Vector3)) * vertices.Length, Usage = ResourceUsage.Default, BindFlags = BindFlags.VertexBuffer }; using (var data = new SharpDX.DataStream(vertexBufferDesc.SizeInBytes, false, true)) { data.WriteRange(vertices); data.Position = 0; VertexBuffer = new Buffer(device, data, vertexBufferDesc); VertexBuffer.Unmap(); } BufferBindings[0] = new VertexBufferBinding(VertexBuffer, 24, 0); }
void makeSurface(int width, int height) { var vertices = new List <Vertex>(); for (int y = 0; y <= height; ++y) { for (int x = 0; x <= width; ++x) { vertices.Add(new Vertex(2 * (x - width / 2.0f) / width, 2 * (y - height / 2.0f) / height, 0, (float)x / width, (float)y / height)); } } var indices = new List <int>(); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { indices.Add(y * (width + 1) + x); indices.Add((y + 1) * (width + 1) + x + 1); indices.Add(y * (width + 1) + x + 1); indices.Add(y * (width + 1) + x); indices.Add((y + 1) * (width + 1) + x); indices.Add((y + 1) * (width + 1) + x + 1); } } //[TODO]なぜかBuffer作成時にDataStreamを指定するとメモリアクセス違反? //現状DynamicなBufferしか作れないのを何とかする //頂点バッファを作成して書き込み _surfaceVertexBuffer = new Buffer(_device, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = vertices.Count * Marshal.SizeOf(typeof(Vertex)), Usage = ResourceUsage.Dynamic }); var vertexStream = _surfaceVertexBuffer.Map(MapMode.WriteDiscard); vertexStream.WriteRange(vertices.ToArray()); _surfaceVertexBuffer.Unmap(); //インデックスバッファを作成して書き込み _surfaceIndexBuffer = new Buffer(_device, new BufferDescription() { BindFlags = BindFlags.IndexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = indices.Count * Marshal.SizeOf(typeof(int)), Usage = ResourceUsage.Dynamic }); var indexStream = _surfaceIndexBuffer.Map(MapMode.WriteDiscard); indexStream.WriteRange(indices.ToArray()); _surfaceIndexBuffer.Unmap(); _surfaceVertexCount = vertices.Count; _surfaceIndexCount = indices.Count; }
public void Update(float deltaTime) { if (_particleSystem == null) { return; } if (_playing == false) { return; } if (_paused) { return; } _timer += deltaTime; if (_timer >= _particleSystem.Duration && _particleSystem.Loop) { _timer = 0; ResetBurstEnumerator(); } float emissionTime = 1.0f / _particleSystem.Emission; _particleSpawnTimer += deltaTime; SortParticles(); if (_particleSystem.MaxParticles > _particles.Count) { int amount = _particleSystem.MaxParticles - _particles.Count; for (int i = 0; i < amount; i++) { _particles.Add(new Particle()); } } else if (_particles.Count > _particleSystem.MaxParticles) { _particles.RemoveRange(_particleSystem.MaxParticles, _particles.Count - _particleSystem.MaxParticles); } _particleCount = 0; DataStream vertexBufferStream = _vertexBuffer.Map(MapMode.WriteDiscard, SharpDX.Direct3D10.MapFlags.None); int burstParticles = 0; for (int i = 0; i < _particleSystem.MaxParticles; i++) { Particle p = _particles[i]; if (p.Active) { p.Update(deltaTime); vertexBufferStream.Write(p.VertexInfo); ++_particleCount; } else if (_hasNext && _timer > _burstEnumerator.Current.Key && burstParticles < _burstEnumerator.Current.Value) { p.Initialize(); vertexBufferStream.Write(p.VertexInfo); ++_particleCount; ++burstParticles; } else if (_particleSpawnTimer >= emissionTime && _timer < _particleSystem.Duration) { p.Initialize(); vertexBufferStream.Write(p.VertexInfo); ++_particleCount; _particleSpawnTimer -= emissionTime; } } if (_hasNext && _timer > _burstEnumerator.Current.Key) { _hasNext = _burstEnumerator.MoveNext(); } _vertexBuffer.Unmap(); vertexBufferStream.Dispose(); }