CalculateNormals() public static method

Calculates the normal vectors.
public static CalculateNormals ( IList positions, IList triangleIndices ) : System.Windows.Media.Media3D.Vector3DCollection
positions IList /// The positions. ///
triangleIndices IList /// The triangle indices. ///
return System.Windows.Media.Media3D.Vector3DCollection
Exemplo n.º 1
0
        /// <summary>
        /// Calculates the texture for the specified model.
        /// </summary>
        /// <param name="model">
        /// The model.
        /// </param>
        /// <param name="mesh">
        /// The mesh.
        /// </param>
        public override void Calculate(TerrainModel model, MeshGeometry3D mesh)
        {
            var normals   = MeshGeometryHelper.CalculateNormals(mesh);
            var texcoords = new PointCollection();
            var up        = new Vector3D(0, 0, 1);

            for (int i = 0; i < normals.Count; i++)
            {
                double slope = Math.Acos(Vector3D.DotProduct(normals[i], up)) * 180 / Math.PI;
                double u     = slope / 40;
                if (u > 1)
                {
                    u = 1;
                }

                if (u < 0)
                {
                    u = 0;
                }

                texcoords.Add(new Point(u, u));
            }

            this.TextureCoordinates = texcoords;
            this.Material           = MaterialHelper.CreateMaterial(this.Brush);
        }
        /// <summary>
        /// Calculates the texture of the specified model.
        /// </summary>
        /// <param name="model">
        /// The model.
        /// </param>
        /// <param name="mesh">
        /// The mesh.
        /// </param>
        public override void Calculate(TerrainModel model, MeshGeometry3D mesh)
        {
            var normals   = MeshGeometryHelper.CalculateNormals(mesh);
            var texcoords = new PointCollection();

            for (int i = 0; i < normals.Count; i++)
            {
                double slopedir = Math.Atan2(normals[i].Y, normals[i].X) * 180 / Math.PI;
                if (slopedir < 0)
                {
                    slopedir += 360;
                }

                double u = slopedir / 360;
                texcoords.Add(new Point(u, u));
            }

            this.TextureCoordinates = texcoords;
            this.Material           = MaterialHelper.CreateMaterial(this.Brush);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Writes a <see cref="GeometryModel3D"/> to a <see cref="BinaryWriter"/> in STL binary format.
        /// </summary>
        /// <param name="writer">The <see cref="BinaryWriter"/> to write to.</param>
        /// <param name="model">The model to write.</param>
        /// <param name="t">All vertices are transformed with this transform before written</param>
        protected override void ExportModel(BinaryWriter writer, GeometryModel3D model, Transform3D t)
        {
            var mesh = (MeshGeometry3D)model.Geometry;

            var normals = mesh.Normals;

            if (normals == null || normals.Count != mesh.Positions.Count)
            {
                normals = MeshGeometryHelper.CalculateNormals(mesh);
            }

            // TODO: Also handle non-uniform scale
            var matrix = t.Clone().Value;

            matrix.OffsetX = 0;
            matrix.OffsetY = 0;
            matrix.OffsetZ = 0;
            var normalTransform = new MatrixTransform3D(matrix);

            for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                int i0 = mesh.TriangleIndices[i + 0];
                int i1 = mesh.TriangleIndices[i + 1];
                int i2 = mesh.TriangleIndices[i + 2];

                // Normal
                var faceNormal = normalTransform.Transform(normals[i0] + normals[i1] + normals[i2]);
                faceNormal.Normalize();
                WriteVector(writer, faceNormal);

                // Vertices
                WriteVertex(writer, t.Transform(mesh.Positions[i0]));
                WriteVertex(writer, t.Transform(mesh.Positions[i1]));
                WriteVertex(writer, t.Transform(mesh.Positions[i2]));

                // Attributes
                const ushort attribute = 0;
                writer.Write(attribute);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Writes a <see cref="GeometryModel3D"/> to a <see cref="BinaryWriter"/> in STL binary format.
        /// </summary>
        /// <param name="writer">The <see cref="BinaryWriter"/> to write to.</param>
        /// <param name="model">The model to write.</param>
        /// <param name="t">All vertices are transformed with this transform before written</param>
        protected override void ExportModel(BinaryWriter writer, GeometryModel3D model, Transform3D t)
        {
            var mesh = (MeshGeometry3D)model.Geometry;

            var normals = mesh.Normals;

            if (normals == null || normals.Count != mesh.Positions.Count)
            {
                normals = MeshGeometryHelper.CalculateNormals(mesh);
            }

            // TODO: Also handle non-uniform scale
            var matrix = t.Clone().Value;

            matrix.OffsetX = 0;
            matrix.OffsetY = 0;
            matrix.OffsetZ = 0;
            var normalTransform = new MatrixTransform3D(matrix);

            var material = model.Material;
            var dm       = material as DiffuseMaterial;

            var mg = material as MaterialGroup;

            if (mg != null)
            {
                foreach (var m in mg.Children)
                {
                    if (m is DiffuseMaterial)
                    {
                        dm = m as DiffuseMaterial;
                    }
                }
            }

            ushort attribute = 0;

            if (dm != null)
            {
                var scb = dm.Brush as SolidColorBrush;
                if (scb != null)
                {
                    byte r = scb.Color.R;
                    byte g = scb.Color.G;
                    byte b = scb.Color.B;
                    attribute = (ushort)((1 << 15) | ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3));
                }
            }

            for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                int i0 = mesh.TriangleIndices[i + 0];
                int i1 = mesh.TriangleIndices[i + 1];
                int i2 = mesh.TriangleIndices[i + 2];

                // Normal
                var faceNormal = normalTransform.Transform(normals[i0] + normals[i1] + normals[i2]);
                faceNormal.Normalize();
                WriteVector(writer, faceNormal);

                // Vertices
                WriteVertex(writer, t.Transform(mesh.Positions[i0]));
                WriteVertex(writer, t.Transform(mesh.Positions[i1]));
                WriteVertex(writer, t.Transform(mesh.Positions[i2]));

                // Attributes
                writer.Write(attribute);
            }
        }