Commit graph

47 commits

Author SHA1 Message Date
Christopher Willis-Ford
1f0cd4b61f Fix resource leaks discovered while testing
- `Drawable` now removes its `Skin` on `dispose`, disconnecting events.
- Creating a new `Drawable` doesn't always create a new `Skin` any more:
  now it can share an existing skin.
- Don't throw when asked to update the properties of a `Drawable` that
  has already been destroyed. This seems like a VM bug but was causing
  overwhelming output in the browser for some projects.
2016-12-29 13:42:58 -08:00
Christopher Willis-Ford
6a0798062b Make a Skin emit an event when it is altered
This allows any `Drawable` using the `Skin` to update its transform as
necessary.
2016-12-29 10:46:12 -08:00
Christopher Willis-Ford
ba5deb9936 Minor cleanup after Drawable/Skin refactor
- Add & correct a few comments
- Make `Drawable` IDs work like `Skin` IDs
- Remove some dead code
- Fix costume resolution in `BitmapSkin` before first `setBitmap` call
- Rename `Drawable` & `Skin` management variables in `RenderWebGL` to
  clarify their purpose and better distinguish them from each other
- Remove problematic premature optimization in `_drawThese`
- Fix missing return statement in `Skin`'s `setRotationCenter`
- Fix mismatch between getTexture & getUniforms in the `Skin` class
2016-12-28 14:38:48 -08:00
Christopher Willis-Ford
64c5991486 Split Skin functionality out of Drawable
There is now a `Skin` base class along with `SVGSkin` and `BitmapSkin`
classes for vector and bitmap skin support. Loading a skin by URL is in
the process of being deprecated. In the future skin data should be
downloaded outside the renderer and supplied through new `create*Skin`
API calls.
2016-12-28 12:17:45 -08:00
Christopher Willis-Ford
4730a057ff Move Drawable.NONE into new RenderConstants
This constant will soon be shared with another class.
2016-12-22 12:58:58 -08:00
Christopher Willis-Ford
b18ddadbe6 Rename color4bToID to color3bToID
A recent change made this function take only RGB instead of RGBA, so the
name might as well change to match.
2016-12-15 10:26:24 -08:00
Christopher Willis-Ford
4afbba3657 Respond to review feedback
- Remove unused alpha parameter from `color4bToID`
- Don't wrap URLs for default skins in `Drawable`
2016-12-14 13:38:43 -08:00
Christopher Willis-Ford
d1ea82b2bf Use this inside => callbacks
Some callbacks used a variable called `instance` to stash `this` for use
inside the callback. Now that those callbacks have been converted from
`function` to `=>`, they can just use `this` instead.
2016-12-13 15:51:46 -08:00
Christopher Willis-Ford
b700560198 Convert class methods to ES6 style
Also un-wrap lines which were originally written for an 80-column limit
but don't need to wrap now that the limit is 120 columns.
2016-12-13 15:32:07 -08:00
Christopher Willis-Ford
3c80ed73a7 Fix lint warnings 2016-12-12 12:06:19 -08:00
Christopher Willis-Ford
5db860efed Use eslint-config-scratch, fix linting errors 2016-12-12 12:06:19 -08:00
Tim Mickel
b35f684478 A start on SVG "quirks mode" (#56)
* Start of an SVG rendering quirks mode

* Provide font defaults in SVG quirks mode

* Remove svg-to-image dependency

* Remove fonts and use package fonts instead.

* try/finally for getBBox
2016-11-07 17:40:59 -05:00
Tim Mickel
be6d2dc4e4 Bounding-box optimizations for touching color, touching drawables (#55)
* Add Rectangle utility and use it in Drawable.getBounds

* Add `getAABB`, `getFastBounds`.

* Add width and height getters to Rectangle

* Add rectangle clamp

* Optimized isTouchingColor

* Optimized isTouchingDrawables

* Rectangle.ceil -> Rectangle.snapToInt

* Refactor to common _touchingQueryCandidates

* Split helper into two
2016-10-24 13:19:20 -04:00
Tim Mickel
2cffa7b643 Add rotation center and bitmap/costume resolution (#51) 2016-10-19 17:02:04 -04:00
Tim Mickel
85c8b599a0 Update dirty transform before calculating bounds (#47) 2016-10-12 15:20:40 -04:00
Tim Mickel
8e7a85d1a6 Fix y coordinates of bounding box (#46) 2016-10-12 14:32:14 -04:00
Tim Mickel
1fa97d248b Expose skin/costume size (#45)
* Expose skin/costume size

* Return skin size as a copy
2016-10-11 18:43:57 -04:00
Tim Mickel
a62d52a2d0 Calculating tight bounding boxes for Drawables (#42) 2016-10-11 18:14:14 -04:00
Tim Mickel
2b81b791b3 Use the right skin format in playground demo (#33) 2016-09-12 12:08:56 -04:00
Tim Mickel
042f0f0353 Fix asset URL handling from #27 (#32)
* Fix URL handling of assets

* Recompile 2 Sept 12
2016-09-12 11:55:08 -04:00
TheBrokenRail
3277584da9 Offline Sprites (#27)
* Added Scratch Cat SVG

* Offline Scratch Cat

* Moved To SRC

* Update README.md

* Delete Travis

* Add Travis YML

* Recompile

* Add files via upload

* Recompile Part 1

* Recompile Part 1

* Recompile Part 1

* Recompile Part 1

* Recompile!

* Not Needed

* Recompile For Real

* Update README.md

* Added Stage Offline

* Less Hacky Offline

* Recompile

* Fixed Line Length
2016-09-12 11:03:57 -04:00
Andrew Sliwinski
92fc916b86 Synchronize with master branch 2016-08-08 14:58:35 -04:00
Christopher Willis-Ford
ad94df4afe Use ES6 class definitions 2016-06-15 15:21:58 -07:00
Christopher Willis-Ford
f0dae7aa48 Fix GL errors when using SVG Drawable early
Drawable now creates its own temporary texture upon construction. This
1x1 transparent texture is ready immediately and is replaced once a real
skin is loaded. This slightly simplifies some of the skin loading code
but more importantly prevents GL errors caused when trying to draw a
Drawable if it hasn't yet created its "real" texture, as in the SVG load
path.
2016-06-13 10:23:54 -07:00
Christopher Willis-Ford
04cebb9eed Improve color handling in isTouchingColor
- Always take color in unsigned-byte terms.
- Moved tolerance value into a class constant.
- Use the same tolerance in JS color comparison as in the shader.
2016-06-09 15:32:23 -07:00
Christopher Willis-Ford
1edb753ac8 Add silhouette color to Drawable's uniforms 2016-06-09 13:44:04 -07:00
Christopher Willis-Ford
91cc797c3b Refactor ShaderManager's DRAW_MODE and effect info
Draw modes are now enabled/disabled by bitmask and can be combined.
Combining silhouette mode and color mask mode will be useful for
color-touching-color, for example.

Effect converters are now one field in a larger EFFECT_INFO map, which
also contains bitmask values for each effect. Drawable no longer makes
any assumptions regarding how ShaderManager will unpack the effect bits.

The shader cache is now an array of arrays, where the outer index is the
mask of active draw modes and the inner index is the mask of active
effects (as it was before). Effects which cannot impact the shape of a
Drawable are masked out in silhouette mode in order to avoid compiling
redundant shaders.
2016-06-09 12:17:12 -07:00
Christopher Willis-Ford
1040189e27 Move shader management to new ShaderManager class 2016-06-08 13:38:49 -07:00
Christopher Willis-Ford
9f59b42901 Implement color-touching-color
The `isTouchingColor` function now takes an optional mask parameter. If
provided, only the parts of the Drawable which match the mask color will
be used for the test. For example:
```
  isTouchingColor(4, red, blue);
```
This means "are there any parts of Drawable #4 which are blue and are
touching a red pixel on some other Drawable?"
2016-06-06 16:21:24 -07:00
Christopher Willis-Ford
4d04ef5de7 Use Buffer instead of Uint8Array (lint fix)
Also suppress lint warning about unused alpha in color4ubToID since
that's intentional (for now).
2016-06-02 15:42:55 -07:00
Christopher Willis-Ford
25d8148ec4 Add more flexibility to draw-time object filtering
The `_drawThese` call now takes both a list of Drawable IDs for positive
filtering and a filter function for negative filtering.
2016-06-01 16:17:56 -07:00
Christopher Willis-Ford
137e4a642e Skin loading improvements
Fixed timing errors related to calling `setSkin()` a second time before
the first callback chain completes.
Fixed a typo causing problems when more than one Drawable is registered.
2016-06-01 15:28:08 -07:00
Christopher Willis-Ford
f6b78b2216 Picking improvements
- Increased canvas border in demo.html to make it more obvious if future
  code forgets to compensate for the border.
- Fixed picking code to compensate for a border around the canvas.
- Completed support for picking with a touch larger than a single point.
2016-05-31 14:00:39 -07:00
Christopher Willis-Ford
94cbe8999d Implement pick(x,y) -> Drawable ID 2016-05-27 16:10:22 -07:00
Christopher Willis-Ford
36439bf665 Multiply projection and model matrices in shader
This will allow rendering the primary view, picking view, and "touching
color" view with different projection matrices without needing to
recalculate the model matrix for every Drawable every time.
2016-05-27 11:55:15 -07:00
Christopher Willis-Ford
c24cfaaf4c Fix pixelate "pixel" size
Pixelate still needs a bit more work: at certain scales the edges of the
"pixels" are ragged. This happens in Scratch 2.0 as well, but for me it
seems slightly worse in this implementation.
2016-05-26 09:53:22 -07:00
Christopher Willis-Ford
03107781ab Use HSL instead of HSV for brightness effect 2016-05-25 14:21:40 -07:00
Christopher Willis-Ford
dcedd0c3cf Fix brightness and ghost effects
The renderer now uses premultiplied alpha in the frame buffer, though
textures are still stored without premultiplied alpha. This makes it
easier to get the desired results from the browser's canvas compositing
step, and might be nicer for mobile hardware depending on which parts of
the Internet you believe.
2016-05-20 15:36:02 -07:00
Christopher Willis-Ford
920271d076 Fix pixelate effect
This implementation of the pixel effect is now very similar to the
PixelBender implementation in Scratch 2.0

I also added a slider to demo.html as a debugging aid: it manipulates a
value that is passed into the shader and used in whatever way helps.
2016-05-20 14:17:44 -07:00
Christopher Willis-Ford
8b461e0651 Tweak whirl, color effects for #ifdef-based shader
Also fixed a typo in effect value conversion
2016-05-19 13:25:01 -07:00
Christopher Willis-Ford
d9a89fdd06 Support setting the stage background color 2016-05-19 12:00:19 -07:00
Christopher Willis-Ford
7f020787fd Generate shader for each effect combo on demand
The combination of effects is encoded bitwise as an index into the
shader cache array. The `getShader` function fetches the relevant index
if it's present, or calls `_buildShader()` to compile a new shader. The
effects are enabled by prepending lines like `#define ENABLE_whirl` at
the beginning of the vertex and fragment shader texts.
2016-05-19 11:36:55 -07:00
Christopher Willis-Ford
798981812d Provide an interface for bulk-updating a Drawable
The renderer can now update any combination of a Drawable's position,
direction, scale, and effect values through a single function call. The
effect values will be adjusted according to per-effect conversion
functions to prepare the values for use inside the shader.
2016-05-18 16:21:26 -07:00
Christopher Willis-Ford
db69b23a0e Support rendering SVGs 2016-05-18 12:35:08 -07:00
Christopher Willis-Ford
06f7d46613 Keep skin until new one loads 2016-05-17 15:24:14 -07:00
Christopher Willis-Ford
f30629ebba Document all methods 2016-05-17 15:12:37 -07:00
Christopher Willis-Ford
dbe3f1370b Draw a squirrel
This change introduces the Drawable class, which corresponds to a
Scratch sprite or clone. It supports setting its "skin" (corresponding
to a Scratch costume) by md5+extension, but currently only supports
bitmap skins. Drawables can be created, destroyed, and otherwise
manipulated by ID through the renderer.
2016-05-17 13:52:37 -07:00