Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 9 additions & 97 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,105 +1,17 @@
# Project 6: Implicit surfaces - Marching cubes

**Goal:** Implement an isosurface created from metaballs using the marching cubes algorithm.
# Project 5: Metaballs

Metaballs are organic-looking n-dimensional objects. We will be implementing a 3-dimensional metaballs. They are great to make bloppy shapes. An isosurface is created whenever the metaball function crosses a certain threshold, called isolevel. The metaball function describes the total influences of each metaball to a given points. A metaball influence is a function between its radius and distance to the point:
https://nnewberg.github.io/Project6-MarchingCubes-Implicit-Surfaces/

`f(point) = (radius * radius) / (distance * distance)`
## Project Overview

By summing up all these influences, you effectively describes all the points that are greater than the isolevel as inside, and less than the isolevel as outside (or vice versa). As an observation, the bigger the metaball's radius is, the bigger its influence is.
I have implemented lit-sphere shaded metaballs : - ) <br>

Marching cubes essentially voxelize the space, then generate triangles based on the density function distribution at the corners of each voxel. By increasing the voxelized grid's resolution, the surface eventually gets that blobby, organic look of the metaballs. Marching cubes can achieve a similar effect to ray marching for rendering implicit surfaces, but in addition to the rendered image, you also retain actual geometries.
Try changing the number of metaballs to activate a happy accident bug.<br>
You can make cool glob shapes!<br>

Marching cubes are commonly used in MRI scanning, where you can generate geometries for the scans. Marching cubes are also used to generate complex terrains with caves in games. The additional geometry information can handily support collision and other physical calculation for game engines. For example, their bounding boxes can then be computed to construct the acceleration data structure for collisions.
## Pics

**Warning**: this assignment option requires more effort than the ray marching option. The two base codes diverge significantly, so switching options midway can be costly for your time and effort.
![img](https://github.com/nnewberg/Project6-MarchingCubes-Implicit-Surfaces/blob/master/screenshots/Screen%20Shot%202017-03-02%20at%207.08.21%20PM.png?raw=true)

## Resources
We suggest reading the following resources before starting your assignment:

- [Generating complex terrain](https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch01.html) from [GPU Gems 3](https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_pref01.html).
- [Polygonising a scalar field](http://paulbourke.net/geometry/polygonise/) by Paul Bourke.
- [Marching squares](http://jamie-wong.com/2014/08/19/metaballs-and-marching-squares/) by Jamie Wong.

## Base code framework

We have provided a basecode as a reference. You are welcome to modify the framework for your project. The basecode implements metaballs on the CPU.

_main.js_:

- `App`:

This is a global configuration object. All information for the marching cubes are stored here.

**Note**: `App.visualDebug` is a global control of all the visual debugging components. Even though it is helpful for development, it could be memory intensive. Toggle this flag off for better perforamance at high resolution.

_marching_cubes.js_:

- `class MarchingCubes`:
This class encapsulates everything about the metaballs, grid, voxels, and sampling information.

- `class Voxel`:
This class contains information about a single voxel, and its sample points. Polygonization happens here.

_inspect_point.js_:

- `class InspectPoint`:
This class simply contains a single sample point that can output its value on the screen at its pixel location.

_metaball.js_:

- `class Metaball`:
This class represents a single metaball.

_marching_cube_LUT.js_:

This file contains the edge table and the triangle table for the marching cubes.

## Animate metaballs (5 points)
Implement the `update` for metaballs to move its position based velocity. Reverse the velocity whenever the metaball goes out of bounds. Since the metaball function is not well defined at the boundaries, maintain an additional small margin so that the metaball can reverse its moving direction before reaching the bounds.

## Metaball function (2 points)
Implement the metaball function inside `sample` of `MarchingCubes`. This function should return the total influences of all moving metaballs with respect to a given point.

## Sampling at corners (15 points)
In order to polygonize a voxel, generate new samples at each corner of the voxel. Their isovalues must be updated as the metaball function changes due of metaballs moving.

## Polygonization (50 points)
Implement `polygonize` inside `Cell` class. This function should return the list of **vertices** and **normals** of the triangles polygonized in the voxel.

### Vertices (30 points out of 50)
To compute the vertices, we have provided the look-up tables from Paul Bourke's. The table assumes the following indexing scheme:
![](./ref_voxel_indexing.png)

- _The eight corners can be represented as an 8-bit number, where 1 means the isovalue is above or below the isolevel based on your implementation._
- _The twelve edges can be represented as a 12-bit number, where 1 means that the isosurface intersects with this edge._

- **EDGE_TABLE**: This table returns a 12-bit number that represents the edges intersected by the isosurface. For each intersected edge, compute the linearly interpolated vertex position on the edge according to the isovalue at each end corner of the edge.

- **TRI_TABLE**: This table acts as the triangle indices. Every 16 elements in the table represents a possible polygonizing configuration. Within each configuration, every three consecutive elements represents the indices of a triangle that should be created from the edges above.

### Normals (20 points out of 50)
Compute the normals using the gradient of the vertex with respect to the x, y, and z. The normals are then used for shading different materials.

## Meshing (18 points)
The mesh for the metaball's isosurface should be created once. At each frame, using the list of **vertices** and **normals** polygonized from the voxels, update the mesh's geometry for the isosurface. Notice that the total volume of the mesh does change.

## Materials and post-processing (10 points)
Interesting shader materials beyond just the provided threejs materials. We encourage using your previous shaders assignment for this part.

## Extra credits (Up to 30 points)
- Metaball can be positive or negative. A negative metaball will substract from the surface, which pushed the surface inward. **Implement a scene with both positive and negative metaballs. (10 points)**
- **More implicit surfaces!** For example: planes, mesh, etc.). Some very interesting ideas are to blend your metaballs into those surfaces. **(5 points for each)**

## Submission

- Update `README.md` to contain a solid description of your project
- Publish your project to gh-pages. `npm run deploy`. It should now be visible at http://username.github.io/repo-name
- Create a [pull request](https://help.github.com/articles/creating-a-pull-request/) to this repository, and in the comment, include a link to your published project.
- Submit the link to your pull request on Canvas.

## Deploy
- `npm run build`
- Add and commit all changes
- `npm run deploy`
- If you're having problems with assets not linking correctly, make sure you wrap you're filepaths in `require()`. This will make the bundler package and your static assets as well. So, instead of `loadTexture('./images/thing.bmp')`, do `loadTexture(require('./images/thing.bmp'))`.
![img](https://github.com/nnewberg/Project6-MarchingCubes-Implicit-Surfaces/blob/master/screenshots/Screen%20Shot%202017-03-02%20at%207.07.52%20PM.png?raw=true)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/sphere-textures/bluegreen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/sphere-textures/bronze.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/sphere-textures/chrome.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/sphere-textures/colorwheel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/sphere-textures/red.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/sphere-textures/slime.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function init(callback, update) {
var renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x020202, 0);
renderer.setClearColor(0x000000, 1);

var controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
Expand Down
10 changes: 10 additions & 0 deletions src/glsl/lit-sphere-frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
uniform sampler2D uSphereTexture;

varying vec2 vNormal;

void main() {

vec3 textureColor = texture2D( uSphereTexture, vNormal ).rgb;
gl_FragColor = vec4( textureColor, 1. );

}
20 changes: 20 additions & 0 deletions src/glsl/lit-sphere-vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
varying vec2 vNormal;

void main(){

vec4 vertPos = vec4(position, 1.0);

vec3 eye = normalize(vec3(modelViewMatrix * vertPos));
vec3 screenNormal = normalize(normalMatrix * normal);

vec3 reflected= reflect(eye, screenNormal);
float m = 2.0 * sqrt(
pow( reflected.x, 2.0 ) +
pow( reflected.y, 2.0 ) +
pow( reflected.z + 1.0, 2.0 )
);
vNormal = reflected.xy/m + 0.50;

gl_Position = projectionMatrix * modelViewMatrix * vertPos;

}
60 changes: 30 additions & 30 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import Framework from './framework'
import LUT from './marching_cube_LUT.js'
import MarchingCubes from './marching_cubes.js'

const DEFAULT_VISUAL_DEBUG = true;
const DEFAULT_VISUAL_DEBUG = false;
const DEFAULT_ISO_LEVEL = 1.0;
const DEFAULT_GRID_RES = 4;
const DEFAULT_GRID_RES = 30;
const DEFAULT_GRID_WIDTH = 10;
const DEFAULT_NUM_METABALLS = 10;
const DEFAULT_MIN_RADIUS = 0.5;
const DEFAULT_MAX_RADIUS = 1;
const DEFAULT_MAX_SPEED = 0.01;
const DEFAULT_MAX_RADIUS = 1.0;
const DEFAULT_MAX_SPEED = 0.025;

var App = {
//
Expand Down Expand Up @@ -71,8 +71,8 @@ function onLoad(framework) {
App.camera = camera;
App.renderer = renderer;

renderer.setClearColor( 0xbfd1e5 );
scene.add(new THREE.AxisHelper(20));
renderer.setClearColor( 0x222222 );
// scene.add(new THREE.AxisHelper(20));

setupCamera(App.camera);
setupLights(App.scene);
Expand All @@ -90,7 +90,7 @@ function onUpdate(framework) {

function setupCamera(camera) {
// set camera position
camera.position.set(5, 5, 30);
camera.position.set(10, 10, 15);
camera.lookAt(new THREE.Vector3(0,0,0));
}

Expand Down Expand Up @@ -131,29 +131,29 @@ function setupGUI(gui) {

// --- DEBUG ---

var debugFolder = gui.addFolder('Debug');
debugFolder.add(App.marchingCubes, 'showGrid').onChange(function(value) {
App.marchingCubes.showGrid = value;
if (value) {
App.marchingCubes.show();
} else {
App.marchingCubes.hide();
}
});

debugFolder.add(App.marchingCubes, 'showSpheres').onChange(function(value) {
App.marchingCubes.showSpheres = value;
if (value) {
for (var i = 0; i < App.config.numMetaballs; i++) {
App.marchingCubes.balls[i].show();
}
} else {
for (var i = 0; i < App.config.numMetaballs; i++) {
App.marchingCubes.balls[i].hide();
}
}
});
debugFolder.open();
// var debugFolder = gui.addFolder('Debug');
// debugFolder.add(App.marchingCubes, 'showGrid').onChange(function(value) {
// App.marchingCubes.showGrid = value;
// if (value) {
// App.marchingCubes.show();
// } else {
// App.marchingCubes.hide();
// }
// });

// debugFolder.add(App.marchingCubes, 'showSpheres').onChange(function(value) {
// App.marchingCubes.showSpheres = value;
// if (value) {
// for (var i = 0; i < App.config.numMetaballs; i++) {
// App.marchingCubes.balls[i].show();
// }
// } else {
// for (var i = 0; i < App.config.numMetaballs; i++) {
// App.marchingCubes.balls[i].hide();
// }
// }
// });
// debugFolder.open();
}

// when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate
Expand Down
Loading