Monday, October 22, 2012

Silverlight 5 using 3d max Car.FBX model

Machine.cs


namespace Silverlight3dApp
{

    public class Machine
    {
        public const string CARCOLOR = "CARCOLOR";
        public const string RIMCOLOR = "RIMCOLOR";

        static readonly GraphicsDevice ResourceDevice = GraphicsDeviceManager.Current.GraphicsDevice;

        Model wallModel;
        Model model;
        Matrix[] boneTransforms;
        Matrix[] wallBoneTransforms;

        public Vector3 SpecularColor { get; set; }
        public float AspectRatio { get; set; }
        public float CurrentEnvMap { get; set; }
        public float CurrentSpecular { get; set; }
        public float CurrentFresnel { get; set; }
        public Dictionary<string,Color> ChangeColors { get; set; }
        public Dictionary<string,BoneProperty> BoneProperties { get; set; }

        UserControl page = new UserControl();
        Texture2D _texture;
        VertexPositionNormalTexture[] _vertices;
       
        /// <summary>
        /// Loads the model.
        /// </summary>
        public void Load(ContentManager content)
        {
            model = content.Load<Model>("Car");
            boneTransforms = new Matrix[model.Bones.Count];
           
            wallModel = content.Load<Model>("Car");
            wallBoneTransforms = new Matrix[wallModel.Bones.Count];

            AspectRatio = 1f;
            SpecularColor = new Vector3(1f, 1f, 1f);
            CurrentEnvMap = 1;
            CurrentSpecular = 1;
            CurrentFresnel = 0;

            ChangeColors = new Dictionary<string, Color>();
            BoneProperties = ContentUtils.ParseCarList(Resource1.Car);

            _vertices = Polyhedron.CreateVertices(Polyhedra.CubeCorners, Polyhedra.CubeFaces);
            _texture = content.Load<Texture2D>("studio");
        }
               
        /// <summary>
        /// Draws the tank model, using the current animation settings.
        /// </summary>
        public void Draw(GraphicsDevice device, Matrix world, Matrix view, Matrix projection, TimeSpan totolTime)
        {
            model.CopyAbsoluteBoneTransformsTo(boneTransforms);
            wallModel.CopyAbsoluteBoneTransformsTo(wallBoneTransforms);

            Matrix view0 = Matrix.CreateLookAt(new Vector3(-1, 1000, 0),
                                              new Vector3(0, 0, 0),
                                              Vector3.Up);
            Matrix scale0 = Matrix.CreateScale(4f,8f,8f);

            ModelMesh mesh0 = wallModel.Meshes["suuri"];
            foreach (EnvironmentMapEffect effect in mesh0.Effects)
            {
                effect.EnvironmentMapAmount = 0.2f;//CurrentEnvMap;
                effect.EnvironmentMapSpecular = SpecularColor * 0.8f;//CurrentSpecular;
                effect.FresnelFactor = CurrentFresnel;

                effect.Texture = _texture;

                effect.View = view0;
                effect.Projection = projection;
                effect.World = wallBoneTransforms[mesh0.ParentBone.Index] * scale0;
                effect.EnableDefaultLighting();
            }
            mesh0.Draw();

            // Draw the model.
            foreach (ModelMesh mesh in model.Meshes)
            {
                //BoneProperty prop = BoneProperties[model.Bones[i].Name];
                Color color = Color.FromNonPremultiplied(0, 0, 0, 255);
                bool envMap = false;
                BoneProperty prop = null;

                if (BoneProperties.ContainsKey(mesh.ParentBone.Name))
                {
                    prop = BoneProperties[mesh.ParentBone.Name];
                    color = prop.TextureColor;
                    envMap = prop.EnvironmentMapEnabled;
                    if (!string.IsNullOrEmpty(prop.ChangeColorKey) && ChangeColors != null
                        && ChangeColors.ContainsKey(prop.ChangeColorKey))
                    {
                        color = ChangeColors[prop.ChangeColorKey];
                    }
                }

                foreach (EnvironmentMapEffect effect in mesh.Effects)
                {
                    if (envMap)
                    {
                        effect.EnvironmentMapAmount = 0.2f;//CurrentEnvMap;
                        effect.EnvironmentMapSpecular = SpecularColor * 0.8f;//CurrentSpecular;
                        effect.FresnelFactor = CurrentFresnel;
                    }
                    else
                    {
                        effect.EnvironmentMapAmount = 0.0f;//CurrentEnvMap;
                        effect.EnvironmentMapSpecular = SpecularColor * 0.1f;//CurrentSpecular;
                        effect.FresnelFactor = CurrentFresnel;
                    }

                    Texture2D texture = new Texture2D(device, 1, 1, false, SurfaceFormat.Color);
                    texture.SetData<Color>(new Color[1] { color });
                    effect.Texture = texture;

                    effect.View = view;
                    effect.Projection = projection;
                    effect.World = boneTransforms[mesh.ParentBone.Index];

                    effect.EnableDefaultLighting();
                }
                mesh.Draw();
            }

            //model.Draw(world, view, projection);
        }

        internal VertexShader CompileVertexShader(GraphicsDevice device, Uri shaderUri)
        {
            VertexShader result;
            Stream vertexShaderStream = Application.GetResourceStream(shaderUri).Stream;
            result = VertexShader.FromStream(device, vertexShaderStream);
            return result;
        }

        internal PixelShader CompilePixelShader(GraphicsDevice device, Uri shaderUri)
        {
            PixelShader result;
            Stream vertexShaderStream = Application.GetResourceStream(shaderUri).Stream;
            result = PixelShader.FromStream(device, vertexShaderStream);
            return result;
        }

        void InitEffect(GraphicsDevice device, string assemblyName, string vertexRootName, string pixelRootName)
        {
            // Uri
            string vertexRootUri = string.Format(@"/{0};component/{1}.vs", assemblyName, vertexRootName);
            string pixelRootUri = string.Format(@"/{0};component/{1}.ps", assemblyName, pixelRootName);

            // Shaders
            Stream vertexShaderStream = Application.GetResourceStream(new Uri(vertexRootUri, UriKind.Relative)).Stream;
            Stream pixelShaderStream = Application.GetResourceStream(new Uri(pixelRootUri, UriKind.Relative)).Stream;

            //_vertexShader = CompileVertexShader(device, new Uri(vertexRootUri, UriKind.Relative));
            //_pixelShader = CompilePixelShader(device, new Uri(pixelRootUri, UriKind.Relative));

            vertexShaderStream.Dispose();
            pixelShaderStream.Dispose();

            // Assembly codes
            //ExtractConstantsRegisters(vertexRootUri + ".constants", false);
            //ExtractConstantsRegisters(pixelRootUri + ".constants", true);
        }
    }
}

Car.Xml resources

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <item Name="suuri" TextureColor="200,200,200" EnvironmentMapEnabled="true" />
  <item Name="mashin" ChangeColorKey="CARCOLOR" TextureColor="255,0,0" EnvironmentMapEnabled="true" />
  <item Name="dugui_1" TextureColor="30,30,30" EnvironmentMapEnabled="false"/>
  <item Name="dugui_2" TextureColor="30,30,30" EnvironmentMapEnabled="false"/>
  <item Name="dugui_3" TextureColor="30,30,30" EnvironmentMapEnabled="false"/>
  <item Name="dugui_4" TextureColor="30,30,30" EnvironmentMapEnabled="false"/>
  <item Name="obud_1" ChangeColorKey="RIMCOLOR" TextureColor="250,250,250" EnvironmentMapEnabled="true"/>
  <item Name="obud_2" ChangeColorKey="RIMCOLOR" TextureColor="250,250,250" EnvironmentMapEnabled="true" />
  <item Name="obud_3" ChangeColorKey="RIMCOLOR" TextureColor="250,250,250" EnvironmentMapEnabled="true" />
  <item Name="obud_4" ChangeColorKey="RIMCOLOR" TextureColor="250,250,250" EnvironmentMapEnabled="true" />
  <item Name="urd_tsonh" TextureColor="0,0,0" EnvironmentMapEnabled="true" />
  <item Name="hoid_tsonh" TextureColor="0,0,0" EnvironmentMapEnabled="true" />
</root>

Images


it is not use Shader Compiler,
good simple

No comments: