Пример #1
0
        Mesh BakeMeshLighting(MeshFilter mf, Color32 tintColor)
        {
            BakedMesh bm = new BakedMesh();

            bm.mesh      = mf.sharedMesh;
            bm.tintColor = tintColor;

            Mesh mesh;

            if (bakedMeshes.TryGetValue(bm, out mesh))
            {
                return(mesh);
            }

            Mesh meshWithColors = Instantiate <Mesh>(bm.mesh);

            meshWithColors.hideFlags = HideFlags.DontSave;

            Color32[] colors32 = meshWithColors.colors32;
            modelMeshColors.Clear();
            int vertexCount = meshWithColors.vertexCount;

            if (colors32 == null || colors32.Length == 0)
            {
                for (int c = 0; c < vertexCount; c++)
                {
                    modelMeshColors.Add(tintColor);
                }
            }
            else
            {
                for (int c = 0; c < vertexCount; c++)
                {
                    Color32 color = tintColor.MultiplyRGB(colors32[c]);
                    modelMeshColors.Add(color);
                }
            }
            meshWithColors.SetColors(modelMeshColors);

            bakedMeshes[bm] = meshWithColors;
            return(meshWithColors);
        }
Пример #2
0
        public static BakedMesh BakeMesh(Mesh mesh, bool hasMaterial)
        {
            var             bakedMesh  = new BakedMesh();
            var             data       = mesh.VertexStream.Data;
            var             gm3d       = new GeometryModel3D();
            var             mesh3d     = new MeshGeometry3D();
            var             mat        = new MaterialGroup();
            DiffuseMaterial diffuseMat = null;
            bool            hasTexture = false;

            if (mesh.Material.TryGetItem(out var meshMat))
            {
                if (meshMat.Textures.Count > 0 &&
                    meshMat.Textures.First().Value.TryGetItem(out var tex) &&
                    tex.HasPixelData)
                {
                    hasTexture = true;
                    var bitmap = ResourceCache.GetImage(tex, 1024, ResourceCache.CHANNEL_MODE_RGBA);
                    var brush  = new ImageBrush(bitmap);
                    // http://csharphelper.com/blog/2014/10/apply-textures-to-triangles-using-wpf-and-c/
                    brush.ViewportUnits = BrushMappingMode.Absolute;
                    diffuseMat          = new DiffuseMaterial(brush);
                }
            }

            if (diffuseMat == null)
            {
                diffuseMat = new DiffuseMaterial(new SolidColorBrush(Colors.Gainsboro));
            }

            mat.Children.Add(diffuseMat);
            mat.Children.Add(new SpecularMaterial());
            double massX = 0d, massY = 0d, massZ = 0d;
            float  geoMinX = float.MaxValue, geoMinY = float.MaxValue, geoMinZ = float.MaxValue;
            float  geoMaxX = float.MinValue, geoMaxY = float.MinValue, geoMaxZ = float.MinValue;
            int    numVert = data.Positions.Length;

            var positions = new Point3DCollection(mesh.VertexCount);

            foreach (var position in data.Positions)
            {
                massX  += position.X;
                massY  += position.Y;
                massZ  += position.Z;
                geoMinX = Math.Min(geoMinX, position.X);
                geoMinY = Math.Min(geoMinY, position.Y);
                geoMinZ = Math.Min(geoMinZ, position.Z);
                geoMaxX = Math.Max(geoMaxX, position.X);
                geoMaxY = Math.Max(geoMaxY, position.Y);
                geoMaxZ = Math.Max(geoMaxZ, position.Z);
                positions.Add(new Point3D(position.X, position.Y, position.Z));
            }

            if (numVert > 0)
            {
                massX /= numVert;
                massY /= numVert;
                massZ /= numVert;
            }
            else
            {
                geoMinX = geoMinY = geoMinZ = geoMaxX = geoMaxY = geoMaxZ = 0f;
            }
            mesh3d.Positions = positions;

            var normals = new Vector3DCollection(mesh.VertexCount);

            foreach (var normal in data.Normals)
            {
                normals.Add(new Vector3D(normal.X, normal.Y, normal.Z));
            }
            mesh3d.Normals = normals;

            if (hasTexture)
            {
                var uvs = new PointCollection(mesh.VertexCount);
                foreach (var uv in data.Uv1)
                {
                    // wpf 3d viewport doesn't support texture wrap
                    double x = uv.X;
                    double y = uv.Y;
                    x = x - Math.Floor(x);
                    y = y - Math.Floor(y);
                    uvs.Add(new Point(x, y));
                }
                mesh3d.TextureCoordinates = uvs;
            }

            var indices = new Int32Collection(mesh.FaceCount * 3);

            foreach (var index in data.Indices)
            {
                indices.Add(index);
            }
            mesh3d.TriangleIndices = indices;

            gm3d.Geometry = mesh3d;
            gm3d.Material = mat;
            gm3d.Freeze();
            bakedMesh.Mesh         = gm3d;
            bakedMesh.CenterOfMass = new Point3D(massX, massY, massZ);
            bakedMesh.BoundingBox  = new BoundingBox(
                new Vector3(geoMinX, geoMinY, geoMinZ),
                new Vector3(geoMaxX, geoMaxY, geoMaxZ));
            return(bakedMesh);
        }