Exemplo n.º 1
0
 void ISerializableObject.Read(EndianBinaryReader reader, object context)
 {
     Type     = reader.ReadUInt16();
     Rotation = new AngleVector(reader.ReadUInt16(), reader.ReadUInt16(), reader.ReadUInt16());
     Position = reader.ReadVector3();
     Value1   = reader.ReadSingle();
     Value2   = reader.ReadSingle();
     Value3   = reader.ReadSingle();
 }
Exemplo n.º 2
0
        private static Node ConvertNode(Assimp.Node aiNode, List <Assimp.Mesh> aiMeshes, List <MaterialBuildInfo> materialBuildInfos)
        {
            Node ConvertHierarchyNodeRecursively(Assimp.Node curAiNode, ref Node previousSibling, Node parent, ref Assimp.Matrix4x4 parentNodeWorldTransform)
            {
                var nodeWorldTransform        = curAiNode.Transform * parentNodeWorldTransform;
                var nodeInverseWorldTransform = nodeWorldTransform;

                nodeInverseWorldTransform.Inverse();

                curAiNode.Transform.Decompose(out var scale, out var rotation, out var translation);

                // Create node
                var node = new Node(AssimpHelper.FromAssimp(translation), AngleVector.FromQuaternion(AssimpHelper.FromAssimp(rotation)),
                                    AssimpHelper.FromAssimp(scale), parent);

                if (curAiNode.HasMeshes)
                {
                    var geometry = new Geometry();

                    // Convert meshes
                    var vertexPositions = new List <Assimp.Vector3D>();
                    var vertexNormals   = new List <Assimp.Vector3D>();
                    var vertexUVs       = new List <Assimp.Vector3D>();
                    var vertexColors    = new List <Assimp.Color4D>();
                    var lastRenderState = new MeshRenderState();

                    foreach (var aiMeshIndex in curAiNode.MeshIndices)
                    {
                        var aiMesh      = aiMeshes[aiMeshIndex];
                        var material    = materialBuildInfos[aiMesh.MaterialIndex];
                        var mesh        = new Mesh();
                        var renderState = new MeshRenderState();

                        renderState.IndexFlags = IndexAttributeFlags.HasPosition | IndexAttributeFlags.Position16BitIndex;
                        var useColors  = false;
                        var hasColors  = aiMesh.HasVertexColors(0);
                        var hasUVs     = aiMesh.HasTextureCoords(0);
                        var hasNormals = aiMesh.HasNormals;

                        if (hasColors || !hasNormals)
                        {
                            renderState.IndexFlags |= IndexAttributeFlags.HasColor | IndexAttributeFlags.Color16BitIndex;
                            useColors = true;
                        }
                        else
                        {
                            renderState.IndexFlags |= IndexAttributeFlags.HasNormal | IndexAttributeFlags.Normal16BitIndex;
                        }

                        if (hasUVs)
                        {
                            renderState.IndexFlags |= IndexAttributeFlags.HasUV | IndexAttributeFlags.UV16BitIndex;
                        }

                        // Convert faces
                        var triangleIndices = new Index[aiMesh.FaceCount * 3];
                        for (var i = 0; i < aiMesh.Faces.Count; i++)
                        {
                            var aiFace = aiMesh.Faces[i];
                            Debug.Assert(aiFace.IndexCount == 3);

                            for (var j = 0; j < aiFace.Indices.Count; j++)
                            {
                                int aiFaceIndex = aiFace.Indices[j];

                                var position      = aiMesh.Vertices[aiFaceIndex];
                                var positionIndex = vertexPositions.IndexOf(position);
                                if (positionIndex == -1)
                                {
                                    positionIndex = vertexPositions.Count;
                                    vertexPositions.Add(position);
                                }

                                var normalIndex = 0;
                                var colorIndex  = 0;
                                var uvIndex     = 0;

                                if (useColors)
                                {
                                    var color = hasColors ? aiMesh.VertexColorChannels[0][aiFaceIndex] : new Assimp.Color4D();
                                    colorIndex = vertexColors.IndexOf(color);
                                    if (colorIndex == -1)
                                    {
                                        colorIndex = vertexColors.Count;
                                        vertexColors.Add(color);
                                    }
                                }
                                else
                                {
                                    var normal = aiMesh.Normals[aiFaceIndex];
                                    normalIndex = vertexNormals.IndexOf(normal);
                                    if (normalIndex == -1)
                                    {
                                        normalIndex = vertexNormals.Count;
                                        vertexNormals.Add(normal);
                                    }
                                }

                                if (hasUVs)
                                {
                                    var uv = aiMesh.TextureCoordinateChannels[0][aiFaceIndex];
                                    uvIndex = vertexUVs.IndexOf(uv);
                                    if (uvIndex == -1)
                                    {
                                        uvIndex = vertexUVs.Count;
                                        vertexUVs.Add(uv);
                                    }
                                }

                                triangleIndices[(i * 3) + j] = new Index
                                {
                                    PositionIndex = ( ushort )positionIndex,
                                    NormalIndex   = ( ushort )normalIndex,
                                    ColorIndex    = ( ushort )colorIndex,
                                    UVIndex       = ( ushort )uvIndex
                                };
                            }
                        }

                        // Build display list
                        var displayList = new GXDisplayList(GXPrimitive.Triangles, triangleIndices);
                        mesh.DisplayLists.Add(displayList);

                        // Set up render params
                        var indexFlagsParam = new IndexAttributeFlagsParam(renderState.IndexFlags);
                        mesh.Parameters.Add(indexFlagsParam);

                        if (useColors)
                        {
                            mesh.Parameters.Add(new LightingParams(LightingParams.Preset.Colors));
                        }
                        else
                        {
                            mesh.Parameters.Add(new LightingParams(LightingParams.Preset.Normals));
                        }

                        mesh.Parameters.Add(new TextureParams(( ushort )(material.TextureId)));
                        mesh.Parameters.Add(new MipMapParams());
                        geometry.OpaqueMeshes.Add(mesh);
                    }

                    // Build vertex buffers
                    if (vertexPositions.Count > 0)
                    {
                        geometry.VertexBuffers.Add(new VertexPositionBuffer(vertexPositions.Select(x =>
                        {
                            Assimp.Unmanaged.AssimpLibrary.Instance.TransformVecByMatrix4(ref x, ref nodeInverseWorldTransform);
                            return(AssimpHelper.FromAssimp(x));
                        }).ToArray()));
                    }

                    if (vertexNormals.Count > 0)
                    {
                        nodeInverseWorldTransform.Transpose();

                        geometry.VertexBuffers.Add(new VertexNormalBuffer(vertexNormals.Select(x =>
                        {
                            Assimp.Unmanaged.AssimpLibrary.Instance.TransformVecByMatrix4(ref x, ref nodeInverseWorldTransform);
                            return(AssimpHelper.FromAssimp(x));
                        }).ToArray()));
                    }

                    if (vertexColors.Count > 0)
                    {
                        geometry.VertexBuffers.Add(new VertexColorBuffer(vertexColors.Select(AssimpHelper.FromAssimp).ToArray()));
                    }

                    if (vertexUVs.Count > 0)
                    {
                        geometry.VertexBuffers.Add(new VertexUVBuffer(vertexUVs.Select(x =>
                                                                                       UVCodec.Encode1023(AssimpHelper
                                                                                                          .FromAssimpAsVector2(x)))
                                                                      .ToArray()));
                    }
                }

                // Set sibling (next) reference of previous
                if (previousSibling != null)
                {
                    previousSibling.Sibling = node;
                }

                previousSibling = node;

                if (curAiNode.HasChildren)
                {
                    Node childPreviousSibling = null;
                    foreach (var aiChildNode in curAiNode.Children)
                    {
                        var childNode = ConvertHierarchyNodeRecursively(aiChildNode, ref childPreviousSibling, node, ref nodeWorldTransform);

                        // Make sure to set the 'first child' reference if we haven't already
                        if (node.Child == null)
                        {
                            node.Child = childNode;
                        }
                    }
                }

                return(node);
            }

            // Dummy!
            Node dummy    = null;
            var  identity = Assimp.Matrix4x4.Identity;

            return(ConvertHierarchyNodeRecursively(aiNode, ref dummy, null, ref identity));
        }
Exemplo n.º 3
0
        public void Update()
        {
            //if (Screen == null)
            //{
            if (Main.newKeyState.IsKeyDown(Keys.Up) && !Main.oldKeyState.IsKeyDown(Keys.Up))
            {
                screenScale *= 2.0f;
                WindowResize();
            }
            if (Main.newKeyState.IsKeyDown(Keys.Down) && !Main.oldKeyState.IsKeyDown(Keys.Down))
            {
                screenScale *= 0.5f;
                WindowResize();
            }

            if (Main.newKeyState.IsKeyDown(Keys.Enter) && !Main.oldKeyState.IsKeyDown(Keys.Enter))
            {
                terrain.Generate(player.pos, player.size);
            }

            if (Main.newKeyState.IsKeyDown(Keys.K) && !Main.oldKeyState.IsKeyDown(Keys.K))
            {
                entities.Clear();
            }

            if (entities.Count < 10)
            {
                AddEntity(EnemyManager.SpawnEnemy(player.pos + AngleVector.Vector(random.NextDouble() * 360.0) * 500.0f, this));
            }

            player.Move();

            projectiles.RemoveAll(item => item.toRemove);
            entities.RemoveAll(item => item.isDead);

            player.Update();


            foreach (var ent in entities)
            {
                ent.Update();
            }
            for (int i = 0; i < projectiles.Count; i++)
            {
                projectiles[i].Update();
            }


            if (player.target != null && player.target.isDead)
            {
                player.target = null;
            }
            if (Main.newMouseState.LeftButton == ButtonState.Pressed && Main.oldMouseState.LeftButton == ButtonState.Released)
            {
                player.target = getEntityAtPos(posFromScreenPos(Main.newMouseState.Position.ToVector2()));
                //Console.WriteLine("Mouse:" + Main.newMouseState.Position);
            }

            if (Main.newKeyState.IsKeyDown(Keys.Back) && !Main.oldKeyState.IsKeyDown(Keys.Back))
            {
                Main.Handler = new SkillHandler(this, player);
            }
            if (Main.newKeyState.IsKeyDown(Keys.P) && !Main.oldKeyState.IsKeyDown(Keys.P))
            {
                Main.Handler = new PauseHandler(this);
            }

            if (Main.newKeyState.IsKeyDown(Keys.C) && !Main.oldKeyState.IsKeyDown(Keys.C))
            {
                drawDebug = !drawDebug;
            }

            //}
            //else
            //{
            //	Screen.Update(this);
            //}
        }