void GenerateDispFaceObject (int index, int model) { surface f = BuildDispFace (index, model, map.facesLump[index].dispinfo); GameObject faceObject = new GameObject ("DispFace: " + f.index); MeshRenderer mr = faceObject.AddComponent<MeshRenderer> (); mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided; //Making model shadows two-sided for better quality. //Material mat; MeshFilter mf = faceObject.AddComponent<MeshFilter> (); mf.sharedMesh = new Mesh (); mf.sharedMesh.name = "DispFace " + f.index; mf.sharedMesh.vertices = f.points; mf.sharedMesh.triangles = f.triangles; mf.sharedMesh.uv = f.uv; mf.sharedMesh.colors32 = f.cols; if (uSrcSettings.Inst.textures) { bsptexdata curTexData = map.texdataLump[map.texinfosLump[map.facesLump[index].texinfo].texdata]; //===========================Material============================= string materialName = ""; //VMTLoader.VMTFile vmtFile=null; //Material tempmat=null; //string if (map.texdataStringDataLump.Length > map.texdataStringTableLump[curTexData.nameStringTableID] + 92) materialName = new string (map.texdataStringDataLump, map.texdataStringTableLump[curTexData.nameStringTableID], 92); else materialName = new string (map.texdataStringDataLump, map.texdataStringTableLump[curTexData.nameStringTableID], map.texdataStringDataLump.Length - map.texdataStringTableLump[curTexData.nameStringTableID]); materialName = materialName.ToLower (); if (materialName.Contains ("\0")) materialName = materialName.Remove (materialName.IndexOf ('\0')); Material tempmat = ResourceManager.Inst.GetMaterial (materialName); //=================Material End======================== mr.material = tempmat; } else { mr.material = Test.Inst.testMaterial; } mf.sharedMesh.RecalculateNormals (); faceObject.transform.parent = dispObject.transform; var o_835_3_636690536766835854 = mf.sharedMesh; faceObject.isStatic = true; if (uSrcSettings.Inst.genColliders) { faceObject.AddComponent<MeshCollider> (); } }
/*bspplane[] ReadPlanes() * { * br.BaseStream.Seek (header.lumps [SourceBSPStructs.LUMP_PLANES].fileofs, SeekOrigin.Begin); * int numPlanes = header.lumps [SourceBSPStructs.LUMP_PLANES].filelen / 20; * bspplane[] temp = new bspplane[numPlanes]; * for(int i=0;i<numPlanes;i++) * { * bspplane plane = new bspplane(); * plane.normal = ConvertUtils.ReadVector(br); * plane.dist = br.ReadSingle (); * plane.type = br.ReadInt32 (); * temp[i] = plane; * } * tempLog+=("Load: "+numPlanes+" Planes \n"); * return temp; * }*/ bsptexdata[] ReadTexData() { br.BaseStream.Seek(header.lumps [SourceBSPStructs.LUMP_TEXDATA].fileofs, SeekOrigin.Begin); int numTexData = header.lumps [SourceBSPStructs.LUMP_TEXDATA].filelen / 32; bsptexdata[] temp = new bsptexdata[numTexData]; for (int i = 0; i < numTexData; i++) { bsptexdata texdata = new bsptexdata(); texdata.reflectivity = ConvertUtils.ReadVector3(br); texdata.nameStringTableID = br.ReadInt32(); texdata.width = br.ReadInt32(); texdata.height = br.ReadInt32(); texdata.view_width = br.ReadInt32(); texdata.view_height = br.ReadInt32(); temp[i] = texdata; } tempLog += ("Load: " + numTexData + " TextureData's \n"); return(temp); }
surface BuildDispFace(int faceIndex, int model, short dispinfoId) { Vector3[] vertices = new Vector3[4]; List <Vector3> disp_verts = new List <Vector3>(); List <Vector2> UVs = new List <Vector2>(); List <Color32> cols = new List <Color32>(); List <int> indices = new List <int>(); bspface curFace = map.facesLump[faceIndex]; bsptexinfo curTexInfo = map.texinfosLump[curFace.texinfo]; bsptexdata curTexData = map.texdataLump[curTexInfo.texdata]; int fEdge = curFace.firstedge; for (int i = 0; i < curFace.numedges; i++) { vertices[i] = (map.surfedgesLump[fEdge + i] > 0 ? map.vertexesLump[map.edgesLump[Mathf.Abs(map.surfedgesLump[fEdge + i])][0]] : map.vertexesLump[map.edgesLump[Mathf.Abs(map.surfedgesLump[fEdge + i])][1]]); } bspdispinfo curDisp = map.dispinfoLump [dispinfoId]; Vector3 startPos = curDisp.startPosition; float dist; float minDist = 0.1f; int minIndex = 0; for (int i = 0; i < 4; i++) { dist = Vector3.Distance(startPos, vertices[i]); if (dist < minDist) { minDist = dist; minIndex = i; } } Vector3 temp; for (int i = 0; i < minIndex; i++) { temp = vertices[0]; vertices[0] = vertices[1]; vertices[1] = vertices[2]; vertices[2] = vertices[3]; vertices[3] = temp; } Vector3 leftEdge = vertices[1] - vertices[0]; Vector3 rightEdge = vertices[2] - vertices[3]; int numEdgeVertices = (1 << curDisp.power) + 1; float subdivideScale = 1.0f / (float)(numEdgeVertices - 1); Vector3 leftEdgeStep = leftEdge * subdivideScale; Vector3 rightEdgeStep = rightEdge * subdivideScale; int firstVertex = 0; Vector3 leftEnd; Vector3 rightEnd; Vector3 leftRightSeg; Vector3 leftRightStep; int dispVertIndex; bspDispVert dispVert; Vector3 flatVertex; Vector3 dispVertex; float scaleU = (float)1f / curTexData.width; float scaleV = (float)1f / curTexData.height; for (int i = 0; i < numEdgeVertices; i++) { leftEnd = leftEdgeStep * (float)i; leftEnd += vertices[0]; rightEnd = rightEdgeStep * (float)i; rightEnd += vertices[3]; leftRightSeg = rightEnd - leftEnd; leftRightStep = leftRightSeg * subdivideScale; for (int j = 0; j < numEdgeVertices; j++) { dispVertIndex = curDisp.DispVertStart; dispVertIndex += i * numEdgeVertices + j; dispVert = map.dispVertsLump[dispVertIndex]; flatVertex = leftEnd + (leftRightStep * (float)j); dispVertex = dispVert.vec * (dispVert.dist /* *scale*/); dispVertex += flatVertex; disp_verts.Add(dispVertex); float tU = Vector3.Dot(flatVertex, curTexInfo.texvecs) + (curTexInfo.texoffs); float tV = Vector3.Dot(flatVertex, curTexInfo.texvect) + (curTexInfo.texofft); UVs.Add(new Vector2(tU * scaleU, tV * scaleV)); cols.Add(new Color32((byte)(dispVert.alpha), 0, 0, 0)); } } int curIndex; for (int i = 0; i < numEdgeVertices - 1; i++) { for (int j = 0; j < numEdgeVertices - 1; j++) { curIndex = i * numEdgeVertices + j; if ((curIndex % 2) == 1) { curIndex += firstVertex; indices.Add(curIndex + 1); indices.Add(curIndex); indices.Add(curIndex + numEdgeVertices); indices.Add(curIndex + numEdgeVertices + 1); indices.Add(curIndex + 1); indices.Add(curIndex + numEdgeVertices); } else { curIndex += firstVertex; indices.Add(curIndex); indices.Add(curIndex + numEdgeVertices); indices.Add(curIndex + numEdgeVertices + 1); indices.Add(curIndex + 1); indices.Add(curIndex); indices.Add(curIndex + numEdgeVertices + 1); } } } for (int i = 0; i < disp_verts.Count; i++) { disp_verts[i] *= uSrcSettings.Inst.worldScale; } surface f = new surface(); f.index = faceIndex; //f.flags = flags; f.dispinfo = dispinfoId; f.points = disp_verts.ToArray(); f.uv = UVs.ToArray(); //f.uv2 = UV2s; f.cols = cols.ToArray(); f.triangles = indices.ToArray(); //f.lightMapW = lightmapW; //f.lightMapH = lightmapH; return(f); }
GameObject GenerateFaceObject(int index) { //List<Vector3> verts=new List<Vector3>(); //List<Vector2> UVs=new List<Vector2>(); //List<Vector2> lightmapUV=new List<Vector2>(); //List<int> tris = new List<int>(); surface f = BuildFace(index); //f.index = index; //if((f.flags & 4) == 4) // return; //tris.AddRange (f.triangles); //verts.AddRange (f.points); //UVs.AddRange (f.uv); //lightmapUV.AddRange(f.uv2); GameObject faceObject = new GameObject("Face: " + index); MeshRenderer mr = faceObject.AddComponent <MeshRenderer>(); //Material mat; MeshFilter mf = faceObject.AddComponent <MeshFilter>(); mf.sharedMesh = new Mesh(); mf.sharedMesh.name = "BSPFace " + index; mf.sharedMesh.vertices = f.points; mf.sharedMesh.triangles = f.triangles; mf.sharedMesh.uv = f.uv; //=======================//????????? /* * Vector4[] tang = new Vector4[verts.Count]; * for (int i=0; i<verts.Count; i++) * tang [i] = new Vector4 (0, 0, 1); * * mf.mesh.tangents = tang; */ //======================== if (uSrcSettings.Inst.textures) { bsptexdata curTexData = map.texdataLump[map.texinfosLump[map.facesLump[index].texinfo].texdata]; //===========================Material============================= string materialName = ""; //VMTLoader.VMTFile vmtFile=null; //Material tempmat=null; //string if (map.texdataStringDataLump.Length > map.texdataStringTableLump [curTexData.nameStringTableID] + 92) { materialName = new string (map.texdataStringDataLump, map.texdataStringTableLump [curTexData.nameStringTableID], 92); } else { materialName = new string (map.texdataStringDataLump, map.texdataStringTableLump [curTexData.nameStringTableID], map.texdataStringDataLump.Length - map.texdataStringTableLump [curTexData.nameStringTableID]); } materialName = materialName.ToLower(); if (materialName.Contains("\0")) { materialName = materialName.Remove(materialName.IndexOf('\0')); } //return VMTLoader.GetMaterial (materialName); Material tempmat = ResourceManager.Inst.GetMaterial(materialName); //=================Material End======================== //if (tempmat.name.Contains ("trigger")||tempmat.name.Contains ("fogvolume")||tempmat.name.Contains ("tools/toolsskybox")) // mr.enabled = false; mr.material = tempmat; if (uSrcSettings.Inst.lightmaps && map.hasLightmaps) // & !vmtFile.translucent) { //Texture2D lightMap = CreateLightmapTex (f); mf.sharedMesh.uv2 = f.uv2; mr.lightmapIndex = 0; } } else { mr.material = Test.Inst.testMaterial; } mf.sharedMesh.RecalculateNormals(); //mf.mesh.RecalculateBounds (); faceObject.transform.parent = modelsObject.transform; mf.sharedMesh.Optimize(); faceObject.isStatic = true; return(faceObject); }
void GenerateDispFaceObject(int index, int model) { face f = BuildDispFace(index, model, map.facesLump[index].dispinfo); GameObject faceObject = new GameObject("DispFace: " + f.index); MeshRenderer mr = faceObject.AddComponent <MeshRenderer>(); //Material mat; MeshFilter mf = faceObject.AddComponent <MeshFilter>(); mf.mesh = new Mesh(); mf.mesh.name = "DispFace " + f.index; mf.mesh.vertices = f.points; mf.mesh.triangles = f.triangles; mf.mesh.uv = f.uv; if (uSrcSettings.Inst.textures) { bsptexdata curTexData = map.texdataLump[map.texinfosLump[map.facesLump[index].texinfo].texdata]; //===========================Material============================= string materialName = ""; //VMTLoader.VMTFile vmtFile=null; //Material tempmat=null; //string if (map.texdataStringDataLump.Length > map.texdataStringTableLump [curTexData.nameStringTableID] + 92) { materialName = new string (map.texdataStringDataLump, map.texdataStringTableLump [curTexData.nameStringTableID], 92); } else { materialName = new string (map.texdataStringDataLump, map.texdataStringTableLump [curTexData.nameStringTableID], map.texdataStringDataLump.Length - map.texdataStringTableLump [curTexData.nameStringTableID]); } materialName = materialName.ToLower(); if (materialName.Contains("\0")) { materialName = materialName.Remove(materialName.IndexOf('\0')); } Material tempmat = ResourceManager.Inst.GetMaterial(materialName); //=================Material End======================== mr.material = tempmat; } else { mr.material = Test.Inst.testMaterial; } mf.mesh.RecalculateNormals(); faceObject.transform.parent = dispObject.transform; mf.mesh.Optimize(); faceObject.isStatic = true; if (uSrcSettings.Inst.genColliders) { faceObject.AddComponent <MeshCollider>(); } }