Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aman #13

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open

Aman #13

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8c95b97
Gonna start meshing
Feb 27, 2017
de1212e
almost have metaballs; mesh is not updating
AmanSachan1 Feb 27, 2017
34f38bf
Just have to make meshes dynamic now
Feb 28, 2017
548aca6
meshing
Feb 28, 2017
4e2f78c
ahhhhhh
AmanSachan1 Feb 28, 2017
aca9efb
I has mesh but it crashes on compute Normal after like half a second
Feb 28, 2017
c84986a
I has metaballs
Feb 28, 2017
1395f44
So Meta!
AmanSachan1 Mar 1, 2017
df1deb6
So Meta
AmanSachan1 Mar 1, 2017
6a4e471
arggghhh just commit
AmanSachan1 Mar 1, 2017
1676ef3
commit
AmanSachan1 Mar 1, 2017
0280007
done
AmanSachan1 Mar 1, 2017
b17a30d
readme
AmanSachan1 Mar 1, 2017
52d60ba
cleaning
Mar 1, 2017
bc19bf6
trying to deploy
AmanSachan1 Mar 9, 2017
08018c8
deploy
AmanSachan1 Mar 9, 2017
deeece2
comments and cleanup
AmanSachan1 Mar 9, 2017
a683996
cleaned
AmanSachan1 Mar 10, 2017
0f74ae5
made it faster, better and use vertex normals
AmanSachan1 Jul 3, 2017
af2ef77
new build for deploying
AmanSachan1 Jul 3, 2017
90d700c
new build for deploying
AmanSachan1 Jul 3, 2017
e03b67b
visual studio files accidentally added, now removed
AmanSachan1 Jul 3, 2017
a42766c
better coloring scheme, removed a gui option
AmanSachan1 Jul 4, 2017
054402a
reshaped the grid
AmanSachan1 Aug 16, 2017
5d3156d
added more gui, camera placement, lit sphere shader, lava lamp
AmanSachan1 Aug 16, 2017
7326184
deploying
AmanSachan1 Aug 16, 2017
4461587
trying to deploy again
AmanSachan1 Aug 16, 2017
ebaf1ed
converted textures to bmp for deploying
AmanSachan1 Aug 16, 2017
4791c7e
slight tweak to numMetaballs in gui
AmanSachan1 Aug 17, 2017
438cb34
deploting
AmanSachan1 Aug 17, 2017
9d0879b
readme update
AmanSachan1 Oct 6, 2017
fb4acf7
readme update
AmanSachan1 Oct 6, 2017
a72f082
readme update
AmanSachan1 Oct 6, 2017
ae0fdb7
readme update
AmanSachan1 Oct 6, 2017
0b97118
readme update
AmanSachan1 Oct 6, 2017
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
Binary file added Images/Lerp_examples.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 Images/MC_Cases.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 Images/MetaballContact.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 Images/Metaballs_vimeoLink.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 Images/NormalCalc1D.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 Images/Untitled.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 Images/Voxel_Indexing.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 Images/isoValueEquation.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 Images/normalEquation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 39 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,105 +1,70 @@
# Project 6: Implicit surfaces - Marching cubes
# Metaballic Lava Lamp

**Goal:** Implement an isosurface created from metaballs using the marching cubes algorithm.
[![](Images/Metaballs_vimeoLink.png)](https://vimeo.com/227361586)

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:
## Overview

`f(point) = (radius * radius) / (distance * distance)`
Metaballs are organic looking n-dimensional objects that are really popular for creating smooth and dynamic shapes. Each metaball is defined as a function having n-dimensions (in this case 3 dimensions). A threshold, also known as the iso-level is chosen to define the iso-surface formed from the combined influence of all the metaballs which defines the resulting shape.

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.
## Technical Approach

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.
Since the resulting iso-surface is defined by a density or influence function, the naive approach would be to evaluate that function for every point in space, where the resolution of the space would define the smoothness of the surface. This as one might expect is ridiculously expensive computationally speaking, but there are better techniques we can leverage:

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.
### Marching Cubes Algorithm

**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.
![](Images/MC_Cases.jpg)

## 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_:
To ease the computation involved, we voxelize the space up to a desired resolution and then for every voxel query the density function and use the reulting values to draw a 3D polygonal approximation of the surface. There are 256 (2^8) configurations for a voxel sampled at its 8 corners, however only 15 different configurations can be used to represent all 256 due to symmetry and with the help of rotations; and all of these configurations can be represented with upto 5 triangles/voxel.

- `App`:
#### Linear Interpolation

This is a global configuration object. All information for the marching cubes are stored here.
![](Images/Lerp_examples.jpg)

**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.
_A 2D example showing how varying densities can effect the edge (isoline) that is formed._

_marching_cubes.js_:
The polygonal configurations above can be made a more accurate representation of the isosurface even at low resolutions by using the density values at the corners to find new points on the bounding box of the voxel to define the lines that make the triangles of the isosurface.

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

- `class Voxel`:
This class contains information about a single voxel, and its sample points. Polygonization happens here.
The eight corners of the voxel can be represented by an 8-bit number, where 1 means the isovalue is above or below the isolevel based on some predefined threshold.And the twelve edges of the voxel can be represented as a 12-bit number, where 1 means that the isosurface intersects with this edge.

_inspect_point.js_:
#### EDGE_TABLE:

- `class InspectPoint`:
This class simply contains a single sample point that can output its value on the screen at its pixel location.
This table returns a 12-bit number that represents the edges intersected by the isosurface. For each intersected edge, we can compute the linearly interpolated vertex position on the edge according to the isovalue at each end corner of the edge.

_metaball.js_:
#### TRI_TABLE:

- `class Metaball`:
This class represents a single metaball.
![](Images/Voxel_Indexing.jpg)

_marching_cube_LUT.js_:
This table stores the triangle indices corresponding to the vertex positions on the edges identified above. 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.

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

## 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.
Having implemented all of the above, our metaballs would look quite low poly at lower resolutions. An easy fix for this is to calculate the normal for all the points that makes the polygonal configuration of the voxel. This gives us per-vertex data for normals which when passed through a fragment shader results in a much smoother surface than before (it is the equivalent of the switch from having normals per face to having normals per vertex of the mesh).

## 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.
Calculating the normal at an arbitrary point involves finding the gradient at that point. This can be expensive so a good approximation for the same is to sample neighboring points along the n-dimensions, and use their difference to approximate the slope.

## 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.
A 1D representation would be:

## 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.
![](Images/NormalCalc1D.jpg)

### 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)
For 3D density functions the normal would be represented as:

- _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._
![](Images/normalEquation.png)

- **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.
### Density Function
Defined as the sum of the influences of all the metaballs within its vicinity, which have a inverse square fall-off in terms of intensity

- **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.
![](Images/isoValueEquation.png)

### 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.
## More about the Marching Cubes Algorithm
The Marching cubes algorithm can achieve a effect similar to ray marching for rendering implicit surfaces, but in addition to the rendered image, you also retain geometry.

## 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.
Common Use Cases:
- In MRI scanning, to generate geometry for the scans.
- To generate complex terrain for games. The resulting geometrical 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.

## 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'))`.
## Resources
- [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.
Binary file added build/assets/Brown-47ac47.bmp
Binary file not shown.
Binary file added build/assets/Flame-6aaec9.bmp
Binary file not shown.
Binary file added build/assets/Gold-18a4ff.bmp
Binary file not shown.
Binary file added build/assets/Green-13adc2.bmp
Binary file not shown.
Loading