Modifying Surfaces

From Flare3D - WIKI
Jump to: navigation, search

This example shows how to modify a Surface3D directly by code.

package tests 
{
	import flare.basic.*;
	import flare.core.*;
	import flare.materials.*;
	import flare.materials.filters.*;
	import flare.primitives.*;
	import flare.system.*;
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
 
	/**
	 * @author Ariel Nehmad.
	 */
	public class ChangeSurface extends Sprite 
	{
		private var scene:Viewer3D;
		private var originalData:Vector.<Number>;
		private var cube:Cube;
 
		public function ChangeSurface() 
		{
			scene = new Viewer3D(this);
			scene.camera = new Camera3D();
			scene.camera.setPosition( 40, 40, -100 );
			scene.camera.lookAt( 0, 0, 0 );
 
			cube = new Cube( "cube", 50, 50, 50, 10, new Shader3D( "material", [new TextureFilter()] ) );
			cube.parent = scene;
 
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
		}
 
		private function updateEvent(e:Event):void 
		{
			changeSurface( cube as Mesh3D );
		}
 
		private function changeSurface( mesh:Mesh3D ):void 
		{
			// gets the first surface on the mesh.
			var surface:Surface3D = mesh.surfaces[0];
 
			// creates a copy of the original data.
			if ( !originalData ) originalData = surface.vertexVector.concat();
 
			// each surface contains only one vertex vector,
			// and the same amount of data is stored for each vertex, for example:
			// vertex 0                     vertex 1                    vertex x n
			// x0,y0,z0,nx0,ny0,nz0,u0,v0,  x1,y1,z1,nx1,ny1,nz1,u1,v1, ..........
			//
			// each surface has a sizee per vertex, in the case of the example 
			// the size is 8 (3 x position, 3 x normals, 2 x uv's).
 
			// gets acces to the offset location of each data inside the vertex.
			var position:int = surface.offset[Surface3D.POSITION];
			var normal:int = surface.offset[Surface3D.NORMAL];
			var uv:int = surface.offset[Surface3D.UV0];
 
			var vector:Vector.<Number> = surface.vertexVector;
			var length:int = surface.vertexVector.length;
			var sizePerVertex:int = surface.sizePerVertex;
			var t:Number = getTimer();
 
			// randomize positions amd scrolls the uv's
			for ( var i:int = 0; i < length; i += sizePerVertex )
			{
				var x:Number = originalData[i + position];
				var y:Number = originalData[i + position + 1];
				var z:Number = originalData[i + position + 2];
 
				vector[i + position] 		= x + Math.sin(t / 150 + y);
				vector[i + position + 1] 	= y + Math.cos(t / 150 + x);
				vector[i + position + 2] 	= z + Math.cos(t / 150 + y);
 
				vector[i + uv] = vector[i + uv] + 0.001;
				vector[i + uv + 1] = vector[i + uv + 1] - 0.01;
			}
 
			// reuploads the data to the gpu.
			// we need to test for the vertex buffer to be sure that the context is available
			// and the data was correctly created.
			if ( surface.vertexBuffer )
				surface.vertexBuffer.uploadFromVector( vector, 0, length / sizePerVertex );
		}
	}
 
}
blog comments powered by Disqus
Personal tools
Namespaces
Variants
Actions
Navigation
Survival kit
Flare3D Physics (Beta)
Toolbox