Skip to content

Instantly share code, notes, and snippets.

@seflless
Created November 7, 2010 00:00
Show Gist options
  • Save seflless/665829 to your computer and use it in GitHub Desktop.
Save seflless/665829 to your computer and use it in GitHub Desktop.
Code for converting a Normal to a color for Normal Mapping Purposes
// Encodes a float with range [-1.0,1.0] into a color channel with range [0,255]
function encodeFloat(f){
if(f<0.0){
f = -f;
return Math.min(Math.floor((1.0-f)*127.0),127);
}
else{
return 128+Math.min(Math.floor(f*128.0),128);
}
}
// Encode a normal into a color
function normalToColor(n){
var c = new THREE.Color(0x000000); // No way to create a color with an r,g,b of range [0,1] or [0,255]?
c.setRGBA(encodeFloat(n.x)/255, encodeFloat(n.y)/255, encodeFloat(n.z)/255,1);
return c;
}
// So when you go to draw an element do something like this.
color = normalToColor(element.normalWorld);
@seflless
Copy link
Author

Sweet, I started cleaning up my code. Have a clean incremental Normal Map generator going. But I need to clean up the rendering code to make it more compose-able to let people build things I never thought of. If I ever get it finished, I'll write up an Article and send you a link to promote in any demos you make of the normal map renderer.

@seflless
Copy link
Author

Example of new API for Generating Normal Maps from Photos
http://endergen.com/labs/normal_mapping/demos/generate_beth.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment