public async System.Threading.Tasks.Task <bool> Init(Graphics.CGfxMaterialInstance material, float halfWidth = 0.1F) { var rc = CEngine.Instance.RenderContext; Material = material; GeomMesh = new Graphics.Mesh.CGfxMeshPrimitives(); GeomMesh.Init(rc, null, 1); CDrawPrimitiveDesc dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.StartIndex = 0xFFFFFFFF; GeomMesh.PushAtomLOD(0, ref dpDesc); var Mesh = new Graphics.Mesh.CGfxMesh(); Mesh.Init(rc, null, GeomMesh); var img = new Graphics.Mesh.CGfxImage2D(); Mesh.Tag = img; UpdateGeomMesh(rc, halfWidth); GraphActor = GamePlay.Actor.GActor.NewMeshActorDirect(Mesh); var meshComp = GraphActor.GetComponent <GamePlay.Component.GMeshComponent>(); await meshComp.SetMaterialInstanceAsync(rc, 0, material, null); //await meshComp.SetMaterial(rc, 0, material, CEngine.Instance.PrebuildPassData.Image2DShadingEnvs, false, true); return(true); }
public void BuildFinalFaces2(List <List <int> > finalLods) { FinalFaces.Clear(); FinalLods.Clear(); int curStart = 0; for (int i = 0; i < finalLods.Count; i++) { var both = new List <int>(); for (int j = curStart; j < FinalFaces.Count; j++) { var at = finalLods[i].IndexOf(FinalFaces[j]); if (at >= 0) { both.Add(FinalFaces[j]); FinalFaces.RemoveAt(j); finalLods[i].RemoveAt(at); } } var desc = new CDrawPrimitiveDesc(); desc.SetDefault(); desc.StartIndex = (UInt32)FinalFaces.Count * 3; desc.NumPrimitives = (UInt32)(finalLods[i].Count + both.Count); FinalLods.Add(desc); FinalFaces.AddRange(both); curStart = FinalFaces.Count; FinalFaces.AddRange(finalLods[i]); } }
public void BuildIndexBuffer(EditableMesh mesh) { var curFaces = new List <int>(); for (var i = 0; i < Faces.Count; i++) { var f = mesh.GetFace(Faces[i]); if (f.Deleted) { continue; } curFaces.Add(Faces[i]); } Faces = curFaces; FaceLods.Add(curFaces); var desc = new CDrawPrimitiveDesc(); desc.SetDefault(); desc.StartIndex = 0; desc.NumPrimitives = (UInt32)Faces.Count; Lods.Clear(); Lods.Add(desc); System.Diagnostics.Debug.WriteLine($"Face({Mtl}) = {desc.NumPrimitives}"); }
public CDrawPrimitiveDesc this[UInt32 idx] { get { CDrawPrimitiveDesc desc = new CDrawPrimitiveDesc(); GetAtom(idx, 0, ref desc); return(desc); } }
public static void MakeCapsule(CRenderContext rc, CGfxMeshPrimitives result, float radius, float halfHeight, EBoxFace faceFlags = EBoxFace.All) { var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = 0; //result.AABB = new BoundingBox(x, y, z, x + xSize, y + ySize, z + zSize); }
public bool SetAtom(UInt32 index, UInt32 lod, ref CDrawPrimitiveDesc desc) { unsafe { fixed(CDrawPrimitiveDesc *p = &desc) { return((bool)SDK_GfxMeshPrimitives_SetAtom(CoreObject, index, lod, p)); } } }
public void PushAtomLOD(UInt32 index, ref CDrawPrimitiveDesc desc) { unsafe { fixed(CDrawPrimitiveDesc *p = &desc) { SDK_GfxMeshDataProvider_PushAtomLOD(CoreObject, index, p); } } }
async System.Threading.Tasks.Task InitUIDrawer() { var rc = EngineNS.CEngine.Instance.RenderContext; var font = CEngine.Instance.FontManager.GetFont(EngineNS.CEngine.Instance.Desc.DefaultFont, 24, 1024, 128); var mtl = await CEngine.Instance.MaterialInstanceManager.GetMaterialInstanceAsync(rc, RName.GetRName("Material/font.instmtl")); var textMeshInfo = new EngineNS.Bricks.FreeTypeFont.CFontMesh(); await textMeshInfo.SetMaterial(rc, mtl, "txDiffuse"); textMeshInfo.DrawText(rc, font, "模型", true); textMeshInfo.RenderMatrix = EngineNS.Matrix.Translate(20, 0, 0); //textMeshInfo.Offset = new Vector2(20, 0); //textMeshInfo.Scale = new Vector2(1, 1); var rp = mPreviewSceneControl.ViewPort.RPolicy as EngineNS.Graphics.RenderPolicy.CGfxRP_EditorMobile; rp.OnDrawUI += (cmd, view) => { var mesh = mPreviewActor.GetComponentMesh(null); string outInfo = $"材质数:{mesh.MeshPrimitives.AtomNumber}:"; for (int i = 0; i < mesh.MeshPrimitives.AtomNumber; i++) { CDrawPrimitiveDesc desc = new CDrawPrimitiveDesc(); mesh.MeshPrimitives.GetAtom((UInt32)i, 0, ref desc); var lodLvl = mesh.MeshPrimitives.GetLodLevel((UInt32)i, CurLod); CDrawPrimitiveDesc desc1 = new CDrawPrimitiveDesc(); mesh.MeshPrimitives.GetAtom((UInt32)i, lodLvl, ref desc1); outInfo += $"({desc1.NumPrimitives}/{desc.NumPrimitives}) "; } textMeshInfo.DrawText(rc, font, outInfo, true); for (int i = 0; i < textMeshInfo.PassNum; i++) { var pass = textMeshInfo.GetPass(i); if (pass == null) { continue; } pass.ViewPort = view.Viewport; pass.BindCBuffer(pass.Effect.ShaderProgram, pass.Effect.CacheData.CBID_View, view.ScreenViewCB); pass.ShadingEnv.BindResources(textMeshInfo.Mesh, pass); cmd.PushPass(pass); } }; }
public CDrawPrimitiveDesc this[UInt32 index, UInt32 lod] { get { CDrawPrimitiveDesc desc = new CDrawPrimitiveDesc(); unsafe { SDK_GfxMeshDataProvider_GetAtom(CoreObject, index, lod, &desc); } return(desc); } set { unsafe { SDK_GfxMeshDataProvider_SetAtom(CoreObject, index, lod, &value); } } }
public void BuildFinalFaces(List <List <int> > finalLods) { FinalFaces.Clear(); FinalLods.Clear(); int curStartFace = 0; for (int i = 0; i < finalLods.Count; i++) { FinalFaces.AddRange(finalLods[i]); var desc = new CDrawPrimitiveDesc(); desc.SetDefault(); desc.StartIndex = (UInt32)curStartFace * 3; desc.NumPrimitives = (UInt32)finalLods[i].Count; FinalLods.Add(desc); curStartFace += finalLods[i].Count; } }
public void BindBuffs(CRenderContext rc, CGfxMeshPrimitives result) { if (Positions.Count < 4) { return; } CCommandList cmd = rc.ImmCommandList; var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = (uint)Positions.Count - (ComposeCount * 2); result.SetAtom(0, 0, ref dpDesc); bool needcreate = Positions.Count > PrePositionCount || VertexIndexes.Count > PreIndexesCount; if (needcreate) { PrePositionCount = Positions.Count; } UInt32 resourceSize = 0; if (needcreate == false) { var mesh = result.GeometryMesh; UInt32 ByteWidth = 0; //mesh.Cleanup(); { var vb = mesh.GetVertexBuffer(EVertexSteamType.VST_Position); unsafe { ByteWidth = (UInt32)(sizeof(Vector3) * Positions.Count); vb.UpdateBuffData(cmd, Positions.UnsafeAddressAt(0), ByteWidth); resourceSize += ByteWidth; } } { var vb = mesh.GetVertexBuffer(EVertexSteamType.VST_Tangent); unsafe { ByteWidth = (UInt32)(sizeof(Vector4) * Tangents.Count); vb.UpdateBuffData(cmd, Tangents.UnsafeAddressAt(0), ByteWidth); resourceSize += ByteWidth; } } { var vb = mesh.GetVertexBuffer(EVertexSteamType.VST_Normal); unsafe { ByteWidth = (UInt32)(sizeof(Vector3) * Normals.Count); vb.UpdateBuffData(cmd, Normals.UnsafeAddressAt(0), ByteWidth); resourceSize += ByteWidth; } } { var vb = mesh.GetVertexBuffer(EVertexSteamType.VST_UV); unsafe { ByteWidth = (UInt32)(sizeof(Vector2) * UVs.Count); vb.UpdateBuffData(cmd, UVs.UnsafeAddressAt(0), ByteWidth); resourceSize += ByteWidth; } } { var vb = mesh.GetVertexBuffer(EVertexSteamType.VST_Color); unsafe { ByteWidth = (UInt32)(sizeof(Byte4) * Colors.Count); vb.UpdateBuffData(cmd, Colors.UnsafeAddressAt(0), ByteWidth); resourceSize += ByteWidth; } } { //if (VertexIndexes.Count == PreIndexesCount) //{ var ib = mesh.GetIndexBuffer(); unsafe { ByteWidth = (UInt32)(sizeof(UInt32) * VertexIndexes.Count); ib.UpdateBuffData(cmd, VertexIndexes.UnsafeAddressAt(0), ByteWidth); resourceSize += ByteWidth; } //} //else //{ // var ibDesc = new CIndexBufferDesc(); // ibDesc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE; // ibDesc.InitData = VertexIndexes.UnsafeAddressAt(0); // ibDesc.Type = EIndexBufferType.IBT_Int32; // ibDesc.ByteWidth = (UInt32)(sizeof(UInt32) * VertexIndexes.Count); // var ib = rc.CreateIndexBuffer(ibDesc); // mesh.BindIndexBuffer(ib); // resourceSize += ibDesc.ByteWidth; // PreIndexesCount = VertexIndexes.Count; //} } result.ResourceState.ResourceSize = (UInt32)(resourceSize); result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; result.GeometryMesh.Dirty = true; return; } //result.GeometryMesh.Cleanup(); unsafe { var vbDesc = new CVertexBufferDesc(); vbDesc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE; var mesh = result.GeometryMesh; //mesh.Cleanup(); { vbDesc.InitData = Positions.UnsafeAddressAt(0); vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)(sizeof(Vector3) * Positions.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Position, vb); resourceSize += vbDesc.ByteWidth; } { vbDesc.InitData = Tangents.UnsafeAddressAt(0); vbDesc.Stride = (UInt32)sizeof(Vector4); vbDesc.ByteWidth = (UInt32)(sizeof(Vector4) * Tangents.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Tangent, vb); resourceSize += vbDesc.ByteWidth; } { vbDesc.InitData = Normals.UnsafeAddressAt(0); vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)(sizeof(Vector3) * Normals.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Normal, vb); resourceSize += vbDesc.ByteWidth; } { vbDesc.InitData = UVs.UnsafeAddressAt(0); vbDesc.Stride = (UInt32)sizeof(Vector2); vbDesc.ByteWidth = (UInt32)(sizeof(Vector2) * UVs.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_UV, vb); resourceSize += vbDesc.ByteWidth; } { vbDesc.InitData = Colors.UnsafeAddressAt(0); vbDesc.Stride = (UInt32)sizeof(Byte4); vbDesc.ByteWidth = (UInt32)(sizeof(Byte4) * Colors.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Color, vb); resourceSize += vbDesc.ByteWidth; } { var ibDesc = new CIndexBufferDesc(); ibDesc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE; ibDesc.InitData = VertexIndexes.UnsafeAddressAt(0); ibDesc.Type = EIndexBufferType.IBT_Int32; ibDesc.ByteWidth = (UInt32)(sizeof(UInt32) * VertexIndexes.Count); var ib = rc.CreateIndexBuffer(ibDesc); mesh.BindIndexBuffer(ib); resourceSize += ibDesc.ByteWidth; PreIndexesCount = VertexIndexes.Count; } result.ResourceState.ResourceSize = (UInt32)(resourceSize); mesh.Dirty = true; } result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
public static void MakeBox(CRenderContext rc, CGfxMeshDataProvider result, float x, float y, float z, float xSize, float ySize, float zSize, EBoxFace faceFlags = EBoxFace.All) { var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = 0; unsafe { UInt32 streams = (1 << (int)EVertexSteamType.VST_Position) | (1 << (int)EVertexSteamType.VST_Normal) | (1 << (int)EVertexSteamType.VST_UV); result.Init(streams, EIndexBufferType.IBT_Int16, 1); // 顶点 //var vbDesc = new CVertexBufferDesc(); var vbPos = new Vector3[8]; vbPos[0].SetValue(x, y, z); vbPos[1].SetValue(x, y + ySize, z); vbPos[2].SetValue(x + xSize, y + ySize, z); vbPos[3].SetValue(x + xSize, y, z); vbPos[4].SetValue(x, y, z + zSize); vbPos[5].SetValue(x, y + ySize, z + zSize); vbPos[6].SetValue(x + xSize, y + ySize, z + zSize); vbPos[7].SetValue(x + xSize, y, z + zSize); var pPos = new Vector3[24]; // front side pPos[0] = vbPos[0]; pPos[1] = vbPos[1]; pPos[2] = vbPos[2]; pPos[3] = vbPos[3]; // back side pPos[4] = vbPos[4]; pPos[5] = vbPos[5]; pPos[6] = vbPos[6]; pPos[7] = vbPos[7]; // left side pPos[8] = vbPos[4]; pPos[9] = vbPos[5]; pPos[10] = vbPos[1]; pPos[11] = vbPos[0]; // right side pPos[12] = vbPos[3]; pPos[13] = vbPos[2]; pPos[14] = vbPos[6]; pPos[15] = vbPos[7]; // top side pPos[16] = vbPos[1]; pPos[17] = vbPos[5]; pPos[18] = vbPos[6]; pPos[19] = vbPos[2]; // bottom side pPos[20] = vbPos[4]; pPos[21] = vbPos[0]; pPos[22] = vbPos[3]; pPos[23] = vbPos[7]; UInt32 curIndex = 0; // 索引 UInt16[] pIndex = new UInt16[36]; if ((faceFlags & EBoxFace.Front) != 0) {// front side pIndex[curIndex++] = 0; pIndex[curIndex++] = 1; pIndex[curIndex++] = 2; pIndex[curIndex++] = 0; pIndex[curIndex++] = 2; pIndex[curIndex++] = 3; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Back) != 0) { // back side pIndex[curIndex++] = 4; pIndex[curIndex++] = 6; pIndex[curIndex++] = 5; pIndex[curIndex++] = 4; pIndex[curIndex++] = 7; pIndex[curIndex++] = 6; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Left) != 0) { // left side pIndex[curIndex++] = 8; pIndex[curIndex++] = 9; pIndex[curIndex++] = 10; pIndex[curIndex++] = 8; pIndex[curIndex++] = 10; pIndex[curIndex++] = 11; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Right) != 0) { // right side pIndex[curIndex++] = 12; pIndex[curIndex++] = 13; pIndex[curIndex++] = 14; pIndex[curIndex++] = 12; pIndex[curIndex++] = 14; pIndex[curIndex++] = 15; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Top) != 0) { // top side pIndex[curIndex++] = 16; pIndex[curIndex++] = 17; pIndex[curIndex++] = 18; pIndex[curIndex++] = 16; pIndex[curIndex++] = 18; pIndex[curIndex++] = 19; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Bottom) != 0) { // bottom side pIndex[curIndex++] = 20; pIndex[curIndex++] = 21; pIndex[curIndex++] = 22; pIndex[curIndex++] = 20; pIndex[curIndex++] = 22; pIndex[curIndex++] = 23; dpDesc.NumPrimitives += 2; } // UV var vLB = new Vector2(0, 1); var vLT = new Vector2(0, 0); var vRT = new Vector2(1, 0); var vRB = new Vector2(1, 1); var vUV = new Vector2[24]; vUV[0] = vLB; vUV[1] = vLT; vUV[2] = vRT; vUV[3] = vRB; vUV[4] = vRB; vUV[5] = vRT; vUV[6] = vLT; vUV[7] = vLB; vUV[8] = vLB; vUV[9] = vLT; vUV[10] = vRT; vUV[11] = vRB; vUV[12] = vLB; vUV[13] = vLT; vUV[14] = vRT; vUV[15] = vRB; vUV[16] = vLB; vUV[17] = vLT; vUV[18] = vRT; vUV[19] = vRB; vUV[20] = vLB; vUV[21] = vLT; vUV[22] = vRT; vUV[23] = vRB; // 法线 var vFront = new Vector3(0, 0, -1); var vBack = new Vector3(0, 0, 1); var vLeft = new Vector3(-1, 0, 0); var vRight = new Vector3(1, 0, 0); var vTop = new Vector3(0, 1, 0); var vBottom = new Vector3(0, -1, 0); var vNor = new Vector3[24]; vNor[0] = vFront; vNor[1] = vFront; vNor[2] = vFront; vNor[3] = vFront; vNor[4] = vBack; vNor[5] = vBack; vNor[6] = vBack; vNor[7] = vBack; vNor[8] = vLeft; vNor[9] = vLeft; vNor[10] = vLeft; vNor[11] = vLeft; vNor[12] = vRight; vNor[13] = vRight; vNor[14] = vRight; vNor[15] = vRight; vNor[16] = vTop; vNor[17] = vTop; vNor[18] = vTop; vNor[19] = vTop; vNor[20] = vBottom; vNor[21] = vBottom; vNor[22] = vBottom; vNor[23] = vBottom; for (int i = 0; i < 24; i++) { //result.AddVertex(ref pPos[i], ref vNor[i], ref vUV[i], 0); } for (int i = 0; i < dpDesc.NumPrimitives * 2; i++) { //result.AddTriangle(pIndex[i * 3 + 0], pIndex[i * 3 + 1], pIndex[i * 3 + 2]); } } result.PushAtomLOD(0, ref dpDesc); }
public static void MakePlane10x10(CRenderContext rc, CGfxMeshPrimitives result, Vector2 uvMin, Vector2 uvMax, bool lh = true, bool flipUVWhenGL = false) { UInt32 tileCount = 10; var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = tileCount * tileCount * 2; result.PushAtomLOD(0, ref dpDesc); using (var posArray = Support.NativeListProxy <Vector3> .CreateNativeList()) using (var normalArray = Support.NativeListProxy <Vector3> .CreateNativeList()) using (var tangentArray = Support.NativeListProxy <Vector4> .CreateNativeList()) using (var uvArray = Support.NativeListProxy <Vector2> .CreateNativeList()) using (var indexArray = Support.NativeListProxy <UInt32> .CreateNativeList()) { // -> tileCount * tileCount * 2 triangles float Step = 2.0f / tileCount; for (Int32 y = 0; y < tileCount; ++y) { // implemented this way to avoid cracks, could be optimized float z0 = y * Step - 1.0f; float z1 = (y + 1) * Step - 1.0f; float V0 = Lerp(uvMin.Y, uvMax.Y, z0 * 0.5f + 0.5f); float V1 = Lerp(uvMin.Y, uvMax.Y, z1 * 0.5f + 0.5f); for (Int32 x = 0; x < tileCount; ++x) { // implemented this way to avoid cracks, could be optimized float x0 = x * Step - 1.0f; float x1 = (x + 1) * Step - 1.0f; float U0 = Lerp(uvMin.X, uvMax.X, x0 * 0.5f + 0.5f); float U1 = Lerp(uvMin.X, uvMax.X, x1 * 0.5f + 0.5f); // Calculate verts for a face pointing down Z posArray.Add(new Vector3(x0, 0, z0)); posArray.Add(new Vector3(x0, 0, z1)); posArray.Add(new Vector3(x1, 0, z1)); posArray.Add(new Vector3(x1, 0, z0)); normalArray.Add(new Vector3(0, 1, 0)); normalArray.Add(new Vector3(0, 1, 0)); normalArray.Add(new Vector3(0, 1, 0)); normalArray.Add(new Vector3(0, 1, 0)); tangentArray.Add(new Vector4(1, 0, 0, 1)); tangentArray.Add(new Vector4(1, 0, 0, 1)); tangentArray.Add(new Vector4(1, 0, 0, 1)); tangentArray.Add(new Vector4(1, 0, 0, 1)); uvArray.Add(new Vector2(U0, V0)); uvArray.Add(new Vector2(U0, V1)); uvArray.Add(new Vector2(U1, V1)); uvArray.Add(new Vector2(U1, V0)); UInt32 Index = (UInt32)((x + y * tileCount) * 4); indexArray.Add(Index + 0); indexArray.Add(Index + 1); indexArray.Add(Index + 2); indexArray.Add(Index + 0); indexArray.Add(Index + 2); indexArray.Add(Index + 3); } } unsafe { UInt32 resourceSize = 0; var vbDesc = new CVertexBufferDesc(); var mesh = result.GeometryMesh; { var pV3 = posArray.UnsafeGetElementAddress(0); vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)(sizeof(Vector3) * posArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Position, vb); resourceSize += vbDesc.ByteWidth; } { var pV3 = normalArray.UnsafeGetElementAddress(0); vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)(sizeof(Vector3) * normalArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Normal, vb); resourceSize += vbDesc.ByteWidth; } { var pV3 = tangentArray.UnsafeGetElementAddress(0); vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector4); vbDesc.ByteWidth = (UInt32)(sizeof(Vector4) * tangentArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Tangent, vb); resourceSize += vbDesc.ByteWidth; } { var pV = uvArray.UnsafeGetElementAddress(0); vbDesc.InitData = (IntPtr)pV; vbDesc.Stride = (UInt32)sizeof(Vector2); vbDesc.ByteWidth = (UInt32)(sizeof(Vector2) * uvArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_UV, vb); resourceSize += vbDesc.ByteWidth; } { var pV = indexArray.UnsafeGetElementAddress(0); var ibDesc = new CIndexBufferDesc(); ibDesc.InitData = (IntPtr)pV; ibDesc.Type = EIndexBufferType.IBT_Int32; ibDesc.ByteWidth = (UInt32)(sizeof(UInt32) * indexArray.Count); var ib = rc.CreateIndexBuffer(ibDesc); mesh.BindIndexBuffer(ib); resourceSize += ibDesc.ByteWidth; } result.ResourceState.ResourceSize = (UInt32)(resourceSize); mesh.Dirty = true; } } result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
public unsafe bool BuildClusterFromMeshSource(CRenderContext rc, RName meshSourceName) { Graphics.Mesh.CGfxMeshDataProvider meshSource = new Graphics.Mesh.CGfxMeshDataProvider(); var meshPrimitive = CEngine.Instance.MeshPrimitivesManager.GetMeshPrimitives(rc, meshSourceName, true); meshSource.InitFromMesh(rc, meshPrimitive); uint a, b, c; a = 0; b = 0; c = 0; List <Cluster> clusterArray = new List <Cluster>(); int vertNum = meshSource.VertexNumber; Vector3 *pPos = (Vector3 *)meshSource.GetVertexPtr(EVertexSteamType.VST_Position, 0).ToPointer(); Vector3 *pNor = (Vector3 *)meshSource.GetVertexPtr(EVertexSteamType.VST_Normal, 0).ToPointer(); Vector4 *pTan = (Vector4 *)meshSource.GetVertexPtr(EVertexSteamType.VST_Tangent, 0).ToPointer(); Vector2 *pDiffUV = (Vector2 *)meshSource.GetVertexPtr(EVertexSteamType.VST_UV, 0).ToPointer(); GpuSceneVertex vert = new GpuSceneVertex(); for (int i = 0; i < vertNum; i++) { vert.Reset(); if (pPos != null) { vert.Position = pPos[i]; } if (pNor != null) { vert.Normal = pNor[i]; } if (pTan != null) { vert.Tangent = pTan[i]; } if (pDiffUV != null) { vert.DiffuseU = pDiffUV[i].X; vert.DiffuseV = pDiffUV[i].Y; } MeshVertices.Add(vert); } for (int i = 0; i < meshSource.TriangleNumber; i++) { meshSource.GetTriangle(i, ref a, ref b, ref c); IndexBuffer.Add(a); IndexBuffer.Add(b); IndexBuffer.Add(c); } Cluster curCluster = null; Vector3 *pPosPtr = (Vector3 *)meshSource.GetVertexPtr(EVertexSteamType.VST_Position, 0).ToPointer(); for (uint i = 0; i < meshSource.AtomNumber; i++) { CDrawPrimitiveDesc desc = meshSource[i, 0]; if (desc.PrimitiveType != EPrimitiveType.EPT_TriangleList) { return(false); } uint startFace = desc.StartIndex / 3; for (uint j = 0; j < desc.NumPrimitives; j++) { if (curCluster == null || curCluster.Data.FaceCount >= 64) { curCluster = new Cluster(); curCluster.Data.InstanceId = i; curCluster.Data.StartFaceIndex = startFace; curCluster.Data.FaceCount = 0; clusterArray.Add(curCluster); } startFace++; curCluster.Data.FaceCount++; } } ClusterDatas.Clear(); for (int i = 0; i < clusterArray.Count; i++) { clusterArray[i].Proccess(pPosPtr, IndexBuffer); ClusterDatas.Add(clusterArray[i].Data); } return(true); }
public static void MakeRect3D(CRenderContext rc, CGfxMeshPrimitives result) { var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = 2; result.PushAtomLOD(0, ref dpDesc); List <Vector3> posArray = new List <Vector3>(); List <Vector3> normalArray = new List <Vector3>(); List <Vector4> tangentArray = new List <Vector4>(); List <Vector2> uvArray = new List <Vector2>(); List <UInt32> indexArray = new List <UInt32>(); List <Byte4> Colors = new List <Byte4>(); Byte4 color = new Byte4(); color.X = 255; color.Y = 255; color.Z = 255; color.W = 255; Colors.Add(color); Colors.Add(color); Colors.Add(color); Colors.Add(color); // -> tileCount * tileCount * 2 triangles float x0 = -0.5f; float x1 = 0.5f; float y0 = -0.5f; float y1 = 0.5f; float V0 = 0.0f; float V1 = 1.0f; float U0 = 0.0f; float U1 = 1.0f; posArray.Add(new Vector3(x0, y0, 0)); posArray.Add(new Vector3(x1, y0, 0)); posArray.Add(new Vector3(x1, y1, 0)); posArray.Add(new Vector3(x0, y1, 0)); normalArray.Add(new Vector3(0, 0, 1)); normalArray.Add(new Vector3(0, 0, 1)); normalArray.Add(new Vector3(0, 0, 1)); normalArray.Add(new Vector3(0, 0, 1)); uvArray.Add(new Vector2(U1, V1)); uvArray.Add(new Vector2(U0, V1)); uvArray.Add(new Vector2(U0, V0)); uvArray.Add(new Vector2(U1, V0)); var targent = CalculationTargent(posArray[0], posArray[3], posArray[1], normalArray[0], uvArray[0], uvArray[3], uvArray[1]); tangentArray.Add(new Vector4(targent.X, targent.Y, targent.Z, 0)); targent = CalculationTargent(posArray[1], posArray[0], posArray[2], normalArray[1], uvArray[1], uvArray[0], uvArray[2]); tangentArray.Add(new Vector4(targent.X, targent.Y, targent.Z, 0)); targent = CalculationTargent(posArray[2], posArray[1], posArray[3], normalArray[2], uvArray[2], uvArray[1], uvArray[3]); tangentArray.Add(new Vector4(targent.X, targent.Y, targent.Z, 0)); targent = CalculationTargent(posArray[3], posArray[0], posArray[2], normalArray[3], uvArray[3], uvArray[0], uvArray[2]); tangentArray.Add(new Vector4(targent.X, targent.Y, targent.Z, 0)); indexArray.Add(0); indexArray.Add(1); indexArray.Add(2); indexArray.Add(0); indexArray.Add(2); indexArray.Add(3); unsafe { UInt32 resourceSize = 0; var vbDesc = new CVertexBufferDesc(); var mesh = result.GeometryMesh; fixed(Vector3 *pV3 = &posArray.ToArray()[0]) { vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)(sizeof(Vector3) * posArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Position, vb); resourceSize += vbDesc.ByteWidth; } fixed(Byte4 *pc = &Colors.ToArray()[0]) { vbDesc.InitData = (IntPtr)pc; vbDesc.Stride = (UInt32)sizeof(Byte4); vbDesc.ByteWidth = (UInt32)(sizeof(Byte4) * Colors.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Color, vb); resourceSize += vbDesc.ByteWidth; } fixed(Vector3 *pV3 = &normalArray.ToArray()[0]) { vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)(sizeof(Vector3) * normalArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Normal, vb); resourceSize += vbDesc.ByteWidth; } fixed(Vector4 *pV4 = &tangentArray.ToArray()[0]) { vbDesc.InitData = (IntPtr)pV4; vbDesc.Stride = (UInt32)sizeof(Vector4); vbDesc.ByteWidth = (UInt32)(sizeof(Vector4) * tangentArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Tangent, vb); resourceSize += vbDesc.ByteWidth; } fixed(Vector2 *pV = &uvArray.ToArray()[0]) { vbDesc.InitData = (IntPtr)pV; vbDesc.Stride = (UInt32)sizeof(Vector2); vbDesc.ByteWidth = (UInt32)(sizeof(Vector2) * uvArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_UV, vb); resourceSize += vbDesc.ByteWidth; } fixed(UInt32 *pV = &indexArray.ToArray()[0]) { var ibDesc = new CIndexBufferDesc(); ibDesc.InitData = (IntPtr)pV; ibDesc.Type = EIndexBufferType.IBT_Int32; ibDesc.ByteWidth = (UInt32)(sizeof(UInt32) * indexArray.Count); var ib = rc.CreateIndexBuffer(ibDesc); mesh.BindIndexBuffer(ib); resourceSize += ibDesc.ByteWidth; } result.ResourceState.ResourceSize = (UInt32)(resourceSize); mesh.Dirty = true; } result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
public void SetPassData(CRenderContext rc, CGfxMesh mesh, UInt32 index, CGfxSceneView vp, CPass pass) { //OnEffectRefresh(rc); if (pass.RenderPipeline == null) { var rplDesc = new CRenderPipelineDesc(); pass.RenderPipeline = rc.CreateRenderPipeline(rplDesc); } pass.RenderPipeline.ShaderProgram = mEffect.ShaderProgram; pass.RenderPipeline.RasterizerState = mMaterial.RasterizerState; pass.RenderPipeline.DepthStencilState = mMaterial.DepthStencilState; pass.RenderPipeline.BlendState = mMaterial.BlendState; pass.GeometryMesh = mesh.MeshPrimitives.GeometryMesh; pass.BindCBuffer(mEffect.ShaderProgram, mEffect.PerInstanceId, CBuffer); pass.BindCBuffer(mEffect.ShaderProgram, mEffect.PerFrameId, CEngine.Instance.PerFrameCBuffer); if (vp != null) { pass.ViewPort = vp.Viewport; } var textures = new CShaderResources(); { Material.BindTextures(textures, mEffect.ShaderProgram); foreach (var i in Textures) { textures.PSBindTexture(i.Key, i.Value); } mesh.ShadingEnv.BindResources(mesh, CBuffer, textures, mEffect.ShaderProgram); } pass.ShaderResources = textures; CShaderSamplers samplers = new CShaderSamplers(); { CSamplerStateDesc sampDesc = new CSamplerStateDesc(); sampDesc.SetDefault(); sampDesc.Filter = ESamplerFilter.SPF_MIN_MAG_MIP_POINT; CSamplerState samp = CEngine.Instance.SamplerStateManager.GetSamplerState(rc, sampDesc); for (UInt32 i = 0; i < mEffect.ShaderProgram.SamplerNumber; i++) { CSamplerBindInfo info = new CSamplerBindInfo(); mEffect.ShaderProgram.GetSamplerBindInfo(i, ref info); samplers.PSBindSampler(info.PSBindPoint, samp); } } pass.ShaderSamplers = samplers; foreach (var i in mesh.MdfQueue.Modifiers) { i.OnSetPassData(pass); } CDrawPrimitiveDesc dpDesc = new CDrawPrimitiveDesc(); mesh.MeshPrimitives.GetAtom(index, 0, ref dpDesc); pass.BindDrawPrimitive(ref dpDesc); }
public static void MakeBox(CRenderContext rc, CGfxMeshPrimitives result, float x, float y, float z, float xSize, float ySize, float zSize, EBoxFace faceFlags = EBoxFace.All) { var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = 0; result.AABB = new BoundingBox(x, y, z, x + xSize, y + ySize, z + zSize); unsafe { UInt32 resourceSize = 0; var mesh = result.GeometryMesh; // 顶点 var vbDesc = new CVertexBufferDesc(); var vbPos = new Vector3[8]; vbPos[0].SetValue(x, y, z); vbPos[1].SetValue(x, y + ySize, z); vbPos[2].SetValue(x + xSize, y + ySize, z); vbPos[3].SetValue(x + xSize, y, z); vbPos[4].SetValue(x, y, z + zSize); vbPos[5].SetValue(x, y + ySize, z + zSize); vbPos[6].SetValue(x + xSize, y + ySize, z + zSize); vbPos[7].SetValue(x + xSize, y, z + zSize); var pPos = new Vector3[24]; // front side pPos[0] = vbPos[0]; pPos[1] = vbPos[1]; pPos[2] = vbPos[2]; pPos[3] = vbPos[3]; // back side pPos[4] = vbPos[4]; pPos[5] = vbPos[5]; pPos[6] = vbPos[6]; pPos[7] = vbPos[7]; // left side pPos[8] = vbPos[4]; pPos[9] = vbPos[5]; pPos[10] = vbPos[1]; pPos[11] = vbPos[0]; // right side pPos[12] = vbPos[3]; pPos[13] = vbPos[2]; pPos[14] = vbPos[6]; pPos[15] = vbPos[7]; // top side pPos[16] = vbPos[1]; pPos[17] = vbPos[5]; pPos[18] = vbPos[6]; pPos[19] = vbPos[2]; // bottom side pPos[20] = vbPos[4]; pPos[21] = vbPos[0]; pPos[22] = vbPos[3]; pPos[23] = vbPos[7]; fixed(Vector3 *pv3 = &pPos[0]) { vbDesc.InitData = (IntPtr)pv3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)sizeof(Vector3) * 24; resourceSize += vbDesc.ByteWidth; var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Position, vb); } UInt32 curIndex = 0; // 索引 UInt16[] pIndex = new UInt16[36]; if ((faceFlags & EBoxFace.Front) != 0) {// front side pIndex[curIndex++] = 0; pIndex[curIndex++] = 1; pIndex[curIndex++] = 2; pIndex[curIndex++] = 0; pIndex[curIndex++] = 2; pIndex[curIndex++] = 3; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Back) != 0) { // back side pIndex[curIndex++] = 4; pIndex[curIndex++] = 6; pIndex[curIndex++] = 5; pIndex[curIndex++] = 4; pIndex[curIndex++] = 7; pIndex[curIndex++] = 6; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Left) != 0) { // left side pIndex[curIndex++] = 8; pIndex[curIndex++] = 9; pIndex[curIndex++] = 10; pIndex[curIndex++] = 8; pIndex[curIndex++] = 10; pIndex[curIndex++] = 11; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Right) != 0) { // right side pIndex[curIndex++] = 12; pIndex[curIndex++] = 13; pIndex[curIndex++] = 14; pIndex[curIndex++] = 12; pIndex[curIndex++] = 14; pIndex[curIndex++] = 15; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Top) != 0) { // top side pIndex[curIndex++] = 16; pIndex[curIndex++] = 17; pIndex[curIndex++] = 18; pIndex[curIndex++] = 16; pIndex[curIndex++] = 18; pIndex[curIndex++] = 19; dpDesc.NumPrimitives += 2; } if ((faceFlags & EBoxFace.Bottom) != 0) { // bottom side pIndex[curIndex++] = 20; pIndex[curIndex++] = 21; pIndex[curIndex++] = 22; pIndex[curIndex++] = 20; pIndex[curIndex++] = 22; pIndex[curIndex++] = 23; dpDesc.NumPrimitives += 2; } fixed(UInt16 *pV = &pIndex[0]) { var ibDesc = new CIndexBufferDesc(); ibDesc.InitData = (IntPtr)pV; ibDesc.ByteWidth = (UInt32)sizeof(UInt16) * curIndex; resourceSize += ibDesc.ByteWidth; var ib = rc.CreateIndexBuffer(ibDesc); mesh.BindIndexBuffer(ib); } // UV var vLB = new Vector2(0, 1); var vLT = new Vector2(0, 0); var vRT = new Vector2(1, 0); var vRB = new Vector2(1, 1); var vUV = new Vector2[24]; vUV[0] = vLB; vUV[1] = vLT; vUV[2] = vRT; vUV[3] = vRB; vUV[4] = vRB; vUV[5] = vRT; vUV[6] = vLT; vUV[7] = vLB; vUV[8] = vLB; vUV[9] = vLT; vUV[10] = vRT; vUV[11] = vRB; vUV[12] = vLB; vUV[13] = vLT; vUV[14] = vRT; vUV[15] = vRB; vUV[16] = vLB; vUV[17] = vLT; vUV[18] = vRT; vUV[19] = vRB; vUV[20] = vLB; vUV[21] = vLT; vUV[22] = vRT; vUV[23] = vRB; fixed(Vector2 *pUV = &vUV[0]) { vbDesc.InitData = (IntPtr)pUV; vbDesc.Stride = (UInt32)sizeof(Vector2); vbDesc.ByteWidth = (UInt32)sizeof(Vector2) * 24; resourceSize += vbDesc.ByteWidth; var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_UV, vb); } // 法线 var vFront = new Vector3(0, 0, -1); var vBack = new Vector3(0, 0, 1); var vLeft = new Vector3(-1, 0, 0); var vRight = new Vector3(1, 0, 0); var vTop = new Vector3(0, 1, 0); var vBottom = new Vector3(0, -1, 0); var vNor = new Vector3[24]; vNor[0] = vFront; vNor[1] = vFront; vNor[2] = vFront; vNor[3] = vFront; vNor[4] = vBack; vNor[5] = vBack; vNor[6] = vBack; vNor[7] = vBack; vNor[8] = vLeft; vNor[9] = vLeft; vNor[10] = vLeft; vNor[11] = vLeft; vNor[12] = vRight; vNor[13] = vRight; vNor[14] = vRight; vNor[15] = vRight; vNor[16] = vTop; vNor[17] = vTop; vNor[18] = vTop; vNor[19] = vTop; vNor[20] = vBottom; vNor[21] = vBottom; vNor[22] = vBottom; vNor[23] = vBottom; fixed(Vector3 *pNor = &vNor[0]) { vbDesc.InitData = (IntPtr)pNor; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)sizeof(Vector3) * 24; resourceSize += vbDesc.ByteWidth; var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Normal, vb); } mesh.Dirty = true; result.ResourceState.ResourceSize = resourceSize; } result.PushAtomLOD(0, ref dpDesc); result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
public static void MakeRect2D(CRenderContext rc, CGfxMeshPrimitives result, float x, float y, float w, float h, float z, bool lh = true, bool flipUVWhenGL = false, ECpuAccess cpuAccess = (ECpuAccess)0) { CDrawPrimitiveDesc dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = 2; result.PushAtomLOD(0, ref dpDesc); unsafe { UInt16[] ibData = new UInt16[6]; ibData[0] = 0; ibData[1] = 1; ibData[2] = 2; ibData[3] = 0; ibData[4] = 2; ibData[5] = 3; if (lh == false) { ibData[1] = 2; ibData[2] = 1; ibData[4] = 3; ibData[5] = 2; } var mesh = result.GeometryMesh; var vbDesc = new CVertexBufferDesc(); Vector3[] vbPos = new Vector3[4]; vbPos[0].SetValue(x, y, z); vbPos[1].SetValue(x + w, y, z); vbPos[2].SetValue(x + w, y + h, z); vbPos[3].SetValue(x, y + h, z); fixed(Vector3 *pV3 = &vbPos[0]) { vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)sizeof(Vector3) * 4; vbDesc.CPUAccess = (uint)cpuAccess; var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Position, vb); } Vector2[] vbUV = new Vector2[4]; vbUV[0].SetValue(0, 0); vbUV[1].SetValue(1, 0); vbUV[2].SetValue(1, 1); vbUV[3].SetValue(0, 1); if (lh == false && CEngine.Instance.Desc.RHIType == ERHIType.RHT_D3D11) {//全屏幕用的-1,-1,2,2这里要换手系 vbUV[0].Y = 1 - vbUV[0].Y; vbUV[1].Y = 1 - vbUV[1].Y; vbUV[2].Y = 1 - vbUV[2].Y; vbUV[3].Y = 1 - vbUV[3].Y; } else if (flipUVWhenGL && CEngine.Instance.Desc.RHIType == ERHIType.RHT_OGL) { vbUV[0].Y = 1 - vbUV[0].Y; vbUV[1].Y = 1 - vbUV[1].Y; vbUV[2].Y = 1 - vbUV[2].Y; vbUV[3].Y = 1 - vbUV[3].Y; } fixed(Vector2 *pV2 = &vbUV[0]) { vbDesc.InitData = (IntPtr)pV2; vbDesc.Stride = (UInt32)sizeof(Vector2); vbDesc.ByteWidth = (UInt32)sizeof(Vector2) * 4; vbDesc.CPUAccess = (uint)cpuAccess; var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_UV, vb); } fixed(UInt16 *pV = &ibData[0]) { var ibDesc = new CIndexBufferDesc(); ibDesc.InitData = (IntPtr)pV; ibDesc.ByteWidth = (UInt32)sizeof(UInt16) * 6; var ib = rc.CreateIndexBuffer(ibDesc); mesh.BindIndexBuffer(ib); } result.ResourceState.ResourceSize = 20 * 4; mesh.Dirty = true; } result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
public void DrawScene(CRenderContext rc, CCommandList cmd, Graphics.CGfxCamera Camera, Graphics.View.CGfxSceneView view) { if (EnableDraw == false) { return; } if (mNeedUpdateGpuBuffer) { mNeedUpdateGpuBuffer = false; UpdateGpuBuffer(rc, cmd, Camera); } var program = mPass.Effect.ShaderProgram; if (UseComputeShader == false) { UpdateIndexBufferCPU(cmd, Camera); } else { ComputeDispatch(rc, cmd, Camera); } if (UseVTF) { CTextureBindInfo tbInfo = new CTextureBindInfo(); if (program.FindTextureBindInfo(null, "gVertexTexture", ref tbInfo)) { mPass.ShaderResources.VSBindTexture(tbInfo.VSBindPoint, mVertexTextureView); } if (program.FindTextureBindInfo(null, "gInstanceDataTexture", ref tbInfo)) { mPass.ShaderResources.VSBindTexture(tbInfo.VSBindPoint, mInstanceDataTextureView); } var spInfo = new CSamplerBindInfo(); if (program.FindSamplerBindInfo(null, "Samp_gVertexTexture", ref spInfo)) { mPass.ShaderSamplerBinder.VSBindSampler(spInfo.VSBindPoint, mSamplerState); } if (program.FindSamplerBindInfo(null, "Samp_gInstanceDataTexture", ref spInfo)) { mPass.ShaderSamplerBinder.VSBindSampler(spInfo.VSBindPoint, mSamplerState); } } else { //CTextureBindInfo tbInfo = new CTextureBindInfo(); //if(program.FindTextureBindInfo(null, "AllVertexArray", ref tbInfo)) //{ // mPass.ShaderResources.VSBindTexture(tbInfo.VSBindPoint, mAllVertexSRV); //} //if (program.FindTextureBindInfo(null, "MeshInstanceArray", ref tbInfo)) //{ // mPass.ShaderResources.VSBindTexture(tbInfo.VSBindPoint, mMeshInstanceSRV); //} mPass.ShaderResources.VSBindTexture(14, mAllVertexSRV); mPass.ShaderResources.VSBindTexture(13, mMeshInstanceSRV); } CConstantBufferDesc cbInfo = new CConstantBufferDesc(); if (mCBMeshBatch != null && program.GetCBufferDesc(program.FindCBuffer("cbMeshBatch"), ref cbInfo)) { mPass.BindCBufferVS(cbInfo.VSBindPoint, mCBMeshBatch); } mPass.ViewPort = Camera.SceneView.Viewport; mPass.BindCBuffer(mPass.Effect.ShaderProgram, mPass.Effect.CacheData.CBID_Camera, Camera.CBuffer); if (view != null) { mPass.BindCBuffer(mPass.Effect.ShaderProgram, mPass.Effect.CacheData.CBID_View, view.SceneViewCB); } //mPass.ViewPort = view.Viewport; if (UseComputeShader == false) { mPass.AttachIndexBuffer = mCpuDrawIndexBuffer; var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = mDrawArgs.IndexCountPerInstance / 3; mPass.GeometryMesh.SetAtom(0, 0, ref dpDesc); } else { mPass.AttachIndexBuffer = mDrawIndexBuffer; mPass.SetIndirectDraw(bufferIndirectDrawArgs, 0); } cmd.PushPass(mPass); }
public static void MakeUIScale9(CRenderContext rc, CGfxMeshPrimitives result, float x, float y, float w, float h, float z, bool lh = true, bool flipUVWhenGL = false) { var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = 18; result.PushAtomLOD(0, ref dpDesc); unsafe { var ibData = new UInt16[54]; ibData[0] = 0; ibData[1] = 1; ibData[2] = 2; ibData[3] = 0; ibData[4] = 2; ibData[5] = 3; ibData[6] = 4; ibData[7] = 5; ibData[8] = 6; ibData[9] = 4; ibData[10] = 6; ibData[11] = 7; ibData[12] = 8; ibData[13] = 9; ibData[14] = 10; ibData[15] = 8; ibData[16] = 10; ibData[17] = 11; ibData[18] = 12; ibData[19] = 13; ibData[20] = 14; ibData[21] = 12; ibData[22] = 14; ibData[23] = 15; ibData[24] = 16; ibData[25] = 17; ibData[26] = 18; ibData[27] = 16; ibData[28] = 18; ibData[29] = 19; ibData[30] = 20; ibData[31] = 21; ibData[32] = 22; ibData[33] = 20; ibData[34] = 22; ibData[35] = 23; ibData[36] = 24; ibData[37] = 25; ibData[38] = 26; ibData[39] = 24; ibData[40] = 26; ibData[41] = 27; ibData[42] = 28; ibData[43] = 29; ibData[44] = 30; ibData[45] = 28; ibData[46] = 30; ibData[47] = 31; ibData[48] = 32; ibData[49] = 33; ibData[50] = 34; ibData[51] = 32; ibData[52] = 34; ibData[53] = 35; if (lh == false) { ibData[1] = 2; ibData[2] = 1; ibData[4] = 3; ibData[5] = 2; ibData[7] = 6; ibData[8] = 5; ibData[10] = 7; ibData[11] = 6; ibData[13] = 10; ibData[14] = 9; ibData[16] = 11; ibData[17] = 10; ibData[19] = 14; ibData[20] = 13; ibData[22] = 15; ibData[23] = 14; ibData[25] = 18; ibData[26] = 17; ibData[28] = 19; ibData[29] = 18; ibData[31] = 22; ibData[32] = 21; ibData[34] = 23; ibData[35] = 22; ibData[37] = 26; ibData[38] = 25; ibData[40] = 27; ibData[41] = 26; ibData[43] = 30; ibData[44] = 29; ibData[46] = 31; ibData[47] = 30; ibData[49] = 34; ibData[50] = 33; ibData[52] = 35; ibData[53] = 34; } var mesh = result.GeometryMesh; var vbDesc = new CVertexBufferDesc(); var vbTempPos = new Vector3[16]; var deltaW = w / 3; var deltaH = h / 3; for (int yV = 0; yV < 4; yV++) { for (int xV = 0; xV < 4; xV++) { vbTempPos[yV * 4 + xV].SetValue(x + deltaW * xV, y + deltaH * yV, z); } } var vbPos = new Vector3[36]; vbPos[0] = vbTempPos[0]; vbPos[1] = vbTempPos[1]; vbPos[2] = vbTempPos[5]; vbPos[3] = vbTempPos[4]; vbPos[4] = vbTempPos[1]; vbPos[5] = vbTempPos[2]; vbPos[6] = vbTempPos[6]; vbPos[7] = vbTempPos[5]; vbPos[8] = vbTempPos[2]; vbPos[9] = vbTempPos[3]; vbPos[10] = vbTempPos[7]; vbPos[11] = vbTempPos[6]; vbPos[12] = vbTempPos[4]; vbPos[13] = vbTempPos[5]; vbPos[14] = vbTempPos[9]; vbPos[15] = vbTempPos[8]; vbPos[16] = vbTempPos[5]; vbPos[17] = vbTempPos[6]; vbPos[18] = vbTempPos[10]; vbPos[19] = vbTempPos[9]; vbPos[20] = vbTempPos[6]; vbPos[21] = vbTempPos[7]; vbPos[22] = vbTempPos[11]; vbPos[23] = vbTempPos[10]; vbPos[24] = vbTempPos[8]; vbPos[25] = vbTempPos[9]; vbPos[26] = vbTempPos[13]; vbPos[27] = vbTempPos[12]; vbPos[28] = vbTempPos[9]; vbPos[29] = vbTempPos[10]; vbPos[30] = vbTempPos[14]; vbPos[31] = vbTempPos[13]; vbPos[32] = vbTempPos[10]; vbPos[33] = vbTempPos[11]; vbPos[34] = vbTempPos[15]; vbPos[35] = vbTempPos[14]; fixed(Vector3 *pV3 = &vbPos[0]) { vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)sizeof(Vector3) * 36; vbDesc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE; var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Position, vb); } var vbTempUV = new Vector2[16]; var deltaU = 1.0f / 3; var deltaV = 1.0f / 3; for (int yV = 0; yV < 4; yV++) { for (int xV = 0; xV < 4; xV++) { if (lh == false && CEngine.Instance.Desc.RHIType == ERHIType.RHT_D3D11) { vbTempUV[yV * 4 + xV].SetValue(deltaU * xV, 1 - deltaV * yV); } else if (flipUVWhenGL && CEngine.Instance.Desc.RHIType == ERHIType.RHT_OGL) { vbTempUV[yV * 4 + xV].SetValue(deltaU * xV, 1 - deltaV * yV); } else { vbTempUV[yV * 4 + xV].SetValue(deltaU * xV, deltaV * yV); } } } var vbUV = new Vector2[36]; vbUV[0] = vbTempUV[0]; vbUV[1] = vbTempUV[1]; vbUV[2] = vbTempUV[5]; vbUV[3] = vbTempUV[4]; vbUV[4] = vbTempUV[1]; vbUV[5] = vbTempUV[2]; vbUV[6] = vbTempUV[6]; vbUV[7] = vbTempUV[5]; vbUV[8] = vbTempUV[2]; vbUV[9] = vbTempUV[3]; vbUV[10] = vbTempUV[7]; vbUV[11] = vbTempUV[6]; vbUV[12] = vbTempUV[4]; vbUV[13] = vbTempUV[5]; vbUV[14] = vbTempUV[9]; vbUV[15] = vbTempUV[8]; vbUV[16] = vbTempUV[5]; vbUV[17] = vbTempUV[6]; vbUV[18] = vbTempUV[10]; vbUV[19] = vbTempUV[9]; vbUV[20] = vbTempUV[6]; vbUV[21] = vbTempUV[7]; vbUV[22] = vbTempUV[11]; vbUV[23] = vbTempUV[10]; vbUV[24] = vbTempUV[8]; vbUV[25] = vbTempUV[9]; vbUV[26] = vbTempUV[13]; vbUV[27] = vbTempUV[12]; vbUV[28] = vbTempUV[9]; vbUV[29] = vbTempUV[10]; vbUV[30] = vbTempUV[14]; vbUV[31] = vbTempUV[13]; vbUV[32] = vbTempUV[10]; vbUV[33] = vbTempUV[11]; vbUV[34] = vbTempUV[15]; vbUV[35] = vbTempUV[14]; fixed(Vector2 *pV2 = &vbUV[0]) { vbDesc.InitData = (IntPtr)pV2; vbDesc.Stride = (UInt32)sizeof(Vector2); vbDesc.ByteWidth = (UInt32)sizeof(Vector2) * 36; vbDesc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE; var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_UV, vb); } fixed(UInt16 *pV = &ibData[0]) { var ibDesc = new CIndexBufferDesc(); ibDesc.InitData = (IntPtr)pV; ibDesc.ByteWidth = (UInt32)sizeof(UInt16) * 54; var ib = rc.CreateIndexBuffer(ibDesc); mesh.BindIndexBuffer(ib); } result.ResourceState.ResourceSize = 20 * 16; mesh.Dirty = true; } result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
public void UpdateGeomMesh(CRenderContext rc, float halfWidth = 0.1F) { if (GeomMesh == null) { return; } CDrawPrimitiveDesc dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); if (UseGeometry) { mTempPosition.Clear(); mTempUV.Clear(); LinesGen.BuildGraph(mTempPosition, mTempUV, halfWidth); mTempNormal.Clear(); mTempNormal.SetGrowStep(mTempPosition.Count); for (int i = 0; i < mTempPosition.Count; i++) { //mTempNormal.Add(mTempPosition[i].CalcNormals()); unsafe { LinePosition tempNorm = new LinePosition(); var ptr = (LinePosition *)mTempPosition.UnsafeAddressAt(i).ToPointer(); ptr->CalcNormals(ref tempNorm); mTempNormal.Add(tempNorm); } } dpDesc.StartIndex = 0xFFFFFFFF; if (halfWidth == 0) { dpDesc.PrimitiveType = EPrimitiveType.EPT_LineList; dpDesc.NumPrimitives = (UInt32)(mTempPosition.Count * 3 * 3); } else { dpDesc.NumPrimitives = (UInt32)(mTempPosition.Count * 3 * 2); } GeomMesh.SetAtom(0, 0, ref dpDesc); unsafe { GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_Position, mTempPosition.GetBufferPtr(), (UInt32)(sizeof(LinePosition) * mTempPosition.Count), (UInt32)sizeof(Vector3), 0); GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_Normal, mTempNormal.GetBufferPtr(), (UInt32)(sizeof(LinePosition) * mTempNormal.Count), (UInt32)sizeof(Vector3), 0); GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_UV, mTempUV.GetBufferPtr(), (UInt32)(sizeof(LineUV) * mTempUV.Count), (UInt32)sizeof(Vector2), 0); } } else { mTempPosition.Clear(); mTempUV.Clear(); LinesGen.BuildGraph(mTempPosition, mTempUV, halfWidth); mTempNormal.Clear(); mTempNormal.SetGrowStep(mTempPosition.Count); for (int i = 0; i < mTempPosition.Count; i++) { //mTempNormal.Add(mTempPosition[i].CalcNormals()); unsafe { LinePosition tempNorm = new LinePosition(); var ptr = (LinePosition *)mTempPosition.UnsafeAddressAt(i).ToPointer(); ptr->CalcNormals(ref tempNorm); mTempNormal.Add(tempNorm); } } dpDesc.StartIndex = 0xFFFFFFFF; if (halfWidth == 0) { dpDesc.PrimitiveType = EPrimitiveType.EPT_LineList; dpDesc.NumPrimitives = (UInt32)(mTempPosition.Count * 3); } else { dpDesc.NumPrimitives = (UInt32)(mTempPosition.Count * 2); } GeomMesh.SetAtom(0, 0, ref dpDesc); unsafe { GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_Position, mTempPosition.GetBufferPtr(), (UInt32)(sizeof(LinePosition) * mTempPosition.Count), (UInt32)sizeof(Vector3), 0); GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_Normal, mTempNormal.GetBufferPtr(), (UInt32)(sizeof(LinePosition) * mTempNormal.Count), (UInt32)sizeof(Vector3), 0); GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_UV, mTempUV.GetBufferPtr(), (UInt32)(sizeof(LineUV) * mTempUV.Count), (UInt32)sizeof(Vector2), 0); } } GeomMesh.AABB = LinesGen.AABB; GeomMesh.GeometryMesh.Dirty = true; }
public void AddDefaultColorBuff(CRenderContext rc, CGfxMeshPrimitives result) { if (result == null) { return; } var mesh = result.GeometryMesh; if (mesh == null) { return; } CVertexBuffer colorbuff = mesh.GetVertexBuffer(EVertexSteamType.VST_Color); if (colorbuff != null) { return; } CVertexBuffer posbuff = mesh.GetVertexBuffer(EVertexSteamType.VST_Position); if (posbuff == null) { return; } var blob = new Support.CBlobObject(); posbuff.GetBufferData(rc, blob); int vertNum = 0; unsafe { vertNum = (int)blob.Size / sizeof(Vector3); } if (vertNum == 0) { return; } Support.NativeList <Byte4> Colors = new Support.NativeList <Byte4>(); var color = new Byte4(); color.X = 255; color.Y = 255; color.Z = 255; color.W = 255; for (int i = 0; i < vertNum; i++) { Colors.Add(color); } var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = (UInt32)vertNum; result.SetAtom(0, 0, ref dpDesc); UInt32 resourceSize = 0; unsafe { var vbDesc = new CVertexBufferDesc(); vbDesc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE; { vbDesc.InitData = Colors.UnsafeAddressAt(0); vbDesc.Stride = (UInt32)sizeof(Byte4); vbDesc.ByteWidth = (UInt32)(sizeof(Byte4) * Colors.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Color, vb); resourceSize += vbDesc.ByteWidth; } result.ResourceState.ResourceSize = (UInt32)(resourceSize); mesh.Dirty = true; } result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
public static void MakeScreenAlignedTriangle(CRenderContext RHICtx, CGfxMeshPrimitives PrimMesh) { CDrawPrimitiveDesc dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = 1; PrimMesh.PushAtomLOD(0, ref dpDesc); unsafe { var GeomData = PrimMesh.GeometryMesh; var vbDesc = new CVertexBufferDesc(); Vector3[] VtxPosBuffer = new Vector3[3]; VtxPosBuffer[0].SetValue(-1.0f, -1.0f, 0.0f); VtxPosBuffer[1].SetValue(-1.0f, 3.0f, 0.0f); VtxPosBuffer[2].SetValue(3.0f, -1.0f, 0.0f); fixed(Vector3 *refVtxPosBuffer = &VtxPosBuffer[0]) { vbDesc.InitData = (IntPtr)refVtxPosBuffer; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)sizeof(Vector3) * 3; var vb = RHICtx.CreateVertexBuffer(vbDesc); GeomData.BindVertexBuffer(EVertexSteamType.VST_Position, vb); } Vector2[] UVBuffer = new Vector2[3]; UVBuffer[0].SetValue(0.0f, 1.0f); UVBuffer[1].SetValue(0.0f, -1.0f); UVBuffer[2].SetValue(2.0f, 1.0f); if (CEngine.Instance.Desc.RHIType == ERHIType.RHT_OGL) { UVBuffer[0].Y = 1.0f - UVBuffer[0].Y; UVBuffer[1].Y = 1.0f - UVBuffer[1].Y; UVBuffer[2].Y = 1.0f - UVBuffer[2].Y; } fixed(Vector2 *refUVBuffer = &UVBuffer[0]) { vbDesc.InitData = (IntPtr)refUVBuffer; vbDesc.Stride = (UInt32)sizeof(Vector2); vbDesc.ByteWidth = (UInt32)sizeof(Vector2) * 3; var vb = RHICtx.CreateVertexBuffer(vbDesc); GeomData.BindVertexBuffer(EVertexSteamType.VST_UV, vb); } UInt16[] IdxBuffer = new UInt16[3]; IdxBuffer[0] = 0; IdxBuffer[1] = 1; IdxBuffer[2] = 2; fixed(UInt16 *refIdxBuffer = &IdxBuffer[0]) { var ibDesc = new CIndexBufferDesc(); ibDesc.InitData = (IntPtr)refIdxBuffer; ibDesc.ByteWidth = (UInt32)sizeof(UInt16) * 3; var ib = RHICtx.CreateIndexBuffer(ibDesc); GeomData.BindIndexBuffer(ib); } PrimMesh.ResourceState.ResourceSize = 66; GeomData.Dirty = true; } PrimMesh.ResourceState.StreamState = EStreamingState.SS_Valid; PrimMesh.ResourceState.KeepValid = true; }