Created
November 7, 2010 00:00
-
-
Save seflless/665829 to your computer and use it in GitHub Desktop.
Code for converting a Normal to a color for Normal Mapping Purposes
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
// 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); |
Author
seflless
commented
Nov 15, 2010
Fixed! :)
mrdoob/three.js@7b58b80
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.
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