示例#1
0
        private void UnsafeSetVertex(ushort inIndex, VertexAttributes inAttributes)
#endif // EXPANDED_REFS
        {
            m_Positions[inIndex] = inAttributes.Position;
            m_Colors[inIndex]    = inAttributes.Color;
            m_UVs[inIndex]       = inAttributes.UV;
        }
示例#2
0
        private void WriteDataBank(EndianBinaryWriter writer, VertexAttributes attribute)
        {
            switch (attribute)
            {
            case VertexAttributes.Position:
                WriteVec3(writer, Positions, PositionType, PositionFractionalBitVal);
                break;

            case VertexAttributes.Normal:
                WriteVec3(writer, Normals, NormalType, NormalFractionalBitVal);
                break;

            case VertexAttributes.Tex0:
            case VertexAttributes.Tex1:
            case VertexAttributes.Tex2:
            case VertexAttributes.Tex3:
            case VertexAttributes.Tex4:
            case VertexAttributes.Tex5:
            case VertexAttributes.Tex6:
            case VertexAttributes.Tex7:
                int uvListIndex = (int)attribute - 13;
                WriteVec2(writer, UVData[uvListIndex], UVType, UVFractionalBitVal);
                break;

            case VertexAttributes.Color0:
            case VertexAttributes.Color1:
                int colorListIndex = (int)attribute - 11;
                WriteColor(writer, ColorData[colorListIndex], ColorType);
                break;
            }
        }
示例#3
0
        public void onEnter()
        {
            this.camera = new PerspecitveCamera(MathHelper.PiOver4, Game.shared.width, Game.shared.height);
            this.shader = Game.assets.shader("test");
            VertexAttributes attrs = new VertexAttributes(VertexAttribute.Position(), VertexAttribute.Color());

            this.rawMesh     = new VertexBufferObject(true, 3, 7, attrs);
            this.rawIndicies = new IndexBufferObject(true);

            float[] data = new float[] {
                -0.8f, -0.8f, 0f, 1f, 0f, 0f, 1.0f,
                0.8f, -0.8f, 0f, 0f, 1f, 0f, 1.0f,
                0f, 0.8f, 0f, 0f, 0f, 1f, 1.0f,
                0.8f, 0.8f, 0f, 1f, 0f, 1f, 1.0f
            };

            uint[] indicies = new uint[] {
                0, 1, 2,
                1, 2, 3
            };

            this.rawIndicies.setIndicies(ref indicies);
            this.rawMesh.setVerticies(ref data);

            this.mviewdata = Matrix4.Translation(0f, 0f, 0f);
            Vector3 cameraPosition = new Vector3(5f, 5f, 5f);

            this.camera.translate(ref cameraPosition);

            Vector3 target = new Vector3(0f, 0f, 0f);

            this.camera.lookAt(ref target);
        }
        protected override void OnDraw(double time, double delta)
        {
            if (TriangleShaderProgram == null)
            {
                throw new NullReferenceException();
            }
            if (TriangleVertexAttributes == null)
            {
                throw new NullReferenceException();
            }
            if (TriangleVertexBuffer == null)
            {
                throw new NullReferenceException();
            }
            if (TriangleTexture == null)
            {
                throw new NullReferenceException();
            }

            TriangleVertexAttributes.Bind();
            TriangleShaderProgram.Bind();
            TriangleTexture.Bind();
            GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
            VertexAttributes.Unbind();
            ShaderProgram.Unbind();
            Texture2D.Unbind();
            State.CheckError();
        }
示例#5
0
 public T[] VertexAttributeArray <T>(Symbol name)
 {
     if (VertexAttributes.TryGetValue(name, out Array array))
     {
         return((T[])array);
     }
     return(null);
 }
示例#6
0
        /// <summary>
        /// Creates a dynamic mesh instance for the given faces, using the specified vertex attributes and triangulation object to construct the vertex data and triangles.
        /// </summary>
        /// <param name="faces">The faces for which to generate triangle meshes.</param>
        /// <param name="vertexAttributes">The types of vertex data to include in the meshes, such as position, color, and normal.</param>
        /// <param name="triangulation">The object which is responsible for turning each individual face into triangles with a mesh.</param>
        /// <param name="maxVerticesPerSubmesh">The maximum number of vertices allowed per submesh.  Defaults to Unity's built-in maximum of 65534.</param>
        /// <returns>A dynamic mesh instance created from the given faces and triangulation.</returns>
        public static DynamicMesh Create(IEnumerable <Topology.Face> faces, VertexAttributes vertexAttributes, ITriangulation triangulation, int maxVerticesPerSubmesh = 65534)
        {
            var dynamicMesh = CreateInstance <DynamicMesh>();

            dynamicMesh._vertexAttributes      = vertexAttributes;
            dynamicMesh._maxVerticesPerSubmesh = maxVerticesPerSubmesh;
            dynamicMesh.Initialize(new IEnumerable <Topology.Face>[] { faces }, triangulation);
            return(dynamicMesh);
        }
示例#7
0
            public void Rebuild(VertexAttributes dirtyVertexAttributes)
            {
                if (isDirty)
                {
                    if (positions != null && (dirtyVertexAttributes & VertexAttributes.Position) != 0)
                    {
                        mesh.vertices = positions;
                    }
                    if (normals != null && (dirtyVertexAttributes & VertexAttributes.Normal) != 0)
                    {
                        mesh.normals = normals;
                    }
                    if (colors != null && (dirtyVertexAttributes & VertexAttributes.Color) != 0)
                    {
                        mesh.colors = colors;
                    }
                    if (colors32 != null && (dirtyVertexAttributes & VertexAttributes.Color32) != 0)
                    {
                        mesh.colors32 = colors32;
                    }
                    if (uvs != null && (dirtyVertexAttributes & VertexAttributes.UV) != 0)
                    {
                        mesh.uv = uvs;
                    }
                    if (uvs2 != null && (dirtyVertexAttributes & VertexAttributes.UV2) != 0)
                    {
                        mesh.uv2 = uvs2;
                    }
                    if (uvs3 != null && (dirtyVertexAttributes & VertexAttributes.UV3) != 0)
                    {
                        mesh.uv3 = uvs3;
                    }
                    if (uvs4 != null && (dirtyVertexAttributes & VertexAttributes.UV4) != 0)
                    {
                        mesh.uv4 = uvs4;
                    }

                    if (tangents != null)
                    {
                        // Since setting the tangents is necessary for updating binormals, then
                        // we need to set the tangents if either the tangents themselves are
                        // dirty or the normals which are used to compute the binormals are dirty.
                        if ((dirtyVertexAttributes & VertexAttributes.Tangent) != 0 || normals != null && (dirtyVertexAttributes & VertexAttributes.Normal) != 0)
                        {
                            mesh.tangents = tangents;
                        }
                    }

                    // If the positions have changed, recalculate the bounds.
                    if (positions != null && (dirtyVertexAttributes & VertexAttributes.Position) != 0)
                    {
                        mesh.RecalculateBounds();
                    }

                    isDirty = false;
                }
            }
示例#8
0
 /// <summary>
 /// Applies and uploads the specified vertex attribute arrays of all dirty submeshes to the Unity meshes themselves.
 /// </summary>
 /// <param name="dirtyVertexAttributes">The vertex attributes that should be uploaded.</param>
 public void RebuildMesh(VertexAttributes dirtyVertexAttributes)
 {
     foreach (var submesh in _submeshes)
     {
         if (submesh.isDirty)
         {
             submesh.Rebuild(dirtyVertexAttributes);
         }
     }
 }
示例#9
0
        public void onEnter()
        {
            this.shader         = Game.assets.shader("test");
            this.camera         = new PerspecitveCamera();
            this.cameraPosition = new Vector3(0f, 5f, -20f);
            this.camera.translate(ref cameraPosition);

            Vector3 target = new Vector3(0f, 0f, 0f);

            this.camera.lookAt(ref target);

            float[] vertData = new float[] {
                -1f, -1f, -1f, 0.0f, 1.0f, 1.0f, 1.0f,
                1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
                1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
                -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
                -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
                1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
                1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
                -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f
            };

            uint[] indicedata = new uint[] {
                //front
                0, 7, 3,
                0, 4, 7,
                //back
                1, 2, 6,
                6, 5, 1,
                //left
                0, 2, 1,
                0, 3, 2,
                //right
                4, 5, 6,
                6, 7, 4,
                //top
                2, 3, 6,
                6, 3, 7,
                //bottom
                0, 1, 5,
                0, 5, 4
            };

            VertexAttributes attrs = new VertexAttributes(VertexAttribute.Position(), VertexAttribute.Color());

            this.mesh = new Mesh(true, 8, 7, attrs);

            this.mesh.setVerticies(ref vertData);
            this.mesh.setIndicies(ref indicedata);

            this.posVector      = Vector3.Zero;
            this.mviewdata      = Matrix4.CreateTranslation(Vector3.Zero);
            this.rotationMatrix = Matrix4.CreateRotationX(0f);
            this.finalMatrix    = new Matrix4();
        }
示例#10
0
        protected void convertMesh(ModelMesh modelMesh)
        {
            var numIndices = 0;

            foreach (var part in modelMesh.parts)
            {
                numIndices += part.indices.Length;
            }

            var attributes  = new VertexAttributes(modelMesh.attributes);
            var numVertices = modelMesh.vertices.Length / (attributes.vertexSize / 4);

            numVertices = modelMesh.vertices.Length;
            var mesh = new Mesh(true, numVertices, numIndices, attributes);

            meshes.Add(mesh);
            disposables.Add(mesh);

            // src, dst, num, offset
            //mesh.setVertices(new float[modelMesh.vertices.Length]);
            //Array.Copy(modelMesh.vertices, mesh.getVerticesBuffer(), modelMesh.vertices.Length);
            mesh.setVertices(modelMesh.vertices);

            var list   = new List <uint>();
            var offset = 0;

            foreach (var part in modelMesh.parts)
            {
                var meshPart = new MeshPart();
                meshPart.id            = part.id;
                meshPart.primitiveType = part.primitiveType;
                meshPart.offset        = offset;
                meshPart.size          = part.indices.Length;
                meshPart.mesh          = mesh;
                //mesh.getIndicesBuffer().put(part.indices);

                // todo: optimize
                list.AddRange(part.indices.ToList());

                offset += meshPart.size;


                meshParts.Add(meshPart);
            }

            mesh.setIndices(list.ToArray());

            foreach (var part in meshParts)
            {
                part.update();
            }
        }
示例#11
0
        public void SetVertex(ushort inIndex, VertexAttributes inAttributes)
#endif // EXPANDED_REFS
        {
            CheckDisposed();

            if (inIndex >= m_VertexCount)
            {
                throw new ArgumentOutOfRangeException("inIndex");
            }

            UnsafeSetVertex(inIndex, inAttributes);
            m_Dirty = true;
        }
示例#12
0
        public void Load(byte[] file, uint address, uint imageBase)
        {
            VertexAttribute attrib = new VertexAttribute(file, address, imageBase);

            while (attrib.Attribute != GXVertexAttribute.Null + 8)
            {
                VertexAttributes.Add(attrib);

                object attrib_data = GetVertexData(file, attrib);
                if (attrib_data != null)
                {
                    SetAttributeData(attrib.Attribute, attrib_data);
                }

                address += 16;
                attrib   = new VertexAttribute(file, address, imageBase);
            }
        }
        protected override void OnInit(double time)
        {
            State.CheckError();

            TriangleShaderProgram = ShaderProgram.Create("TriangleShaderProgram");
            State.CheckError();

            using (var vert = Content.LoadShader("TriangleVertex.vert", GFX.ShaderType.Vertex))
            {
                using (var frag = Content.LoadShader("TriangleVertex.frag", GFX.ShaderType.Fragment))
                {
                    TriangleShaderProgram.AttachShaders(vert, frag);
                    TriangleShaderProgram.Link();
                    TriangleShaderProgram.DetachShaders(vert, frag);
                }
            }
            State.CheckError();

            TriangleUniformTextureSampler = TriangleShaderProgram.GetUniformLocation("textureSampler");
            State.CheckError();

            TriangleVertexBuffer = Buffer <Vector3f> .Create(
                "TriangleVertexBuffer", 3, BufferStorageFlags.MapReadBit | BufferStorageFlags.MapWriteBit
                );

            State.CheckError();
            unsafe
            {
                var ptr = (Vector3f *)TriangleVertexBuffer.Map(BufferAccess.WriteOnly);
                State.CheckError();
                ptr[0] = new Vector3f(-1f, 1f, 0f);
                ptr[1] = new Vector3f(1f, 1f, 0f);
                ptr[2] = new Vector3f(0f, -1f, 0f);
                TriangleVertexBuffer.Unmap();
                State.CheckError();
            }

            TriangleVertexAttributes = VertexAttributes.Create("TriangleVertexAttributes");
            TriangleVertexAttributes.SetVertexAttributeEnabled(0, true);
            TriangleVertexAttributes.SetVertexAttributeLayout(0, "Position", GFX.AttributeType.Vector3, false, 0, 0, TriangleVertexBuffer);
            State.CheckError();

            TriangleTexture = Content.LoadTexture2D("rainbow.png");
        }
示例#14
0
            public DynamicVertexAttributeArrays(VertexAttributes attributes, int capacity)
            {
                _capacity = capacity;

                if ((attributes & VertexAttributes.Position) != 0)
                {
                    positions = new Vector3[capacity];
                }
                if ((attributes & VertexAttributes.Normal) != 0)
                {
                    normals = new Vector3[capacity];
                }
                if ((attributes & VertexAttributes.Color) != 0)
                {
                    colors = new Color[capacity];
                }
                if ((attributes & VertexAttributes.Color32) != 0)
                {
                    colors32 = new Color32[capacity];
                }
                if ((attributes & VertexAttributes.UV) != 0)
                {
                    uvs = new Vector2[capacity];
                }
                if ((attributes & VertexAttributes.UV2) != 0)
                {
                    uvs2 = new Vector2[capacity];
                }
                if ((attributes & VertexAttributes.UV3) != 0)
                {
                    uvs3 = new Vector2[capacity];
                }
                if ((attributes & VertexAttributes.UV4) != 0)
                {
                    uvs4 = new Vector2[capacity];
                }
                if ((attributes & VertexAttributes.Tangent) != 0)
                {
                    tangents = new Vector4[capacity];
                }
            }
示例#15
0
        private int[] getAttributeLocations(VertexAttributes attrs)
        {
            int n = attrs.size();

            if (tempArray.Length != n)
            {
                tempArray = new int[n];                               // todo: maybe implement intarray properly ?
            }
            for (int i = 0; i < n; i++)
            {
                var key = attrs[i].getKey();
                var tmp = -1;
                if (attributes.ContainsKey(key))
                {
                    tmp = attributes[key];
                }

                tempArray[i] = tmp;
            }
            return(tempArray);
        }
示例#16
0
        private void WriteDataBankOffset(EndianBinaryWriter writer, VertexAttributes attribute)
        {
            int bankOffset = 0;

            #region Offset switch
            switch (attribute)
            {
            case VertexAttributes.Position:
                bankOffset = 0;
                break;

            case VertexAttributes.Normal:
                bankOffset = 1;
                break;

            case VertexAttributes.Color0:
                bankOffset = 3;
                break;

            case VertexAttributes.Color1:
                bankOffset = 4;
                break;

            case VertexAttributes.Tex0:
                bankOffset = 5;
                break;

            case VertexAttributes.Tex1:
                bankOffset = 6;
                break;

            case VertexAttributes.Tex2:
                bankOffset = 7;
                break;

            case VertexAttributes.Tex3:
                bankOffset = 8;
                break;

            case VertexAttributes.Tex4:
                bankOffset = 9;
                break;

            case VertexAttributes.Tex5:
                bankOffset = 10;
                break;

            case VertexAttributes.Tex6:
                bankOffset = 11;
                break;

            case VertexAttributes.Tex7:
                bankOffset = 12;
                break;
            }
            #endregion

            writer.Seek(0xC + (4 * bankOffset), System.IO.SeekOrigin.Begin);
            writer.Write((int)writer.BaseStream.Length);
            writer.Seek(0, System.IO.SeekOrigin.End);
        }
示例#17
0
        public void init(ShaderProgram program, Renderable renderable)
        {
            if (locations != null)
            {
                throw new Exception("Already initialized");
            }
            if (!program.isCompiled_())
            {
                throw new Exception(program.getLog());
            }
            this.program = program;

            int n = uniforms.Count;

            locations = new int[n];
            for (int i = 0; i < n; i++)
            {
                string    input     = uniforms[i];
                Validator validator = validators[i];
                Setter    setter    = setters[i];
                if (validator != null && !validator.validate(this, i, renderable))
                {
                    locations[i] = -1;
                }
                else
                {
                    locations[i] = program.fetchUniformLocation(input, false);
                    if (locations[i] >= 0 && setter != null)
                    {
                        if (setter.isGlobal(this, i))
                        {
                            globalUniforms.Add(i);
                        }
                        else
                        {
                            localUniforms.Add(i);
                        }
                    }
                }

                if (locations[i] < 0)
                {
                    validators[i] = null;
                    setters[i]    = null;
                }
            }

            if (renderable != null)
            {
                VertexAttributes attrs = renderable.meshPart.mesh.getVertexAttributes();
                int c = attrs.size();
                for (int i = 0; i < c; i++)
                {
                    VertexAttribute attr     = attrs[i];
                    int             location = program.getAttributeLocation(attr.alias);
                    if (location >= 0)
                    {
                        attributes.Add(attr.getKey(), location);
                    }
                }
            }
        }
        /// <summary>
        /// Allocate and fill the requested attribute arrays.
        /// </summary>
        /// <remarks>
        /// If you are using this function to rebuild a mesh, use SetMesh instead. SetMesh handles setting null arrays where appropriate for you.
        /// </remarks>
        /// <seealso cref="SetMesh"/>
        /// <param name="vertices">The source vertices.</param>
        /// <param name="position">A new array of the vertex position values if requested by the attributes parameter, or null.</param>
        /// <param name="color">A new array of the vertex color values if requested by the attributes parameter, or null.</param>
        /// <param name="uv0">A new array of the vertex uv0 values if requested by the attributes parameter, or null.</param>
        /// <param name="normal">A new array of the vertex normal values if requested by the attributes parameter, or null.</param>
        /// <param name="tangent">A new array of the vertex tangent values if requested by the attributes parameter, or null.</param>
        /// <param name="uv2">A new array of the vertex uv2 values if requested by the attributes parameter, or null.</param>
        /// <param name="uv3">A new array of the vertex uv3 values if requested by the attributes parameter, or null.</param>
        /// <param name="uv4">A new array of the vertex uv4 values if requested by the attributes parameter, or null.</param>
        /// <param name="attributes">A flag with the MeshAttributes requested.</param>
        /// <seealso cref="HasArrays"/>
        public static void GetArrays(
            IList <Vertex> vertices,
            out Vector3[] position,
            out Color[] color,
            out Vector2[] uv0,
            out Vector3[] normal,
            out Vector4[] tangent,
            out Vector2[] uv2,
            out List <Vector4> uv3,
            out List <Vector4> uv4,
            VertexAttributes attributes)
        {
            if (vertices == null)
            {
                throw new ArgumentNullException("vertices");
            }

            int vc    = vertices.Count;
            var first = vc < 1 ? new Vertex() : vertices[0];

            bool hasPosition = ((attributes & VertexAttributes.Position) == VertexAttributes.Position) && first.hasPosition;
            bool hasColor    = ((attributes & VertexAttributes.Color) == VertexAttributes.Color) && first.hasColor;
            bool hasUv0      = ((attributes & VertexAttributes.Texture0) == VertexAttributes.Texture0) && first.hasUV0;
            bool hasNormal   = ((attributes & VertexAttributes.Normal) == VertexAttributes.Normal) && first.hasNormal;
            bool hasTangent  = ((attributes & VertexAttributes.Tangent) == VertexAttributes.Tangent) && first.hasTangent;
            bool hasUv2      = ((attributes & VertexAttributes.Texture1) == VertexAttributes.Texture1) && first.hasUV2;
            bool hasUv3      = ((attributes & VertexAttributes.Texture2) == VertexAttributes.Texture2) && first.hasUV3;
            bool hasUv4      = ((attributes & VertexAttributes.Texture3) == VertexAttributes.Texture3) && first.hasUV4;

            position = hasPosition ? new Vector3[vc] : null;
            color    = hasColor ? new Color[vc] : null;
            uv0      = hasUv0 ? new Vector2[vc] : null;
            normal   = hasNormal ? new Vector3[vc] : null;
            tangent  = hasTangent ? new Vector4[vc] : null;
            uv2      = hasUv2 ? new Vector2[vc] : null;
            uv3      = hasUv3 ? new List <Vector4>(vc) : null;
            uv4      = hasUv4 ? new List <Vector4>(vc) : null;

            for (int i = 0; i < vc; i++)
            {
                if (hasPosition)
                {
                    position[i] = vertices[i].position;
                }
                if (hasColor)
                {
                    color[i] = vertices[i].color;
                }
                if (hasUv0)
                {
                    uv0[i] = vertices[i].uv0;
                }
                if (hasNormal)
                {
                    normal[i] = vertices[i].normal;
                }
                if (hasTangent)
                {
                    tangent[i] = vertices[i].tangent;
                }
                if (hasUv2)
                {
                    uv2[i] = vertices[i].uv2;
                }
                if (hasUv3)
                {
                    uv3.Add(vertices[i].uv3);
                }
                if (hasUv4)
                {
                    uv4.Add(vertices[i].uv4);
                }
            }
        }
示例#19
0
        private void WriteAttribute(EndianBinaryWriter writer, VertexAttributes attribute)
        {
            writer.Write((int)attribute);

            // Component count. Normals use 0, all others use 1
            if (attribute == VertexAttributes.Normal)
            {
                writer.Write((int)0);
            }
            else
            {
                writer.Write((int)1);
            }

            switch (attribute)
            {
            case VertexAttributes.Position:
                writer.Write((int)PositionType);
                if (PositionType == DataTypes.S16)
                {
                    writer.Write((byte)PositionFractionalBitVal);
                }
                else
                {
                    writer.Write((byte)0);
                }
                break;

            case VertexAttributes.Normal:
                writer.Write((int)NormalType);
                if (NormalType == DataTypes.S16)
                {
                    writer.Write((byte)NormalFractionalBitVal);
                }
                else
                {
                    writer.Write((byte)0);
                }
                break;

            case VertexAttributes.Tex0:
            case VertexAttributes.Tex1:
            case VertexAttributes.Tex2:
            case VertexAttributes.Tex3:
            case VertexAttributes.Tex4:
            case VertexAttributes.Tex5:
            case VertexAttributes.Tex6:
            case VertexAttributes.Tex7:
                writer.Write((int)UVType);
                if (UVType == DataTypes.S16)
                {
                    writer.Write((byte)UVFractionalBitVal);
                }
                else
                {
                    writer.Write((byte)0);
                }
                break;

            case VertexAttributes.Color0:
            case VertexAttributes.Color1:
                writer.Write((int)ColorType);
                writer.Write((byte)0);
                break;

            case VertexAttributes.NullAttr:
                writer.Write((int)0);
                writer.Write((byte)0);
                break;
            }

            writer.Write((byte)0xFF);     // Padding part 1
            writer.Write((ushort)0xFFFF); // Padding part 2
        }
示例#20
0
 /// <summary>
 /// Find if a vertex attribute has been set.
 /// </summary>
 /// <param name="attribute">The attribute or attributes to test for.</param>
 /// <returns>True if this vertex has the specified attributes set, false if they are default values.</returns>
 public bool HasArrays(VertexAttributes attribute)
 {
     return((m_Attributes & attribute) == attribute);
 }