Created
April 15, 2013 14:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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