/* * public void RenderSoftBodyTextured(SoftBody softBody) * { * if (!(softBody.UserObject is Array)) * return; * * object[] userObjArr = softBody.UserObject as object[]; * FloatArray vertexBuffer = userObjArr[0] as FloatArray; * IntArray indexBuffer = userObjArr[1] as IntArray; * * int vertexCount = (vertexBuffer.Count / 8); * * if (vertexCount > 0) * { * int faceCount = indexBuffer.Count / 2; * * bool index32 = vertexCount > 65536; * * Mesh mesh = new Mesh(device, faceCount, vertexCount, * MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0), * VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture1); * * SlimDX.DataStream indices = mesh.LockIndexBuffer(LockFlags.Discard); * if (index32) * { * foreach (int i in indexBuffer) * indices.Write(i); * } * else * { * foreach (int i in indexBuffer) * indices.Write((ushort)i); * } * mesh.UnlockIndexBuffer(); * * SlimDX.DataStream verts = mesh.LockVertexBuffer(LockFlags.Discard); * foreach (float f in vertexBuffer) * verts.Write(f); * mesh.UnlockVertexBuffer(); * * mesh.ComputeNormals(); * mesh.DrawSubset(0); * mesh.Dispose(); * } * } * */ public static Buffer CreateScreenQuad(Device device) { Buffer vertexBuffer; BufferDescription vertexBufferDesc = new BufferDescription() { SizeInBytes = sizeof(float) * 5 * 4, Usage = ResourceUsage.Default, BindFlags = BindFlags.VertexBuffer, }; using (var data = new SharpDX.DataStream(vertexBufferDesc.SizeInBytes, false, true)) { data.Write(new Vector3(0.5f, 0.5f, 0)); data.Write(new Vector2(1, 0)); data.Write(new Vector3(0.5f, -0.5f, 0)); data.Write(new Vector2(1, 1)); data.Write(new Vector3(-0.5f, 0.5f, 0)); data.Write(new Vector2(0, 0)); data.Write(new Vector3(-0.5f, -0.5f, 0)); data.Write(new Vector2(0, 1)); data.Position = 0; vertexBuffer = new Buffer(device, data, vertexBufferDesc); } return(vertexBuffer); }
public static SharpDX.Direct3D11.Buffer CreateBuffer <T>(SharpDX.Direct3D11.Device1 device, BindFlags bindFlags, params T[] items) where T : struct { var len = Utilities.SizeOf(items); var stream = new DataStream(len, true, true); foreach (var item in items) { stream.Write(item); } stream.Position = 0; var buffer = new SharpDX.Direct3D11.Buffer(device, stream, len, ResourceUsage.Default, bindFlags, CpuAccessFlags.None, ResourceOptionFlags.None, 0); return(buffer); }
/// <summary> /// Creates the constant buffer object. /// </summary> protected internal override D3D11.Buffer CreateConstantBuffer(EngineDevice device) { using (var dataStream = new SharpDX.DataStream(SharpDX.Utilities.SizeOf <T>(), true, true)) { dataStream.Write(m_initialData); dataStream.Position = 0; return(new D3D11.Buffer( device.DeviceD3D11_1, dataStream, new D3D11.BufferDescription( m_structureSize, D3D11.ResourceUsage.Dynamic, D3D11.BindFlags.ConstantBuffer, D3D11.CpuAccessFlags.Write, D3D11.ResourceOptionFlags.None, 0))); } }
void ModifyVertexBuffer() { var bufferHandle = m_VertexBufferHandle; int vertexCount = m_VertexBufferVertexCount; if (bufferHandle == System.IntPtr.Zero) { return; } float t = m_Time * 3.0f; System.Func <int, byte[]> getPadding = bufferSize => { var stride = (int)(bufferSize / vertexCount); return(new byte[stride - (3 + 3 + 2) * 4]); }; System.Action <System.IntPtr, int, byte[]> callback = (ptr, size, padding) => { using (var stream = new SharpDX.DataStream(ptr, size, false, true)) { // modify vertex Y position with several scrolling sine waves, // copy the rest of the source data unmodified for (int i = 0; i < vertexCount; ++i) { var src = m_VertexSource[i]; stream.Write(src.pos.x); stream.Write(src.pos.y + UnityEngine.Mathf.Sin(src.pos[0] * 1.1f + t) * 0.4f + UnityEngine.Mathf.Sin(src.pos[2] * 0.9f - t) * 0.3f); stream.Write(src.pos.z); stream.Write(src.normal); stream.Write(src.uv); stream.Write(padding, 0, padding.Length); } } }; BeginModifyVertexBuffer(bufferHandle , getPadding , callback); }
/* public void RenderSoftBodyTextured(SoftBody softBody) { if (!(softBody.UserObject is Array)) return; object[] userObjArr = softBody.UserObject as object[]; FloatArray vertexBuffer = userObjArr[0] as FloatArray; IntArray indexBuffer = userObjArr[1] as IntArray; int vertexCount = (vertexBuffer.Count / 8); if (vertexCount > 0) { int faceCount = indexBuffer.Count / 2; bool index32 = vertexCount > 65536; Mesh mesh = new Mesh(device, faceCount, vertexCount, MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0), VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture1); SlimDX.DataStream indices = mesh.LockIndexBuffer(LockFlags.Discard); if (index32) { foreach (int i in indexBuffer) indices.Write(i); } else { foreach (int i in indexBuffer) indices.Write((ushort)i); } mesh.UnlockIndexBuffer(); SlimDX.DataStream verts = mesh.LockVertexBuffer(LockFlags.Discard); foreach (float f in vertexBuffer) verts.Write(f); mesh.UnlockVertexBuffer(); mesh.ComputeNormals(); mesh.DrawSubset(0); mesh.Dispose(); } } * */ public static Buffer CreateScreenQuad(Device device) { Buffer vertexBuffer; BufferDescription vertexBufferDesc = new BufferDescription() { SizeInBytes = sizeof(float) * 5 * 4, Usage = ResourceUsage.Default, BindFlags = BindFlags.VertexBuffer, }; using (var data = new DataStream(vertexBufferDesc.SizeInBytes, false, true)) { data.Write(new Vector3(0.5f, 0.5f, 0)); data.Write(new Vector2(1, 0)); data.Write(new Vector3(0.5f, -0.5f, 0)); data.Write(new Vector2(1, 1)); data.Write(new Vector3(-0.5f, 0.5f, 0)); data.Write(new Vector2(0, 0)); data.Write(new Vector3(-0.5f, -0.5f, 0)); data.Write(new Vector2(0, 1)); data.Position = 0; vertexBuffer = new Buffer(device, data, vertexBufferDesc); } return vertexBuffer; }