-
Notifications
You must be signed in to change notification settings - Fork 350
Emulate silhouette rendering for pick and _getConvexHullPointsForDrawable #196
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1926b49
Add Silhouette class
mzgoddard 448c88e
Add EffectTransform
mzgoddard a0df715
Add Skin.isTouching
mzgoddard 8bdf567
Add Drawable.isTouching
mzgoddard 59f6492
Use isTouching methods in RenderWebGL.pick and getConvexHull
mzgoddard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @fileoverview | ||
* A utility to transform a texture coordinate to another texture coordinate | ||
* representing how the shaders apply effects. | ||
*/ | ||
|
||
const twgl = require('twgl.js'); | ||
|
||
const ShaderManager = require('./ShaderManager'); | ||
|
||
/** | ||
* A texture coordinate is between 0 and 1. 0.5 is the center position. | ||
* @const {number} | ||
*/ | ||
const CENTER_X = 0.5; | ||
|
||
/** | ||
* A texture coordinate is between 0 and 1. 0.5 is the center position. | ||
* @const {number} | ||
*/ | ||
const CENTER_Y = 0.5; | ||
|
||
class EffectTransform { | ||
/** | ||
* Transform a texture coordinate to one that would be select after applying shader effects. | ||
* @param {Drawable} drawable The drawable whose effects to emulate. | ||
* @param {twgl.v3} vec The texture coordinate to transform. | ||
* @param {?twgl.v3} dst A place to store the output coordinate. | ||
* @return {twgl.v3} The coordinate after being transform by effects. | ||
*/ | ||
static transformPoint (drawable, vec, dst) { | ||
dst = dst || twgl.v3.create(); | ||
twgl.v3.copy(vec, dst); | ||
|
||
const uniforms = drawable.getUniforms(); | ||
const effects = drawable.getEnabledEffects(); | ||
|
||
if ((effects & ShaderManager.EFFECT_INFO.mosaic.mask) !== 0) { | ||
// texcoord0 = fract(u_mosaic * texcoord0); | ||
dst[0] = uniforms.u_mosaic * dst[0] % 1; | ||
dst[1] = uniforms.u_mosaic * dst[1] % 1; | ||
} | ||
if ((effects & ShaderManager.EFFECT_INFO.pixelate.mask) !== 0) { | ||
const skinUniforms = drawable.skin.getUniforms(); | ||
// vec2 pixelTexelSize = u_skinSize / u_pixelate; | ||
const texelX = skinUniforms.u_skinSize[0] * uniforms.u_pixelate; | ||
const texelY = skinUniforms.u_skinSize[1] * uniforms.u_pixelate; | ||
// texcoord0 = (floor(texcoord0 * pixelTexelSize) + kCenter) / | ||
// pixelTexelSize; | ||
dst[0] = (Math.floor(dst[0] * texelX) + CENTER_X) / texelX; | ||
dst[1] = (Math.floor(dst[1] * texelY) + CENTER_Y) / texelY; | ||
} | ||
if ((effects & ShaderManager.EFFECT_INFO.whirl.mask) !== 0) { | ||
// const float kRadius = 0.5; | ||
const RADIUS = 0.5; | ||
// vec2 offset = texcoord0 - kCenter; | ||
const offsetX = dst[0] - CENTER_X; | ||
const offsetY = dst[1] - CENTER_Y; | ||
// float offsetMagnitude = length(offset); | ||
const offsetMagnitude = twgl.v3.length(dst); | ||
// float whirlFactor = max(1.0 - (offsetMagnitude / kRadius), 0.0); | ||
const whirlFactor = Math.max(1.0 - (offsetMagnitude / RADIUS), 0.0); | ||
// float whirlActual = u_whirl * whirlFactor * whirlFactor; | ||
const whirlActual = uniforms.u_whirl * whirlFactor * whirlFactor; | ||
// float sinWhirl = sin(whirlActual); | ||
const sinWhirl = Math.sin(whirlActual); | ||
// float cosWhirl = cos(whirlActual); | ||
const cosWhirl = Math.cos(whirlActual); | ||
// mat2 rotationMatrix = mat2( | ||
// cosWhirl, -sinWhirl, | ||
// sinWhirl, cosWhirl | ||
// ); | ||
const rot00 = cosWhirl; | ||
const rot10 = -sinWhirl; | ||
const rot01 = sinWhirl; | ||
const rot11 = cosWhirl; | ||
|
||
// texcoord0 = rotationMatrix * offset + kCenter; | ||
dst[0] = (rot00 * offsetX) + (rot10 * offsetY) + CENTER_X; | ||
dst[1] = (rot01 * offsetX) + (rot11 * offsetY) + CENTER_Y; | ||
} | ||
if ((effects & ShaderManager.EFFECT_INFO.fisheye.mask) !== 0) { | ||
// vec2 vec = (texcoord0 - kCenter) / kCenter; | ||
const vX = (dst[0] - CENTER_X) / CENTER_X; | ||
const vY = (dst[1] - CENTER_Y) / CENTER_Y; | ||
// float vecLength = length(vec); | ||
const vLength = Math.sqrt((vX * vX) + (vY * vY)); | ||
// float r = pow(min(vecLength, 1.0), u_fisheye) * max(1.0, vecLength); | ||
const r = Math.pow(Math.min(vLength, 1), uniforms.u_fisheye) * Math.max(1, vLength); | ||
// vec2 unit = vec / vecLength; | ||
const unitX = vX / vLength; | ||
const unitY = vY / vLength; | ||
// texcoord0 = kCenter + r * unit * kCenter; | ||
dst[0] = CENTER_X + (r * unitX * CENTER_X); | ||
dst[1] = CENTER_Y + (r * unitY * CENTER_Y); | ||
} | ||
|
||
return dst; | ||
} | ||
} | ||
|
||
module.exports = EffectTransform; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as abuse.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as abuse.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.