PV3Dのカメラテストとピクセル等倍

by tkinjo
♥1 | Line 168 | Modified 2009-08-14 15:35:12 | MIT License
play

ActionScript3 source code

/**
 * Copyright tkinjo ( http://wonderfl.net/user/tkinjo )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cFuv
 */

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="465" height="465" paddingLeft="0" paddingTop="0" paddingRight="0" paddingBottom="0" applicationComplete="applicationCompleteHandler( event )">
    
    <mx:Script>
        <![CDATA[
			
			/**
			 * PV3Dのカメラテストとピクセル等倍
			 * 
			 * - 参考 -
			 * note.x - [Papervision3D2.0] ピクセル等倍表示にトライ
			 * http://blog.r3c7.net/?p=166
			 */
			
			import flash.display.Bitmap;
			import flash.display.BitmapData;
			import flash.events.Event;
			import mx.events.FlexEvent;
			import org.papervision3d.core.proto.CameraObject3D;
			import org.papervision3d.materials.BitmapMaterial;
			import org.papervision3d.materials.ColorMaterial;
			import org.papervision3d.materials.utils.MaterialsList;
			import org.papervision3d.objects.primitives.Cube;
			import org.papervision3d.objects.primitives.Plane;
			import org.papervision3d.scenes.Scene3D;
			import org.papervision3d.view.AbstractView;
			import org.papervision3d.view.BasicView;
			
			private var camera:CameraObject3D;
			private var initZ:Number;
			private var initFocus:Number;
			private var initZoom:Number;
			private var initFov:Number;
            
			
			
			
			
            /** --------------------------------------------------
             * 初期設定
             */
			
            /**
             * 初期設定をします。
             * 
             * @param    event
             */
            private function applicationCompleteHandler( event:Event ):void {
                
				var noiseBitmapData:BitmapData = createNoiseBitmapData( 100, 100 );
				
				setupPV3D( noiseBitmapData );
				
				setCameraInitProperty();
				
				resetProperty();
				
				setupNoiseBitmap( noiseBitmapData );
            }
			
			/**
			 * ノイズビットマップデータを生成します。
			 */
			private function createNoiseBitmapData( width:int, height:int ):BitmapData {
				
				var noiseBitmapData:BitmapData = new BitmapData( width, height );
				noiseBitmapData.noise( 0 );
				
				return noiseBitmapData;
			}
			
			/**
			 * ノイズビットマップを配置します。
			 */
			private function setupNoiseBitmap( noiseBitmapData:BitmapData ):void {
				
				var noiseBitmap:Bitmap = new Bitmap( noiseBitmapData );
				noiseBitmap.x = stage.stageWidth - noiseBitmap.width;
				noiseBitmap.y = stage.stageHeight - noiseBitmap.height;
				imageContentHolder.addChild( noiseBitmap );
			}
			
			/**
			 * PV3D の設定
			 */
			private function setupPV3D( noiseBitmapData:BitmapData ):void {
				
				var view:AbstractView = new BasicView( stage.stageWidth, stage.stageHeight / 2 );
				imageContentHolder.addChild( view );
				
				var scene:Scene3D = view.scene;
				
				camera = view.camera;
				
				var cube:Cube = createCube( noiseBitmapData );
				cube.z = -50;
				scene.addChild( cube );
				
				view.startRendering();
			}
			
			/**
			 * キューブの作成
			 * 
			 * @param	noiseBitmapData
			 */
			private function createCube( noiseBitmapData:BitmapData ):Cube {
				
				var cubeMaterialParams:Array = [ 
						{ name:"right", color:0x00ff00 }, 
						{ name:"left", color:0x00ffff }, 
						{ name:"top", color:0x0000ff },
						{ name:"bottom", color:0xff00ff } 
					];
				
				var cubeMaterialsList:MaterialsList = new MaterialsList();
				
				var frontMaterial:BitmapMaterial = new BitmapMaterial( noiseBitmapData );
				frontMaterial.doubleSided = true;
				cubeMaterialsList.addMaterial( frontMaterial, "front" );
				
				for ( var i:uint = 0; i < cubeMaterialParams.length; i++ ) {
					
					var colorMaterial:ColorMaterial = new ColorMaterial( cubeMaterialParams[ i ].color );
					colorMaterial.doubleSided = true;
					cubeMaterialsList.addMaterial( colorMaterial, cubeMaterialParams[ i ].name );
				}
				
				var cube:Cube = new Cube( cubeMaterialsList, 100, 100, 100 );
				
				return cube;
			}
			
			/**
			 * カメラの初期プロパティを保存します。
			 */
			private function setCameraInitProperty():void {
				
				initZ = camera.z;
				initFocus = camera.focus;
				initZoom = camera.zoom;
				initFov = camera.fov;
			}
			
			
			
			
            /** --------------------------------------------------
             * cameraProperty
             */
			
            /**
             * リセットボタンが押されたときに各値を初期値に戻します。
             * 
             * @param    event
             */
            private function propertyResetButtonDownHandler( event:FlexEvent ):void {
                
                resetProperty();
            }
			
			/**
			 * プロパティボックス内のコンポーネントの値を初期化します。
			 */
			private function resetProperty():void {
				
				camera.z = initZ;
				camera.focus = initFocus;
				camera.zoom = initZoom;
				camera.fov = initFov;
				
				setPropertyBox();
			}
			
			private function setPropertyBox():void {
				
				zNumericStepper.value = camera.z;
				focusNumericStepper.value = camera.focus;
				zoomNumericStepper.value = camera.zoom;
				fovNumericStepper.value = camera.fov;
			}
			
            /**
             * 
             * @param    event
             */
            private function propertyChangeHandler( event:Event ):void {
				
				camera.z = zNumericStepper.value;
				
				switch( event.currentTarget ) {
					
					case focusNumericStepper:
						camera.focus = focusNumericStepper.value;
						break;
						
					case zoomNumericStepper:
						camera.zoom = zoomNumericStepper.value;
						break;
						
					case fovNumericStepper:
						camera.fov = fovNumericStepper.value;
						break;
				}
				
                setPropertyBox();
            }
			
			
			
            /** --------------------------------------------------
			 * ピクセル等倍
			 */
			
			/**
			 * @param	event
			 */
			private function zDisplayMagnificationButtonDownHandler( event:FlexEvent ):void {
				
				camera.z = -( camera.focus * camera.zoom );
				setPropertyBox();
			}
			
			/**
			 * @param	event
			 */
			private function focusDisplayMagnificationButtonDownHandler( event:FlexEvent ):void {
				
				camera.focus = -camera.z / camera.zoom;
				setPropertyBox();
			}
			
			/**
			 * @param	event
			 */
			private function zoomDisplayMagnificationButtonDownHandler( event:FlexEvent ):void {
				
				camera.zoom = -camera.z / camera.focus;
				setPropertyBox();
			}
			
			
			
			
			
            /** --------------------------------------------------
             * propertyBox
             */
            
            /**
             * プロパティボックスが表示されているかのフラグです。
             */
            private var propertyBoxVisible:Boolean = true;
            
            /**
             * プロパティボックスを隠すボタンが押されたときに呼ばれます。
             * 
             * @param    event
             */
            private function propertyBoxVisibleButtonDownHandler( event:FlexEvent ):void {
                
                propertyBoxMove.end();
                
                if ( propertyBoxVisible ) {
                    
                    propertyBoxMove.xTo = -propertyBox.width;
                    propertyBoxVisibleButton.label = "プロパティボックスを表示";
                    
                } else {
                    
                    propertyBoxMove.xTo = 0;
                    propertyBoxVisibleButton.label = "プロパティボックスを隠す";
                }
                
                propertyBoxMove.play();
                
                propertyBoxVisible = !propertyBoxVisible;
            }
        ]]>
    </mx:Script>
    
    <mx:Move id="propertyBoxMove" target="{ propertyBox }" />
    
    <mx:Canvas id="canvas" width="100%" height="100%" backgroundColor="0xccccff">
		
		<mx:UIComponent id="imageContentHolder" />
		
        <mx:Box id="propertyBox" verticalAlign="middle" alpha="0.9" backgroundColor="0xffffff" paddingLeft="10" paddingBottom="10" paddingTop="40" paddingRight="10">
			<mx:Box id="cameraProperty" horizontalAlign="center" alpha="1.1">
				
				<mx:Button id="propertyResetButton" label="プロパティの値をリセット" buttonDown="propertyResetButtonDownHandler( event )" />
				<mx:Spacer height="5" />
				
				<mx:HBox>
					<mx:Label width="100" text="z" />
					<mx:NumericStepper id="zNumericStepper" width="60" minimum="-9999" maximum="0" stepSize="1" change="propertyChangeHandler( event )" />
				</mx:HBox>
				<mx:HBox>
					<mx:Label width="100" text="focus" />
					<mx:NumericStepper id="focusNumericStepper" width="60" minimum="0" maximum="255" stepSize="1" change="propertyChangeHandler( event )" />
				</mx:HBox>
				<mx:HBox>
					<mx:Label width="100" text="zoom" />
					<mx:NumericStepper id="zoomNumericStepper" width="60" minimum="0" maximum="255" stepSize="1" change="propertyChangeHandler( event )" />
				</mx:HBox>
				<mx:HBox>
					<mx:Label width="100" text="fov" />
					<mx:NumericStepper id="fovNumericStepper" width="60" minimum="0" maximum="180" stepSize="1" change="propertyChangeHandler( event )" />
				</mx:HBox>
				
				<mx:Button id="zDisplayMagnificationButton" label="zの値を変更してピクセル等倍" buttonDown="zDisplayMagnificationButtonDownHandler( event )" />
				<mx:Button id="focusDisplayMagnificationButton" label="focusの値を変更してピクセル等倍" buttonDown="focusDisplayMagnificationButtonDownHandler( event )" />
				<mx:Button id="zoomDisplayMagnificationButton" label="zoomの値を変更してピクセル等倍" buttonDown="zoomDisplayMagnificationButtonDownHandler( event )" />
			</mx:Box>
		</mx:Box>
        
        <mx:Box width="100%" alpha="0.9" backgroundColor="0xffffff" paddingLeft="2" paddingTop="2" paddingBottom="2">
            <mx:HBox alpha="1.1">
                <mx:Button id="propertyBoxVisibleButton" label="プロパティボックスを隠す" buttonDown="propertyBoxVisibleButtonDownHandler( event )" />
            </mx:HBox>
        </mx:Box>
    </mx:Canvas>
    
</mx:Application>

Forked