Drawing a triangle / polygon from scratch

Software: Away3D 4.x

prankard, Newbie
Posted: 28 September 2011 09:58 PM   Total Posts: 3

Hey,

I’ve been playing with the Broomstick API and have been struggling with some (what I thought) would be some basic drawing of triangles in 3d space.

I’ve had a few problems and would love to hear if someone else has done this successfully and what method they have used.

I’ve hacked a little way round of drawing. Here is how I’ve done it.

So I’ve got and 2d multidimensional array of points. And I want to pass this through my function and add it:

var triangle:Array = [[0,0],[0,50],[50,50]];
_myContainer.addChild(buildMesh(triangle)); 

And now here’s the function that makes the mesh:

private function buildMesh(trianglePoints:Array, offset:Vector3D nullwindingClockwise:Boolean true):Mesh
        {
            
if (offset == null)
                
offset = new Vector3D();
           
            
// Data for Mesh.build
            
var verticesRaw:Vector.<Number> = new Vector.<Number>();
            var 
indiciesRaw:Vector.<uint> = new Vector.<uint>();
            var 
uvsRaw:Vector.<Number> = new Vector.<Number>();
           
            
// Data for FaceHelper.addFace
            
var verticies:Vector.<Vertex> = new Vector.<Vertex>();
            var 
uvs:Vector.<UV> = new Vector.<UV>();
           
            if (!
windingClockwise)
                
trianglePoints trianglePoints.reverse();
           
            for (var 
i:int 0trianglePoints.lengthi++)
            
{
                
// Data
                
var x:Number trianglePoints[i][0] offset.x;
                var 
y:Number trianglePoints[i][1] offset.y;
               
                
verticesRaw.push(offset.xoffset.yoffset.z);
                
indiciesRaw.push(i,i,i);
                
uvsRaw.push(xy);
               
                
//uvs.push(new UV(x, y));
                
uvs.push(new UV(01));
                
verticies.push(new Vertex(offset.xoffset.yoffset.z));
            
}
   
   
// Note: This doesn't seem to make the mesh, but does create a valid subgeometry that I can push a face onto
   
var mesh:Mesh MeshHelper.build(verticesRawindiciesRawuvsRaw);
   
// Here I'm making a face, and adding it (But shouldn't the line above work?)
            
FaceHelper.addFace(meshverticies[0]verticies[1]verticies[2]uvs[0]uvs[1]uvs[2]0);
            
mesh.material _globalMaterial;
            return 
mesh;
        

So… does anyone know how to build it properly? I’m running into an error now. That when I’ve made my triangle. I can add a ColorMaterial to it fine. But I attach a light to that material, my mesh is missing the normals and breaks on rendering.

Any help would be much appreciated.

Thanks,

James

   

Avatar
Alejandro Santander, Administrator
Posted: 30 September 2011 01:46 PM   Total Posts: 414   [ # 1 ]

Hey James,

If you have a look at some of the primitives, you will have a better idea of what the engine expects for geometries. If you are not using lights (or non-basic shading), then things like normals, tangents are not needed. If you are not using textures, then uvs are not needed. If they are needed tho, and you haven’t specified them, you should get an error.

An alternative that I haven’t tried much is to locate the sub geometry you are using and ask it to auto derive vertex normals and tangents, that way you don’t need to specify them, but may not achieve what you want.

For example, if you want to build a triangle, you could do something like this:

public class Triangle extends PrimitiveBase
{
    
private var _v0:Vector3D;
    private var 
_v1:Vector3D;
    private var 
_v2:Vector3D;
    private var 
_uv0:Vector3D;
    private var 
_uv1:Vector3D;
    private var 
_uv2:Vector3D;

    
// Define vertices in a clockwise manner.
    
public function Triangle(material:MaterialBasev0:Vector3Dv1:Vector3Dv2:Vector3D,
                                                    
uv0:Vector3D nulluv1:Vector3D nulluv2:Vector3D null)
    
{
        super
(material);

        
_v0 v0;
        
_v1 v1;
        
_v2 v2;
        
_uv0 uv0;
        
_uv1 uv1;
        
_uv2 uv2;
    
}

    override 
protected function buildGeometry(target:SubGeometry):void
    {
        
var rawVertices:Vector.<Number> = Vector.<Number>([_v0.x_v0.y_v0.z_v1.x_v1.y_v1.z_v2.x_v2.y_v2.z]);
        var 
rawIndices:Vector.<uint> = Vector.<uint>([012]);

        var 
d0:Vector3D _v1.subtract(_v0);
        var 
d1:Vector3D _v2.subtract(_v0);
        var 
rawTangents:Vector.<Number> = Vector.<Number>([000]);

        var 
normal:Vector3D d0.crossProduct(d1);
        
normal.normalize();
        var 
rawNormals:Vector.<Number> = Vector.<Number>([normal.xnormal.ynormal.znormal.xnormal.ynormal.znormal.xnormal.ynormal.z]);

        
target.updateVertexData(rawVertices);
        
target.updateIndexData(rawIndices);
        
target.updateVertexNormalData(rawNormals);
        
target.updateVertexTangentData(rawTangents);
    
}

    override 
protected function buildUVs(target:SubGeometry):void
    {
        
if(_uv0 && _uv1 && _uv2)
            var 
rawUvData:Vector.<Number> = Vector.<Number>([_uv0.x_uv0.y_uv1.x_uv1.y_uv2.x_uv2.y]);
    
}
   

Tayyab, Member
Posted: 28 March 2012 06:08 AM   Total Posts: 72   [ # 2 ]

How would you use the triangle class in away3d 4 beta,

it gives me an error on super(material):
“Incorrect number arguments. Expected no more than 0.”

if i delete the material from super(material) nothing shows up on the stage.

var tri:Triangle = new Triangle(offMaterial, new Vector3D(0,128,0), new Vector3D(-128,0,0), new Vector3D(128,0,0) );
var 
triangleMesh:Mesh = new Mesh(tri);
scene.addChild(triangleMesh); 

I am actually trying to make a roof on top of a shed.

so i would need 2 triangles (one at the front and one at the back).

and i need to fill in the space in between with triangles or a four sided polygon

what would be the best way??

Cheers.

   

Tayyab, Member
Posted: 28 March 2012 12:41 PM   Total Posts: 72   [ # 3 ]

by the way i am using away3D 4.0 beta

   

Tayyab, Member
Posted: 31 March 2012 12:59 PM   Total Posts: 72   [ # 4 ]

ok guys this is my 8th post in this away3d forum, haven’t even got a single reply :( which is quite disappointing.

but i will keep on trying until i get an answer…

I have used this Triangle class mentioned above.

but somehow i cannot add a bitmap texture on it.

   

John Brookes, Moderator
Posted: 31 March 2012 01:27 PM   Total Posts: 732   [ # 5 ]

Just remove materiial from the super

   

Tayyab, Member
Posted: 31 March 2012 01:31 PM   Total Posts: 72   [ # 6 ]

Thanx so much for ur reply… really appreciate it..

Iam sorry i did manage to use the traingle class after gettring rid of the material from super. but my next problem is that i cannot add a bitmap texture to this triangle. it just does not render at all.. i have an idea it has something to do with the uv data. but coz iam a newbie i have no idea how to apply the uvdata or even how the uv data works…

Thanx
Tayyab

   

John Brookes, Moderator
Posted: 31 March 2012 02:49 PM   Total Posts: 732   [ # 7 ]

Remove
material:MaterialBase,

form the constructor. (sorry missed that)

In beta the geometry doesnt get passed a material.
You would do something like.

var myMaterial:ColorMaterial = new ColorMaterial(0xff0000);
var myTriangleGeometry:Triangle = new Triangle( ALL YOUR PARAMETERS)
var myMesh:Mesh = new Mesh(myTriangleGeometry, myMaterial);
view.scene.addChild(myMesh);

   

John Brookes, Moderator
Posted: 31 March 2012 02:57 PM   Total Posts: 732   [ # 8 ]

Just to add if you replace the buildUvs function with

var rawUvData:Vector.<Number>
if(
_uv0 && _uv1 && _uv2)
  
rawUvData Vector.<Number>([_uv0.x_uv0.y_uv1.x_uv1.y_uv2.x_uv2.y]);
else
rawUvData Vector.<Number>([000111]);

target.updateUVData(rawUvData); 

You should be able to not bother passing Uvs (not tested but used to work)

Have a look at (also out of date was for pre Beta) may help.
forum/viewthread/1672/

   

Tayyab, Member
Posted: 01 April 2012 12:05 PM   Total Posts: 72   [ # 9 ]

Thanx alot james.. now it works

except the normalMap does not work. it throws an error:

RangeError: Error #3669: Bad input size.
at flash.display3D::VertexBuffer3D/uploadFromVector()
at away3d.core.base::SubGeometry/getVertexTangentBuffer()[D:\PROJECTS\GREY\AWAY3D\Working\ShedBuilderFlashApp\away3d4\away3d\core\base\SubGeometry.as:307]
at away3d.core.base::SubMesh/getVertexTangentBuffer()[D:\PROJECTS\GREY\AWAY3D\Working\ShedBuilderFlashApp\away3d4\away3d\core\base\SubMesh.as:186]
at away3d.materials.passes::DefaultScreenPass/render()

but atleast it works smile thanx again..

also iam trying to put a metal shed texture on the triangles but the texture is rotated . how can i rotate the texture so that its always vertical..

thanx once again for your help.
Tayyab

   

Tayyab, Member
Posted: 01 April 2012 12:17 PM   Total Posts: 72   [ # 10 ]

I just commented out the light source on the material and it shows up.
so there is probably something wrong with the lighting??

   

John Brookes, Moderator
Posted: 01 April 2012 01:22 PM   Total Posts: 732   [ # 11 ]

Im not sure but I seem to remember that there was an issue with lighting and or normal mapping with single triangles. Never looked into it any further as Ive never actually had to use a single tri.

As for rotating UVs for a single triangle just change the order or position of the UVs
All Uvs are is a position in a 2d grid from 0 to 1(can be bigger/smaller)

   

Tayyab, Member
Posted: 26 July 2012 04:28 AM   Total Posts: 72   [ # 12 ]

I have been using this triangle class for a while now, the problem i have now is that, i want to have a triangle which can be seen from both sides.

I tried putting in material.bothsides = true;

It does show both sides but one side is totally black. what is wrong??

thanx in advance..

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X