private void ShowPositionsArray(Vector3[] positionsArray, float pixelSize, Color4 pixelColor, Bounds positionBounds)
        {
            if (_isUsingPixelsVisual3D)
            {
                // The easiest way to show many pixels is to use PixelsVisual3D.
                var pixelsVisual3D = new PixelsVisual3D()
                {
                    Positions  = positionsArray,
                    PixelColor = pixelColor.ToWpfColor(),
                    PixelSize  = pixelSize
                };

                // It is highly recommended to manually set the PositionsBounds.
                // If this is not done, the bounds are calculated by the DXEngine with checking all the positions.
                pixelsVisual3D.PositionsBounds = positionBounds;

                MainViewport.Children.Add(pixelsVisual3D);

                // !!! IMPORTANT !!!
                // When PixelsVisual3D is not used any more, it needs to be disposed (we are using DisposeList to dispose all in Unloaded event handler)
                _disposables.Add(pixelsVisual3D);

                return;
            }


            // First stop in showing positions in the positionsArray as pixels is to create a SimpleMesh<Vector3>.
            // This will create a DirectX VertexBuffer that will be passed to the shaders.
            var simpleMesh = new SimpleMesh <Vector3>(vertexBufferArray: positionsArray,
                                                      indexBufferArray: null,
                                                      inputLayoutType: InputLayoutType.Position);

            simpleMesh.PrimitiveTopology = PrimitiveTopology.PointList; // We need to change the default PrimitiveTopology.TriangleList to PointList

            // To correctly set the Camera's Near and Far distance, we need to provide the correct bounds of each shown 3D model.

            if (positionBounds != null && !positionBounds.IsEmpty)
            {
                // It is highly recommended to manually set the Bounds.
                simpleMesh.Bounds = positionBounds;
            }
            else
            {
                // if we do not manually set the Bounds, then we need to call CalculateBounds to calculate the bounds
                simpleMesh.CalculateBounds();
            }

            simpleMesh.CalculateBounds();


            // We will need to dispose the SimpleMesh
            _disposables.Add(simpleMesh);


            // Create a new PixelMaterial
            _pixelMaterial = new PixelMaterial()
            {
                PixelColor = pixelColor,
                PixelSize  = pixelSize
            };

            _pixelMaterial.InitializeResources(MainDXViewportView.DXScene.DXDevice);

            _disposables.Add(_pixelMaterial);


            // Now create a new MeshObjectNode
            _meshObjectNode = new Ab3d.DirectX.MeshObjectNode(simpleMesh, _pixelMaterial);

            _disposables.Add(_meshObjectNode);

            // To be able to add the MeshObjectNode (or any other SceneNode) to WPF's Viewport3D,
            // we need to create a SceneNodeVisual3D
            var sceneNodeVisual3D = new SceneNodeVisual3D(_meshObjectNode);

            MainViewport.Children.Add(sceneNodeVisual3D);
        }
Exemple #2
0
        private void CreateScene()
        {
            int pixelsXCount   = 20;
            var positionsArray = PixelRenderingSample.CreatePositionsArray(new Point3D(0, 0, 0), new Size3D(80, 40, 80), pixelsXCount, pixelsXCount / 2, pixelsXCount);

            int pixelsCount = positionsArray.Length;
            var pixelColors = new Color4[pixelsCount];

            for (int i = 0; i < pixelsCount; i++)
            {
                float green = (float)i / (float)pixelsCount;
                pixelColors[i] = new Color4(new Color3((float)(i % pixelsXCount) / (float)pixelsXCount, green, 1.0f - green));
            }

            var pixelsVisual3DWithColors = new PixelsVisual3D(positionsArray);

            pixelsVisual3DWithColors.PixelColors = pixelColors;
            pixelsVisual3DWithColors.PixelColor  = System.Windows.Media.Colors.White; // White (255,255,255) means that no color mask is used and the color specified in PixelColors are used (this is also the default value and is set here only for demonstration purpose)
            pixelsVisual3DWithColors.PixelSize   = 2.0f;                              // Default pixel size

            pixelsVisual3DWithColors.Transform = new TranslateTransform3D(0, 20, -200);

            MainViewport.Children.Add(pixelsVisual3DWithColors);

            // !!! IMPORTANT !!!
            // When PixelsVisual3D is not used any more, it needs to be disposed (we are using DisposeList to dispose all in Unloaded event handler)
            _disposables.Add(pixelsVisual3DWithColors);



            var pixelsVisual3DWithColorsAndColorFilter = new PixelsVisual3D(positionsArray);

            pixelsVisual3DWithColorsAndColorFilter.PixelColors = pixelColors;
            pixelsVisual3DWithColorsAndColorFilter.PixelColor  = System.Windows.Media.Color.FromRgb(0, 255, 0); // color mask is multiplied with each color defined in PixelColors
            pixelsVisual3DWithColorsAndColorFilter.PixelSize   = 2.0f;

            pixelsVisual3DWithColorsAndColorFilter.Transform = new TranslateTransform3D(0, 20, -100);

            MainViewport.Children.Add(pixelsVisual3DWithColorsAndColorFilter);

            _disposables.Add(pixelsVisual3DWithColorsAndColorFilter);


            var pixelSizes = new float[pixelsCount];

            for (int i = 0; i < pixelsCount; i++)
            {
                float percent = (float)i / (float)pixelsCount;
                pixelSizes[i] = (1f - percent) * 4f;
            }

            var pixelsVisual3DWithSize = new PixelsVisual3D(positionsArray);

            pixelsVisual3DWithSize.PixelSizes = pixelSizes;
            pixelsVisual3DWithSize.PixelSize  = 1.0f;
            pixelsVisual3DWithSize.PixelColor = System.Windows.Media.Colors.Orange;

            pixelsVisual3DWithSize.Transform = new TranslateTransform3D(0, 20, 0);

            MainViewport.Children.Add(pixelsVisual3DWithSize);

            _disposables.Add(pixelsVisual3DWithSize);


            var pixelsVisual3DWithSizeAndPixelSizeFactor = new PixelsVisual3D(positionsArray);

            pixelsVisual3DWithSizeAndPixelSizeFactor.PixelSizes = pixelSizes;
            pixelsVisual3DWithSizeAndPixelSizeFactor.PixelSize  = 3f;
            pixelsVisual3DWithSizeAndPixelSizeFactor.PixelColor = System.Windows.Media.Colors.Orange;

            pixelsVisual3DWithSizeAndPixelSizeFactor.Transform = new TranslateTransform3D(0, 20, 100);

            MainViewport.Children.Add(pixelsVisual3DWithSizeAndPixelSizeFactor);

            _disposables.Add(pixelsVisual3DWithSizeAndPixelSizeFactor);


            var pixelsVisual3DWithColorAndSize = new PixelsVisual3D(positionsArray);

            pixelsVisual3DWithColorAndSize.PixelColors = pixelColors;
            pixelsVisual3DWithColorAndSize.PixelSizes  = pixelSizes;
            pixelsVisual3DWithColorAndSize.PixelSize   = 1.0f;
            pixelsVisual3DWithColorAndSize.PixelColor  = System.Windows.Media.Colors.White;

            pixelsVisual3DWithColorAndSize.Transform = new TranslateTransform3D(0, 20, 200);

            MainViewport.Children.Add(pixelsVisual3DWithColorAndSize);

            _disposables.Add(pixelsVisual3DWithColorAndSize);
        }