Skip to content

Commit 7c77635

Browse files
authored
add migration guide for upgrading from Lightning 3 v2.x to v3.0 (#623)
2 parents e8d5d77 + 10da089 commit 7c77635

File tree

5 files changed

+1206
-0
lines changed

5 files changed

+1206
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@ await stage.loadFont('sdf', {
131131
```
132132

133133
For more information see [Font Loading](./docs/fontLoading.md)
134+
135+
## Migration Guide
136+
137+
Upgrading from Lightning 3 v2.x? See the [Migration Guide](./docs/migration-2x-to-3.0.md) for detailed information about breaking changes and how to update your code.

docs/canvas-shader-types.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Canvas ShaderTypes
2+
3+
The [Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) does not really have a concept called shaders, however for it to fit better into our architecture we are calling it shaders anyway. You can use the following properties to define your `CanvasShaderType`.
4+
5+
## render
6+
7+
The render property is the only `required` property for the CanvasShaderType. This property requires a function that draws the shader/effect.
8+
9+
```js
10+
export const Border = {
11+
render(ctx, quad, renderContext) {
12+
//renders node context(if not called the context won't be drawn)
13+
renderContext();
14+
15+
ctx.strokeStyle = 'green';
16+
ctx.lineWidth = 10;
17+
ctx.beginPath();
18+
//draw an innerBorder
19+
ctx.strokeRect(quad.tx + 5, quad.ty + 5, quad.width - 10, quad.height - 10);
20+
},
21+
};
22+
```
23+
24+
## update
25+
26+
The update property is an `optional` property. We can use to computed and store some values we don't want to calculate / mutate everytime a quad is drawn with canvas.
27+
28+
```js
29+
export const Border = {
30+
props: {
31+
color: 0x00ff00ff,
32+
width: 20,
33+
},
34+
update() {
35+
this.computed.borderColor = formatRGBAtoString(this.props.color);
36+
},
37+
render(ctx, quad, renderContext) {
38+
//renders node context(if not called the context won't be drawn)
39+
renderContext();
40+
const borderWidth = this.props.width;
41+
ctx.strokeStyle = this.computed.borderColor;
42+
ctx.lineWidth = borderWidth;
43+
const halfW = borderWidth * 0.5;
44+
ctx.beginPath();
45+
//draw an innerBorder
46+
ctx.strokeRect(
47+
quad.tx + halfW,
48+
quad.ty + halfW,
49+
quad.width - borderWidth,
50+
quad.height - borderWidth,
51+
);
52+
},
53+
};
54+
```
55+
56+
## saveAndRestore
57+
58+
Generally when you are about to transform shape, rotation or clip with canvas methods you use `ctx.save` > apply methods > `ctx.restore`. Saving and restoring is very costly if used a lot. To reduce save and restore calls there is a property called `saveAndRestore` you can use to let the renderer know you need it to save and restore before and after this shader is executed.
59+
60+
```js
61+
export const Rounded = {
62+
render(ctx, quad, renderContext) {
63+
const path = new Path2D();
64+
roundRect(
65+
path,
66+
quad.tx,
67+
quad.ty,
68+
quad.width,
69+
quad.height,
70+
this.computed.radius!,
71+
);
72+
ctx.clip(path);
73+
//renders node context(if not called the context won't be drawn)
74+
renderContext()
75+
}
76+
}
77+
```
78+
79+
See following source to learn more about the Rounded canvas shader.
80+
81+
You might still want to use `ctx.save` and `ctx.restore` in your render function. But this is only done when you want to more effects on top of a node. Just be careful not to overuse it.

docs/custom-shader-types.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Custom Shaders
2+
3+
To create your own shaders for the Lightning Renderer 3.x you'll have to define a `ShaderType`, this page contains basic knowledge for how Shader Types work.
4+
5+
## ShaderType basics
6+
7+
A ShaderType is a configuration object the renderer needs to create a shader node and optionally extra programs to make it actually render something on the screen.
8+
9+
Any basic ShaderType can consist of the following optional properties
10+
11+
### props
12+
13+
Similar to Blits props, you can use specific values to alter the way an effect is rendered by the renderer. The main difference between the Blits props and ShaderType props is that theses props need a default value, therefor you can only use objects.
14+
15+
```js
16+
const ShaderType = {
17+
props: {
18+
foo: 1,
19+
fooToo: {
20+
default: 1,
21+
},
22+
},
23+
};
24+
```
25+
26+
#### pointer props
27+
28+
You can also use "pointer" props to adjust other props. This is handy in cases you use arrays as value f.e;
29+
30+
```js
31+
const ShaderType = {
32+
props: {
33+
foo: [0, 0, 0, 0],
34+
fooOne: {
35+
set(v, props) {
36+
props.foo[1] = v;
37+
},
38+
get(props) {
39+
return props.foo[1];
40+
},
41+
},
42+
},
43+
};
44+
```
45+
46+
### getCacheMarkers
47+
48+
In some cases you might want to generate code based on the props you pass when using a specific ShaderType.
49+
50+
## ShaderTypes for different render modes
51+
52+
The Renderer that Blits uses makes use of different ShaderTypes for each render mode, [webgl](./webgl-shader-types.md) or [canvas](./canvas-shader-types.md). Blits(and the Renderer) by default use `webgl` ShaderTypes.
53+
54+
## Including ShaderTypes to your project
55+
56+
You can register ShaderTypes in the Launch settings of your App (in `src/index.js`). The `shaders`-key in the settings is an `Array` that specifies your custom shaders in your App.
57+
58+
```js
59+
const renderer = new RendererMain();
60+
61+
renderer.stage.shManager.registerShaderType('customShader', CustomShaderType);
62+
```

0 commit comments

Comments
 (0)