public static byte[] MakePlayerUpdatedBuffer(Vector2 touchWorldPos)
        {
            var builder = new FlatBufferBuilder(DefaultBufferBytes);

            PlayerUpdatePacket.StartPlayerUpdatePacket(builder);

            PlayerUpdatePacket.AddOpcode(builder, (short)Opcode.PlayerUpdate);

            var positionOffset = Vec2.CreateVec2(builder, touchWorldPos.x, touchWorldPos.y);

            PlayerUpdatePacket.AddPosX(builder, touchWorldPos.x);
            PlayerUpdatePacket.AddPosY(builder, touchWorldPos.y);

            var playerUpdatedOffset = PlayerUpdatePacket.EndPlayerUpdatePacket(builder);

            builder.Finish(playerUpdatedOffset.Value);

            return(builder.SizedByteArray());
        }
    public byte[] ConstructClientInputState(ClientMessageType type, ushort clientId, uint tick, Vector3 desiredMove, bool isTurbo)
    {
        fbBuilder.Clear();


        ClientInputState.StartClientInputState(fbBuilder);
        ClientInputState.AddTick(fbBuilder, tick);
        ClientInputState.AddDesiredMove(fbBuilder, Vec2.CreateVec2(fbBuilder, desiredMove.x, desiredMove.y));
        ClientInputState.AddIsTurbo(fbBuilder, isTurbo);
        Offset <ClientInputState> clientInputStateOffset = ClientInputState.EndClientInputState(fbBuilder);

        ClientMessage.StartClientMessage(fbBuilder);
        //var clientIdOffset = clientId;
        ClientMessage.AddClientId(fbBuilder, clientId);
        ClientMessage.AddMsgType(fbBuilder, type);
        ClientMessage.AddMsg(fbBuilder, clientInputStateOffset.Value);
        Offset <ClientMessage> clientMessageOffset = ClientMessage.EndClientMessage(fbBuilder);

        ClientMessage.FinishClientMessageBuffer(fbBuilder, clientMessageOffset);

        return(fbBuilder.SizedByteArray());
    }
        public static ByteBuffer Save(UnityEngine.Material material)
        {
            FlatBufferBuilder builder = new FlatBufferBuilder(InitBufferSize);

            int count = ShaderUtil.GetPropertyCount(material.shader);
            List <Offset <ShaderProperty> > listOffShaderProperties = new List <Offset <ShaderProperty> >();

            for (int i = 0; i < count; i++)
            {
                string name = ShaderUtil.GetPropertyName(material.shader, i);
                ShaderUtil.ShaderPropertyType type      = ShaderUtil.GetPropertyType(material.shader, i);
                Schema.ShaderPropertyValue    valueType = ShaderPropertyValue.NONE;
                int valueOffset = -1;
                switch (type)
                {
                case ShaderUtil.ShaderPropertyType.Color:
                {
                    UnityEngine.Color c = material.GetColor(name);
                    ShaderPropertyColor.StartShaderPropertyColor(builder);
                    ShaderPropertyColor.AddColor(builder, Schema.Color.CreateColor(builder, c.a, c.b, c.g, c.r));
                    Offset <ShaderPropertyColor> offset = ShaderPropertyColor.EndShaderPropertyColor(builder);
                    valueType   = ShaderPropertyValue.ShaderPropertyColor;
                    valueOffset = offset.Value;
                }
                break;

                case ShaderUtil.ShaderPropertyType.Vector:
                {
                    Vector4 v = material.GetVector(name);
                    ShaderPropertyVector.StartShaderPropertyVector(builder);
                    ShaderPropertyVector.AddVector(builder, Schema.Vec4.CreateVec4(builder, v.x, v.y, v.z, v.w));
                    Offset <ShaderPropertyVector> offset = ShaderPropertyVector.EndShaderPropertyVector(builder);
                    valueType   = ShaderPropertyValue.ShaderPropertyVector;
                    valueOffset = offset.Value;
                }
                break;

                case ShaderUtil.ShaderPropertyType.Range:
                case ShaderUtil.ShaderPropertyType.Float:
                {
                    float f = material.GetFloat(name);
                    ShaderPropertyFloat.StartShaderPropertyFloat(builder);
                    ShaderPropertyFloat.AddValue(builder, f);
                    Offset <ShaderPropertyFloat> offset = ShaderPropertyFloat.EndShaderPropertyFloat(builder);
                    valueType   = ShaderPropertyValue.ShaderPropertyFloat;
                    valueOffset = offset.Value;
                }
                break;

                case ShaderUtil.ShaderPropertyType.TexEnv:
                {
                    UnityEngine.Texture t = material.GetTexture(name);
                    string textureName    = "$NULL_TEXTURE";
                    if (t != null)
                    {
                        textureName = AssetDatabase.GetAssetPath(t.GetInstanceID());
                        if (string.IsNullOrEmpty(textureName))
                        {
                            textureName = t.name;
                        }
                        else
                        {
                            textureName = textureName.Substring(ArtWork.path.Length);
                            textureName = System.IO.Path.GetDirectoryName(textureName) + "/" + System.IO.Path.GetFileNameWithoutExtension(textureName);
                        }
                    }
                    Vector2 toffset = material.GetTextureOffset(name);
                    Vector2 tscale  = material.GetTextureScale(name);

                    StringOffset pathOffset = builder.CreateString(textureName);
                    ShaderPropertyTexture.StartShaderPropertyTexture(builder);
                    ShaderPropertyTexture.AddName(builder, pathOffset);
                    ShaderPropertyTexture.AddOffset(builder, Vec2.CreateVec2(builder, toffset.x, toffset.y));
                    ShaderPropertyTexture.AddScale(builder, Vec2.CreateVec2(builder, tscale.x, tscale.y));
                    Offset <ShaderPropertyTexture> offset = ShaderPropertyTexture.EndShaderPropertyTexture(builder);
                    valueType   = ShaderPropertyValue.ShaderPropertyTexture;
                    valueOffset = offset.Value;
                }
                break;
                }

                if (valueOffset >= 0)
                {
                    listOffShaderProperties.Add(
                        ShaderProperty.CreateShaderProperty(
                            builder, builder.CreateString(name), (Schema.ShaderPropertyType)type, valueType, valueOffset
                            ));
                }
            }
            StringOffset             offMaterialName = builder.CreateString(material.name);
            StringOffset             offShader       = builder.CreateString(material.shader.name);
            Offset <Schema.Material> offMaterial     = Schema.Material.CreateMaterial(
                builder, offMaterialName, offShader, Schema.Material.CreatePropertiesVector(builder, listOffShaderProperties.ToArray()));

            builder.Finish(offMaterial.Value);
            return(builder.DataBuffer);
        }
Exemple #4
0
        public static ByteBuffer Save(UnityEngine.Mesh mesh, UnityEngine.Transform[] bones)
        {
            FlatBufferBuilder builder = new FlatBufferBuilder(InitBufferSize);

            // 创建顶点成分
            MeshComponent[] components =
            {
                // vertices
                new MeshComponent(
                    () =>   { return(true);                                                        },
                    () =>   { Schema.Mesh.StartVerticesVector(builder, mesh.vertexCount);          },
                    (i) =>  { Vec3.CreateVec3(builder, mesh.vertices[i].x, mesh.vertices[i].y, mesh.vertices[i].z);},
                    (vo) => { Schema.Mesh.AddVertices(builder, vo);                                }
                    ),
                // uv
                new MeshComponent(
                    () =>   { return((mesh.uv != null) && (mesh.uv.Length > 0));                   },
                    () =>   { Schema.Mesh.StartUvVector(builder, mesh.vertexCount);                },
                    (i) =>  { Vec2.CreateVec2(builder, mesh.uv[i].x, mesh.uv[i].y);                },
                    (vo) => { Schema.Mesh.AddUv(builder, vo);                                      }
                    ),
                // uv2
                new MeshComponent(
                    () =>   { return((mesh.uv2 != null) && (mesh.uv2.Length > 0));                 },
                    () =>   { Schema.Mesh.StartUv2Vector(builder, mesh.vertexCount);               },
                    (i) =>  { Vec2.CreateVec2(builder, mesh.uv2[i].x, mesh.uv2[i].y);              },
                    (vo) => { Schema.Mesh.AddUv2(builder, vo);                                     }
                    ),
                // uv3
                new MeshComponent(
                    () =>   { return((mesh.uv3 != null) && (mesh.uv3.Length > 0));                 },
                    () =>   { Schema.Mesh.StartUv3Vector(builder, mesh.vertexCount);               },
                    (i) =>  { Vec2.CreateVec2(builder, mesh.uv3[i].x, mesh.uv3[i].y);              },
                    (vo) => { Schema.Mesh.AddUv3(builder, vo);                                     }
                    ),
                // uv4
                new MeshComponent(
                    () =>   { return((mesh.uv4 != null) && (mesh.uv4.Length > 0));                 },
                    () =>   { Schema.Mesh.StartUv4Vector(builder, mesh.vertexCount);               },
                    (i) =>  { Vec2.CreateVec2(builder, mesh.uv4[i].x, mesh.uv4[i].y);              },
                    (vo) => { Schema.Mesh.AddUv4(builder, vo);                                     }
                    ),
                // normals
                new MeshComponent(
                    () =>   { return((mesh.normals != null) && (mesh.normals.Length > 0));         },
                    () =>   { Schema.Mesh.StartNormalsVector(builder, mesh.vertexCount);           },
                    (i) =>  { Vec3.CreateVec3(builder, mesh.normals[i].x, mesh.normals[i].y, mesh.normals[i].z);},
                    (vo) => { Schema.Mesh.AddNormals(builder, vo);                                 }
                    ),
                // tangents
                new MeshComponent(
                    () =>   { return((mesh.tangents != null) && (mesh.tangents.Length > 0));       },
                    () =>   { Schema.Mesh.StartTangentsVector(builder, mesh.vertexCount);          },
                    (i) =>  { Vec4.CreateVec4(builder, mesh.tangents[i].x, mesh.tangents[i].y, mesh.tangents[i].z, mesh.tangents[i].w);},
                    (vo) => { Schema.Mesh.AddTangents(builder, vo);                                }
                    ),
                // colors
                new MeshComponent(
                    () =>   { return((mesh.colors != null) && (mesh.colors.Length > 0));           },
                    () =>   { Schema.Mesh.StartColorsVector(builder, mesh.vertexCount);            },
                    (i) =>  { Schema.Color.CreateColor(builder, mesh.colors[i].a, mesh.colors[i].b, mesh.colors[i].g, mesh.colors[i].r);},
                    (vo) => { Schema.Mesh.AddColors(builder, vo);                                  }
                    ),
                // colors32
                new MeshComponent(
                    () =>   { return((mesh.colors32 != null) && (mesh.colors.Length > 0));         },
                    () =>   { Schema.Mesh.StartColors32Vector(builder, mesh.vertexCount);          },
                    (i) =>  { Schema.Color32.CreateColor32(builder, mesh.colors32[i].a, mesh.colors32[i].b, mesh.colors32[i].g, mesh.colors32[i].r);},
                    (vo) => { Schema.Mesh.AddColors32(builder, vo);                                }
                    ),
                // BoneWeigths
                new MeshComponent(
                    () =>   { return((mesh.boneWeights != null) && (mesh.boneWeights.Length > 0)); },
                    () =>   { Schema.Mesh.StartBoneWeightsVector(builder, mesh.vertexCount);       },
                    (i) =>  { Schema.BoneWeight.CreateBoneWeight(builder,
                                                                 mesh.boneWeights[i].boneIndex0, mesh.boneWeights[i].boneIndex1, mesh.boneWeights[i].boneIndex2, mesh.boneWeights[i].boneIndex3,
                                                                 mesh.boneWeights[i].weight0, mesh.boneWeights[i].weight1, mesh.boneWeights[i].weight2, mesh.boneWeights[i].weight3
                                                                 ); },
                    (vo) => { Schema.Mesh.AddBoneWeights(builder, vo);                             }
                    )
            };

            int componentCount = components.Length;

            VectorOffset[] vectorOffsets = new VectorOffset[componentCount];
            for (int c = 0; c < componentCount; c++)
            {
                if (components[c].funcExist())
                {
                    components[c].actStart();
                    for (int i = mesh.vertexCount - 1; i >= 0; i--) // 添加时顺序颠倒。
                    {
                        components[c].actElem(i);                   // 结构vector不需要AddOffset
                    }
                    vectorOffsets[c] = builder.EndVector();
                }
            }

            // 创建 Submesh
            Offset <SubMesh>[] offSubmeshes = new Offset <SubMesh> [mesh.subMeshCount];
            uint offset = 0;

            for (int i = 0; i < mesh.subMeshCount; i++)
            {
                int[] _is = mesh.GetIndices(i);
                offSubmeshes[i] = SubMesh.CreateSubMesh(builder, offset, (uint)_is.Length, (Schema.MeshTopology)mesh.GetTopology(i));
                offset         += (uint)_is.Length;
            }
            // 网格所涉及到的骨骼
            StringOffset[] offJoints = new StringOffset[bones.Length];
            for (int i = 0; i < bones.Length; i++)
            {
                offJoints[i] = builder.CreateString(bones[i].name);
            }
            // BindPoses
            Schema.Mesh.StartBindposesVector(builder, mesh.bindposes.Length);
            for (int i = mesh.bindposes.Length - 1; i >= 0; i--)
            {
                Matrix4x4 m = mesh.bindposes[i];
                Matrix16.CreateMatrix16(builder,
                                        m.m00, m.m01, m.m02, m.m03,
                                        m.m10, m.m11, m.m12, m.m13,
                                        m.m20, m.m21, m.m22, m.m23,
                                        m.m30, m.m31, m.m32, m.m33
                                        );
            }
            VectorOffset vecBindPoses = builder.EndVector();

            // 三角面索引数据  标量数组可以直接添加。
            VectorOffset vecTrangles = Schema.Mesh.CreateTrianglesVector(builder, mesh.triangles);
            // Submesh 数据
            VectorOffset vecSubmeshes = Schema.Mesh.CreateSubmeshesVector(builder, offSubmeshes);
            // 涉及到的骨骼
            VectorOffset vecJoints = Schema.Mesh.CreateJointsVector(builder, offJoints);

            // 放入Mesh
            Schema.Mesh.StartMesh(builder);
            // 顶点数据
            for (int c = 0; c < componentCount; c++)
            {
                if (components[c].funcExist())
                {
                    components[c].actAdd(vectorOffsets[c]);
                }
            }
            Schema.Mesh.AddTriangles(builder, vecTrangles);
            Schema.Mesh.AddSubmeshes(builder, vecSubmeshes);
            Schema.Mesh.AddJoints(builder, vecJoints);
            Schema.Mesh.AddBindposes(builder, vecBindPoses);

            // 边界
            Vector3 min = mesh.bounds.min;
            Vector3 max = mesh.bounds.max;

            Schema.Mesh.AddBound(builder, Schema.Bound.CreateBound(builder, min.x, min.y, min.z, max.x, max.y, max.z));
            Offset <Schema.Mesh> offMesh = Schema.Mesh.EndMesh(builder);

            builder.Finish(offMesh.Value);
            return(builder.DataBuffer);
        }
Exemple #5
0
 private Offset <Vec2> FromPositionVector(FlatBufferBuilder builder, Vector2 vector)
 {
     return(Vec2.CreateVec2(builder, (short)vector.X, (short)vector.Y));
 }