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

Pass lights into the shader in drawObject.ts. #11

Merged
merged 9 commits into from
May 3, 2018

Conversation

armcburney
Copy link
Member

@armcburney armcburney commented May 2, 2018

Changes

Examples

One Light

screen shot 2018-05-02 at 6 51 56 pm

Two Lights

screen shot 2018-05-02 at 6 52 04 pm

@armcburney armcburney changed the title GPass lights into the shader in drawObject.ts. Pass lights into the shader in drawObject.ts. May 2, 2018
*/
export function drawObject(
regl: REGL.regl
regl: REGL.regl,
numLights: number = 1,
Copy link
Member

Choose a reason for hiding this comment

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

Do you think we can make numLights be a prop we pass in? so like the regl.prop( ... ) things from before. regl.prop('something') is a shortcut for (context, props, batchId) => props['something'], so we can maybe use a function like that to do the numLights assertion

'lightIntensities[0]': 256,
'lightColors[0]': [1, 1, 1]
numLights: numLights,
...buildLightMetadata(numLights, maxLights)
Copy link
Member

Choose a reason for hiding this comment

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

Can we also add information about lights to the parameters that get passed in? so like defining an interface for a light containing a position, intensity, and color that you can use when building metadata instead of always using multiple copies of the hardcoded light I had before

* Shader to draw a single object with Phong shading to the screen
* Shader to draw a single object with Phong shading to the screen.
*
* @param {REGL.regl} regl: The regl object we're drawing.
Copy link
Member

Choose a reason for hiding this comment

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

nit: regl is like a factory to build a function to draw an object rather than the object being drawn


return visibleLightsJSON
.concat(nonVisibleLightsJSON)
.reduce((accum: {}, obj: {}) => { return { ...accum, ...obj }; }, {});
Copy link
Member

Choose a reason for hiding this comment

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

👌

): REGL.DrawCommand<REGL.DefaultContext, DrawObjectProps> {
if (numLights > maxLights) {
throw new RangeError('numLights must be less than or equal to maxLights.');
Copy link
Member

Choose a reason for hiding this comment

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

Can we put the actual number of lights at the end of the error message? And maybe the requested number of lights?
I'm not sure if it's bad practice to make the error message dynamic like that, but I think it would be useful to have
"numLights (24) must be less than or equal to maxLights (20)"

* @param {REGL.Vec3} lightPositions A vector of length 3 denoting the x, y, z point where the light resides in the
* vector-space.
* @param {REGL.Vec3} lightColors A vector of length 3 denoting _.
* @param {REGL.Vec3} lightIntensities A vector of length 3 denoting _.
Copy link
Member Author

Choose a reason for hiding this comment

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

Could use some help with these descriptions @davepagurek 😳

Copy link
Member

Choose a reason for hiding this comment

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

  • lightColor is a vec3 representing the r, g, and b values of the light's color, where each channel is in [0, 1]
  • lightIntensity is a number affecting specular reflection where a lower number produces a softer, larger reflection and a higher number produces a smaller, harder reflection

@@ -64,7 +66,7 @@ export function drawObject(
frag: `
precision mediump float;

const int MAX_LIGHTS = 1; // TODO(davepagurek) [#10] Increase this to allow more lights
const int MAX_LIGHTS = ${regl.prop('maxLights')};
Copy link
Member Author

Choose a reason for hiding this comment

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

This is currently giving me an error. I'm a bit unfamiliar with regl, so I'd appreciate any help @davepagurek.

screen shot 2018-05-02 at 4 25 46 pm

Copy link
Member

Choose a reason for hiding this comment

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

This is because regl.prop('maxLights') returns a function, not a value. I think you just want maxLights directly since it's passed in at the shader's compile time

Copy link
Member

Choose a reason for hiding this comment

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

so actually I think keeping maxLights as a second argument in addition to a regl instance is good

import REGL = require('regl');

export interface LightMetadata {
lightPositions: REGL.Vec3[];
Copy link
Member

Choose a reason for hiding this comment

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

since this is the data for one light (maybe just call it Light?), these properties should be singular, i.e. lightPosition: REGL.Vec3

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. 👍


export const blankLightMetadata: LightMetadata = {
lightPositions: [0, 0, 0],
lightIntensities: [0, 0, 0],
Copy link
Member

Choose a reason for hiding this comment

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

Intensity should just be a scalar instead of an array. Unfortunately it looks like typescript is bad at actually typechecking REGL.anything types

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, my linter was complaining. Should it be lightIntensities or just lightIntensity then?

Copy link
Member

Choose a reason for hiding this comment

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

just the singular I think

const visibleLightsJSON: {}[] = range(maxLights).map((index: number) => {
return {
[`lightPositions[${index}]`]: (_context, props, _batch_id) => {
const light: LightMetadata = props.lights[index];
Copy link
Member

Choose a reason for hiding this comment

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

I think technically the type for light here would be LightMetadata | undefined

lightIntensity: 256
}

xdescribe('addLight', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

#20

})
indices: o.indices,
numLights: this.lights.length,
maxLights: this.maxLights,
Copy link
Member

Choose a reason for hiding this comment

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

tiny nit: we can take out maxLights here since we pass it in along with regl earlier so this prop doesn't get used

Copy link
Member

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

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

Just one small change taking out a prop we don't use, after that lgtm!

@armcburney armcburney merged commit 7afbf5d into calder-gl:master May 3, 2018
@armcburney armcburney deleted the lights_in_shader branch May 3, 2018 00:52
@armcburney armcburney mentioned this pull request May 3, 2018
Copy link
Member

@pbardea pbardea left a comment

Choose a reason for hiding this comment

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

[DELETED]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants