Sunday, April 5, 2015

21: Real-Time Fur Working in XNA 4.0 & MonoGame


       With the help of this awesome tutorial I found here by Catalin Zima, I was able to implement real-time fur. I also converted it to XNA 4.0. In this next blog post, I will show you how. Changing the draw code to the code below made it work in XNA 4.0. The XNA 3.1 to 4.0 Conversion Cheat Sheet was also a big help.


private void DrawModelGeometry(Model model, Matrix[] bones, Effect effect)
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
           
                foreach (ModelMeshPart meshpart in mesh.MeshParts)
                {
                 
effect.Parameters["World"].SetValue(bones[mesh.ParentBone.Index]); //set World Matrix
                    effect.Parameters["Texture"].SetValue(furColorTexture);            //set Texture
                    // effect.CommitChanges(); //commit changes
                    //graphics.GraphicsDevice.VertexDeclaration = meshpart.VertexDeclaration; (XNA 3.1)
                    graphics.GraphicsDevice.SetVertexBuffer(meshpart.VertexBuffer);
                    //graphics.GraphicsDevice.Vertices[0].SetSource(mesh.VertexBuffer, meshpart.StreamOffset, meshpart.VertexStride);
                    graphics.GraphicsDevice.Indices = meshpart.IndexBuffer;
                    //draw the geometry
                    graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
                                                                                    meshpart.NumVertices, meshpart.StartIndex, meshpart.PrimitiveCount);
                }
            }
        }

        private void DrawFurModel(Model model)
        {
            Matrix[] bones = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(bones);

            furEffect.Parameters["Displacement"].SetValue(displacement);
            furEffect.Parameters["MaxHairLength"].SetValue(maxHairLength);
            furEffect.Parameters["FurTexture"].SetValue(furTexture);

            furEffect.Parameters["View"].SetValue(camera.View);
            furEffect.Parameters["Projection"].SetValue(camera.Projection);
            furEffect.Parameters["MaxHairLength"].SetValue(maxHairLength);
            furEffect.Parameters["Texture"].SetValue(furColorTexture);

           // furEffect.Begin();
            for (int i = 0; i < nrOfLayers; i++)
            {
                furEffect.Parameters["CurrentLayer"].SetValue((float)i / nrOfLayers);
                //furEffect.CommitChanges();
                //furEffect.CurrentTechnique.Passes[0].Begin();
                furEffect.CurrentTechnique.Passes[0].Apply();
                //draw geometry of current layer
                DrawModelGeometry(model, bones, furEffect);
                //furEffect.CurrentTechnique.Passes[0].End();
            }
            //furEffect.End();
        }

If you are having trouble watching the video, below are some screenshots.










I am working on applying this fur shader effect to the animated spider beast model. Stay tuned!


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.