void Start() { group = gameObject.AddComponent <LODGroup>(); LOD[] lods = new LOD[4]; int i = 0; while (i < 4) { PrimitiveType primType = PrimitiveType.Cube; if (i == 1) { primType = PrimitiveType.Capsule; } if (i == 2) { primType = PrimitiveType.Sphere; } if (i == 3) { primType = PrimitiveType.Cylinder; } GameObject go = GameObject.CreatePrimitive(primType); go.transform.parent = gameObject.transform; Renderer[] renderers = new Renderer[1]; renderers[0] = go.renderer; lods[i] = new LOD(1.0F / (i + 1), renderers); i++; } group.SetLODS(lods); group.RecalculateBounds(); }
protected GameObject BuildRiftSegment(int segmentNumber, List <Rigidbody> rigidBodies, List <MeshRenderer> meshRenderers) { GameObject riftSegment = new GameObject(segmentNumber.ToString()); GameObject riftSegmentLOD1 = riftSegment.CreateChild("LOD1").gameObject; GameObject riftSegmentLOD2 = riftSegment.CreateChild("LOD2").gameObject; riftSegment.layer = Globals.LayerNumSolidTerrain; riftSegmentLOD1.layer = Globals.LayerNumScenery; riftSegmentLOD1.layer = Globals.LayerNumScenery; riftSegment.tag = Globals.TagGroundStone; MeshFilter mf = riftSegment.AddComponent <MeshFilter>(); MeshRenderer pr = riftSegment.AddComponent <MeshRenderer>(); pr.sharedMaterial = RiftSegmentMaterial; mf.sharedMesh = RiftSegment; Rigidbody rb = riftSegment.AddComponent <Rigidbody>(); rb.isKinematic = true; rb.detectCollisions = true; #if UNITY_EDITOR //should i create colliders? #else MeshCollider mc = riftSegment.AddComponent <MeshCollider>(); mc.sharedMesh = RiftSegment; #endif rigidBodies.Add(rb); meshRenderers.Add(pr); #if UNITY_EDITOR MeshRenderer lod1Mr = riftSegmentLOD1.AddComponent <MeshRenderer>(); lod1Mr.sharedMaterial = RiftSegmentMaterialLOD1; mf = riftSegmentLOD1.AddComponent <MeshFilter>(); mf.sharedMesh = RiftSegmentLOD1; #else MeshRenderer lod1Mr = riftSegmentLOD1.AddComponent <MeshRenderer>(); lod1Mr.sharedMaterial = RiftSegmentMaterialLOD1; mf = riftSegmentLOD1.AddComponent <MeshFilter>(); mf.sharedMesh = RiftSegmentLOD1; MeshRenderer lod2Mr = riftSegmentLOD2.AddComponent <MeshRenderer>(); lod2Mr.sharedMaterial = RiftSegmentMaterialLOD2; mf = riftSegmentLOD2.AddComponent <MeshFilter>(); mf.sharedMesh = RiftSegmentLOD2; LODGroup lodGroup = riftSegment.AddComponent <LODGroup>(); Renderer[] primaryRenderer = new Renderer [] { pr }; Renderer[] lod1Renderer = new Renderer [] { lod1Mr }; Renderer[] lod2Renderer = new Renderer [] { lod2Mr }; LOD primary = new LOD(Globals.SceneryLODRatioPrimary, new Renderer [] { pr }); LOD lod1 = new LOD(Globals.SceneryLODRatioSecondary, new Renderer [] { lod1Mr }); LOD lod2 = new LOD(Globals.SceneryLODRatioOff, new Renderer [] { lod2Mr }); lodGroup.SetLODS(new LOD [] { primary, lod1, lod2 }); #endif return(riftSegment); }
void AddLODGroupForTransform(Transform target) { LODGroup lodGroup = target.gameObject.AddComponent <LODGroup>(); LOD[] lods = LODChildCount(target); lodGroup.SetLODS(lods); lodGroup.RecalculateBounds(); }
static int SetLODS(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 2); LODGroup obj = LuaScriptMgr.GetNetObject <LODGroup>(L, 1); LOD[] objs0 = LuaScriptMgr.GetArrayObject <LOD>(L, 2); obj.SetLODS(objs0); return(0); }
public void AddIt() { LODGroup lODGroup = this.gameObject.GetComponent <LODGroup>(); LOD[] Lods = new LOD[1]; // Fill up Renderer[] arrays. This is the list of drawables per LOD level Lods[0].renderers = this.GetComponentsInChildren <Renderer>(); // Make it live! lODGroup.SetLODS(Lods); lODGroup.RecalculateBounds(); }
public static void CreateFileForResult(List <Core_Voxel.Result> resultList, Shader[] shaders, string[] shaderKeywords, Vector2[] shaderRemaps, float scale, Vector3 pivot) { var diffuseShader = shaders[0]; for (int index = 0; index < resultList.Count; index++) { var result = resultList[index]; bool lod = result.VoxelModels.Length > 1; bool isRig = !lod && result.IsRigged; int realLodNum = result.IsRigged ? 1 : result.VoxelModels.Length; var root = new GameObject(result.FileName).transform; var meshs = new List <Mesh>(); var materialsMap = new Dictionary <Texture2D, Material[]>(); Transform[] lodRoots = new Transform[realLodNum]; for (int lodIndex = 0; lodIndex < realLodNum; lodIndex++) { var voxelModel = result.VoxelModels[lodIndex]; var model = CreateModelFrom(voxelModel.RootNode, voxelModel.Materials, root, pivot, ref meshs, ref materialsMap, isRig, result.WithAvatar, shaders, shaderKeywords, shaderRemaps, scale); model.name = string.Format("Root{0}", lod ? "_lod " + lodIndex.ToString() : ""); lodRoots[lodIndex] = model; // Rig if (isRig) { Vector3 halfModelSize = voxelModel.ModelSize[0] * 0.5f; halfModelSize.x = Mathf.Floor(halfModelSize.x); halfModelSize.y = Mathf.Floor(halfModelSize.y); halfModelSize.z = Mathf.Floor(halfModelSize.z); var skinMR = model.GetComponent <SkinnedMeshRenderer>(); if (skinMR) { Vector3 rootBoneOffset = halfModelSize * scale; var boneTFList = new List <Transform>(); if (voxelModel.RootBones != null) { for (int i = 0; i < voxelModel.RootBones.Length; i++) { var boneTF = CreateBoneTransform(voxelModel.RootBones[i], model, scale, ref boneTFList); if (boneTF) { boneTF.localPosition -= rootBoneOffset; } } } skinMR.bones = boneTFList.ToArray(); skinMR.rootBone = model; // Bind Poses var poses = new Matrix4x4[boneTFList.Count]; for (int i = 0; i < boneTFList.Count; i++) { poses[i] = boneTFList[i].worldToLocalMatrix * model.localToWorldMatrix; } skinMR.sharedMesh.bindposes = poses; } // Foot Fix model.localPosition = (halfModelSize - voxelModel.FootPoints[lodIndex]) * scale; } } // Lod if (lod) { LODGroup group = root.gameObject.AddComponent <LODGroup>(); LOD[] lods = new LOD[realLodNum]; for (int i = 0; i < realLodNum; i++) { lods[i] = new LOD( i == realLodNum - 1 ? 0.001f : GetLodRant(result.VoxelModels[i].MaxModelBounds, i), lodRoots[i].GetComponentsInChildren <MeshRenderer>(true) ); } #if UNITY_5_0 || UNITY_5_1 || UNITY_4 group.SetLODS(lods); group.RecalculateBounds(); #else group.SetLODs(lods); group.RecalculateBounds(); #endif } else if (!isRig && root.childCount > 0) { var newRoot = root.GetChild(0); newRoot.name = root.name; root = newRoot; } // File string path = Util.CombinePaths( result.ExportRoot, result.ExportSubRoot, result.FileName + result.Extension ); path = Util.FixPath(path); string parentPath = Util.GetParentPath(path); Util.CreateFolder(parentPath); if (result.Extension == ".prefab") { Object prefab; if (Util.FileExists(path)) { prefab = AssetDatabase.LoadAssetAtPath <Object>(path); if (prefab as GameObject) { var group = (prefab as GameObject).GetComponent <LODGroup>(); if (group) { Object.DestroyImmediate(group, true); } } Object[] things = AssetDatabase.LoadAllAssetRepresentationsAtPath(path); foreach (Object o in things) { Object.DestroyImmediate(o, true); } } else { #if UNITY_4 || UNITY_5 || UNITY_2017 || UNITY_2018_1 || UNITY_2018_2 prefab = PrefabUtility.CreateEmptyPrefab(path); #else // 2018.3+ var tempObject = new GameObject(); prefab = PrefabUtility.SaveAsPrefabAsset(tempObject, path); Object.DestroyImmediate(tempObject, false); #endif } if (prefab) { // Assets for (int i = 0; i < meshs.Count; i++) { meshs[i].name = GetIndexedName("Mesh", i, meshs.Count); AssetDatabase.AddObjectToAsset(meshs[i], path); } int currentIndex = 0; foreach (var textureMat in materialsMap) { textureMat.Key.name = GetIndexedName("Texture", currentIndex, materialsMap.Count); AssetDatabase.AddObjectToAsset(textureMat.Key, path); var mats = textureMat.Value; for (int i = 0; i < mats.Length; i++) { mats[i].name = GetIndexedName("Material", currentIndex, materialsMap.Count) + "_" + i.ToString(); AssetDatabase.AddObjectToAsset(mats[i], path); } currentIndex++; } // Avatar if (isRig && result.WithAvatar) { var avatar = GetVoxelAvatarInRoot(root); if (avatar) { avatar.name = result.FileName; AssetDatabase.AddObjectToAsset(avatar, path); // Animator var ani = root.GetComponent <Animator>(); if (!ani) { ani = root.gameObject.AddComponent <Animator>(); } ani.avatar = avatar; } else { Debug.LogWarning("[Voxel to Unity] Failed to get avatar from the prefab. Use \"+ Human Bones\" button in rig editor to create bones and don\'t change their names and layout."); } } // Prefab #if UNITY_4 || UNITY_5 || UNITY_2017 || UNITY_2018_1 || UNITY_2018_2 PrefabUtility.ReplacePrefab(root.gameObject, prefab, ReplacePrefabOptions.ReplaceNameBased); #else // 2018.3+ prefab = PrefabUtility.SaveAsPrefabAsset(root.gameObject, path); #endif } } else // Obj { string objFolderPath = Util.CombinePaths(parentPath, result.FileName); string textureFolderPath = Util.CombinePaths(objFolderPath, "Textures"); Util.CreateFolder(objFolderPath); VoxelPostprocessor.TheShader = diffuseShader; // Assets var model = result.VoxelModels[0]; for (int modelIndex = 0; modelIndex < model.Meshs.Length; modelIndex++) { string modelIndexedName = GetIndexedName(result.FileName, modelIndex, model.Meshs.Length); string modelPathRoot = Util.CombinePaths(objFolderPath, modelIndexedName); // Texture string texturePath = Util.CombinePaths(textureFolderPath, modelIndexedName + ".png"); texturePath = Util.FixPath(texturePath); var texture = model.Textures[modelIndex]; Util.ByteToFile(texture.EncodeToPNG(), texturePath); VoxelPostprocessor.AddTexture(texturePath); // Meshs var uMesh = model.Meshs[modelIndex]; for (int i = 0; i < uMesh.Count; i++) { uMesh[i].name = GetIndexedName("Mesh", i, uMesh.Count); string obj = Util.GetObj(uMesh[i]); string objPath = GetIndexedName(modelPathRoot, i, uMesh.Count) + ".obj"; bool hasObjBefore = Util.FileExists(objPath); Util.Write(obj, objPath); VoxelPostprocessor.AddObj(objPath, texturePath); if (hasObjBefore) { AssetDatabase.ImportAsset(Util.FixedRelativePath(objPath), ImportAssetOptions.ForceUpdate); } } } } // Delete Objects if (root.parent) { Object.DestroyImmediate(root.parent.gameObject, false); } else { Object.DestroyImmediate(root.gameObject, false); } } AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); AssetDatabase.SaveAssets(); Resources.UnloadUnusedAssets(); EditorApplication.delayCall += VoxelPostprocessor.ClearAsset; }