Axiom.Samples.VolumeTexture.VolumeRendable.Initialize C# (CSharp) Метод

Initialize() защищенный Метод

protected Initialize ( ) : void
Результат void
		protected void Initialize()
		{
			// Create geometry
			int nvertices = slices * 4; // n+1 planes
			int elemsize = 3 * 3;
			int dsize = elemsize * nvertices;
			int x;

			IndexData indexData = new IndexData();
			VertexData vertexData = new VertexData();
			float[] vertices = new float[ dsize ];

			float[ , ] coords = new float[ 4, 2 ]
			{
			    {0.0f, 0.0f},
			    {0.0f, 1.0f},
			    {1.0f, 0.0f},
			    {1.0f, 1.0f}
			};

			for ( x = 0; x < slices; x++ )
			{
				for ( int y = 0; y < 4; y++ )
				{
					float xcoord = coords[ y, 0 ] - 0.5f;
					float ycoord = coords[ y, 1 ] - 0.5f;
					float zcoord = -( (float)x / (float)( slices - 1 ) - 0.5f );
					// 1.0f .. a/(a+1)
					// coordinate
					vertices[ x * 4 * elemsize + y * elemsize + 0 ] = xcoord * ( size / 2.0f );
					vertices[ x * 4 * elemsize + y * elemsize + 1 ] = ycoord * ( size / 2.0f );
					vertices[ x * 4 * elemsize + y * elemsize + 2 ] = zcoord * ( size / 2.0f );
					// normal
					vertices[ x * 4 * elemsize + y * elemsize + 3 ] = 0.0f;
					vertices[ x * 4 * elemsize + y * elemsize + 4 ] = 0.0f;
					vertices[ x * 4 * elemsize + y * elemsize + 5 ] = 1.0f;
					// tex
					vertices[ x * 4 * elemsize + y * elemsize + 6 ] = xcoord * Utility.Sqrt( 3.0f );
					vertices[ x * 4 * elemsize + y * elemsize + 7 ] = ycoord * Utility.Sqrt( 3.0f );
					vertices[ x * 4 * elemsize + y * elemsize + 8 ] = zcoord * Utility.Sqrt( 3.0f );
				}
			}

			short[] faces = new short[ slices * 6 ];
			for ( x = 0; x < slices; x++ )
			{
				faces[ x * 6 + 0 ] = (short)( x * 4 + 0 );
				faces[ x * 6 + 1 ] = (short)( x * 4 + 1 );
				faces[ x * 6 + 2 ] = (short)( x * 4 + 2 );
				faces[ x * 6 + 3 ] = (short)( x * 4 + 1 );
				faces[ x * 6 + 4 ] = (short)( x * 4 + 2 );
				faces[ x * 6 + 5 ] = (short)( x * 4 + 3 );
			}

			//setup buffers
			vertexData.vertexStart = 0;
			vertexData.vertexCount = nvertices;

			VertexDeclaration decl = vertexData.vertexDeclaration;
			VertexBufferBinding bind = vertexData.vertexBufferBinding;
			int offset = 0;
			offset += decl.AddElement( 0, 0, VertexElementType.Float3, VertexElementSemantic.Position ).Size;
			offset += decl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.Normal ).Size;
			offset += decl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.TexCoords ).Size;

			HardwareVertexBuffer vertexBuffer =
				HardwareBufferManager.Instance.CreateVertexBuffer( decl, nvertices, BufferUsage.StaticWriteOnly );

			bind.SetBinding( 0, vertexBuffer );

			HardwareIndexBuffer indexBuffer =
				HardwareBufferManager.Instance.CreateIndexBuffer( IndexType.Size16, slices * 6, BufferUsage.StaticWriteOnly );

			indexData.indexBuffer = indexBuffer;
			indexData.indexCount = slices * 6;
			indexData.indexStart = 0;

			indexBuffer.WriteData( 0, indexBuffer.Size, faces, true );
            vertexBuffer.WriteData( 0, vertexBuffer.Size, vertices );
			vertices = null;
			faces = null;

			// Now make the render operation
			renderOperation.operationType = OperationType.TriangleList;
			renderOperation.indexData = indexData;
			renderOperation.vertexData = vertexData;
			renderOperation.useIndices = true;

			// Create a brand new private material
			if ( !ResourceGroupManager.Instance.GetResourceGroups().Contains( "VolumeRendable" ) )
			{
				ResourceGroupManager.Instance.CreateResourceGroup( "VolumeRendable" );
			}

            Material material = (Material)MaterialManager.Instance.Create( texture, "VolumeRendable" );
			// Remove pre-created technique from defaults
			material.RemoveAllTechniques();

			// Create a techinique and a pass and a texture unit
			Technique technique = material.CreateTechnique();
			Pass pass = technique.CreatePass();
			TextureUnitState textureUnit = pass.CreateTextureUnitState();

			// Set pass parameters
			pass.SetSceneBlending( SceneBlendType.TransparentAlpha );
			pass.DepthWrite = false;
			pass.CullingMode = CullingMode.None;
            pass.LightingEnabled = false;
            textureUnit.SetTextureAddressingMode( TextureAddressing.Clamp );
            textureUnit.SetTextureName( texture, TextureType.ThreeD );
            textureUnit.SetTextureFiltering( TextureFiltering.Trilinear );

			unit = textureUnit;
			base.material = material;
		}
	}