@@ -31,6 +31,8 @@ var defaultShaders = {
3131 __dirname + '/shaders/light_texture.frag' ,
3232 'utf-8'
3333 ) ,
34+ phongVert : fs . readFileSync ( __dirname + '/shaders/phong.vert' , 'utf-8' ) ,
35+ phongFrag : fs . readFileSync ( __dirname + '/shaders/phong.frag' , 'utf-8' ) ,
3436 lineVert : fs . readFileSync ( __dirname + '/shaders/line.vert' , 'utf-8' ) ,
3537 lineFrag : fs . readFileSync ( __dirname + '/shaders/line.frag' , 'utf-8' )
3638} ;
@@ -59,6 +61,8 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) {
5961 attr . preserveDrawingBuffer === undefined
6062 ? true
6163 : attr . preserveDrawingBuffer ;
64+ this . attributes . perPixelLighting =
65+ attr . perPixelLighting === undefined ? false : attr . perPixelLighting ;
6266 this . _initContext ( ) ;
6367 this . isP3D = true ; //lets us know we're in 3d mode
6468 this . GL = this . drawingContext ;
@@ -189,9 +193,12 @@ p5.RendererGL.prototype._resetContext = function(attr, options, callback) {
189193 } , 0 ) ;
190194 }
191195} ;
192-
193196/**
194- *
197+ * @module Rendering
198+ * @submodule Rendering
199+ * @for p5
200+ */
201+ /**
195202 * Set attributes for the WebGL Drawing context.
196203 * This is a way of adjusting ways that the WebGL
197204 * renderer works to fine-tune the display and performance.
@@ -219,6 +226,10 @@ p5.RendererGL.prototype._resetContext = function(attr, options, callback) {
219226 * (note that p5 clears automatically on draw loop)
220227 * default is true
221228 * <br><br>
229+ * perPixelLighting - if true, per-pixel lighting will be used in the
230+ * lighting shader.
231+ * default is false
232+ * <br><br>
222233 * @method setAttributes
223234 * @for p5
224235 * @param {String } key Name of attribute
@@ -227,7 +238,7 @@ p5.RendererGL.prototype._resetContext = function(attr, options, callback) {
227238 * <div>
228239 * <code>
229240 * function setup() {
230- * createCanvas(150, 150 , WEBGL);
241+ * createCanvas(100, 100 , WEBGL);
231242 * }
232243 *
233244 * function draw() {
@@ -248,7 +259,7 @@ p5.RendererGL.prototype._resetContext = function(attr, options, callback) {
248259 * <div>
249260 * <code>
250261 * function setup() {
251- * createCanvas(150, 150 , WEBGL);
262+ * createCanvas(100, 100 , WEBGL);
252263 * setAttributes('antialias', true);
253264 * }
254265 *
@@ -265,6 +276,59 @@ p5.RendererGL.prototype._resetContext = function(attr, options, callback) {
265276 * </code>
266277 * </div>
267278 *
279+ * <div>
280+ * <code>
281+ * // press the mouse button to enable perPixelLighting
282+ * function setup() {
283+ * createCanvas(100, 100, WEBGL);
284+ * noStroke();
285+ * fill(255);
286+ * }
287+ *
288+ * var lights = [
289+ * { c: '#f00', t: 1.12, p: 1.91, r: 0.2 },
290+ * { c: '#0f0', t: 1.21, p: 1.31, r: 0.2 },
291+ * { c: '#00f', t: 1.37, p: 1.57, r: 0.2 },
292+ * { c: '#ff0', t: 1.12, p: 1.91, r: 0.7 },
293+ * { c: '#0ff', t: 1.21, p: 1.31, r: 0.7 },
294+ * { c: '#f0f', t: 1.37, p: 1.57, r: 0.7 }
295+ * ];
296+ *
297+ * function draw() {
298+ * var t = millis() / 1000 + 1000;
299+ * background(0);
300+ * directionalLight(color('#222'), 1, 1, 1);
301+ *
302+ * for (var i = 0; i < lights.length; i++) {
303+ * var light = lights[i];
304+ * pointLight(
305+ * color(light.c),
306+ * p5.Vector.fromAngles(t * light.t, t * light.p, width * light.r)
307+ * );
308+ * }
309+ *
310+ * specularMaterial(255);
311+ * sphere(width * 0.1);
312+ *
313+ * rotateX(t * 0.77);
314+ * rotateY(t * 0.83);
315+ * rotateZ(t * 0.91);
316+ * torus(width * 0.3, width * 0.07, 30, 10);
317+ * }
318+ *
319+ * function mousePressed() {
320+ * setAttributes('perPixelLighting', true);
321+ * noStroke();
322+ * fill(255);
323+ * }
324+ * function mouseReleased() {
325+ * setAttributes('perPixelLighting', false);
326+ * noStroke();
327+ * fill(255);
328+ * }
329+ * </code>
330+ * </div>
331+ *
268332 * @alt a rotating cube with smoother edges
269333 */
270334/**
@@ -278,13 +342,17 @@ p5.prototype.setAttributes = function(key, value) {
278342 var attr ;
279343 if ( typeof value !== 'undefined' ) {
280344 attr = { } ;
281- attr . key = value ;
345+ attr [ key ] = value ;
282346 } else if ( key instanceof Object ) {
283347 attr = key ;
284348 }
285349 this . _renderer . _resetContext ( attr ) ;
286350} ;
287351
352+ /**
353+ * @class p5.RendererGL
354+ */
355+
288356p5 . RendererGL . prototype . _computeCameraDefaultSettings = function ( ) {
289357 this . defaultCameraFOV = 60 / 180 * Math . PI ;
290358 this . defaultCameraAspect = this . width / this . height ;
@@ -879,11 +947,19 @@ p5.RendererGL.prototype._useImmediateModeShader = function() {
879947
880948p5 . RendererGL . prototype . _getLightShader = function ( ) {
881949 if ( ! this . _defaultLightShader ) {
882- this . _defaultLightShader = new p5 . Shader (
883- this ,
884- defaultShaders . lightVert ,
885- defaultShaders . lightTextureFrag
886- ) ;
950+ if ( this . attributes . perPixelLighting ) {
951+ this . _defaultLightShader = new p5 . Shader (
952+ this ,
953+ defaultShaders . phongVert ,
954+ defaultShaders . phongFrag
955+ ) ;
956+ } else {
957+ this . _defaultLightShader = new p5 . Shader (
958+ this ,
959+ defaultShaders . lightVert ,
960+ defaultShaders . lightTextureFrag
961+ ) ;
962+ }
887963 }
888964 //this.drawMode = constants.FILL;
889965 return this . _defaultLightShader ;
0 commit comments