private void CreateSquare(float lineThickness, bool isAntiAlias, Color lineColor, Vector3[] vertices) { using (var lineContext = base.BeginLineStrips(lineThickness, isAntiAlias)) { lineContext.SetVertexColor(lineColor); // Pass Entity ID value for a hit test purpose ulong selectionColor = SCRTImmediateDraw.EncodeSelectionId(EntityId, 0); lineContext.SetSelectionId(selectionColor); foreach (var v in vertices) { SetVertex(lineContext, v); } SetVertex(lineContext, vertices.First()); lineContext.Freeze(); lineContext.Draw(); } }
/// <summary> /// Called when the 3D Engine wishes to render this element. This is where geometry must be drawn to the 3D scene /// </summary> /// <param name="e">The <see cref="IRenderPassInfo3D" /> containing parameters for the current render pass.</param> public override void RenderScene(IRenderPassInfo3D e) { float bottomRightCoordX = bottomRight.X; float bottomRightCoordY = bottomRight.Y; float bottomRightCoordZ = bottomRight.Z; float topLeftCoordX = topLeft.X; float topLeftCoordY = topLeft.Y; float topLeftCoordZ = topLeft.Z; // Commented code below is the example of treating the Location value // as 3D point in Data Coordinates Space but not in World Coordinates Space //bottomRightCoordX = (float)e.XCalc.GetCoordinate(bottomRight.X) - e.WorldDimensions.X / 2.0f; //bottomRightCoordY = (float)e.YCalc.GetCoordinate(bottomRight.Y); //bottomRightCoordZ = (float)e.ZCalc.GetCoordinate(bottomRight.Z) - e.WorldDimensions.Z / 2.0f; //topLeftCoordX = (float)e.XCalc.GetCoordinate(topLeft.X) - e.WorldDimensions.X / 2.0f; //topLeftCoordY = (float)e.YCalc.GetCoordinate(topLeft.Y); //topLeftCoordZ = (float)e.ZCalc.GetCoordinate(topLeft.Z) - e.WorldDimensions.Z / 2.0f; // y 1--------0 // | /| /| // | 5--------4 | // | | | | | // | | | | | // | | 2--------3 // | z | / |/ // | / 6--------7 // |/ // ----------- X Vector3[] corners = { new Vector3(topLeftCoordX, topLeftCoordY, topLeftCoordZ), //0 new Vector3(bottomRightCoordX, topLeftCoordY, topLeftCoordZ), //1 new Vector3(bottomRightCoordX, bottomRightCoordY, topLeftCoordZ), //2 new Vector3(topLeftCoordX, bottomRightCoordY, topLeftCoordZ), //3 new Vector3(topLeftCoordX, topLeftCoordY, bottomRightCoordZ), //4 new Vector3(bottomRightCoordX, topLeftCoordY, bottomRightCoordZ), //5 new Vector3(bottomRightCoordX, bottomRightCoordY, bottomRightCoordZ), //6 new Vector3(topLeftCoordX, bottomRightCoordY, bottomRightCoordZ), //7 }; Vector3[] normals = { new Vector3(+0.0f, +0.0f, -1.0f), //front new Vector3(+0.0f, +0.0f, +1.0f), //back new Vector3(+1.0f, +0.0f, +0.0f), //right new Vector3(-1.0f, +0.0f, +0.0f), //left new Vector3(+0.0f, +1.0f, +0.0f), //top new Vector3(+0.0f, -1.0f, +0.0f), //bottom }; // Pass Entity ID ulong selectionColor = SCRTImmediateDraw.EncodeSelectionId(EntityId, 0); SCRTImmediateDraw.SelectionColor(selectionColor); // We create a mesh context. There are various mesh render modes. The simplest is Triangles // For this mode we have to draw a single triangle (three vertices) for each corner of the cube // You can see using (var meshContext = base.BeginLitMesh(TSRRenderMode.TRIANGLES)) { // Set the Rasterizer State for this entity SCRTImmediateDraw.PushRasterizerState(RasterizerStates.CullBackFacesState.TSRRasterizerState); // Set the color before drawing vertices meshContext.SetVertexColor(cubeColor); // Now draw the triangles. Each face of the cube is made up of two triangles // Front face SetNormal(meshContext, normals[0]); SetVertex(meshContext, corners[0]); SetVertex(meshContext, corners[2]); SetVertex(meshContext, corners[1]); SetVertex(meshContext, corners[2]); SetVertex(meshContext, corners[0]); SetVertex(meshContext, corners[3]); // Right side face SetNormal(meshContext, normals[2]); SetVertex(meshContext, corners[1]); SetVertex(meshContext, corners[2]); SetVertex(meshContext, corners[6]); SetVertex(meshContext, corners[1]); SetVertex(meshContext, corners[6]); SetVertex(meshContext, corners[5]); // Top face SetNormal(meshContext, normals[4]); SetVertex(meshContext, corners[2]); SetVertex(meshContext, corners[7]); SetVertex(meshContext, corners[6]); SetVertex(meshContext, corners[7]); SetVertex(meshContext, corners[2]); SetVertex(meshContext, corners[3]); // Left side face SetNormal(meshContext, normals[3]); SetVertex(meshContext, corners[3]); SetVertex(meshContext, corners[0]); SetVertex(meshContext, corners[4]); SetVertex(meshContext, corners[3]); SetVertex(meshContext, corners[4]); SetVertex(meshContext, corners[7]); // Back face SetNormal(meshContext, normals[1]); SetVertex(meshContext, corners[7]); SetVertex(meshContext, corners[5]); SetVertex(meshContext, corners[6]); SetVertex(meshContext, corners[7]); SetVertex(meshContext, corners[4]); SetVertex(meshContext, corners[5]); // Bottom face SetNormal(meshContext, normals[5]); SetVertex(meshContext, corners[0]); SetVertex(meshContext, corners[1]); SetVertex(meshContext, corners[5]); SetVertex(meshContext, corners[0]); SetVertex(meshContext, corners[5]); SetVertex(meshContext, corners[4]); } // Revert raster state SCRTImmediateDraw.PopRasterizerState(); // Set the Rasterizer State for wireframe SCRTImmediateDraw.PushRasterizerState(RasterizerStates.WireframeState.TSRRasterizerState); // Create a Line Context for a continuous line and draw the outline of the cube var lineColor = Color.FromArgb(0xFF, cubeColor.R, cubeColor.G, cubeColor.B); CreateSquare(2.0f, true, lineColor, new[] { corners[0], corners[1], corners[2], corners[3] }); CreateSquare(2.0f, true, lineColor, new[] { corners[4], corners[5], corners[6], corners[7] }); CreateSquare(2.0f, true, lineColor, new[] { corners[0], corners[4], corners[7], corners[3] }); CreateSquare(2.0f, true, lineColor, new[] { corners[5], corners[1], corners[2], corners[6] }); // Revert raster state SCRTImmediateDraw.PopRasterizerState(); }