Skip to content

Instantly share code, notes, and snippets.

@AncientPixel
Created April 15, 2013 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AncientPixel/5388546 to your computer and use it in GitHub Desktop.
Save AncientPixel/5388546 to your computer and use it in GitHub Desktop.
Code to transform a subdivided cube mesh into a sphere using WPFs 3D classes.
// Load base mesh
_currentModel = ModelImporter.Load(_localPath + @"Models\planetCube.3ds");
Model3DGroup modelGroup = (Model3DGroup)_currentModel;
GeometryModel3D model = (GeometryModel3D)modelGroup.Children[0]; // The first child of model group is the imported mesh.
model.BackMaterial = null;
// Make spherical
MeshGeometry3D mesh = (MeshGeometry3D)model.Geometry; // Grab reference to raw mesh data.
mesh.Normals = new Vector3DCollection(mesh.Positions.Count); // Create a new collection of normals.
for (int i = 0; i < mesh.Positions.Count; i++)
{
// Grab vectory and normilize.
Vector3D v = mesh.Positions[i].ToVector3D();
v.Normalize();
// Add nomilized vector as normal.
mesh.Normals.Add(v);
// Project normilized vector outword.
v *= 2.0;
mesh.Positions[i] = new Point3D(v.X, v.Y, v.Z); // Overwrite original.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment