~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to src/cobweb/Components/Layering/X3DLayerNode.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
 
2
 *******************************************************************************
 
3
 *
 
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
5
 *
 
6
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
 
7
 *
 
8
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
 
9
 *
 
10
 * The copyright notice above does not evidence any actual of intended
 
11
 * publication of such source code, and is an unpublished work by create3000.
 
12
 * This material contains CONFIDENTIAL INFORMATION that is the property of
 
13
 * create3000.
 
14
 *
 
15
 * No permission is granted to copy, distribute, or create derivative works from
 
16
 * the contents of this software, in whole or in part, without the prior written
 
17
 * permission of create3000.
 
18
 *
 
19
 * NON-MILITARY USE ONLY
 
20
 *
 
21
 * All create3000 software are effectively free software with a non-military use
 
22
 * restriction. It is free. Well commented source is provided. You may reuse the
 
23
 * source in any way you please with the exception anything that uses it must be
 
24
 * marked to indicate is contains 'non-military use only' components.
 
25
 *
 
26
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
27
 *
 
28
 * Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
 
29
 *
 
30
 * This file is part of the Cobweb Project.
 
31
 *
 
32
 * Cobweb is free software: you can redistribute it and/or modify it under the
 
33
 * terms of the GNU General Public License version 3 only, as published by the
 
34
 * Free Software Foundation.
 
35
 *
 
36
 * Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
 
37
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 
38
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
 
39
 * details (a copy is included in the LICENSE file that accompanied this code).
 
40
 *
 
41
 * You should have received a copy of the GNU General Public License version 3
 
42
 * along with Cobweb.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
 
43
 * copy of the GPLv3 License.
 
44
 *
 
45
 * For Silvio, Joy and Adi.
 
46
 *
 
47
 ******************************************************************************/
 
48
 
 
49
 
 
50
define ([
 
51
        "jquery",
 
52
        "cobweb/Components/Core/X3DNode",
 
53
        "cobweb/Rendering/X3DRenderObject",
 
54
        "cobweb/Components/Layering/X3DViewportNode",
 
55
        "cobweb/Execution/BindableStack",
 
56
        "cobweb/Execution/BindableList",
 
57
        "cobweb/Components/Navigation/NavigationInfo",
 
58
        "cobweb/Components/EnvironmentalEffects/Fog",
 
59
        "cobweb/Components/EnvironmentalEffects/Background",
 
60
        "cobweb/Bits/X3DCast",
 
61
        "cobweb/Bits/TraverseType",
 
62
        "cobweb/Bits/X3DConstants",
 
63
        "standard/Math/Geometry/Camera",
 
64
        "standard/Math/Numbers/Vector3",
 
65
        "standard/Math/Numbers/Matrix4",
 
66
],
 
67
function ($,
 
68
          X3DNode,
 
69
          X3DRenderObject,
 
70
          X3DViewportNode,
 
71
          BindableStack,
 
72
          BindableList,
 
73
          NavigationInfo,
 
74
          Fog,
 
75
          Background,
 
76
          X3DCast,
 
77
          TraverseType,
 
78
          X3DConstants,
 
79
          Camera,
 
80
          Vector3,
 
81
          Matrix4)
 
82
{
 
83
"use strict";
 
84
 
 
85
        var projectionMatrix = new Matrix4 ();
 
86
 
 
87
        function X3DLayerNode (executionContext, defaultViewpoint, groupNode)
 
88
        {
 
89
                X3DNode         .call (this, executionContext);
 
90
                X3DRenderObject .call (this, executionContext);
 
91
 
 
92
                this .addType (X3DConstants .X3DLayerNode);
 
93
 
 
94
                this .groupNode       = groupNode;
 
95
                this .currentViewport = null;
 
96
 
 
97
                this .defaultBackground     = new Background (executionContext);
 
98
                this .defaultFog            = new Fog (executionContext);
 
99
                this .defaultNavigationInfo = new NavigationInfo (executionContext);
 
100
                this .defaultViewpoint      = defaultViewpoint;
 
101
 
 
102
                this .backgroundStack     = new BindableStack (executionContext, this, this .defaultBackground);
 
103
                this .fogStack            = new BindableStack (executionContext, this, this .defaultFog);
 
104
                this .navigationInfoStack = new BindableStack (executionContext, this, this .defaultNavigationInfo);
 
105
                this .viewpointStack      = new BindableStack (executionContext, this, this .defaultViewpoint);
 
106
 
 
107
                this .backgrounds     = new BindableList (executionContext, this, this .defaultBackground)
 
108
                this .fogs            = new BindableList (executionContext, this, this .defaultFog);
 
109
                this .navigationInfos = new BindableList (executionContext, this, this .defaultNavigationInfo);
 
110
                this .viewpoints      = new BindableList (executionContext, this, this .defaultViewpoint);
 
111
 
 
112
                this .defaultBackground .setHidden (true);
 
113
                this .defaultFog        .setHidden (true);
 
114
 
 
115
                this .collisionTime = 0;
 
116
        }
 
117
 
 
118
        X3DLayerNode .prototype = $.extend (Object .create (X3DNode .prototype),
 
119
                X3DRenderObject .prototype,
 
120
        {
 
121
                constructor: X3DLayerNode,
 
122
                layer0: false,
 
123
                initialize: function ()
 
124
                {
 
125
                        X3DNode         .prototype .initialize .call (this);
 
126
                        X3DRenderObject .prototype .initialize .call (this);
 
127
 
 
128
                        this .groupNode .children_ = this .children_;
 
129
                        this .groupNode .setPrivate (true);
 
130
                        this .groupNode .setup ();
 
131
 
 
132
                        this .defaultNavigationInfo .setup ();
 
133
                        this .defaultBackground     .setup ();
 
134
                        this .defaultFog            .setup ();
 
135
                        this .defaultViewpoint      .setup ();
 
136
 
 
137
                        this .backgroundStack     .setup ();
 
138
                        this .fogStack            .setup ();
 
139
                        this .navigationInfoStack .setup ();
 
140
                        this .viewpointStack      .setup ();
 
141
        
 
142
                        this .backgrounds     .setup ();
 
143
                        this .fogs            .setup ();
 
144
                        this .navigationInfos .setup ();
 
145
                        this .viewpoints      .setup ();
 
146
 
 
147
                        this .viewport_       .addInterest ("set_viewport__", this);
 
148
                        this .addChildren_    .addFieldInterest (this .groupNode .addChildren_);
 
149
                        this .removeChildren_ .addFieldInterest (this .groupNode .removeChildren_);
 
150
                        this .children_       .addFieldInterest (this .groupNode .children_);
 
151
 
 
152
                        this .set_viewport__ ();
 
153
                },
 
154
                isLayer0: function (value)
 
155
                {
 
156
                        this .layer0 = value;
 
157
                        this .defaultBackground .setHidden (! value);
 
158
                },
 
159
                getLayer: function ()
 
160
                {
 
161
                        return this;
 
162
                },
 
163
                getGroup: function ()
 
164
                {
 
165
                        return this .groupNode;
 
166
                },
 
167
                getViewport: function ()
 
168
                {
 
169
                        return this .currentViewport;
 
170
                },
 
171
                getBackground: function ()
 
172
                {
 
173
                        return this .backgroundStack .top ();
 
174
                },
 
175
                getFog: function ()
 
176
                {
 
177
                        return this .fogStack .top ();
 
178
                },
 
179
                getNavigationInfo: function ()
 
180
                {
 
181
                        return this .navigationInfoStack .top ();
 
182
                },
 
183
                getViewpoint: function ()
 
184
                {
 
185
                        return this .viewpointStack .top ();
 
186
                },
 
187
                getBackgrounds: function ()
 
188
                {
 
189
                        return this .backgrounds;
 
190
                },
 
191
                getFogs: function ()
 
192
                {
 
193
                        return this .fogs;
 
194
                },
 
195
                getNavigationInfos: function ()
 
196
                {
 
197
                        return this .navigationInfos;
 
198
                },
 
199
                getViewpoints: function ()
 
200
                {
 
201
                        return this .viewpoints;
 
202
                },
 
203
                getUserViewpoints: function ()
 
204
                {
 
205
                        var userViewpoints = [ ];
 
206
 
 
207
                        for (var i = 0; i < this .viewpoints .get () .length; ++ i)
 
208
                        {
 
209
                                var viewpoint = this .viewpoints .get () [i];
 
210
 
 
211
                                if (viewpoint .description_ .length)
 
212
                                        userViewpoints .push (viewpoint);
 
213
                        }
 
214
 
 
215
                        return userViewpoints;
 
216
                },
 
217
                getBackgroundStack: function ()
 
218
                {
 
219
                        return this .backgroundStack;
 
220
                },
 
221
                getFogStack: function ()
 
222
                {
 
223
                        return this .fogStack;
 
224
                },
 
225
                getNavigationInfoStack: function ()
 
226
                {
 
227
                        return this .navigationInfoStack;
 
228
                },
 
229
                getViewpointStack: function ()
 
230
                {
 
231
                        return this .viewpointStack;
 
232
                },
 
233
                getBBox: function (bbox)
 
234
                {
 
235
                        return this .groupNode .getBBox (bbox);
 
236
                },
 
237
                set_viewport__: function ()
 
238
                {
 
239
                        this .currentViewport = X3DCast (X3DConstants .X3DViewportNode, this .viewport_);
 
240
 
 
241
                        if (! this .currentViewport)
 
242
                                this .currentViewport = this .getBrowser () .getDefaultViewport ();
 
243
                },
 
244
                bind: function (viewpointName)
 
245
                {
 
246
                        this .traverse (TraverseType .CAMERA, this);
 
247
 
 
248
                        // Bind first viewpoint in viewpoint list.
 
249
 
 
250
                        var
 
251
                                navigationInfo = this .navigationInfos .getBound (),
 
252
                                background     = this .backgrounds     .getBound (),
 
253
                                fog            = this .fogs            .getBound (),
 
254
                                viewpoint      = this .viewpoints      .getBound (viewpointName);
 
255
 
 
256
                        this .navigationInfoStack .forcePush (navigationInfo);
 
257
                        this .backgroundStack     .forcePush (background);
 
258
                        this .fogStack            .forcePush (fog);
 
259
                        this .viewpointStack      .forcePush (viewpoint);
 
260
 
 
261
                        navigationInfo .addLayer (this);
 
262
                        background     .addLayer (this);
 
263
                        fog            .addLayer (this);
 
264
                        viewpoint      .addLayer (this);
 
265
 
 
266
                        viewpoint .resetUserOffsets ();
 
267
                },
 
268
                traverse: function (type, renderObject)
 
269
                {
 
270
                        renderObject = renderObject || this;
 
271
 
 
272
                        var viewpoint = this .getViewpoint ();
 
273
 
 
274
                        this .getCameraSpaceMatrix        () .pushMatrix (viewpoint .getCameraSpaceMatrix ());
 
275
                        this .getInverseCameraSpaceMatrix () .pushMatrix (viewpoint .getInverseCameraSpaceMatrix ());
 
276
                        this .getProjectionMatrix         () .pushMatrix (viewpoint .getProjectionMatrix (this));
 
277
 
 
278
                        switch (type)
 
279
                        {
 
280
                                case TraverseType .POINTER:
 
281
                                        this .pointer (type, renderObject);
 
282
                                        break;
 
283
                                case TraverseType .CAMERA:
 
284
                                        this .camera (type, renderObject);
 
285
                                        break;
 
286
                                case TraverseType .COLLISION:
 
287
                                        this .collision (type, renderObject);
 
288
                                        break;
 
289
                                case TraverseType .DEPTH:
 
290
                                case TraverseType .DISPLAY:
 
291
                                        this .display (type, renderObject);
 
292
                                        break;
 
293
                        }
 
294
 
 
295
                        this .getProjectionMatrix         () .pop ();
 
296
                        this .getInverseCameraSpaceMatrix () .pop ();
 
297
                        this .getCameraSpaceMatrix        () .pop ();
 
298
                },
 
299
                pointer: function (type, renderObject)
 
300
                {
 
301
                        if (this .isPickable_ .getValue ())
 
302
                        {
 
303
                                var
 
304
                                        browser  = this .getBrowser (),
 
305
                                        viewport = this .currentViewport .getRectangle (browser);
 
306
 
 
307
                                if (browser .getSelectedLayer ())
 
308
                                {
 
309
                                        if (browser .getSelectedLayer () !== this)
 
310
                                                return;
 
311
                                }
 
312
                                else
 
313
                                {
 
314
                                        if (! browser .isPointerInRectangle (viewport))
 
315
                                                return;
 
316
                                }
 
317
 
 
318
                                browser .setHitRay (this .getProjectionMatrix () .get (), viewport);
 
319
                                this .getModelViewMatrix () .pushMatrix (this .getInverseCameraSpaceMatrix () .get ());
 
320
 
 
321
                                this .currentViewport .push (this);
 
322
                                this .groupNode .traverse (type, renderObject);
 
323
                                this .currentViewport .pop (this);
 
324
 
 
325
                                this .getModelViewMatrix () .pop ()
 
326
                        }
 
327
                },
 
328
                camera: function (type, renderObject)
 
329
                {
 
330
                        this .getModelViewMatrix () .pushMatrix (Matrix4 .Identity);
 
331
        
 
332
                        this .currentViewport .push (this);
 
333
                        this .groupNode .traverse (type, renderObject);
 
334
                        this .currentViewport .pop (this);
 
335
 
 
336
                        this .navigationInfos .update ();
 
337
                        this .backgrounds     .update ();
 
338
                        this .fogs            .update ();
 
339
                        this .viewpoints      .update ();
 
340
 
 
341
                        this .getViewpoint () .update ();
 
342
 
 
343
                        this .getModelViewMatrix () .pop ()
 
344
                },
 
345
                collision: function (type, renderObject)
 
346
                {
 
347
                        this .collisionTime = 0;
 
348
 
 
349
                        var
 
350
                                navigationInfo  = this .getNavigationInfo (),
 
351
                                collisionRadius = navigationInfo .getCollisionRadius (),
 
352
                                avatarHeight    = navigationInfo .getAvatarHeight (),
 
353
                                size            = Math .max (collisionRadius * 2, avatarHeight * 2);
 
354
 
 
355
                        Camera .ortho (-size, size, -size, size, -size, size, projectionMatrix);
 
356
 
 
357
                        this .getProjectionMatrix () .pushMatrix (projectionMatrix);
 
358
                        this .getModelViewMatrix  () .pushMatrix (this .getInverseCameraSpaceMatrix () .get ());
 
359
        
 
360
                        // Render
 
361
                        this .currentViewport .push (this);
 
362
                        renderObject .render (type, this .groupNode);
 
363
                        this .currentViewport .pop (this);
 
364
 
 
365
                        this .getModelViewMatrix  () .pop ()
 
366
                        this .getProjectionMatrix () .pop ()
 
367
                },
 
368
                display: function (type, renderObject)
 
369
                {
 
370
                        this .getNavigationInfo () .enable (type, renderObject);
 
371
 
 
372
                        this .getModelViewMatrix () .pushMatrix (this .getInverseCameraSpaceMatrix () .get ());
 
373
 
 
374
                        this .currentViewport .push (this);
 
375
                        renderObject .render (type, this .groupNode);
 
376
                        this .currentViewport .pop (this);
 
377
 
 
378
                        this .getModelViewMatrix () .pop ()
 
379
                },
 
380
        });
 
381
 
 
382
        return X3DLayerNode;
 
383
});
 
384
 
 
385