Beispiel #1
0
 //---------------------------------------------------------------------------	CTOR
 public CByteArray(string sNameBlenderModule, string sBlenderAccessString)
 {
     _sBlenderAccessString = sBlenderAccessString;
     _memBA = new CMemAlloc<byte>();
     CGame.gBL_SendCmd_GetMemBuffer(sNameBlenderModule, sBlenderAccessString, ref _memBA);      //###DESIGN#11: Always force Unity to access everything from Blender through CBody?? ###SOON
     _oBA = (byte[])_memBA.L;					// Obtain convenience pointer to beginning of raw memory obtained from Blender
     _nLength = _memBA.L.Length;
     CheckMagicNumber_Begin();					// Ensure stream starts with proper magic number
 }
Beispiel #2
0
    public virtual void UpdateVertsFromBlenderMesh(bool bUpdateNormals)
    {
        // Ask Blender to update our copy of the verts.  Assumes the topology of the mesh hasn't changed!
        if (_bConnectedToBlenderMesh == false)       //###NOW### ###BROKEN??
            CUtility.ThrowException("Exception in CBMesh.UpdateVertsFromBlenderMesh().  Mesh is not exported / shared from Blender!");
        CGame.gBL_SendCmd("Client", "gBL_UpdateClientVerts('" + _sNameBlenderMesh + "')");

        //###BUG!!! (Fixed with hack)  For some reason we can't get an update on our own buffer!  WTF?  Hack is to create a temp pinned array, get updated results and manually copy array to its source = WTF crap!!
        //###IDEA: Is it possible it is because each vert is an object and we replaced these object's reference from their original pinned array?? (Verify this with pointer addresses!)
        //int nError = ErosEngine.gBL_UpdateClientVerts(_sNameBlenderMesh, _memVerts.P);
        //if (nError != 0)
        //    CUtility.ThrowException("Exception in CBMesh.gBL_UpdateClientVerts().  DLL returns error " + nError + " on mesh " + gameObject.name);

        //###HACK: Create temporary pinned array of the same size, get updated results, manually copy to where the results should go!
        CMemAlloc<Vector3> memVertsCopy = new CMemAlloc<Vector3>(_memVerts.L.Length);
        int nError = ErosEngine.gBL_UpdateClientVerts(_sNameBlenderMesh, memVertsCopy.P);
        if (nError != 0)
            CUtility.ThrowException("Exception in CBMesh.gBL_UpdateClientVerts().  DLL returns error " + nError + " on mesh " + gameObject.name);
        _memVerts.L = (Vector3[])memVertsCopy.L.Clone();      //###CHECK: Will screw up pin??  Copy each vert by value??
        memVertsCopy = null;

        _oMeshNow.MarkDynamic();		// Docs say "Call this before assigning vertices to get better performance when continually updating mesh"
        _oMeshNow.vertices = _memVerts.L;       //###CHECK: This ok/needed with next call??
        CopyOriginalVertsToVerts(true);
        if (bUpdateNormals)
            UpdateNormals();								// Verts have moved, update * fix normals...
    }
Beispiel #3
0
 public static int gBL_SendCmd_GetMemBuffer(string sModuleList, string sCmd, ref CMemAlloc<byte> mem)
 {
     //####SOON ####IMPROVE: Can get rid of damn import list by importing everything once at game init??
     string sCmdFull = "__import__(" + sModuleList + ")." + sCmd;
     IntPtr pSizeInBuf = ErosEngine.gBL_Cmd(sCmdFull, true);
     int nSizeByteBuf = pSizeInBuf.ToInt32();
     if (nSizeByteBuf <= 0)
         CUtility.ThrowException("gBL_SendCmd_GetMemBuffer() could not obtain valid byte buffer from gBL_Cmd()!");
     mem.Allocate(nSizeByteBuf);
     ErosEngine.gBL_Cmd_GetLastInBuf(mem.P, nSizeByteBuf);
     return nSizeByteBuf;
 }
Beispiel #4
0
    public virtual void OnDeserializeFromBlender()
    {
        //===== Very important top-level call to begin the process of deserializing a mesh from Blender.  Central to all our meshes! =====
        //=== Send blender command to obtain the mesh header.  This will return a large stream containing material names, bones, bone weight, etc ===
        CByteArray oBA = new CByteArray("'CBody'", _oBodyBase._sBlenderInstancePath_CBodyBase + _sNameCBodyInstanceMember + ".Unity_GetMesh()");

        //=== Create various casts to subclasses to facilitate deserializing info into the proper subclasses ===
        bool bCreatingSkinnedMesh = ((this as CBSkin) != null);     // We create a skinned mesh if we're a CBSkin or related rim... otherwise we're (probably) a body part requiring softbody/cloth simulation so we're just a simple mesh
        _bConnectedToBlenderMesh = true;							// Mark this mesh as having a valid C++ connection to Blender

        //=== Read the number of verts and tris.  Must be < 64K for Unity! ===
        int nVerts = oBA.ReadInt();
        int nTris  = oBA.ReadInt();
        byte nMats = oBA.ReadByte();
        Debug.Log("CBMesh obtains mesh '" + _sNameCBodyInstanceMember + "' for body '" + _oBodyBase._nBodyID + "' with " + nVerts + " verts " + nTris + " triangles and " + nMats + " materials.");

        if (nVerts > 65530)
            CUtility.ThrowException("ERROR in CBMesh.ctor().  Mesh has over 64K verts: " + nVerts);

        //=== Create the new mesh / skinned mesh now that we have the beginning of a valid stream ===
        MeshFilter   		oMeshFilter 	= null;
        MeshRenderer 		oMeshRenderer 	= null;
        if (bCreatingSkinnedMesh == false) {
            oMeshFilter 	= (MeshFilter)  CUtility.FindOrCreateComponent(transform, typeof(MeshFilter));
            oMeshRenderer	= (MeshRenderer)CUtility.FindOrCreateComponent(transform, typeof(MeshRenderer));
        }
        _oMeshNow = new Mesh();				// Create the actual mesh the object will use.  Will persist throughout...
        //_oMeshNow.MarkDynamic();			//###CHECK: Use by default or case-by-case basis in subclasses??    ###OPT!

        if (GetComponent<Collider>() != null)
            Destroy(GetComponent<Collider>());                      //###LEARN: Hugely expensive mesh collider created by the above lines... turn it off!

        //===== READ THE MATERIALS =====
        //=== Read the material list from Blender and create the necessary materials linking to the provided texture images ===
        _aMats = new Material[nMats];
        for (byte nMat = 0; nMat < nMats; nMat++) {
            string sCodedMaterial = oBA.ReadString();
            sCodedMaterial = sCodedMaterial.Replace('\\', '/');						// Replace backslashes to the slash Unity requires.
            if (sCodedMaterial != "NoTexture") {			//###IMPROVE: What to do??  ###IMPROVE: Standarize constants between blender and Unity!

                if (sCodedMaterial.StartsWith("Material_")) {			// Blender material that start with this string are processed differently: We attempt to find the same name material here and attach to this submesh.

                    Material oMat = Resources.Load("Materials/" + sCodedMaterial, typeof(Material)) as Material;
                    if (oMat != null)
                        _aMats[nMat] = oMat;
                    else
                        Debug.LogWarning("ERROR: Unknown special material '" + sCodedMaterial + "' in CBMesh.ctor()");

                } else {

                    int nLocTexPath = sCodedMaterial.IndexOf(CGame.C_RelPath_Textures);		// Blender gives a fully-qualified path that MUST point to our Resources folder
                    if (nLocTexPath != -1) {
                        string sTextureResourcePath = sCodedMaterial.Substring(nLocTexPath + CGame.C_RelPath_Textures.Length);
                        int nLocExtDot = sTextureResourcePath.IndexOf('.');			// Strip the extension... ###CHECK: Assume jpg throughout??
                        if (nLocExtDot != -1)
                            sTextureResourcePath = sTextureResourcePath.Substring(0, nLocExtDot);

                        Material oMat = Resources.Load(sTextureResourcePath + "_Mat", typeof(Material)) as Material;		// We attempt to load the pre-defined material with _Mat suffix if its there...
                        if (oMat == null) {
                            if (sTextureResourcePath.EndsWith("_Transp"))			// If texture ends with this string we create a standard transparent material
                                oMat = new Material(Shader.Find("Transparent/Diffuse"));
                            else
                                oMat = new Material(Shader.Find("Diffuse"));		// If material was not found (usual case) we just create a standard diffuse on
                            //Debug.Log("-Texture: " + sTextureResourcePath);
                            object o = Resources.Load(sTextureResourcePath, typeof(Texture));
                            Texture oTex = o as Texture;
                            if (oTex != null) {
                                int nLocLastSlash = sTextureResourcePath.LastIndexOf('/');
                                oMat.name = sTextureResourcePath.Substring(nLocLastSlash + 1);
                                oMat.mainTexture = oTex;
                            } else {
                                Debug.LogError("**ERROR: Texture '" + sTextureResourcePath + "' not found!");
                            }
                        } else {
                            //Debug.Log("-Material: " + sTextureResourcePath);			// If material was defined we leave it to material designer to connect whatever texture...
                        }
                        _aMats[nMat] = oMat;
                    } else {
                        Debug.LogError("ERROR: in CBMesh.ctor().  Could not find Unity resource path in texture path " + sCodedMaterial);
                    }
                }
            }
        }

        if (nMats == 0) {					// If Blender didn't have a material for this mesh we now create a dummy one as we need at least one 'submesh' for DLL to pass in triangles to us ===
            nMats = 1;
            _aMats = new Material[nMats];
            _aMats[0] = new Material(Shader.Find("Diffuse"));	// Create a default material so we can see the material-less Blender mesh in Unity
        }
        _oMeshNow.subMeshCount = nMats;
        oBA.CheckMagicNumber_End();                 // Check the 'end magic number' that always follows a stream.

        //===== CREATE THE MESH =====
        //=== Create the memory buffers that can extract the data from our C++ dll ===
        CMemAlloc<Vector3> memVerts 	= new CMemAlloc<Vector3>();
        CMemAlloc<Vector3> memNormals 	= new CMemAlloc<Vector3>();		//###DESIGN! ###OPT!! We don't use Blender's normals anymore... keep serializing??
        CMemAlloc<Vector2> memUVs 		= new CMemAlloc<Vector2>();
        memVerts  .Allocate(nVerts);
        memNormals.Allocate(nVerts);
        memUVs    .Allocate(nVerts);

        //=== With the header data process, the GetMeshHeader command also copied the large verts & tri arrays in shared memory for us to access quickly ===
        int nError = ErosEngine.gBL_GetMeshArrays(_sNameBlenderMesh, nMats, memVerts.P, memNormals.P, memUVs.P);
        if (nError != 0)
            CUtility.ThrowException("Exception in CBMesh.ctor().  gBL_GetMeshArrays('" + _sNameBlenderMesh + "') returns error " + nError + " on mesh " + gameObject.name);

        _oMeshNow.vertices 	= memVerts.L;
        _oMeshNow.normals 	= memNormals.L;
        _oMeshNow.uv 		= memUVs.L;

        //=== Create separation of mesh by material ===
        for (int nMat = 0; nMat < nMats; nMat++) {
            int nTrisThisMat = ErosEngine.gBL_GetNumTrianglesAtMaterial(nMat);			//###HACK: These are 'last access' only!
            if (nTrisThisMat > 0) {								//###IMPROVE: Issue warning when no materials?  (So Blender mesh can clean up its unused materials)?
                CMemAlloc<int> memTris = new CMemAlloc<int>();
                memTris.Allocate(3 * nTrisThisMat);				// Number of triangles = 3 * number of triangles
                ErosEngine.gBL_GetTrianglesAtMaterial(nMat, memTris.P);
                _oMeshNow.SetTriangles(memTris.L, nMat);
            }
        }

        //=== Read the 'shared normals' flattened map ===
        _aMapSharedNormals = CByteArray.GetArray_USHORT("'CBody'", _oBodyBase._sBlenderInstancePath_CBodyBase + _sNameCBodyInstanceMember + ".aMapSharedNormals.Unity_GetBytes()");

        //=== Finalize the mesh creation by stuffing _oMeshRender into mesh filter or skinned mesh renderer as appropriate ===
        if (bCreatingSkinnedMesh == false) {
            UpdateNormals();									// Fix the normals with the just-serialized map of shared normals
            _oMeshNow.RecalculateBounds();
            oMeshRenderer.materials = _aMats;
            oMeshFilter.mesh = _oMeshNow;
        }

        _memVerts		.AssignAndPin(_oMeshNow.vertices);
        _memVertsStart	.AssignAndPin(_oMeshNow.vertices);
        _memNormals		.AssignAndPin(_oMeshNow.normals);
        _memTris		.AssignAndPin(_oMeshNow.triangles);

        //=== Mesh is loaded in Unity.  Close link to Blender to save memory unless maintaining the link is requested by base class ===
        if (_bKeepBlenderShare == false)
            ReleaseBlenderMesh();
    }