Esempio 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);
        }
Esempio n. 2
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();
            }
        }