private ScreenSpaceLineNode CreateLinesWithLineMesh(Vector3[] linePositions, bool isLineStrip, bool isLineClosed, Color lineColor, float xOffset, out ScreenSpaceLineMesh screenSpaceLineMesh) { if (linePositions == null || linePositions.Length < 2) { screenSpaceLineMesh = null; return(null); } // If line is closed but the first position is not the same as the last position, then add the first position as the last one if (isLineClosed && linePositions[0] != linePositions[linePositions.Length - 1]) { Array.Resize(ref linePositions, linePositions.Length + 1); linePositions[linePositions.Length - 1] = linePositions[0]; } // If we can easily calculate the bounding box from line positions // it is recommended to specify it in the ScreenSpaceLineMesh constructor. // If boundingBox is not specified, it will be calculated in the ScreenSpaceLineMesh constructor with checking all the positions. // // NOTE: If bounding box is not correct then camera's near and far planes can be invalid and this can cut some 3D objects at near or far plane (when DXScene.OptimizeNearAndFarCameraPlanes is true - by default) //var boundingBox = new BoundingBox(new Vector3(startX, 0, startZ), new Vector3(startX + linesCount * margin, 0, endZ)); // Create ScreenSpaceLineMesh - it is used to create DirectX vertex buffer from positions screenSpaceLineMesh = new ScreenSpaceLineMesh(linePositions, isLineStrip); // When the line positions are changed many times, it is recommended to set CreateDynamicVertexBuffer to true. screenSpaceLineMesh.CreateDynamicVertexBuffer = true; var lineMaterial = new LineMaterial() { LineColor = lineColor.ToColor4(), LineThickness = 2 }; var screenSpaceLineNode = new ScreenSpaceLineNode(screenSpaceLineMesh, lineMaterial); screenSpaceLineNode.Transform = new Transformation(SharpDX.Matrix.Translation(xOffset, 0, 0)); // To show ScreenSpaceLineNode in DXViewportView we need to put it inside a SceneNodeVisual3D var sceneNodeVisual3D = new SceneNodeVisual3D(screenSpaceLineNode); MainViewport.Children.Add(sceneNodeVisual3D); _disposables.Add(screenSpaceLineMesh); _disposables.Add(screenSpaceLineNode); _disposables.Add(lineMaterial); return(screenSpaceLineNode); }
private void ChangePositions(ScreenSpaceLineNode screenSpaceLineNode, ScreenSpaceLineMesh screenSpaceLineMesh) { ChangePositions(screenSpaceLineMesh.Positions); // Recreate vertex buffer that is defined with ScreenSpaceLineMesh screenSpaceLineMesh.RecreateMesh(); // Update bounds of the mesh and SceneNode. BoundingBox boundingBox; BoundingBox.FromPoints(screenSpaceLineMesh.Positions, out boundingBox); // We could set boundingBox manually, but for demonstration purpose we calculate it from the positions screenSpaceLineMesh.Bounds.SetBoundingBox(ref boundingBox); screenSpaceLineNode.UpdatePositions(); screenSpaceLineNode.UpdateBounds(); }