My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package org.papervision3d.view
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Dictionary;

import org.papervision3d.core.culling.DefaultLineCuller;
import org.papervision3d.core.culling.DefaultParticleCuller;
import org.papervision3d.core.culling.DefaultTriangleCuller;
import org.papervision3d.core.culling.ILineCuller;
import org.papervision3d.core.culling.IParticleCuller;
import org.papervision3d.core.culling.ITriangleCuller;
import org.papervision3d.core.culling.RectangleLineCuller;
import org.papervision3d.core.culling.RectangleParticleCuller;
import org.papervision3d.core.culling.RectangleTriangleCuller;
import org.papervision3d.core.culling.ViewportObjectFilter;
import org.papervision3d.core.geom.renderables.Triangle3D;
import org.papervision3d.core.log.PaperLogger;
import org.papervision3d.core.render.IRenderEngine;
import org.papervision3d.core.render.command.IRenderListItem;
import org.papervision3d.core.render.command.RenderableListItem;
import org.papervision3d.core.render.data.RenderHitData;
import org.papervision3d.core.render.data.RenderSessionData;
import org.papervision3d.core.utils.InteractiveSceneManager;
import org.papervision3d.core.view.IViewport3D;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.view.layer.ViewportBaseLayer;
import org.papervision3d.view.layer.ViewportLayer;

/**
* @Author Ralph Hauwert
*/

/* Changed to protected methods on 11/27/2007 by John */
/* Added LineCulling on 22 May 08 by Seb Lee-Delisle */
public class Viewport3D extends Sprite implements IViewport3D
{
//use namespace org.papervision3d.core.ns.pv3dview;
/** @private */
protected var _width:Number;
/** @private */
protected var _hWidth:Number;
/** @private */
protected var _height:Number;
/** @private */
protected var _hHeight:Number;
/** @private */
protected var _autoClipping:Boolean;
/** @private */
protected var _autoCulling:Boolean;
/** @private */
protected var _autoScaleToStage:Boolean;
/** @private */
protected var _interactive:Boolean;
/** @private */
protected var _lastRenderer:IRenderEngine;
/** @private */
protected var _viewportObjectFilter:ViewportObjectFilter;
/** @private */
protected var _containerSprite:ViewportBaseLayer;
/** @private */
protected var _layerInstances:Dictionary;
/**
* sizeRectangle stores the width and the height of the Viewport3D sprite
* @see #viewportWidth
* @see #viewportHeight
*/
public var sizeRectangle:Rectangle;
/**
* cullingRectangle stores the width, height, x, y of the culling rectangle. It's used to determine the bounds in which the triangles are drawn.
* @see #autoCulling
*/
public var cullingRectangle:Rectangle;
/**
* triangleCuller uses the cullingRectangle to determine which triangles will not be rendered in BasicRenderEngine
* @see #autoCulling
*/
public var triangleCuller:ITriangleCuller;
/**
* particleCuller uses the cullingRectangle to determine which particles will not be rendered in BasicRenderEngine
* @see #autoCulling
*/
public var particleCuller:IParticleCuller;
/**
* lineCuller uses the culling Rectangle to determine which particles will not be rendered in BasicRenderEngine
* @see #autoCulling
*/
public var lineCuller:ILineCuller;
/**
* lastRenderList stores RenderableListItems (Triangles, Lines, Pixels, Particles, Fog) of everything that was rendered in the last pass. This list is used to determine hitTests in hitTestPoint2D.
* @see #hitTestPoint2D()
*/
public var lastRenderList:Array;
/**
* interactiveSceneManager manages the interaction between the user's mouse and the Papervision3D scene. This is done by checking the mouse against renderHitData. renderHitData is generated from hitTestPoint2D and passed into the interactiveSceneManager to check agains the various mouse actions.
* @see #hitTestPoint2D()
* @see org.papervision3d.core.utils.InteractiveSceneManager#renderHitData
*/
public var interactiveSceneManager:InteractiveSceneManager;
/** @private */
protected var renderHitData:RenderHitData;
private var stageScaleModeSet :Boolean = false;

/**
* @param viewportWidth Width of the viewport
* @param viewportHeight Height of the viewport
* @param autoScaleToStage Determines whether the viewport should resize when the stage resizes
* @param interactive Determines whether the viewport should listen for Mouse events by creating an <code>InteractiveSceneManager</code>
* @param autoClipping Determines whether DisplayObject3Ds outside the rectangle of the viewport should be rendered
* @param autoCulling Detemines whether only the objects in front of the camera should be rendered. In other words, if a triangle is hidden by another triangle from the camera, it will not be rendered.
*/
public function Viewport3D(viewportWidth:Number = 640, viewportHeight:Number = 480, autoScaleToStage:Boolean = false, interactive:Boolean = false, autoClipping:Boolean = true, autoCulling:Boolean = true)
{
super();
init();

this.interactive = interactive;

this.viewportWidth = viewportWidth;
this.viewportHeight = viewportHeight;

this.autoClipping = autoClipping;
this.autoCulling = autoCulling;

this.autoScaleToStage = autoScaleToStage;

this._layerInstances = new Dictionary(true);
}

/**
* Removes all references and sets the viewport's
* InteractiveSceneManager to null for a future
* garbage collection sweep
*/
public function destroy():void
{
if(interactiveSceneManager)
{
interactiveSceneManager.destroy();
interactiveSceneManager = null;
}
lastRenderList = null;
}

/**
* @private
*/
protected function init():void
{
this.renderHitData = new RenderHitData();

lastRenderList = new Array();
sizeRectangle = new Rectangle();
cullingRectangle = new Rectangle();

_containerSprite = new ViewportBaseLayer(this);
_containerSprite.doubleClickEnabled = true;

addChild(_containerSprite);

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}

/**
* Checks the Mouse x and y against the <code>RenderHitData</code>
* @return RenderHitData of the current mouse location
*/
public function hitTestMouse():RenderHitData
{
var p:Point = new Point(containerSprite.mouseX, containerSprite.mouseY);
return hitTestPoint2D(p);
}

/**
* Checks a <code>Point</code> against the <code>RenderHitData</code>
* of the viewport
* @param point a 2d <code>Point</code> you want to analyze into 3d space
* @return <code>RenderHitData</code> of the given <code>Point</code>
*/
public function hitTestPoint2D(point:Point):RenderHitData
{
renderHitData.clear();
if(interactive)
{
var rli:RenderableListItem;
var rhd:RenderHitData = renderHitData;
var rc:IRenderListItem;
for(var i:uint = lastRenderList.length;rc = lastRenderList[--i]; )
{
if(rc is RenderableListItem)
{
rli = rc as RenderableListItem;
rhd = rli.hitTestPoint2D(point, rhd);
if(rhd.hasHit)
{
return rhd;
}
}
}
}
return renderHitData;
}

public function hitTestPointObject(point:Point, object:DisplayObject3D):RenderHitData
{
if(interactive){
var rli:RenderableListItem;
var rhd:RenderHitData = new RenderHitData();
var rc:IRenderListItem;

for(var i:uint = lastRenderList.length; rc = lastRenderList[--i]; )
{
if(rc is RenderableListItem)
{
rli = rc as RenderableListItem;

if(rli.renderableInstance is Triangle3D){
if(Triangle3D(rli.renderableInstance).instance != object)
continue;
}else{
continue;
}

rhd = rli.hitTestPoint2D(point, rhd);

if(rhd.hasHit)
{
return rhd;
}
}
}
}

return new RenderHitData();
}

/**
* Creates or receives a <code>ViewportLayer</code> of the given <code>DisplayObject3D</code>
* @param do3d A <code>DisplayObject3D</code> used to either find the layer or create a new one
* @param createNew Forces the creation of a new layer
* @param recurse Adds the <code>DisplayObject3D</code> as well as all of its children to a new layer
* @return <code>ViewportLayer</code> of the given <code>DisplayObject3D</code>
*/
public function getChildLayer(do3d:DisplayObject3D, createNew:Boolean = true, recurse:Boolean = true):ViewportLayer
{
return containerSprite.getChildLayer(do3d, createNew, recurse);
}

/**
* Gets the layer of the RenderListItem. Most-likely internal use.
* @param rc A RenderableListItem to look for
* @param setInstance sets the container to the layer
* @return The found <code>ViewportLayer</code>
*/
public function accessLayerFor(rc:RenderableListItem, setInstance:Boolean = false):ViewportLayer
{
var do3d:DisplayObject3D;

if(rc.renderableInstance)
{
do3d = rc.renderableInstance.instance;

do3d = do3d.parentContainer ? do3d.parentContainer : do3d;

if(containerSprite.layers[do3d])
{
if(setInstance)
{
do3d.container = containerSprite.layers[do3d];
}
return containerSprite.layers[do3d];
}else if(do3d.useOwnContainer)
{
return containerSprite.getChildLayer(do3d, true, true);
}
}

return containerSprite;
}

/**
* Triggered when added to the stage to start listening to stage resizing
*/
protected function onAddedToStage(event:Event):void
{
if(_autoScaleToStage)
{
setStageScaleMode();
}
stage.addEventListener(Event.RESIZE, onStageResize);
onStageResize();
}

/**
* Triggered when removed from the stage to remove the stage resizing listener
*/
protected function onRemovedFromStage(event:Event):void
{
stage.removeEventListener(Event.RESIZE, onStageResize);
}

/**
* Resizes the viewport when the stage is resized (if autoScaleToStage == true)
*/
protected function onStageResize(event:Event = null):void
{
if(_autoScaleToStage)
{
viewportWidth = stage.stageWidth;
viewportHeight = stage.stageHeight;
}
}

protected function setStageScaleMode() : void
{
if(!stageScaleModeSet)
{
PaperLogger.info("Viewport autoScaleToStage : Papervision has changed the Stage scale mode.");

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stageScaleModeSet = true;
}

}

/**
* Sets the viewport width
* @param width A number designating the width of the viewport
*/
public function set viewportWidth(width:Number):void
{
_width = width;
_hWidth = width / 2;
containerSprite.x = _hWidth;

cullingRectangle.x = -_hWidth;
cullingRectangle.width = width;

sizeRectangle.width = width;
if(_autoClipping)
{
scrollRect = sizeRectangle;
}
}

/**
* Width of the <code>Viewport3D</code>
*/
public function get viewportWidth():Number
{
return _width;
}

/**
* Sets the the height of the <code>Viewport3D</code>
* @param height A number designating the height of the <code>Viewport3D</code>
*/
public function set viewportHeight(height:Number):void
{
_height = height;
_hHeight = height / 2;
containerSprite.y = _hHeight;

cullingRectangle.y = -_hHeight;
cullingRectangle.height = height;

sizeRectangle.height = height;
if(_autoClipping)
{
scrollRect = sizeRectangle;
}
}

/**
* Height of the Viewport
*/
public function get viewportHeight():Number
{
return _height;
}

/**
* The <code>Sprite</code> holding the <code>Viewport3D</code>
*/
public function get containerSprite():ViewportLayer
{
return _containerSprite;
}

/**
* Whether clipping is enabled (not rendering bitmap data outside the rectangle of the viewport by making use of the <code>Sprite.scrollRect</code>)
* @see flash.display.Sprite#scrollRect
* @see http://www.gskinner.com/blog/archives/2006/11/understanding_d.html
*/
public function get autoClipping():Boolean
{
return _autoClipping;
}

public function set autoClipping(clip:Boolean):void
{
if(clip)
{
scrollRect = sizeRectangle;
}else
{
scrollRect = null;
}
_autoClipping = clip;
}

/**
* Whether culling is enabled (not rendering triangles hidden behind other triangles)
* @see #lineCuller
* @see #particleCuller
* @see #triangleCuller
*/
public function get autoCulling():Boolean
{
return _autoCulling;
}

public function set autoCulling(culling:Boolean):void
{
if(culling)
{
triangleCuller = new RectangleTriangleCuller(cullingRectangle);
particleCuller = new RectangleParticleCuller(cullingRectangle);
lineCuller = new RectangleLineCuller(cullingRectangle);
}else if(!culling)
{
triangleCuller = new DefaultTriangleCuller();
particleCuller = new DefaultParticleCuller();
lineCuller = new DefaultLineCuller();
}
_autoCulling = culling;
}

/**
* Whether the <code>Viewport3D</code> should scale with the <code>Stage</code>
*/
public function set autoScaleToStage(scale:Boolean):void
{
_autoScaleToStage = scale;
if(scale && stage != null)
{
setStageScaleMode();
onStageResize();
}

}

/**
* The auto scale to stage boolean flag
*/
public function get autoScaleToStage():Boolean
{
return _autoScaleToStage;
}

/**
* Whether the <code>Viewport3D</code> should listen for <code>Mouse</code> events and create an <code>InteractiveSceneManager</code>
*/
public function set interactive(b:Boolean):void
{
if(b != _interactive)
{
if(_interactive && interactiveSceneManager)
{
interactiveSceneManager.destroy();
interactiveSceneManager = null;
}
_interactive = b;
if(b)
{
interactiveSceneManager = new InteractiveSceneManager(this);
}
}
}

/**
* The interactive boolean flag
*/
public function get interactive():Boolean
{
return _interactive;
}

/**
* Updates a <code>ViewportLayer</code> prior to the 3d data being rendered into the 2d scene
* @param renderSessionData All the information regarding the current renderSession packed into one class
*/
public function updateBeforeRender(renderSessionData:RenderSessionData):void
{
lastRenderList.length = 0;

if(renderSessionData.renderLayers)
{
for each(var vpl:ViewportLayer in renderSessionData.renderLayers)
{
vpl.updateBeforeRender();
}
}else
{
_containerSprite.updateBeforeRender();
}

_layerInstances = new Dictionary(true);
}

/**
* Updates a <code>ViewportLayer</code> after the 3d data is rendered into the 2d scene
* @param renderSessionData All the information regarding the current renderSession packed into one class
*/
public function updateAfterRender(renderSessionData:RenderSessionData):void
{
if(interactive)
{
interactiveSceneManager.updateAfterRender();
}

if(renderSessionData.renderLayers)
{
for each(var vpl:ViewportLayer in renderSessionData.renderLayers)
{
vpl.updateInfo();
vpl.sortChildLayers();
vpl.updateAfterRender();
}
}else
{
containerSprite.updateInfo();
containerSprite.updateAfterRender();
}

containerSprite.sortChildLayers();
}

/**
* Sets the <code>ViewportObjectFilter</code> of the <code>Viewport3D</code>
* @param vof The <code>ViewportObjectFilter</code> you want applied
*/
public function set viewportObjectFilter(vof:ViewportObjectFilter):void
{
_viewportObjectFilter = vof;
}

/**
* The <code>ViewportObjectFilter</code>
*/
public function get viewportObjectFilter():ViewportObjectFilter
{
return _viewportObjectFilter;
}
}
}

Change log

r759 by neoriley on Oct 18, 2008   Diff
updating do3d
Go to: 
Project members, sign in to write a code review

Older revisions

r723 by johnlindquist on Sep 10, 2008   Diff
[documentation]
r722 by johnlindquist on Sep 10, 2008   Diff
[documentation] view/Viewport3D.as
documentation updates
r717 by r.hauwert on Sep 9, 2008   Diff
Made PV3D PowerFlasher FDT safe.
All revisions of this file

File info

Size: 16229 bytes, 562 lines
Powered by Google Project Hosting