basic effect blend effect on monogame.forms

how to make this blending effect with monogame ?


this is my initilize and drawing methods

protected override void Initialize()
        {
            instance = this;
            spriteBatch = new SpriteBatch(GraphicsDevice);
            basicEffect = new BasicEffect(GraphicsDevice);
            blendState = new BlendState();
            this.graphicsDevice = GraphicsDevice;
            basicEffect.EnableDefaultLighting();
            basicEffect.LightingEnabled = true;
            basicEffect.DiffuseColor = Color.White.ToVector3();
            basicEffect.DirectionalLight0.Enabled = true;
            basicEffect.DirectionalLight0.DiffuseColor = Color.Yellow.ToVector3();
            basicEffect.AmbientLightColor = Color.Red.ToVector3();
            basicEffect.DirectionalLight0.Direction = new Vector3(1, 1, 1);
            GraphicsDevice.BlendState = BlendState.AlphaBlend;
            blendState.AlphaBlendFunction = BlendFunction.Add;
            blendState.AlphaSourceBlend = Blend.One;
            blendState.AlphaDestinationBlend = Blend.InverseSourceAlpha;
            basicEffect.CurrentTechnique.Passes[0].Apply();
           

            Bones = new List<Bone>();
        
            base.Initialize();
        }
 public void DrawOctahedralBone(Vector3 headPos, Vector3 tailPos , Color color)
        {
            
            Vector3 direction = tailPos - headPos;
            float length = direction.Length();
            Vector3 unitDirection = direction / length;
            Vector3 right = Vector3.Cross(unitDirection, Vector3.Up);
            if (right.Length() < 0.001f)
            {
                right = Vector3.Cross(unitDirection, Vector3.Right);
            }
            right.Normalize();
            Vector3 up = Vector3.Cross(unitDirection, right);
            up.Normalize();

            float bodyDistance = length * 0.1f;
            Vector3 bodyUp = up * bodyDistance;
            Vector3 bodyRight = right * bodyDistance;
            Vector3 bodyCenter = headPos + (unitDirection * length * 0.1f);

            Vector3[] vertices = new Vector3[6];
            vertices[0] = headPos;
            vertices[1] = tailPos;
            vertices[2] = bodyCenter + bodyUp + bodyRight;
            vertices[3] = bodyCenter + bodyUp - bodyRight;
            vertices[4] = bodyCenter - bodyUp + bodyRight;
            vertices[5] = bodyCenter - bodyUp - bodyRight;
            short[] indices = new short[] {
        2, 0, 3,
        2, 0, 4,
        3, 0, 5,
        5, 0, 4,
        2, 1, 3,
        2, 1, 4,
        3, 1, 5,
        5, 1, 4
    };

            GraphicsDevice.BlendState = new BlendState
            {
                AlphaBlendFunction = BlendFunction.Add,
                AlphaSourceBlend = Blend.SourceAlpha,
                AlphaDestinationBlend = Blend.InverseSourceAlpha
            };

            Color colorWithOpacity = new Color(color.R, color.G, color.B, (byte)(255 * 0.6f));

            VertexPositionColor[] vertexArray = new VertexPositionColor[6];
            for (int i = 0; i < 6; i++)
            {
                vertexArray[i] = new VertexPositionColor(vertices[i], colorWithOpacity);
            }


            RasterizerState rasterizerState = new RasterizerState();
            if (isOktaWireDisEnabled)
            {
                rasterizerState.FillMode = FillMode.WireFrame;
            }
         
         
            rasterizerState.CullMode = CullMode.None;
            GraphicsDevice.RasterizerState = rasterizerState;
            GraphicsDevice.BlendState = blendState;
            basicEffect.World = Matrix.CreateWorld(headPos, unitDirection, up);
            basicEffect.View = ViewMatrix;
            basicEffect.Projection = ProjectionMatrix;

            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, vertexArray, 0, 6, indices, 0, 8);
        }

protected override void Draw()
        {

           base.Draw();


            GraphicsDevice.Clear(MBlue);
            Editor.spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            
            GraphicsDevice.BlendState = BlendState.AlphaBlend;
            DrawAxes();
            BoneDisplay();

            Editor.spriteBatch.End();

        }

BoneDisplay(); is waht initiates DrawOctahedralBone

but what i get

You are setting BlendState.Alpha… , you want to set BlendState.Color…
Also, it’s a bad idea to create new states every frame.
You probably don’t even need to create those states yourself, the preconfigured ones should work, BlendState.Additive or BlendState.NonPremultiplied

1 Like