Book notes in the Essential Guide [3] [Chapter 2]

Source: Internet
Author: User
Tags addchild
1. What is Mesh? Why is it?

The book explains: A mesh is a collection of vertices made visible by a collection of elements that use those vertices. as a saving measure, vertices can be reused by multiple elements in a mesh; it is rarely necessary to build elements with unique vertex points. A
Simple cube, for example, can have its shape defined by eight vertices, positioned at each corner. to create a solid object, these vertices are shared between twelve faces; six sides composed of two triangles each.

We know that away3d has basic data structure classes, such as Number3D, which do not inherit from any native flash classes. Vertex is the data structure class of the advanced Vertex, and Segment and Face are the advanced ones. Their Inheritance relationships are as follows:

Vertex-> ValueObject-> flash. events. EventDispatcher

Face-> Element-> flash. events. EventDispatcher

Segment-> Element-> flash. events. EventDispatcher

Vertex, Face, and Segment classes all contain sufficient information for creating a Vertex, a plane, and a line in a 3D space. Unfortunately, they are not subclasses of Object3D and therefore do not have the display function. Let's look at the inheritance relationship of the Mesh class:

Mesh-> Object3D-> flash. events. EventDispatcher

It directly inherits from the ObjectD class, similar to a data container with the display function. We use its own addFace () and addSegment () Methods to add data information to the container, in this way, the engine can recognize the data structure and render it to the screen. Here we will answer what is a Mesh class. Let's take a look at how to use the Mesh class to display a Face instance on the screen:

TestMesh. as [Set Document Class]

Package {import away3d. animators. vertexAnimator; import away3d. containers. view3D; import away3d. core. base. face; import away3d. core. base. mesh; import away3d. core. base. segment; import away3d. core. base. vertex; import away3d. materials. colorMaterial; import away3d. materials. wireColorMaterial; import away3d. materials. wireframeMaterial; import flash. display. sprite; import flash. events. event ;/***... * @ author wws */[SWF (width = '000000', height = '000000', backgroundColor = '0x000000')] public class TestMesh extends Sprite {private var _ view: View3D; private var mesh: Mesh; public function TestMesh () {_ view = new View3D ({x: 275, y: 200}); // For elements in the Mesh container, use a green outer frame, white on the back, and red on the front. "hide on the back" is not enabled ". Mesh = new Mesh ({back: new ColorMaterial (0 xffffff), material: new WireColorMaterial (0xff0000, {wireColor: 0xffff00}), bothsides: 1}); var v1: vertex = new Vertex (100, 0, 1000); var v2: Vertex = new Vertex (-100, 0, 1000); var v3: Vertex = new Vertex (0,100,100 0 ); var face1: Face = new Face (v1, v2, v3); // create an instance_view.scene.addChild (mesh) of the data structure class Face; mesh. addFace (face1); // Add a Data Object (usually called an element) to the mesh container. The engine will process and final Dye the element inside the mesh. Stage. addChild (_ view); stage. addEventListener (Event. ENTER_FRAME, render);} private function render (e: Event = null): void {_ view. render ();}}}

:
Looking at the origin of the Mesh class, this is actually answering another question: Why is Away3d not a built-in data class inherited from Object3D, do you have to use the [abstract data class + Data container class with display function] method?

This is because the data information of the displayed object is abstracted into data classes such as Face, Element, and Vertex, which can reduce memory consumption in two aspects:

First, the book says "As a saving measure, vertices can be reused by multiple elements in a mesh; it is rarely necessary to build elements with unique vertex points ", this is the "vertex list + point index mechanism" in "3D programming Masters". For example, adjacent surfaces of 3D objects frequently share vertices, we store all the vertices separately in an array, while Face only describes the array indexes. It takes much less memory to store an index value than to store a Vertex instance. (For simplicity, the above Code does not use the "vertex list + vertex index mechanism ")

Second, we can see on the Internet that multiple Mesh instances may share some data structures.

2. Relationship between face normal direction and Front Orientation

This is because of the engine.

Away3d stipulates that The direction the normal vector points is calculated such that if we were to observe the face by looking along its normal vector (so that it is pointing away from us ), the vertices making up the face wocould be arranged in a counterclockwise order. It can be seen that the direction of the face normal is opposite to that of the front.

Let's look at what 3D programming masters stipulate:

From the sentence if (...), then poly is visible, we can see that the normal orientation of the surface is consistent with that of the front.

To determine whether a plane is positive, follow these steps:

1. The plane normal vector is obtained through the cross product according to the clockwise order of the vertex of the component plane. (In advanced mathematics, Cross Product operations are often performed in the right-hand coordinate system. Both 3D programming masters and away3d use the left-hand coordinate system)

2. The next step is the engine. For example, in 3D programming masters, the front point is the face normal orientation, while the away3d judges that the front point is the opposite of the face normal orientation.

For example, let's take a look at the code above. The top of face1 is (1000, 0, 1000), (-0,100,100, 0,), (), clockwise, the face normal is reversed to the positive direction of the Z axis, so the front of face1 is oriented to the positive direction of the Z axis. We can only see the white back from (1000.

3. Mesh + Element is the basic display mode.

In the away3d getting started tutorial, a sphere or cube is basically created first, which is easy to tell: It seems that the Primitive class is the basic Display object class. Actually not. The Primitive class only encapsulates the Mesh class, Primitive-> AbstractPrimitive-> Object3D-> flash. events. EventDispatcher. To display objects, Mesh + Element is enough.

The following describes how to use mesh + face to write an obj file parsing class to demonstrate the basics of mesh. (Because I only know the v and f elements in the obj format, the simplicity of this class can be imagined)

ObjParser.

//. Obj file shoshould be stored in unicode. // atten: in. obj file, new line with a "\ n", not "\ r \ n" // atten: vertice index start by 1, not 0 package {import away3d. core. base. face; import away3d. core. base. mesh; import away3d. core. base. vertex; import away3d. materials. wireColorMaterial; import flash. display. sprite; import flash. events. event; import flash.net. URLLoader; import flash.net. URLRequest; import flash.net. URLLoaderDataFormat; import flash. text. textField ;/***... * @ author wws */public class ObjParser {private var vertices: Array; private var scaler: Number; public var mesh: Mesh; // The default material is silver gray, the coordinate scaling ratio is 10 public function ObjParser (path: String, matColor: uint = 0xcc0000, _ scaler: Number = 10) {vertices = new Array (); scaler = _ scaler; mesh = new Mesh ({material: new WireColorMaterial (matColor), bothsides: 0}); var request: URLRequest = new URLRequest (path); var urlLoader: URLLoader = new URLLoader (request); urlLoader. dataFormat = URLLoaderDataFormat. TEXT; urlLoader. addEventListener (Event. COMPLETE, completeHandler);} private function completeHandler (e: Event = null): void {var srcStr: String = e.tar get. data; readVertices (srcStr); readFaces (srcStr);} private function readFaces (str: String): void {// pattern_f: regExp =/\ nf \ s/; // pattern_number matches the vertex index value var pattern_number: RegExp =/\ s [0-9] +/g; var fIndex: int = str. search (pattern_f) + 1; trace ("fIndex =" + fIndex); pattern_number.lastIndex = fIndex; while (str. charAt (fIndex) = 'F' & str. charAt (fIndex + 1) = '') {var valueArr: Array = new Array (); var resultArr: Array = new Array (); for (var I: int = 0; I <= 3; I ++) {resultArr = pattern_number.exec (str); valueArr [I] = Number (resultArr [0]);} var nextLeadingIndex: int = str. indexOf ("\ n", pattern_number.lastIndex) + 1; fIndex = pattern_number.lastIndex = nextLeadingIndex; // in obj format, the surface is stored in a quadrilateral and divided into two triangles var v1: vertex = vertices [valueArr [0]-1]; var v2: Vertex = vertices [valueArr [1]-1]; var v3: vertex = vertices [valueArr [2]-1]; var v4: Vertex = vertices [valueArr [3]-1]; mesh. addFace (new Face (v1, v2, v3); mesh. addFace (new Face (v1, v3, v4);} trace ("faces-read finished .. "); trace (" mesh. faces. length = "+ mesh. faces. length + "mesh. vetices. length = "+ mesh. vertices. length);} private function readVertices (str: String): void {// pattern_v: Find the vertex description line starting with v, var pattern_v: RegExp =/\ nv \ s /; // pattern_number matches the vertex coordinate value var pattern_number: RegExp =/\ s \ S + \ B/g; var vIndex: int = str. search (pattern_v) + 1; pattern_number.lastIndex = vIndex; while (str. charAt (vIndex) = 'V' & str. charAt (vIndex + 1) = '') {var valueArr: Array = new Array (); for (var I: int = 0; I <= 2; I ++) {var arr: Array = pattern_number.exec (str); valueArr [I] = Number (arr [0]) * scaler;} vertices [vertices. length] = new Vertex (valueArr [0], valueArr [1], valueArr [2]); pattern_number.lastIndex ++; // point at 'V' vIndex = pattern_number.lastIndex ;} trace ("vertice-read finished .. "); trace (" vertices. length = "+ vertices. length );}}}

Main. as [Set Document Class]

Package {import away3d. containers. view3D; import away3d. materials. material; import away3d. materials. wireColorMaterial; import away3d. primitives. cube; import flash. display. sprite; import flash. events. event ;/***... * @ author wws */[SWF (width = '000000', height = '000000')] public class Main extends Sprite {private var _ view: View3D; private var _ objParser: objParser; public function Main (): void {if (stage) init (); else addEventListener (Event. ADDED_TO_STAGE, init);} private function init (e: Event = null): void {removeEventListener (Event. ADDED_TO_STAGE, init); stage. addEventListener (Event. ENTER_FRAME, render); // entry point_view = new View3D ({x: 275, y: 200}); _ objParser = new ObjParser ("D: /wws/3d/lib/zuqiu. obj ", 0 xcccccc, 20); _ view. scene. addChild (_ objParser. mesh); this. addChild (_ view);} private function render (e: Event = null): void {_ view. render (); // Utils is a static class. The display method is used to rotate the object Utils. display (_ objParser. mesh );}}}

:

The zuqiu. obj (click to download) file is very large, and the effect is pretty after it is fully loaded. This class can only load part of it.

4. Is the outline parameter in the Mesh constructor useful?

From document:

Mesh () Constructor
public function Mesh(init:Object = null)Parameters

  init:Object(Default =null)-[Optional] An initialisation object for specifying default instance properties.

Init Parameters

  outline:Material
  material:Material
  back:Material
  bothsides:Boolean(Default = false)

I tested it using the following code and couldn't get the green (0x00ff00) mesh outer frame:

TestMesh. as [Set Document Class]

Package {import away3d. animators. vertexAnimator; import away3d. containers. view3D; import away3d. core. base. face; import away3d. core. base. mesh; import away3d. core. base. segment; import away3d. core. base. vertex; import away3d. materials. colorMaterial; import away3d. materials. wireColorMaterial; import away3d. materials. wireframeMaterial; import away3d. primitives. cube; import flash. display. sprite; import flash. events. event ;/***... * @ author wws */[SWF (width = '000000', height = '000000', backgroundColor = '0x00ffcc')] public class TestMesh extends Sprite {private var cube1: Cube; private var _ view: View3D; public function TestMesh () {_ view = new View3D ({x: 275, y: 200}); // worried that the outline is too fine, I specifically set the thickness attribute to 3 var mesh: Mesh = new Mesh ({outline: new WireframeMaterial (0x00ff00, {thickness: 3}), back: new ColorMaterial (0 xffffffff ), material: new WireColorMaterial (0xff0000, {wireColor: 0xffff00}), bothsides: 1}); var v1: Vertex = new Vertex (100, 0, 1000); var v2: vertex = new Vertex (-100, 0, 1000); var v3: Vertex = new Vertex (0,100,100 0); var v4: Vertex = new Vertex (0,-50, 0); var segment1: Segment = new Segment (v1, v4); var f1: Face = new Face (v2, v3, v1); _ view. scene. addChild (mesh); mesh. addFace (f1); mesh. addSegment (segment1); stage. addChild (_ view); stage. addEventListener (Event. ENTER_FRAME, render);} private function render (e: Event = null): void {_ view. render ();}}}

:

No green border...

I guess that segment1 affects the border, so I removed mesh. addSegment (segment1); the result is still a bald white triangle.

Reserved for questions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.