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

Feature implementation: noLights() #3955

Merged
merged 10 commits into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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
64 changes: 63 additions & 1 deletion src/webgl/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ p5.prototype.lightFalloff = function(
* </div>
*
* @alt
* Something
* Spot light on a sphere which changes position with mouse
*/
/**
* @method spotLight
Expand Down Expand Up @@ -832,4 +832,66 @@ p5.prototype.spotLight = function(
return this;
};

/**
* This function will remove all the lights from the sketch for the
* subsequent materials rendered. It affects all the subsequent methods.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put a note in here about how to re-enable lights?

Copy link
Contributor Author

@sanketsingh24 sanketsingh24 Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this: To Re-enable lights, you need to call the lighting methods again?

Copy link
Contributor

@aferriss aferriss Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "Calls to lighting methods made after noLights() will re-enable lights in the sketch."

* @method noLights
* @chainable
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
* }
* function draw() {
* background(0);
* noStroke();
*
* ambientLight(150, 0, 0);
* translate(-25, 0, 0);
* ambientMaterial(250);
* sphere(20);
*
* noLights();
* ambientLight(0, 150, 0);
* translate(50, 0, 0);
* ambientMaterial(250);
* sphere(20);
* }
* </code>
* </div>
*
* @alt
* Two spheres showing different colors
*/
p5.prototype.noLights = function() {
this._assert3d('noLights');
p5._validateParameters('noLights', arguments);

this._renderer.ambientLightColors.length = 0;
this._renderer.specularColors = [1, 1, 1];

this._renderer.directionalLightDirections.length = 0;
this._renderer.directionalLightDiffuseColors.length = 0;
this._renderer.directionalLightSpecularColors.length = 0;

this._renderer.pointLightPositions.length = 0;
this._renderer.pointLightDiffuseColors.length = 0;
this._renderer.pointLightSpecularColors.length = 0;

this._renderer.spotLightPositions.length = 0;
this._renderer.spotLightDirections.length = 0;
this._renderer.spotLightDiffuseColors.length = 0;
this._renderer.spotLightSpecularColors.length = 0;
this._renderer.spotLightAngle.length = 0;
this._renderer.spotLightConc.length = 0;

this._renderer.constantAttenuation = 1;
this._renderer.linearAttenuation = 0;
this._renderer.quadraticAttenuation = 0;
this._renderer._useShininess = 1;

return this;
};

export default p5;
17 changes: 17 additions & 0 deletions test/manual-test-examples/webgl/lights/noLights/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="../../../styles.css">
<script language="javascript" type="text/javascript" src="../../../../../lib/p5.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>
<script src="../../stats.js"></script>
</head>

<body>
</body>

</html>
18 changes: 18 additions & 0 deletions test/manual-test-examples/webgl/lights/noLights/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
}

function draw() {
background(0);

ambientLight(150, 0, 0);
translate(-200, 0, 0);
ambientMaterial(250);
sphere(50, 64);

noLights();
ambientLight(0, 150, 0);
translate(400, 0, 0);
ambientMaterial(250);
sphere(50, 64);
}
23 changes: 23 additions & 0 deletions test/unit/webgl/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ suite('light', function() {
0
]);
});
test('noLights works', function() {
myp5.ambientLight(200, 0, 0);
myp5.pointLight(255, 0, 0, 0, 0, 0);
myp5.directionalLight(255, 0, 0, 0, 0, 0);
myp5.specularColor(255, 0, 0);
myp5.spotLight(255, 0, 255, 1, 2, 3, 0, 1, 0, Math.PI / 4, 7);
myp5.shininess(50);

myp5.noLights();
assert.deepEqual([], myp5._renderer.ambientLightColors);
assert.deepEqual([], myp5._renderer.pointLightDiffuseColors);
assert.deepEqual([], myp5._renderer.pointLightSpecularColors);
assert.deepEqual([], myp5._renderer.pointLightPositions);
assert.deepEqual([], myp5._renderer.directionalLightDiffuseColors);
assert.deepEqual([], myp5._renderer.directionalLightSpecularColors);
assert.deepEqual([], myp5._renderer.directionalLightDirections);
assert.deepEqual([1, 1, 1], myp5._renderer.specularColors);
assert.deepEqual([], myp5._renderer.spotLightDiffuseColors);
assert.deepEqual([], myp5._renderer.spotLightSpecularColors);
assert.deepEqual([], myp5._renderer.spotLightPositions);
assert.deepEqual([], myp5._renderer.spotLightDirections);
assert.deepEqual(1, myp5._renderer._useShininess);
});
});

suite('spotlight inputs', function() {
Expand Down
15 changes: 15 additions & 0 deletions test/unit/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ suite('p5.RendererGL', function() {
done();
});

test('push/pop and noLights() works', function(done) {
myp5.createCanvas(100, 100, myp5.WEBGL);
myp5.ambientLight(0, 0, 200);
var ambColors = myp5._renderer.ambientLightColors.slice();
myp5.push();
myp5.ambientLight(0, 200, 0);
var ambPopColors = myp5._renderer.ambientLightColors.slice();
myp5.noLights();
assert.notEqual(ambColors, myp5._renderer.ambientLightColors);
assert.notEqual(ambPopColors, myp5._renderer.ambientLightColors);
myp5.pop();
assert.deepEqual(ambColors, myp5._renderer.ambientLightColors);
done();
});

test('push/pop and texture() works', function(done) {
myp5.createCanvas(100, 100, myp5.WEBGL);
var tex1 = myp5.createGraphics(1, 1);
Expand Down