Exemplo n.º 1
0
        private void BuildPlanes()
        {
            PlaneGeometry = new ObservableElement3DCollection();
            var builder = new MeshBuilder(true);

            builder.AddBox(new SharpDX.Vector3(0, 0, 0), 15, 15, 0.5);
            var mesh = builder.ToMesh();

            var material = new PhongMaterial();

            material.DiffuseColor = new SharpDX.Color4(1, 0, 0, 0.5f);

            var model = new MeshGeometryModel3D()
            {
                Geometry      = mesh,
                Material      = material,
                Transform     = new Media3D.TranslateTransform3D(-15, 0, 0),
                IsTransparent = true,
                CullMode      = SharpDX.Direct3D11.CullMode.Back
            };

            PlaneGeometry.Add(model);

            material = new PhongMaterial();
            material.DiffuseColor = new SharpDX.Color4(0, 1, 0, 0.5f);

            model = new MeshGeometryModel3D()
            {
                Geometry      = mesh,
                Material      = material,
                Transform     = new Media3D.TranslateTransform3D(-20, 5, -10),
                IsTransparent = true,
                CullMode      = SharpDX.Direct3D11.CullMode.Back
            };
            PlaneGeometry.Add(model);

            material = new PhongMaterial();
            material.DiffuseColor = new SharpDX.Color4(0, 0, 1, 0.5f);

            model = new MeshGeometryModel3D()
            {
                Geometry      = mesh,
                Material      = material,
                Transform     = new Media3D.TranslateTransform3D(-25, 10, -20),
                IsTransparent = true,
                CullMode      = SharpDX.Direct3D11.CullMode.Back
            };
            PlaneGeometry.Add(model);
        }
Exemplo n.º 2
0
    public HelixMeshPoser(RobotSystem robot, PBRMaterial material, ObservableElement3DCollection robotModels)
    {
        _default     = robot.DefaultPose;
        _robotModels = robotModels;

        foreach (var joint in _default.Meshes.SelectMany(m => m))
        {
            var model = new MeshGeometryModel3D
            {
                Geometry         = ToWPF(joint),
                Material         = material,
                Transform        = Transform3D.Identity,
                IsThrowingShadow = true
            };

            robotModels.Add(model);
        }
    }
Exemplo n.º 3
0
        /// <summary>
        /// Updates the model in the 3D viewport
        /// </summary>
        /// <param name="mdlData">The model data</param>
        /// <param name="textureDataDictionary">The texture dictionary for the model</param>
        public void UpdateModel(XivMdl mdlData, Dictionary <int, ModelTextureData> textureDataDictionary)
        {
            SharpDX.BoundingBox?boundingBox = null;

            var totalMeshCount = mdlData.LoDList[0].MeshCount + mdlData.LoDList[0].ExtraMeshCount;

            for (var i = 0; i < totalMeshCount; i++)
            {
                var meshData = mdlData.LoDList[0].MeshDataList[i].VertexData;

                var meshGeometry3D = new MeshGeometry3D
                {
                    Positions          = meshData.Positions,
                    Normals            = meshData.Normals,
                    Indices            = meshData.Indices,
                    TextureCoordinates = meshData.TextureCoordinates0,
                };

                try
                {
                    MeshBuilder.ComputeTangents(meshGeometry3D);
                }
                catch
                {
                    Debug.WriteLine($"Unable to compute tangents for mesh {i}");
                }

                if (meshData.BiNormals != null && meshData.BiNormals.Count > 0)
                {
                    meshGeometry3D.BiTangents = meshData.BiNormals;
                }

                var textureData = textureDataDictionary[mdlData.LoDList[0].MeshDataList[i].MeshInfo.MaterialIndex];

                Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null;

                var pixelSettings =
                    new PixelReadSettings(textureData.Width, textureData.Height, StorageType.Char, PixelMapping.RGBA);

                if (textureData.Diffuse != null && textureData.Diffuse.Length > 0)
                {
                    using (var image = new MagickImage(textureData.Diffuse, pixelSettings))
                    {
                        diffuse = new MemoryStream(image.ToByteArray(MagickFormat.Bmp));
                    }
                }

                if (textureData.Specular != null && textureData.Specular.Length > 0)
                {
                    using (var image = new MagickImage(textureData.Specular, pixelSettings))
                    {
                        specular = new MemoryStream(image.ToByteArray(MagickFormat.Bmp));
                    }
                }

                if (textureData.Normal != null && textureData.Normal.Length > 0)
                {
                    using (var image = new MagickImage(textureData.Normal, pixelSettings))
                    {
                        normal = new MemoryStream(image.ToByteArray(MagickFormat.Bmp));
                    }
                }

                if (textureData.Alpha != null && textureData.Alpha.Length > 0)
                {
                    using (var image = new MagickImage(textureData.Alpha, pixelSettings))
                    {
                        alpha = new MemoryStream(image.ToByteArray(MagickFormat.Bmp));
                    }
                }

                if (textureData.Emissive != null && textureData.Emissive.Length > 0)
                {
                    using (var image = new MagickImage(textureData.Emissive, pixelSettings))
                    {
                        emissive = new MemoryStream(image.ToByteArray(MagickFormat.Bmp));
                    }
                }


                var material = new PhongMaterial
                {
                    DiffuseColor      = PhongMaterials.ToColor(1, 1, 1, 1),
                    SpecularShininess = 1f,
                    DiffuseMap        = diffuse,
                    DiffuseAlphaMap   = alpha,
                    SpecularColorMap  = specular,
                    NormalMap         = normal,
                    EmissiveMap       = emissive
                };

                var mgm3d = new CustomMeshGeometryModel3D
                {
                    Geometry = meshGeometry3D,
                    Material = material,
                    IsBody   = mdlData.LoDList[0].MeshDataList[i].IsBody
                };

                boundingBox = meshGeometry3D.Bound;

                mgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back;


                Models.Add(mgm3d);
            }

            SpecularShine = 1;

            var center = boundingBox.GetValueOrDefault().Center;

            _lightX = center.X;
            _lightY = center.Y;
            _lightZ = center.Z;

            Light3Direction = new Vector3D(_lightX, _lightY, _lightZ);

            Camera.UpDirection = new Vector3D(0, 1, 0);
            Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged;
        }
        /// <summary>
        /// Updates the model in the 3D viewport
        /// </summary>
        /// <param name="mdlData">The model data</param>
        /// <param name="textureDataDictionary">The texture dictionary for the model</param>
        public void UpdateModel(TTModel model, Dictionary <int, ModelTextureData> textureDataDictionary)
        {
            SharpDX.BoundingBox?boundingBox = null;
            ModelModifiers.CalculateTangents(model);

            var totalMeshCount = model.MeshGroups.Count;

            for (var i = 0; i < totalMeshCount; i++)
            {
                var meshGeometry3D = GetMeshGeometry(model, i);

                var textureData = textureDataDictionary[model.GetMaterialIndex(i)];

                Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null;

                if (textureData.Diffuse != null && textureData.Diffuse.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Diffuse, textureData.Width, textureData.Height))
                    {
                        diffuse = new MemoryStream();
                        img.Save(diffuse, new PngEncoder());
                    }

                    streamList.Add(diffuse);
                }

                if (textureData.Specular != null && textureData.Specular.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Specular, textureData.Width, textureData.Height))
                    {
                        specular = new MemoryStream();
                        img.Save(specular, new PngEncoder());
                    }

                    streamList.Add(specular);
                }

                if (textureData.Normal != null && textureData.Normal.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Normal, textureData.Width, textureData.Height))
                    {
                        normal = new MemoryStream();
                        img.Save(normal, new PngEncoder());
                    }

                    streamList.Add(normal);
                }

                if (textureData.Alpha != null && textureData.Alpha.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Alpha, textureData.Width, textureData.Height))
                    {
                        alpha = new MemoryStream();
                        img.Save(alpha, new PngEncoder());
                    }

                    streamList.Add(alpha);
                }

                if (textureData.Emissive != null && textureData.Emissive.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Emissive, textureData.Width, textureData.Height))
                    {
                        emissive = new MemoryStream();
                        img.Save(emissive, new PngEncoder());
                    }

                    streamList.Add(emissive);
                }

                var material = new PhongMaterial
                {
                    DiffuseColor      = PhongMaterials.ToColor(1, 1, 1, 1),
                    SpecularShininess = 1f,
                    DiffuseMap        = diffuse,
                    DiffuseAlphaMap   = alpha,
                    SpecularColorMap  = specular,
                    NormalMap         = normal,
                    EmissiveMap       = emissive
                };

                var mgm3d = new CustomMeshGeometryModel3D
                {
                    Geometry = meshGeometry3D,
                    Material = material //,
                                        //IsBody = mdlData.LoDList[0].MeshDataList[i].IsBody
                };

                boundingBox = meshGeometry3D.Bound;

                mgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back;

                Models.Add(mgm3d);
            }

            SpecularShine = 1;

            var center = boundingBox.GetValueOrDefault().Center;

            _lightX = center.X;
            _lightY = center.Y;
            _lightZ = center.Z;

            Light3Direction    = new Vector3D(_lightX, _lightY, _lightZ);
            Camera.UpDirection = new Vector3D(0, 1, 0);
            Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Updates the model in the 3D viewport
        /// </summary>
        /// <param name="mdlData">The model data</param>
        /// <param name="textureDataDictionary">The texture dictionary for the model</param>
        public void UpdateModel(XivMdl mdlData, Dictionary <int, ModelTextureData> textureDataDictionary)
        {
            SharpDX.BoundingBox?boundingBox = null;

            var totalMeshCount = mdlData.LoDList[0].MeshCount + mdlData.LoDList[0].ExtraMeshCount;

            for (var i = 0; i < totalMeshCount; i++)
            {
                var meshData = mdlData.LoDList[0].MeshDataList[i].VertexData;

                var meshGeometry3D = new MeshGeometry3D
                {
                    Positions          = new Vector3Collection(meshData.Positions),
                    Normals            = new Vector3Collection(meshData.Normals),
                    Indices            = new IntCollection(meshData.Indices),
                    Colors             = new Color4Collection(meshData.Colors4),
                    TextureCoordinates = new Vector2Collection(meshData.TextureCoordinates0),
                };

                try
                {
                    MeshBuilder.ComputeTangents(meshGeometry3D);
                }
                catch
                {
                    Debug.WriteLine($"Unable to compute tangents for mesh {i}");
                }

                if (meshData.BiNormals != null && meshData.BiNormals.Count > 0)
                {
                    meshGeometry3D.BiTangents = new Vector3Collection(meshData.BiNormals);
                }

                var textureData = textureDataDictionary[mdlData.LoDList[0].MeshDataList[i].MeshInfo.MaterialIndex];

                Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null;

                if (textureData.Diffuse != null && textureData.Diffuse.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Diffuse, textureData.Width, textureData.Height))
                    {
                        diffuse = new MemoryStream();
                        img.Save(diffuse, new PngEncoder());
                    }

                    streamList.Add(diffuse);
                }

                if (textureData.Specular != null && textureData.Specular.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Specular, textureData.Width, textureData.Height))
                    {
                        specular = new MemoryStream();
                        img.Save(specular, new PngEncoder());
                    }

                    streamList.Add(specular);
                }

                if (textureData.Normal != null && textureData.Normal.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Normal, textureData.Width, textureData.Height))
                    {
                        normal = new MemoryStream();
                        img.Save(normal, new PngEncoder());
                    }

                    streamList.Add(normal);
                }

                if (textureData.Alpha != null && textureData.Alpha.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Alpha, textureData.Width, textureData.Height))
                    {
                        alpha = new MemoryStream();
                        img.Save(alpha, new PngEncoder());
                    }

                    streamList.Add(alpha);
                }

                if (textureData.Emissive != null && textureData.Emissive.Length > 0)
                {
                    using (var img = Image.LoadPixelData <Rgba32>(textureData.Emissive, textureData.Width, textureData.Height))
                    {
                        emissive = new MemoryStream();
                        img.Save(emissive, new PngEncoder());
                    }

                    streamList.Add(emissive);
                }

                var material = new PhongMaterial
                {
                    DiffuseColor      = PhongMaterials.ToColor(1, 1, 1, 1),
                    SpecularShininess = 1f,
                    DiffuseMap        = diffuse,
                    DiffuseAlphaMap   = alpha,
                    SpecularColorMap  = specular,
                    NormalMap         = normal,
                    EmissiveMap       = emissive
                };

                var mgm3d = new CustomMeshGeometryModel3D
                {
                    Geometry = meshGeometry3D,
                    Material = material,
                    IsBody   = mdlData.LoDList[0].MeshDataList[i].IsBody
                };

                boundingBox = meshGeometry3D.Bound;

                mgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back;

                Models.Add(mgm3d);
            }

            SpecularShine = 1;

            var center = boundingBox.GetValueOrDefault().Center;

            _lightX = center.X;
            _lightY = center.Y;
            _lightZ = center.Z;

            Light3Direction    = new Vector3D(_lightX, _lightY, _lightZ);
            Camera.UpDirection = new Vector3D(0, 1, 0);
            Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged;
        }