Exemple #1
0
 public void ApplyRightHanded()
 {
     foreach (var panel in this.panels)
     {
         panel.position = FileUtil.ApplyRightHanded(panel.position);
         for (int j = 0; j < 4; j++)
         {
             panel.vertices[j] = FileUtil.ApplyRightHanded(panel.vertices[j]);
         }
     }
 }
Exemple #2
0
    private static void WriteMetaFile(string path,
                                      Vector3 offset, bool toRightHanded)
    {
        BlockGroup blockGroup = EditManager.Instance.Layers[0].GetBlockGroup();
        ModelGroup modelGroup = EditManager.Instance.Layers[0].GetModelGroup();

        Mesh mesh = blockGroup.GetRouteMesh();

        Block[] blocks = blockGroup.GetEnterableBlocks();

        // モデルを見て移動可能かどうかをチェックする
        blocks = blocks.Where(block => {
            var model = modelGroup.GetModel(block.position);
            return(model == null || model.shape.enterable);
        }).ToArray();

        var fieldPanels = new FieldPanelGroup(mesh, blocks, offset);

        fieldPanels.ApplyRoutePath(blockGroup, EditManager.Instance.RoutePath);

        // バウンディングボックス
        Vector3 minpos = mesh.bounds.min + offset;
        Vector3 maxpos = mesh.bounds.max + offset;

        // 座標系反転処理
        if (toRightHanded)
        {
            fieldPanels.ApplyRightHanded();
            minpos = FileUtil.ApplyRightHanded(minpos);
            maxpos = FileUtil.ApplyRightHanded(maxpos);
            float z = minpos.z;
            minpos.z = maxpos.z;
            maxpos.z = z;
        }

        var mem    = new MemoryStream();
        var writer = new BinaryWriter(mem);

        writer.Write("E3MT".ToArray());         // Identifier
        writer.Write(2);                        // Version

        // バウンディングボックス
        writer.Write(minpos);
        writer.Write(maxpos);

        // 移動パネル
        fieldPanels.Write(writer);

        // 当たり判定ブロック
        Block[] colliderBlocks = blockGroup.GetAllBlocks();
        writer.Write(colliderBlocks.Length);
        Vector3 colliderOffset = new Vector3(0.5f, 0.25f, 0.5f);

        foreach (var block in colliderBlocks)
        {
            Vector3 position = block.position;
            if (toRightHanded)
            {
                position = FileUtil.ApplyRightHanded(position);
            }
            position += colliderOffset;
            writer.Write(position);
        }

        // 3Dモデル配置
        Model[] models = modelGroup.GetAllModels();
        writer.Write(models.Length);
        foreach (var model in models)
        {
            Vector3 position = model.position;
            if (toRightHanded)
            {
                position = FileUtil.ApplyRightHanded(position);
            }
            writer.Write(model.shape.id);
            writer.Write(position);
            writer.Write(model.offset);
            writer.Write((float)model.rotation);
            writer.Write(model.shape.scale * model.scale);
        }

        File.WriteAllBytes(path, mem.ToArray());
    }
Exemple #3
0
    private static void WriteModelFile(string path, Vector3 offset, bool toRightHanded)
    {
        Vector3 center = Vector3.zero;
        Vector3 size   = Vector3.zero;

        var vertexData = new List <StaticMeshVertex>();
        var indexData  = new List <int>();

        int numLayers = EditManager.Instance.Layers.Count;

        int[] batchIndexOffset = new int[numLayers];
        int[] batchIndexCount  = new int[numLayers];

        for (int layerId = 0; layerId < numLayers; layerId++)
        {
            var  blockGroup = EditManager.Instance.Layers[layerId].GetBlockGroup();
            Mesh mesh       = blockGroup.GetSurfaceMesh();

            if (layerId == 0)
            {
                center = mesh.bounds.center + offset;
                size   = mesh.bounds.size;
            }


            int[] indices = mesh.GetIndices(0);
            for (int i = 0; i < indices.Length; i++)
            {
                indices[i] += vertexData.Count;
            }

            batchIndexOffset[layerId] = indexData.Count;
            batchIndexCount[layerId]  = indices.Length;

            indexData.AddRange(indices);

            var       vertices  = new StaticMeshVertex[mesh.vertexCount];
            Vector3[] positions = mesh.vertices;
            Vector3[] normals   = mesh.normals;
            Vector2[] texCoords = mesh.uv;
            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].position = positions[i] + offset;
                vertices[i].normal   = normals[i];
                vertices[i].texCoord = texCoords[i];
            }
            if (toRightHanded)
            {
                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i].position = FileUtil.ApplyRightHanded(vertices[i].position);
                    vertices[i].normal   = FileUtil.ApplyRightHanded(vertices[i].normal);
                }
            }
            vertexData.AddRange(vertices);
        }

        if (vertexData.Count > ushort.MaxValue)
        {
            Debug.LogError("Vertex count is over than 65535.");
            return;
        }

        var mem    = new MemoryStream();
        var writer = new BinaryWriter(mem);

        int textureCount  = 0;
        int materialCount = numLayers;
        int batchCount    = numLayers;

        writer.Write(Encoding.UTF8.GetBytes("E3D3"));
        writer.Write(StaticMeshVertex.GetSize());                       // VertexSize
        writer.Write(StaticMeshVertex.GetAttribCount());                // AttribCount

        writer.Write(textureCount);                                     // TextureCount
        writer.Write(materialCount);                                    // MaterialCount
        writer.Write(batchCount);                                       // BatchCount
        writer.Write(0);                                                // BoneCount
        writer.Write(0);                                                // AnimClipCount

        writer.Write(vertexData.Count * StaticMeshVertex.GetSize());    // VertexDataSize
        writer.Write(indexData.Count * 2);                              // IndexDataSize

        writer.Write(center);
        writer.Write(size);

        // AttribInfo
        var attribs = new ShaderAttrib[] {
            new ShaderAttrib(10, 3, 0, 0),              // "a_Position",
            new ShaderAttrib(10, 3, 12, 0),             // "a_Normal",
            new ShaderAttrib(10, 2, 24, 0)              // "a_TexCoord",
        };

        for (int i = 0; i < attribs.Length; i++)
        {
            attribs[i].Write(writer);
        }

        // MaterialInfo
        for (int i = 0; i < numLayers; i++)
        {
            writer.Write(new Color(0.8f, 0.8f, 0.8f, 1.0f));                    // diffuseColor
            writer.Write(new Color(0.4f, 0.4f, 0.4f, 1.0f));                    // ambientColor
            writer.Write(new Color(0.0f, 0.0f, 0.0f, 1.0f));                    // emissionColor
            writer.Write(new Color(0.0f, 0.0f, 0.0f, 1.0f));                    // specularColor
            writer.Write(0.0f);                                                 // shiniess
            writer.Write(-1);                                                   // TextureId0
            writer.Write(-1);                                                   // TextureId1
            writer.Write(-1);                                                   // TextureId2
            writer.Write(-1);                                                   // TextureId3
        }

        // BatchInfo
        for (int i = 0; i < numLayers; i++)
        {
            writer.Write(batchIndexOffset[i]);                  // IndexOffset
            writer.Write(batchIndexCount[i]);                   // IndexCount
            writer.Write(i);                                    // MaterialId
        }

        // Output Vertices
        for (int i = 0; i < vertexData.Count; i++)
        {
            vertexData[i].Write(writer);
        }
        // Output Indeces
        for (int i = 0; i < indexData.Count; i++)
        {
            writer.Write((ushort)indexData[i]);
        }

        writer.Flush();
        mem.Flush();

        File.WriteAllBytes(path, mem.ToArray());
    }