http://3ddm.yoz.sk
Copyright (c) 2010, Jozef Chúťka
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by the Jozef Chúťka.
4. Neither the name of the Jozef Chúťka nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Jozef Chúťka ''AS IS'' AND ANY
EXPRESS OR IMPLIED WA
/*
http://3ddm.yoz.sk
Copyright (c) 2010, Jozef Chúťka
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by the Jozef Chúťka.
4. Neither the name of the Jozef Chúťka nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Jozef Chúťka ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Jozef Chúťka BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;
[SWF(frameRate="30", backgroundColor="#FFFFFF")]
public class WonderflApp extends Sprite
{
private static const MAX_ROTATION_X:Number = 10;
private static const MAX_ROTATION_Y:Number = 10;
private var imageLoader:Loader = new Loader();
private var mapLoader:Loader = new Loader();
private var shape:Sprite = new Sprite();
private var index:int = -1;
private var data:XML;
private var displacer:SimpleDisplacerPV;
private var renderedRotationX:Number;
private var renderedRotationY:Number;
public function WonderflApp():void
{
super();
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
shape.graphics.beginFill(0, 0);
shape.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
shape.graphics.endFill();
addChild(shape);
data = <data>
<image
source="http://3ddm.yoz.sk/images/mountain.jpg"
map="http://3ddm.yoz.sk/images/mountainMap.jpg"
rotationXMax="20" rotationYMax="20"/>
<image
source="http://3ddm.yoz.sk/images/china.jpg"
map="http://3ddm.yoz.sk/images/chinaMap.jpg"
depth="100" segmentsW="35" segmentsH="35"
rotationXMultiplier="1.1" rotationYMultiplier="1.1"
rotationXMax="20" rotationYMax="20"/>
<image
source="http://3ddm.yoz.sk/images/face2.jpg"
map="http://3ddm.yoz.sk/images/face2Map.jpg"
rotationXMax="20" rotationYMax="20"/>
<image
source="http://3ddm.yoz.sk/images/texture1.jpg"
map="http://3ddm.yoz.sk/images/texture1Map.jpg"
rotationXMax="20" rotationYMax="20"/>
<image
source="http://3ddm.yoz.sk/images/texture2.jpg"
map="http://3ddm.yoz.sk/images/texture2Map.jpg"
rotationXMax="20" rotationYMax="20"/>
<image
source="http://3ddm.yoz.sk/images/house.jpg"
map="http://3ddm.yoz.sk/images/houseMap.jpg"
rotationXMax="20" rotationYMax="20"/>
</data>;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
addEventListener(MouseEvent.CLICK, changeSource);
changeSource();
}
private function changeSource(... rest):void
{
index = index + 1 >= data.image.length() ? 0 : index + 1;
var image:XML = data.image[index];
var type:String = Event.COMPLETE;
var context:LoaderContext = new LoaderContext(true);
var imageRequest:URLRequest = new URLRequest(image.@source);
var mapRequest:URLRequest = new URLRequest(image.@map);
imageLoader && imageLoader.unload();
imageLoader.load(imageRequest, context);
imageLoader.contentLoaderInfo.addEventListener(type, onImageComplete);
mapLoader && mapLoader.unload();
mapLoader.load(mapRequest, context);
mapLoader.contentLoaderInfo.addEventListener(type, onImageComplete);
displacer && removeChild(displacer);
displacer = null;
}
private function onImageComplete(event:Event):void
{
if(!imageLoader.content || !mapLoader.content || displacer)
return;
displacer = new SimpleDisplacerPV(
stage.stageWidth, stage.stageHeight,
Bitmap(imageLoader.content).bitmapData,
Bitmap(mapLoader.content).bitmapData,
data.image[index].@rotationXMax,
data.image[index].@rotationYMax,
data.image[index].@depth,
data.image[index].@segmentsW,
data.image[index].@segmentsH);
addChild(displacer);
}
private function onEnterFrame(event:Event):void
{
if(!imageLoader.content || !mapLoader.content || !displacer)
return;
var w:Number = stage.stageWidth / 2;
var h:Number = stage.stageHeight / 2;
var dx:Number = (mouseX - w) / w;
var dy:Number = (mouseY - h) / h;
displacer.rotX +=
(displacer.rotationXMax * -dy - displacer.rotX) / 10;
displacer.rotY +=
(displacer.rotationYMax * -dx - displacer.rotY) / 10;
var rx:Number = Math.round(displacer.rotX * 40);
var ry:Number = Math.round(displacer.rotY * 40);
if(renderedRotationX == rx && renderedRotationY == ry)
return;
renderedRotationX = rx;
renderedRotationY = ry;
displacer.render();
}
}
}
import flash.display.BitmapData;
import flash.display.Sprite;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
internal class AbstractDisplacer extends Sprite
{
public var rotationXMax:Number = 10;
public var rotationYMax:Number = 10;
public var rotationXMultiplier:Number = 1;
public var rotationYMultiplier:Number = 1;
public var image:BitmapData;
public var map:BitmapData;
private var _rotX:Number = 0;
private var _rotY:Number = 0;
public function AbstractDisplacer(image:BitmapData, map:BitmapData,
rotationXMax:Number = 10, rotationYMax:Number = 10,
rotationXMultiplier:Number = 1, rotationYMultiplier:Number = 1)
{
super();
this.image = image;
this.map = map;
this.rotationXMax = Math.abs(rotationXMax) || this.rotationXMax;
this.rotationYMax = Math.abs(rotationYMax) || this.rotationYMax;
this.rotationXMultiplier =
Math.abs(rotationXMultiplier) || this.rotationXMultiplier;
this.rotationYMultiplier =
Math.abs(rotationYMultiplier) || this.rotationYMultiplier;
}
public function get rotX():Number
{
return _rotX;
}
public function set rotX(value:Number):void
{
_rotX = value;
}
public function get rotY():Number
{
return _rotY;
}
public function set rotY(value:Number):void
{
_rotY = value;
}
public function render():void
{
}
}
internal class SimpleDisplacerPV extends AbstractDisplacer
{
public var viewport:Viewport3D;
public var scene:Scene3D = new Scene3D();
public var camera:Camera3D = new Camera3D();
public var renderer:BasicRenderEngine = new BasicRenderEngine();
public var plane:Plane;
public var depth:Number = 100;
public var segmentsW:uint = 15;
public var segmentsH:uint = 15;
public function SimpleDisplacerPV(
viewportWidth:Number, viewportHeight:Number,
image:BitmapData, map:BitmapData,
rotationXMax:Number = 30, rotationYMax:Number = 30,
depth:Number = 100, segmentsW:uint = 15, segmentsH:uint = 15):void
{
super(image, map, rotationXMax, rotationYMax, 0, 0);
this.depth = depth || this.depth;
this.segmentsW = segmentsW || this.segmentsW;
this.segmentsH = segmentsH || this.segmentsH;
viewport = new Viewport3D(viewportWidth, viewportHeight, false,
false, false);
addChild(viewport);
camera.zoom = 120;
plane = new Plane(material, image.width, image.height,
this.segmentsW, this.segmentsH);
var length:uint = plane.geometry.vertices.length;
var vertex:Vertex3D;
var color:uint;
var w:Number = (image.width - 1) / 2;
var h:Number = (image.height - 1) / 2;
for(var i:uint = 0; i < length; i++)
{
vertex = plane.geometry.vertices[i];
color = map.getPixel(w + vertex.x, h - vertex.y);
vertex.z = -(color >> 16) / 0xff * this.depth;
}
scene.addChild(plane);
}
protected function get material():BitmapMaterial
{
var material:BitmapMaterial = new BitmapMaterial(image, false);
material.oneSide = true;
material.smooth = true;
return material;
}
override public function set rotX(value:Number):void
{
super.rotX = value;
plane.rotationX = value;
}
override public function set rotY(value:Number):void
{
super.rotY = value;
plane.rotationY = value;
}
override public function render():void
{
super.render();
renderer.renderScene(scene, camera, viewport);
}
}