Ejemplo n.º 1
0
        public static DrawIndexedData CreateDrawIndexedData(MeshInfo meshInfo, int submeshIndex)
        {
            DrawIndexedData drawIndexedData = new DrawIndexedData();

            if (submeshIndex >= meshInfo.submeshs.Count)
            {
                return(drawIndexedData);
            }

            drawIndexedData.id         = _drawIndexedIDCount;
            drawIndexedData.indexCount = meshInfo.submeshs[submeshIndex].indexCount;
            drawIndexedData.firstIndex = meshInfo.submeshs[submeshIndex].firstIndex;

            drawIndexedData.instanceCount = 1;
            drawIndexedData.firstInstance = 0;

            if (!_drawIndexedDataCache.ContainsKey(meshInfo.id))
            {
                _drawIndexedDataCache.Add(meshInfo.id, new Dictionary <int, DrawIndexedData>());
            }

            _drawIndexedDataCache[meshInfo.id][submeshIndex] = drawIndexedData;
            _drawIndexedIDCount++;

            return(drawIndexedData);
        }
Ejemplo n.º 2
0
 public static bool GetMeshInfoFromCache(int cacheID, out MeshInfo result)
 {
     if (_meshInfoCache.ContainsKey(cacheID))
     {
         result = _meshInfoCache[cacheID];
         return(true);
     }
     result = null;
     return(false);
 }
Ejemplo n.º 3
0
 public static DrawIndexedData GetOrCreateDrawIndexedData(MeshInfo meshInfo, int submeshIndex)
 {
     if (_drawIndexedDataCache.ContainsKey(meshInfo.id) && _drawIndexedDataCache[meshInfo.id].ContainsKey(submeshIndex))
     {
         return(_drawIndexedDataCache[meshInfo.id][submeshIndex]);
     }
     else
     {
         return(CreateDrawIndexedData(meshInfo, submeshIndex));
     }
 }
Ejemplo n.º 4
0
 public static VertexIndexDrawData GetOrCreateVertexIndexDrawData(MeshInfo meshInfo)
 {
     if (_vertexIndexDrawDataCache.ContainsKey(meshInfo.id))
     {
         return(_vertexIndexDrawDataCache[meshInfo.id]);
     }
     else
     {
         return(CreateVertexIndexDrawData(meshInfo));
     }
 }
Ejemplo n.º 5
0
 public static IndexBufferData GetOrCreateIndexBufferData(MeshInfo meshInfo)
 {
     if (_indexBufferDataCache.ContainsKey(meshInfo.id))
     {
         return(_indexBufferDataCache[meshInfo.id]);
     }
     else
     {
         return(CreateIndexBufferData(meshInfo));
     }
 }
Ejemplo n.º 6
0
        public static IndexBufferData CreateIndexBufferData(MeshInfo meshInfo)
        {
            IndexBufferData indexBufferData = new IndexBufferData();

            indexBufferData.id               = meshInfo.id;
            indexBufferData.triangles        = meshInfo.triangles;
            indexBufferData.use32BitIndicies = meshInfo.use32BitIndicies;

            _indexBufferDataCache[meshInfo.id] = indexBufferData;

            return(indexBufferData);
        }
Ejemplo n.º 7
0
        public static VertexBuffersData CreateVertexBuffersData(MeshInfo meshInfo)
        {
            VertexBuffersData vertexBuffers = new VertexBuffersData();

            vertexBuffers.id = meshInfo.id;

            vertexBuffers.verticies = meshInfo.verticies;
            vertexBuffers.normals   = meshInfo.normals;
            vertexBuffers.tangents  = meshInfo.tangents;
            vertexBuffers.colors    = meshInfo.colors;
            vertexBuffers.uv0       = meshInfo.uv0;
            vertexBuffers.uv1       = meshInfo.uv1;

            _vertexBuffersDataCache[meshInfo.id] = vertexBuffers;

            return(vertexBuffers);
        }
Ejemplo n.º 8
0
        public static VertexIndexDrawData CreateVertexIndexDrawData(MeshInfo meshInfo)
        {
            VertexIndexDrawData vertexIndexDrawData = new VertexIndexDrawData();

            vertexIndexDrawData.id               = meshInfo.id;
            vertexIndexDrawData.triangles        = meshInfo.triangles;
            vertexIndexDrawData.use32BitIndicies = meshInfo.use32BitIndicies;

            vertexIndexDrawData.verticies = meshInfo.verticies;
            vertexIndexDrawData.normals   = meshInfo.normals;
            vertexIndexDrawData.tangents  = meshInfo.tangents;
            vertexIndexDrawData.colors    = meshInfo.colors;
            vertexIndexDrawData.uv0       = meshInfo.uv0;
            vertexIndexDrawData.uv1       = meshInfo.uv1;

            _vertexIndexDrawDataCache[meshInfo.id] = vertexIndexDrawData;

            return(vertexIndexDrawData);
        }
Ejemplo n.º 9
0
        public static MeshInfo CreateMeshInfo(Mesh mesh)
        {
            MeshInfo meshinfo = new MeshInfo();

            meshinfo.id = mesh.GetInstanceID();

            int[] triangles = mesh.triangles;
            CoordSytemConverter.FlipTriangleFaces(triangles);

            meshinfo.triangles        = NativeUtils.WrapArray(triangles);
            meshinfo.use32BitIndicies = mesh.indexFormat == IndexFormat.UInt32 ? 1 : 0;

            for (int i = 0; i < mesh.subMeshCount; i++)
            {
                MeshInfo.SubMesh submesh = new MeshInfo.SubMesh
                {
                    firstIndex = mesh.GetIndexStart(i),
                    indexCount = mesh.GetIndexCount(i)
                };
                meshinfo.submeshs.Add(submesh);
            }

            Vector3[] vertices = mesh.vertices;
            CoordSytemConverter.Convert(vertices);
            meshinfo.verticies = NativeUtils.WrapArray(vertices);

            Vector3[] normals = mesh.normals;
            CoordSytemConverter.Convert(normals);
            meshinfo.normals = NativeUtils.WrapArray(normals);

            Vector4[] tangents = mesh.tangents;
            CoordSytemConverter.Convert(tangents);
            meshinfo.tangents = NativeUtils.WrapArray(tangents);

            meshinfo.colors = NativeUtils.WrapArray(mesh.colors);
            meshinfo.uv0    = NativeUtils.WrapArray(mesh.uv);
            meshinfo.uv1    = NativeUtils.WrapArray(mesh.uv2);

            _meshInfoCache[meshinfo.id] = meshinfo;

            return(meshinfo);
        }
Ejemplo n.º 10
0
 public static void AddMeshInfoToCache(MeshInfo meshInfo, int cacheID)
 {
     _meshInfoCache[cacheID] = meshInfo;
 }
Ejemplo n.º 11
0
        private static void ExportMesh(Mesh mesh, MeshRenderer meshRenderer, Transform gotrans, ExportSettings settings, List <PipelineData> storePipelines = null)
        {
            bool addedCullGroup = false;

            if (settings.autoAddCullNodes)
            {
                CullData culldata = new CullData();
                Vector3  center   = meshRenderer.bounds.center - gotrans.position;
                CoordSytemConverter.Convert(ref center);
                culldata.center = NativeUtils.ToNative(center);
                culldata.radius = meshRenderer.bounds.size.magnitude * 0.5f;
                GraphBuilderInterface.unity2vsg_AddCullGroupNode(culldata);
                addedCullGroup = true;
            }

            //
            Material[] materials = meshRenderer.sharedMaterials;

            if (mesh != null && mesh.isReadable && mesh.vertexCount > 0 && mesh.GetIndexCount(0) > 0)
            {
                int meshid = mesh.GetInstanceID();

                MeshInfo meshInfo = MeshConverter.GetOrCreateMeshInfo(mesh);

                int subMeshCount = mesh.subMeshCount;

                // shader instance id, Material Data, sub mesh indicies
                Dictionary <int, Dictionary <MaterialInfo, List <int> > > meshMaterials = new Dictionary <int, Dictionary <MaterialInfo, List <int> > >();
                for (int matindex = 0; matindex < materials.Length && matindex < subMeshCount; matindex++)
                {
                    Material mat = materials[matindex];
                    if (mat == null)
                    {
                        continue;
                    }

                    MaterialInfo matdata     = MaterialConverter.GetOrCreateMaterialData(mat);
                    int          matshaderid = matdata.shaderStages.id;

                    if (!meshMaterials.ContainsKey(matshaderid))
                    {
                        meshMaterials.Add(matshaderid, new Dictionary <MaterialInfo, List <int> >());
                    }
                    if (!meshMaterials[matshaderid].ContainsKey(matdata))
                    {
                        meshMaterials[matshaderid].Add(matdata, new List <int>());
                    }

                    meshMaterials[matshaderid][matdata].Add(matindex);
                }

                if (subMeshCount > 1)
                {
                    // create mesh data, if the mesh has already been created we only need to pass the ID to the addGeometry function
                    foreach (int shaderkey in meshMaterials.Keys)
                    {
                        List <MaterialInfo> mds = new List <MaterialInfo>(meshMaterials[shaderkey].Keys);

                        if (mds.Count == 0)
                        {
                            continue;
                        }

                        // add stategroup and pipeline for shader
                        GraphBuilderInterface.unity2vsg_AddStateGroupNode();

                        PipelineData pipelineData = NativeUtils.CreatePipelineData(meshInfo); //WE NEED INFO ABOUT THE SHADER SO WE CAN BUILD A PIPLE LINE
                        pipelineData.descriptorBindings = NativeUtils.WrapArray(mds[0].descriptorBindings.ToArray());
                        pipelineData.shaderStages       = mds[0].shaderStages.ToNative();
                        pipelineData.useAlpha           = mds[0].useAlpha;
                        pipelineData.id = NativeUtils.ToNative(NativeUtils.GetIDForPipeline(pipelineData));
                        storePipelines.Add(pipelineData);

                        if (GraphBuilderInterface.unity2vsg_AddBindGraphicsPipelineCommand(pipelineData, 1) == 1)
                        {
                            GraphBuilderInterface.unity2vsg_AddCommandsNode();

                            VertexBuffersData vertexBuffersData = MeshConverter.GetOrCreateVertexBuffersData(meshInfo);
                            GraphBuilderInterface.unity2vsg_AddBindVertexBuffersCommand(vertexBuffersData);

                            IndexBufferData indexBufferData = MeshConverter.GetOrCreateIndexBufferData(meshInfo);
                            GraphBuilderInterface.unity2vsg_AddBindIndexBufferCommand(indexBufferData);


                            foreach (MaterialInfo md in mds)
                            {
                                BindDescriptors(md, false);

                                foreach (int submeshIndex in meshMaterials[shaderkey][md])
                                {
                                    DrawIndexedData drawIndexedData = MeshConverter.GetOrCreateDrawIndexedData(meshInfo, submeshIndex);
                                    GraphBuilderInterface.unity2vsg_AddDrawIndexedCommand(drawIndexedData);
                                }
                            }

                            GraphBuilderInterface.unity2vsg_EndNode(); // step out of commands node for descriptors and draw indexed commands
                        }
                        GraphBuilderInterface.unity2vsg_EndNode();     // step out of stategroup node for shader
                    }
                }
                else
                {
                    List <int> sids = new List <int>(meshMaterials.Keys);
                    if (sids.Count > 0)
                    {
                        List <MaterialInfo> mds = new List <MaterialInfo>(meshMaterials[sids[0]].Keys);

                        if (mds.Count > 0)
                        {
                            // add stategroup and pipeline for shader
                            GraphBuilderInterface.unity2vsg_AddStateGroupNode();

                            PipelineData pipelineData = NativeUtils.CreatePipelineData(meshInfo); //WE NEED INFO ABOUT THE SHADER SO WE CAN BUILD A PIPLE LINE
                            pipelineData.descriptorBindings = NativeUtils.WrapArray(mds[0].descriptorBindings.ToArray());
                            pipelineData.shaderStages       = mds[0].shaderStages.ToNative();
                            pipelineData.useAlpha           = mds[0].useAlpha;
                            pipelineData.id = NativeUtils.ToNative(NativeUtils.GetIDForPipeline(pipelineData));
                            storePipelines.Add(pipelineData);

                            if (GraphBuilderInterface.unity2vsg_AddBindGraphicsPipelineCommand(pipelineData, 1) == 1)
                            {
                                BindDescriptors(mds[0], true);

                                VertexIndexDrawData vertexIndexDrawData = MeshConverter.GetOrCreateVertexIndexDrawData(meshInfo);
                                GraphBuilderInterface.unity2vsg_AddVertexIndexDrawNode(vertexIndexDrawData);

                                GraphBuilderInterface.unity2vsg_EndNode(); // step out of vertex index draw node
                            }
                            GraphBuilderInterface.unity2vsg_EndNode();     // step out of stategroup node
                        }
                    }
                }
            }
            else
            {
                string reason = mesh == null ? "mesh is null." : (!mesh.isReadable ? "mesh '" + mesh.name + "' is not readable. Please enabled read/write in the models import settings." : "mesh '" + mesh.name + "' has an unknown error.");
                NativeLog.WriteLine("ExportMesh: Unable to export mesh for gameobject " + gotrans.gameObject.name + ", " + reason);
            }

            if (addedCullGroup)
            {
                GraphBuilderInterface.unity2vsg_EndNode();
            }
        }
Ejemplo n.º 12
0
        public static TerrainInfo CreateTerrainInfo(Terrain terrain, GraphBuilder.ExportSettings settings)
        {
            TerrainInfo terrainInfo         = new TerrainInfo();
            bool        usingCustomMaterial = false;

            if (terrain.materialType == Terrain.MaterialType.Custom)
            {
                // try and load a shader mapping file to match the custom terrain material
                terrainInfo.shaderMapping = ShaderMappingIO.ReadFromJsonFile(terrain.materialTemplate.shader);
                if (terrainInfo.shaderMapping != null)
                {
                    usingCustomMaterial = true;
                }
            }
            else
            {
                // load the default terrain shader mapping file
                terrainInfo.shaderMapping = ShaderMappingIO.ReadFromJsonFile(ShaderMappingIO.GetPathForShaderMappingFile("DefaultTerrain"));
            }

            if (terrainInfo.shaderMapping == null)
            {
                // no mapping loaded, use a default shader so we can at least export and render the terrain mesh
                NativeLog.WriteLine("GraphBuilder: Failed to load Terrain Shader Mapping file for terrain '" + terrain.name + "'.");
                terrainInfo.shaderMapping = ShaderMappingIO.ReadFromJsonFile(ShaderMappingIO.GetPathForShaderMappingFile("Default"));
                usingCustomMaterial       = true;
                return(null);
            }

            terrainInfo.shaderDefines.Add("VSG_LIGHTING");

            // build mesh
            int samplew = terrain.terrainData.heightmapWidth;
            int sampleh = terrain.terrainData.heightmapHeight;

            int cellw = terrain.terrainData.heightmapWidth - 1;
            int cellh = terrain.terrainData.heightmapHeight - 1;

            Vector3 size = terrain.terrainData.size;

            Vector2 cellsize   = new Vector3(size.x / cellw, size.z / cellh);
            Vector2 uvcellsize = new Vector2(1.0f / cellw, 1.0f / cellh);

            float[,] terrainHeights = terrain.terrainData.GetHeights(0, 0, samplew, sampleh);

            int vertcount = samplew * sampleh;

            Vector3[] verts   = new Vector3[vertcount];
            Vector3[] normals = new Vector3[vertcount];
            Vector2[] uvs     = new Vector2[vertcount];

            int[] indicies = new int[(cellw * cellh) * 6];

            // vertices and UVs
            for (int y = 0; y < samplew; y++)
            {
                for (int x = 0; x < sampleh; x++)
                {
                    verts[y * samplew + x].Set(x * cellsize.x, terrainHeights[y, x] * size.y, y * cellsize.y);
                    normals[y * samplew + x] = terrain.terrainData.GetInterpolatedNormal((float)x / (float)samplew, (float)y / (float)sampleh);
                    uvs[y * samplew + x].Set(x * uvcellsize.x, y * uvcellsize.y);
                }
            }

            CoordSytemConverter.Convert(verts);
            CoordSytemConverter.Convert(normals);

            // triangles
            int index = 0;

            for (int y = 0; y < cellw; y++)
            {
                for (int x = 0; x < cellh; x++)
                {
                    indicies[index++] = (y * samplew) + x;
                    indicies[index++] = ((y + 1) * samplew) + x;
                    indicies[index++] = (y * samplew) + x + 1;

                    indicies[index++] = ((y + 1) * samplew) + x;
                    indicies[index++] = ((y + 1) * samplew) + x + 1;
                    indicies[index++] = (y * samplew) + x + 1;
                }
            }

            CoordSytemConverter.FlipTriangleFaces(indicies);

            // convert to meshinfo
            MeshInfo mesh = null;

            if (!MeshConverter.GetMeshInfoFromCache(terrain.GetInstanceID(), out mesh))
            {
                mesh = new MeshInfo
                {
                    id               = terrain.GetInstanceID(),
                    verticies        = NativeUtils.WrapArray(verts),
                    normals          = NativeUtils.WrapArray(normals),
                    uv0              = NativeUtils.WrapArray(uvs),
                    triangles        = NativeUtils.WrapArray(indicies),
                    use32BitIndicies = 1
                };

                MeshConverter.AddMeshInfoToCache(mesh, terrain.GetInstanceID());
            }
            terrainInfo.terrainMesh = mesh;
            terrainInfo.terrainSize = size;

            // gather material info

            if (!usingCustomMaterial)
            {
                // use standard terrain layers
                TerrainLayer[] layers = terrain.terrainData.terrainLayers;
                for (int i = 0; i < layers.Length; i++)
                {
                    ImageData layerData = TextureConverter.GetOrCreateImageData(layers[i].diffuseTexture);
                    terrainInfo.diffuseTextureDatas.Add(layerData);

                    terrainInfo.diffuseScales.Add(new Vector4(1.0f / layers[i].tileSize.x, 1.0f / layers[i].tileSize.y));
                }

                for (int i = 0; i < terrain.terrainData.alphamapTextureCount; i++)
                {
                    Texture2D srcmask   = terrain.terrainData.GetAlphamapTexture(i);
                    ImageData splatData = TextureConverter.GetOrCreateImageData(srcmask);

                    terrainInfo.maskTextureDatas.Add(splatData);
                }

                if (terrainInfo.diffuseTextureDatas.Count > 0)
                {
                    terrainInfo.descriptorBindings.Add(new VkDescriptorSetLayoutBinding()
                    {
                        binding = 0, descriptorType = VkDescriptorType.VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, stageFlags = VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT, descriptorCount = (uint)terrainInfo.diffuseTextureDatas.Count
                    });
                    terrainInfo.descriptorBindings.Add(new VkDescriptorSetLayoutBinding()
                    {
                        binding = 2, descriptorType = VkDescriptorType.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, stageFlags = VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT, descriptorCount = (uint)terrainInfo.diffuseScales.Count
                    });

                    terrainInfo.shaderConsts.Add(terrainInfo.diffuseTextureDatas.Count);
                    terrainInfo.shaderDefines.Add("VSG_TERRAIN_LAYERS");
                }

                terrainInfo.descriptorBindings.Add(new VkDescriptorSetLayoutBinding()
                {
                    binding = 3, descriptorType = VkDescriptorType.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, stageFlags = VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT, descriptorCount = 1
                });

                if (terrainInfo.maskTextureDatas.Count > 0)
                {
                    terrainInfo.descriptorBindings.Add(new VkDescriptorSetLayoutBinding()
                    {
                        binding = 1, descriptorType = VkDescriptorType.VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, stageFlags = VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT, descriptorCount = (uint)terrainInfo.maskTextureDatas.Count
                    });
                    terrainInfo.shaderConsts.Add(terrainInfo.maskTextureDatas.Count);
                }
            }
            else
            {
                Material customMaterial = terrain.materialTemplate;
                terrainInfo.customMaterial = MaterialConverter.CreateMaterialData(customMaterial, terrainInfo.shaderMapping);
                terrainInfo.customMaterial.customDefines = terrainInfo.shaderDefines;
            }

            return(terrainInfo);
        }