@@ -145,6 +145,66 @@ Sometimes you want to squeeze in a new scene between two other scenes that you a
145
145
Often you will perhaps only shorten the preceding scene and leave the start of the following scene intact.
146
146
But for completeness, here is an example where we both shorten the previous scene, and chop down the beginning of the following scene.
147
147
148
+ #### Adding backgrounds to scenes
149
+
150
+ Adding backgrounds ain't all that hard, once you have an overweiv of whats needed!
151
+
152
+ First of all, make sure there is an actual existing file containing the code for your background.
153
+ If you want to render your background on a canvas the content should be something like:
154
+
155
+ ``` js
156
+ (function (global ) {
157
+ const F = (frame , from , delta ) => (
158
+ frame - FRAME_FOR_BEAN (from)) / (FRAME_FOR_BEAN (from + delta) - FRAME_FOR_BEAN (from)
159
+ );
160
+
161
+ constructor (id ) {
162
+ super (id, {
163
+ outputs: {
164
+ render: new NIN.TextureOutput ()
165
+ }
166
+ });
167
+
168
+ this .canvas = document .createElement (' canvas' );
169
+ this .ctx = this .canvas .getContext (' 2d' );
170
+
171
+ this .width = 1024 ;
172
+ this .height = 1024 ;
173
+
174
+ this .canvas .width = this .width ;
175
+ this .canvas .height = this .height ;
176
+
177
+ this .output = new THREE.VideoTexture (this .canvas );
178
+ this .output .minFilter = THREE .LinearFilter ;
179
+ this .output .magFilter = THREE .LinearFilter ;
180
+
181
+ // ... other constructor comment
182
+ }
183
+
184
+ update (frame ) {
185
+ super .update (frame);
186
+ // Other stuff you want to do each frame
187
+ }
188
+
189
+ // The warmup block exsist to prevent stutter related to compiling shaders when the scene starts.
190
+ // Usually shaders are compield for your GPU just in time at the moment they are first called.
191
+ // This can take some time, and result in perceived jitter at the start of your scene.
192
+ // To prevent this, when NIN starts running a demo, before the playback looks like it begins, it will run through each scene and call its `warmup`.
193
+ // In practice, this is like rendering one frame from each scene, so that all shaders are loaded before the smooth playback you want to show off.
194
+ warmup (renderer ) {
195
+ this .update (6710 ); // the first frame in this scene cointaining the shaders you want pre-loaded.
196
+ this .render (renderer);
197
+ }
198
+
199
+ render () {
200
+ this .output .needsUpdate = true ;
201
+ this .outputs .render .setValue (this .output );
202
+ }
203
+
204
+ global .greetsBackgroundCanvas = greetsBackgroundCanvas;
205
+ })(this );
206
+ ```
207
+
148
208
### Randomness
149
209
150
210
To ensure that random things happen consistently across runs it is recommended to use the ` Random ` -class rather than ` Math.random() ` .
0 commit comments