| /trunk/src/com/as3dmod/modifiers/Pivot.as r89 | /trunk/src/com/as3dmod/modifiers/Pivot.as r90 | ||
| 1 | package com.as3dmod.modifiers { | 1 | package com.as3dmod.modifiers { |
|---|---|---|---|
| 2 | import com.as3dmod.IModifier; | 2 | import com.as3dmod.IModifier; |
| 3 | import com.as3dmod.core.Modifier; | 3 | import com.as3dmod.core.Modifier; |
| 4 | import com.as3dmod.core.Vector3; | 4 | import com.as3dmod.core.Vector3; |
| 5 | import com.as3dmod.core.VertexProxy; | 5 | import com.as3dmod.core.VertexProxy; |
| 6 | 6 | ||
| 7 | /** | 7 | /** |
| 8 | * <b>Pivot modifier.</b> Allows to move the pivot point of a 3D mesh. | 8 | * <b>Pivot modifier.</b> Allows to move the pivot point of a 3D mesh. |
| 9 | * <br> | 9 | * <br> |
| 10 | * <br>The pivot point will be moved by the amount specified by the pivot parameter. | 10 | * <br>The pivot point will be moved by the amount specified by the pivot vector. |
| 11 | * <br>Common use case is to set the values of the pivot vector, add it to the modifier stack | 11 | * <br>Common use case is to set the values of the pivot vector, add it to the modifier stack |
| 12 | * and collapse the stack. This way the pivot point will be moved and the modifier discarded. The | 12 | * and collapse the stack. This way the pivot point will be moved and the modifier discarded. The |
| 13 | * same stack can be later resused for other modifiers. | 13 | * same stack can be later resused for other modifiers. |
| 14 | * <br> | 14 | * <br> |
| 15 | * <br>It is possible to animtate the pivot vector propeties also. | 15 | * <br>It is also possible to animtate the pivot vector propeties. |
| 16 | * | 16 | * |
| 17 | * @version 1.0 | 17 | * @version 1.0 |
| 18 | * @author Bartek Drozdz | 18 | * @author Bartek Drozdz |
| 19 | */ | 19 | */ |
| 20 | public class Pivot extends Modifier implements IModifier { | 20 | public class Pivot extends Modifier implements IModifier { |
| 21 | 21 | ||
| 22 | public var pivot:Vector3; | 22 | public var pivot:Vector3; |
| 23 | 23 | ||
| 24 | public function Pivot(x:Number=0, y:Number=0, z:Number=0) { | 24 | public function Pivot(x:Number=0, y:Number=0, z:Number=0) { |
| 25 | this.pivot = new Vector3(x, y, z); | 25 | this.pivot = new Vector3(x, y, z); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | /** | 28 | /** |
| 29 | * Sets the values of the pivot vector so that the pivot point of the mesh will be moved to it's center. | 29 | * Sets the values of the pivot vector so that the pivot point of the mesh will be moved to it's center. |
| 30 | */ | 30 | */ |
| 31 | public function setMeshCenter():void { | 31 | public function setMeshCenter():void { |
| 32 | var vx:Number = -(mod.minX + mod.width / 2); | 32 | var vx:Number = -(mod.minX + mod.width / 2); |
| 33 | var vy:Number = -(mod.minY + mod.height / 2); | 33 | var vy:Number = -(mod.minY + mod.height / 2); |
| 34 | var vz:Number = -(mod.minZ + mod.depth / 2); | 34 | var vz:Number = -(mod.minZ + mod.depth / 2); |
| 35 | pivot = new Vector3(vx, vy, vz); | 35 | pivot = new Vector3(vx, vy, vz); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | public function apply():void { | 38 | public function apply():void { |
| 39 | var vs:Array = mod.getVertices(); | 39 | var vs:Array = mod.getVertices(); |
| 40 | var vc:int = vs.length; | 40 | var vc:int = vs.length; |
| 41 | 41 | ||
| 42 | for (var i:int = 0; i < vc; i++) { | 42 | for (var i:int = 0; i < vc; i++) { |
| 43 | var v:VertexProxy = vs[i] as VertexProxy; | 43 | var v:VertexProxy = vs[i] as VertexProxy; |
| 44 | v.vector = v.vector.add(pivot); | 44 | v.vector = v.vector.add(pivot); |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | var npivot:Vector3 = pivot.clone(); | 47 | var npivot:Vector3 = pivot.clone(); |
| 48 | mod.updateMeshPosition(npivot.negate()); | 48 | mod.updateMeshPosition(npivot.negate()); |
| 49 | } | 49 | } |
| 50 | } | 50 | } |
| 51 | } | 51 | } |