void Awake() { //单例 if (Instance == null) { Instance = this; } //指定跟随相机 if (FollowTarget == null) { Debug.LogWarning("Please specify the target to follow!"); } //默认阴影质量低 RTAntiAliasing = AntiAliasing.Samples2; //设置衰减 FalloffTex = (Texture2D)Resources.Load("Texture/shadow_falloff"); //projector初始化 projector = GetComponent <Projector> (); if (projector == null) { Debug.LogError("Projector Component Missing!!"); } projector.orthographic = true; projector.orthographicSize = ProjectionSize; projector.aspectRatio = Size.x / Size.y; shadowMat = new Material(Shader.Find("ShadowSystem/ProjectorShadow")); projector.material = shadowMat; shadowMat.SetTexture("_FalloffTex", FalloffTex); shadowMat.SetFloat("_Intensity", Intensity); projector.ignoreLayers = LayerIgnoreReceiver; //camera初始化 shadowCam = GetComponent <Camera> (); if (shadowCam == null) { Debug.LogError("Camera Component Missing!!"); } shadowCamTrans = shadowCam.transform; shadowCam.clearFlags = CameraClearFlags.SolidColor; shadowCam.backgroundColor = new Color(0, 0, 0, 0); shadowCam.orthographic = true; shadowCam.orthographicSize = ProjectionSize; shadowCam.depth = int.MinValue; shadowCam.cullingMask = LayerCaster; shadowRT = new RenderTexture((int)Size.x, (int)Size.y, 0, RenderTextureFormat.ARGB32); shadowRT.name = "ShadowRT"; shadowRT.antiAliasing = (int)RTAntiAliasing; shadowRT.filterMode = FilterMode.Bilinear; shadowRT.wrapMode = TextureWrapMode.Clamp; shadowCam.targetTexture = shadowRT; shadowMat.SetTexture("_ShadowTex", shadowRT); }
private void BuildMap() { Transform tilemapTransform = tileMap.transform; Transform floor = tilemapTransform.Find("floor"); if (floor == null) { floor = new GameObject("floor").transform; floor.SetParent(tilemapTransform); floor.localPosition = Vector3.zero; floor.localScale = Vector3.one; } floor.localScale = new Vector3(1, 1, 1.0f / Mathf.Sin(55.0f * Mathf.PI / 180.0f)); Transform wall = tilemapTransform.Find("wall"); if (wall == null) { wall = new GameObject("wall").transform; wall.SetParent(tilemapTransform); wall.localPosition = Vector3.zero; wall.localScale = Vector3.one; } Transform obstruct = tilemapTransform.Find("obstruct"); if (obstruct == null) { obstruct = new GameObject("obstruct").transform; obstruct.SetParent(tilemapTransform); obstruct.localPosition = Vector3.zero; obstruct.localScale = Vector3.one; } Transform environment = tilemapTransform.Find("environment"); if (environment == null) { environment = new GameObject("environment").transform; environment.SetParent(tilemapTransform); environment.localPosition = Vector3.zero; environment.localScale = Vector3.one; } GameObject cameraFollow = GameObject.Find("CameraFollow"); if (cameraFollow == null) { GameObject prefabCameraFollow = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/RawResources/Scene/Prefab/CameraFollow.prefab"); cameraFollow = Instantiate(prefabCameraFollow); cameraFollow.name = "CameraFollow"; } GameObject canvas = GameObject.Find("Canvas"); if (canvas == null) { GameObject prefabCanvas = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/RawResources/Scene/Prefab/Canvas.prefab"); canvas = Instantiate(prefabCanvas); canvas.name = "Canvas"; } GameObject mobileFastShadow = GameObject.Find("MobileFastShadow"); if (mobileFastShadow == null) { GameObject prefabShadow = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/RawResources/Scene/Prefab/MobileFastShadow.prefab"); mobileFastShadow = Instantiate(prefabShadow); mobileFastShadow.name = "MobileFastShadow"; } GameObject entityObj = GameObject.Find("entity"); if (entityObj == null) { entityObj = new GameObject("entity"); entityObj.transform.localPosition = Vector3.zero; GameObject heroObj = new GameObject("hero"); heroObj.transform.SetParent(entityObj.transform); GameObject monsterObj = new GameObject("monster"); monsterObj.transform.SetParent(entityObj.transform); } MobileFastShadow shadowCtrl = mobileFastShadow.GetComponent <MobileFastShadow>(); shadowCtrl.FollowTarget = entityObj; GameUtil.SetLayer(entityObj.transform, GameUtil.PlayerLayer); if (tileMap.wallPrefabs != null && tileMap.wallPrefabs.Length > 0) { for (int i = wall.childCount - 1; i >= 0; i--) { DestroyImmediate(wall.GetChild(i).gameObject); } GameObject prefabWall = tileMap.wallPrefabs[0]; MeshFilter mf = prefabWall.GetComponentInChildren <MeshFilter>(); Mesh m = mf.sharedMesh; float yPos = m.bounds.center.y - m.bounds.extents.y; float grid = m.bounds.extents.x; float zg = m.bounds.extents.z; float xOffset = tileMap.width * grid * 2.0f * 0.5f; float zOffset = tileMap.length * grid * 2.0f * 0.5f; float[] xList = new float[] { 0, tileMap.width - 1 }; float[] yList = new float[] { 0, tileMap.length - 1 }; for (int i = 0; i < xList.Length; i++) { float x = xList[i]; float xPos = 2 * (x + i) * grid - zg + i * zg * 2; for (int y = 0; y < tileMap.length; y++) { float zPos = (2 * y) * grid + grid; Transform tileChild = GameObject.Instantiate(prefabWall).transform; tileChild.SetParent(wall); tileChild.position = new Vector3(xPos - xOffset, yPos, zPos - zOffset); tileChild.forward = Vector3.right; } } for (int i = 0; i < yList.Length; i++) { float y = yList[i]; float zPos = 2 * (y + i) * grid - zg + i * zg * 2; for (int x = 0; x < tileMap.width; x++) { float xPos = (2 * x) * grid + grid; Transform tileChild = GameObject.Instantiate(prefabWall).transform; tileChild.SetParent(wall); tileChild.position = new Vector3(xPos - xOffset, yPos, zPos - zOffset); } } } if (tileMap.tilePrefabs != null && tileMap.tilePrefabs.Length > 0) { for (int i = floor.childCount - 1; i >= 0; i--) { DestroyImmediate(floor.GetChild(i).gameObject); } GameObject prefabObj = tileMap.tilePrefabs[0]; MeshFilter mf = prefabObj.GetComponent <MeshFilter>(); Mesh m = mf.sharedMesh; float colNum = tileMap.width + 10; float rowNum = tileMap.length + 15; float yPos = m.bounds.center.y - m.bounds.extents.y; float grid = m.bounds.extents.x; float zg = m.bounds.extents.z; float xOffset = colNum * grid * 2.0f * 0.5f; float zOffset = rowNum * grid * 2.0f * 0.5f; for (int x = 0; x < colNum; x++) { float xPos = (2 * x + 1) * grid; for (int y = 0; y < rowNum; y++) { int idx = Random.Range(0, tileMap.tilePrefabs.Length); prefabObj = tileMap.tilePrefabs[idx]; mf = prefabObj.GetComponent <MeshFilter>(); m = mf.sharedMesh; yPos = m.bounds.center.y - m.bounds.extents.y; float zPos = (2 * y + 1) * grid; Transform tileChild = GameObject.Instantiate(prefabObj).transform; tileChild.SetParent(floor); tileChild.position = new Vector3(xPos - xOffset, yPos, zPos - zOffset); } } } GameUtil.SetLayer(floor, GameUtil.MapLayer); GameUtil.SetLayer(wall, GameUtil.MapOutWallLayer); /* * if (GUILayout.Button("分类", GUILayout.Height(30))) * { * List<GameObject> floors = new List<GameObject>(); * List<GameObject> environments = new List<GameObject>(); * * for (int i = 0; i < tilemapTransform.childCount; i++) * { * Transform childTrans = tilemapTransform.GetChild(i); * GameObject child = childTrans.gameObject; * if(child.name == "floor") * { * floor = child; * } * else if(child.name == "environment") * { * environment = child; * } * else if(child.name.StartsWith("T_")) * { * floors.Add(child); * } * else * { * environments.Add(child); * } * } * if(floor == null) * { * floor = new GameObject("floor"); * floor.transform.SetParent(tilemapTransform); * floor.transform.localPosition = Vector3.zero; * floor.transform.localScale = Vector3.one; * } * if(environment == null) * { * environment = new GameObject("environment"); * environment.transform.SetParent(tilemapTransform); * environment.transform.localPosition = Vector3.zero; * environment.transform.localScale = Vector3.one; * } * foreach(GameObject obj in floors) * { * obj.transform.SetParent(floor.transform, true); * } * foreach(GameObject obj in environments) * { * obj.transform.SetParent(environment.transform, true); * } * } * * if(GUILayout.Button("对齐到地面", GUILayout.Height(30))) * { * Transform floor = tilemapTransform.Find("floor"); * if(floor != null) * { * for(int i = 0; i < floor.childCount; i++) * { * Transform child = floor.GetChild(i); * MeshFilter meshFilter = child.GetComponent<MeshFilter>(); * if(meshFilter != null) * { * Vector3 childPos = child.position; * Mesh mesh = meshFilter.sharedMesh; * Bounds b = mesh.bounds; * Vector3 centerPos = child.TransformPoint(b.center); * if(child.forward == Vector3.up) * { * childPos.y = 0f; * child.position = childPos; * } * else * { * childPos.y = b.center.z; * child.position = childPos; * } * } * } * } * } */ }