void OnChangeScene(Scene current, Scene next) { if (next.name != "passto") { player = LsyCommon.FindPlayer(); //寻找进度条 if (!progressBar) { if (GameObject.Find("UGUI/UILayerOverPop/LoadingView/loading_panel_new(Clone)/RectStrecth/Progress")) { progressBar = GameObject.Find("UGUI/UILayerOverPop/LoadingView/loading_panel_new(Clone)/RectStrecth/Progress").GetComponent <Image>(); } if (GameObject.Find("UGUI/UILayerOverPop/LoadingView/loading_panel_new_special(Clone)/RectStrecth/Progress")) { progressBar = GameObject.Find("UGUI/UILayerOverPop/LoadingView/loading_panel_new_special(Clone)/RectStrecth/Progress").GetComponent <Image>(); } } if (player) { if (this) { ani = player.GetComponent <Animator>(); ac = ani.runtimeAnimatorController; ani.runtimeAnimatorController = null; count = 0; } } } }
public void Update() { player = LsyCommon.FindPlayer(); if (player == null) { player = Camera.main.transform; } UpdateVolume(); UpdateEvent(); }
void Update() { if (shadowRoot != null) { foreach (var item in shadowRoot.GetComponentsInChildren <MeshRenderer>()) { item.enabled = false; } } if (player == null || !player.gameObject.activeInHierarchy) { player = LsyCommon.FindPlayer(); } var allR = LsyCommon.FindAllChars <Renderer> (); foreach (var item in allR) { //objs with Decal should not have real time shadow if (item.GetComponentInChildren <CB_Decal> () != null) { if (item.transform.parent == null) { item.gameObject.layer = 0; } else { foreach (var r in item.transform.parent.GetComponentsInChildren <Renderer>(true)) { r.gameObject.layer = 0; } } } //Set chars Darker for cave if (!rendererColors.ContainsKey(item) && item.material.HasProperty("_Color")) { rendererColors.Add(item, item.material.color); } } foreach (var item in rendererColors) { if (item.Key == null) { continue; } var newColor = item.Value; newColor = new Color(newColor.r * playerColor.r, newColor.g * playerColor.g, newColor.b * playerColor.b, newColor.a * playerColor.a); item.Key.material.SetColor("_Color", newColor); } }
void Update() { if (FSManager.Instance != null) { player = FSManager.Instance.player; } else { if (player == null || !player.gameObject.activeInHierarchy) { player = LsyCommon.FindPlayer(); } } if (player != null && solidObj != null) { solidObj.transform.position = player.transform.position + new Vector3(0, 2, 0); } UpdateLight(); UpdateShadow(); }
void Update() { #if UNITY_EDITOR DestroyImmediate(gameObject); #else Destroy(gameObject); #endif var mainCam = Camera.main; if (mainCam != null) { mainCam.cullingMask &= ~(1 << 9); } if (player == null) { player = LsyCommon.FindPlayer(); } if (player == null) { return; } transform.position = new Vector3(player.position.x, BasicHeight, player.position.z); //No rotation for aabb yet //transform.eulerAngles = new Vector3 (0, player.eulerAngles.y, 0); var p = transform.position; aabb = new Vector4( p.x - cam.orthographicSize, p.x + cam.orthographicSize, p.z - cam.orthographicSize, p.z + cam.orthographicSize); Shader.SetGlobalVector("_footprintAABB", aabb); }
// Use this for initialization void Start() { tilingOffset = new Vector4[3]; //tilingOffset[0] = new Vector4(0.5f, 0.5f, 0f, 0f); //tilingOffset[1] = new Vector4(0.5f, 0.5f, 0.5f, 0f); tilingOffset[0] = new Vector4(0.333f, 0.5f, 0f, 0.5f); tilingOffset[1] = new Vector4(0.333f, 0.5f, 0.325f, 0.5f); tilingOffset[2] = new Vector4(0.333f, 0.5f, 0.665f, 0.5f); //tilingOffset[0] = new Vector4(1f, 0f, 0f, 1f); //tilingOffset[1] = new Vector4(0f, 1f, 0f, 1f); //tilingOffset[2] = new Vector4(0f, 0f, 1f, 1f); mats = new List <Matrix4x4>(); randomWingSinStart = new List <float>(); randomFlyingSinStarts = new List <float>(); idleWingSpeeds = new List <float>(); sinFactor1s = new List <float>(); sinFactor2s = new List <float>(); gpuBirdStates = new List <float>(); //butterflyColors = new List<Vector4>(); butterflyColors1 = new List <float>(); butterflyColors2 = new List <float>(); butterflyColors3 = new List <float>(); butterflyColors4 = new List <float>(); mpb = new MaterialPropertyBlock(); if (!player) { player = LsyCommon.FindPlayer(); } randomWingSinStartIndex = Shader.PropertyToID("_RandomWingSinStart"); idleWingSpeedIndex = Shader.PropertyToID("_IdleWingSpeed"); randomFlyingSinStartIndex = Shader.PropertyToID("_RandomFlyingSinStart"); sinFactor1Index = Shader.PropertyToID("_SinFactor1"); sinFactor2Index = Shader.PropertyToID("_SinFactor2"); gpuBirdStateIndex = Shader.PropertyToID("_GPUBirdState"); tilingOffsetIndex = Shader.PropertyToID("_ZzcButterflyMainTex_ST1111"); tilingOffsetIndex1 = Shader.PropertyToID("_ZzcButterflyMainTex_ST1"); tilingOffsetIndex2 = Shader.PropertyToID("_ZzcButterflyMainTex_ST2"); tilingOffsetIndex3 = Shader.PropertyToID("_ZzcButterflyMainTex_ST3"); tilingOffsetIndex4 = Shader.PropertyToID("_ZzcButterflyMainTex_ST4"); for (int i = 0; i < butterflyData.Count; i++) { butterflyData[i].positions = new List <Vector3>(); butterflyData[i].startPos = new List <Vector3>(); butterflyData[i].endPos = new List <Vector3>(); butterflyData[i].cullDistance = cullDistance; butterflyData[i].mainCamera = Camera.main; butterflyData[i].butterFlyState = DrawInstancing.ButterFlyData.ButterFlyState.idle; butterflyData[i].randomDirections = new List <Vector3>(); butterflyData[i].randomSinStart = new List <float>(); butterflyData[i].lastPositions = new List <Vector3>(); butterflyData[i].quaternions = new List <Quaternion>(); butterflyData[i].randomScale = new List <float>(); butterflyData[i].tilingOffsets = new List <Vector4>(); for (int j = 0; j < butterflyData[i].chunkLength; j++) { butterflyData[i].randomDirections.Add(new Vector3(Random.Range(-1f, 1f), butterflyData[i].bezierYFactor, Random.Range(-1f, 1f))); butterflyData[i].randomSinStart.Add(Random.Range(0f, 360f)); tempVec = new Vector3(Random.Range(-butterflyData[i].startArea.x, butterflyData[i].startArea.x), Random.Range(-butterflyData[i].startArea.y, butterflyData[i].startArea.y), Random.Range(-butterflyData[i].startArea.z, butterflyData[i].startArea.z)); tempVec = Quaternion.AngleAxis(butterflyData[i].startRotate, Vector3.up) * tempVec; butterflyData[i].startPos.Add(butterflyData[i].chunkPos + tempVec); tempVec = new Vector3(Random.Range(-butterflyData[i].endArea.x, butterflyData[i].endArea.x), Random.Range(-butterflyData[i].endArea.y, butterflyData[i].endArea.y), Random.Range(-butterflyData[i].endArea.z, butterflyData[i].endArea.z)); tempVec = Quaternion.AngleAxis(butterflyData[i].endRotate, Vector3.up) * tempVec; butterflyData[i].endPos.Add(butterflyData[i].endAreaPos + tempVec); butterflyData[i].lastPositions.Add(butterflyData[i].startPos[j]); butterflyData[i].quaternions.Add(Quaternion.LookRotation(butterflyData[i].randomDirections[j])); butterflyData[i].positions.Add(butterflyData[i].startPos[j]); randomWingSinStart.Add(Random.Range(0f, 360f)); randomFlyingSinStarts.Add(Random.Range(0f, 360f)); idleWingSpeeds.Add(1f); gpuBirdStates.Add(0); sinFactor1s.Add(butterflyData[i].sinYFactor1); sinFactor2s.Add(butterflyData[i].sinYFactor2); butterflyData[i].randomScale.Add(Random.Range(0.7f, 1.1f)); butterflyData[i].tilingOffsets.Add(tilingOffset[Random.Range(0, 3)]); butterflyData[i].randomArriveTimes.Add(Random.Range(0f, 3f)); //butterflyColors.Add(tilingOffset[Random.Range(0, 3)]); butterflyColors1.Add(1f); butterflyColors2.Add(1f); butterflyColors3.Add(1f); butterflyColors4.Add(1f); } } mpb.SetFloatArray(sinFactor1Index, sinFactor1s); mpb.SetFloatArray(sinFactor2Index, sinFactor2s); mpb.SetFloatArray(randomFlyingSinStartIndex, randomFlyingSinStarts); mpb.SetFloatArray(randomWingSinStartIndex, randomWingSinStart); camTrans = Camera.main.transform; }
// Update is called once per frame void Update() { if (!player) { t += Time.deltaTime; if (t > 1f) { player = LsyCommon.FindPlayer(); t = 0f; } } if (!player) { return; } if (!camTrans) { camTrans = Camera.main.transform; } if (!camTrans) { return; } for (int i = 0; i < butterflyData.Count; i++) { if (Vector3.Distance(butterflyData[i].chunkPos, camTrans.position) >= cullDistance && Vector3.Distance(butterflyData[i].endAreaPos, camTrans.position) >= cullDistance) { continue; } Vector3 cam2Chunk = butterflyData[i].chunkPos - camTrans.position; Vector3 cam2End = butterflyData[i].endAreaPos - camTrans.position; if (Vector3.Dot(cam2Chunk, camTrans.forward) < 0 && Vector3.Dot(cam2End, camTrans.forward) < 0) { continue; } //待机状态 if (butterflyData[i].butterFlyState == DrawInstancing.ButterFlyData.ButterFlyState.idle) { if (Vector3.Distance(player.position, butterflyData[i].chunkPos) <= butterflyData[i].fleeDistance) { butterflyData[i].butterFlyState = DrawInstancing.ButterFlyData.ButterFlyState.flyingTo; } for (int j = 0; j < butterflyData[i].chunkLength; j++) { mats.Add(Matrix4x4.TRS(butterflyData[i].positions[j], butterflyData[i].quaternions[j], butterflyData[i].randomScale[j] * Vector3.one * butterflyData[i].scale)); gpuBirdStates.Add(0); idleWingSpeeds.Add(0.2f); //butterflyColors.Add(butterflyData[i].tilingOffsets[j]); butterflyColors1.Add(butterflyData[i].tilingOffsets[j].x); butterflyColors2.Add(butterflyData[i].tilingOffsets[j].y); butterflyColors3.Add(butterflyData[i].tilingOffsets[j].z); butterflyColors4.Add(butterflyData[i].tilingOffsets[j].w); } } else //在终点待机 if (butterflyData[i].butterFlyState == DrawInstancing.ButterFlyData.ButterFlyState.endIdle) { if (Vector3.Distance(player.position, butterflyData[i].endAreaPos) <= butterflyData[i].fleeDistance) { butterflyData[i].butterFlyState = DrawInstancing.ButterFlyData.ButterFlyState.flyingBack; } for (int j = 0; j < butterflyData[i].chunkLength; j++) { mats.Add(Matrix4x4.TRS(butterflyData[i].positions[j], butterflyData[i].quaternions[j], butterflyData[i].randomScale[j] * Vector3.one * butterflyData[i].scale)); gpuBirdStates.Add(0); idleWingSpeeds.Add(0.2f); //butterflyColors.Add(butterflyData[i].tilingOffsets[j]); butterflyColors1.Add(butterflyData[i].tilingOffsets[j].x); butterflyColors2.Add(butterflyData[i].tilingOffsets[j].y); butterflyColors3.Add(butterflyData[i].tilingOffsets[j].z); butterflyColors4.Add(butterflyData[i].tilingOffsets[j].w); } } else //起点飞往终点 if (butterflyData[i].butterFlyState == DrawInstancing.ButterFlyData.ButterFlyState.flyingTo) { butterflyData[i].currentTime += Time.deltaTime; if (butterflyData[i].currentTime >= butterflyData[i].flyTime) { butterflyData[i].currentTime = 0f; butterflyData[i].butterFlyState = DrawInstancing.ButterFlyData.ButterFlyState.endIdle; for (int j = 0; j < butterflyData[i].chunkLength; j++) { mats.Add(Matrix4x4.TRS(butterflyData[i].positions[j], butterflyData[i].quaternions[j], butterflyData[i].randomScale[j] * Vector3.one * butterflyData[i].scale)); gpuBirdStates.Add(0); idleWingSpeeds.Add(0.2f); //butterflyColors.Add(butterflyData[i].tilingOffsets[j]); butterflyColors1.Add(butterflyData[i].tilingOffsets[j].x); butterflyColors2.Add(butterflyData[i].tilingOffsets[j].y); butterflyColors3.Add(butterflyData[i].tilingOffsets[j].z); butterflyColors4.Add(butterflyData[i].tilingOffsets[j].w); } continue; } //tt = butterflyData[i].currentTime / (butterflyData[i].flyTime); //t = Mathf.Pow(tt, 0.7f); for (int j = 0; j < butterflyData[i].chunkLength; j++) { tt = butterflyData[i].currentTime / (butterflyData[i].flyTime - butterflyData[i].randomArriveTimes[j]); tt = Mathf.Clamp01(tt); t = Mathf.Pow(tt, 0.7f); butterflyData[i].positions[j] = (1f - t) * (1f - t) * butterflyData[i].startPos[j] + 2f * t * (1f - t) * (butterflyData[i].startPos[j] + butterflyData[i].randomDirections[j].normalized * butterflyData[i].bezierFactor) + t * t * butterflyData[i].endPos[j]; if (t >= 0.8f) { butterflyData[i].positions[j] += butterflyData[i].finalCurve.Evaluate((t - 0.8f) * 5f) * Vector3.up; } if (t < 0.8f) { forward = butterflyData[i].positions[j] - butterflyData[i].lastPositions[j]; butterflyData[i].quaternions[j] = Quaternion.LookRotation(forward); euler = butterflyData[i].quaternions[j].eulerAngles; euler -= Vector3.right * 45f; butterflyData[i].quaternions[j] = Quaternion.Euler(euler); } butterflyData[i].lastPositions[j] = butterflyData[i].positions[j]; mats.Add(Matrix4x4.TRS(butterflyData[i].positions[j], butterflyData[i].quaternions[j], butterflyData[i].randomScale[j] * Vector3.one * butterflyData[i].scale)); gpuBirdStates.Add(Mathf.Sqrt(1f - Mathf.Abs((Mathf.Clamp01(tt) - 0.5f) * 2f))); if (tt > 0.95f) { idleWingSpeeds.Add(0.3f); } else { idleWingSpeeds.Add(1f); } //butterflyColors.Add(butterflyData[i].tilingOffsets[j]); butterflyColors1.Add(butterflyData[i].tilingOffsets[j].x); butterflyColors2.Add(butterflyData[i].tilingOffsets[j].y); butterflyColors3.Add(butterflyData[i].tilingOffsets[j].z); butterflyColors4.Add(butterflyData[i].tilingOffsets[j].w); } } else //终点飞往起点 if (butterflyData[i].butterFlyState == DrawInstancing.ButterFlyData.ButterFlyState.flyingBack) { butterflyData[i].currentTime += Time.deltaTime; if (butterflyData[i].currentTime >= butterflyData[i].flyTime) { butterflyData[i].currentTime = 0f; butterflyData[i].butterFlyState = DrawInstancing.ButterFlyData.ButterFlyState.idle; for (int j = 0; j < butterflyData[i].chunkLength; j++) { mats.Add(Matrix4x4.TRS(butterflyData[i].positions[j], butterflyData[i].quaternions[j], butterflyData[i].randomScale[j] * Vector3.one * butterflyData[i].scale)); gpuBirdStates.Add(0); idleWingSpeeds.Add(0.2f); //butterflyColors.Add(butterflyData[i].tilingOffsets[j]); butterflyColors1.Add(butterflyData[i].tilingOffsets[j].x); butterflyColors2.Add(butterflyData[i].tilingOffsets[j].y); butterflyColors3.Add(butterflyData[i].tilingOffsets[j].z); butterflyColors4.Add(butterflyData[i].tilingOffsets[j].w); } continue; } //tt = butterflyData[i].currentTime / butterflyData[i].flyTime; //t = Mathf.Pow(tt, 0.7f); for (int j = 0; j < butterflyData[i].chunkLength; j++) { tt = butterflyData[i].currentTime / (butterflyData[i].flyTime - butterflyData[i].randomArriveTimes[j]); tt = Mathf.Clamp01(tt); t = Mathf.Pow(tt, 0.7f); butterflyData[i].positions[j] = (1f - t) * (1f - t) * butterflyData[i].endPos[j] + 2f * t * (1f - t) * (butterflyData[i].endPos[j] + butterflyData[i].randomDirections[j].normalized * butterflyData[i].bezierFactor) + t * t * butterflyData[i].startPos[j]; if (t >= 0.8f) { butterflyData[i].positions[j] += butterflyData[i].finalCurve.Evaluate((t - 0.8f) * 5f) * Vector3.up; } if (t < 0.8f) { forward = butterflyData[i].positions[j] - butterflyData[i].lastPositions[j]; butterflyData[i].quaternions[j] = Quaternion.LookRotation(forward); euler = butterflyData[i].quaternions[j].eulerAngles; euler -= Vector3.right * 45f; butterflyData[i].quaternions[j] = Quaternion.Euler(euler); } butterflyData[i].lastPositions[j] = butterflyData[i].positions[j]; mats.Add(Matrix4x4.TRS(butterflyData[i].positions[j], butterflyData[i].quaternions[j], butterflyData[i].randomScale[j] * Vector3.one * butterflyData[i].scale)); gpuBirdStates.Add(Mathf.Sqrt(1f - Mathf.Abs((Mathf.Clamp01(tt) - 0.5f) * 2f))); if (tt > 0.95f) { idleWingSpeeds.Add(0.3f); } else { idleWingSpeeds.Add(1f); } //butterflyColors.Add(butterflyData[i].tilingOffsets[j]); butterflyColors1.Add(butterflyData[i].tilingOffsets[j].x); butterflyColors2.Add(butterflyData[i].tilingOffsets[j].y); butterflyColors3.Add(butterflyData[i].tilingOffsets[j].z); butterflyColors4.Add(butterflyData[i].tilingOffsets[j].w); } } } mpb.SetFloatArray(idleWingSpeedIndex, idleWingSpeeds); mpb.SetFloatArray(gpuBirdStateIndex, gpuBirdStates); //mpb.SetVectorArray(tilingOffsetIndex, butterflyColors); mpb.SetFloatArray(tilingOffsetIndex1, butterflyColors1); mpb.SetFloatArray(tilingOffsetIndex2, butterflyColors2); mpb.SetFloatArray(tilingOffsetIndex3, butterflyColors3); mpb.SetFloatArray(tilingOffsetIndex4, butterflyColors4); //Debug.Log(butterflyColors.Count); Graphics.DrawMeshInstanced(butterflyMesh, 0, butterflyMaterial, mats, mpb); mats.Clear(); idleWingSpeeds.Clear(); gpuBirdStates.Clear(); //butterflyColors.Clear(); butterflyColors1.Clear(); butterflyColors2.Clear(); butterflyColors3.Clear(); butterflyColors4.Clear(); }
private void Start() { if (!player) { player = LsyCommon.FindPlayer(); } fishAlphaIndex = Shader.PropertyToID("_FishAlpha"); fishColorIndex1 = Shader.PropertyToID("_FishColor1Main"); fishColorIndex2 = Shader.PropertyToID("_FishColor1Sub"); //fishColorIndex3 = Shader.PropertyToID("_FishColor2Main"); //fishColorIndex4 = Shader.PropertyToID("_FishColor2Sub"); randomNumIndex = Shader.PropertyToID("_RandomNum"); mats = new List <Matrix4x4>(); colors1 = new List <Vector4>(); colors2 = new List <Vector4>(); //colors3 = new List<Vector4>(); //colors4 = new List<Vector4>(); randomNum = new List <float>(); scales = new List <Vector3>(); speed = new List <float>(); fishAlphas = new List <float>(); mpb = new MaterialPropertyBlock(); for (int i = 0; i < fishData.Count; i++) { fishData[i].positions = new List <Vector3>(); fishData[i].fishColors1 = new List <Vector4>(); fishData[i].fishColors2 = new List <Vector4>(); fishData[i].cullDistance = cullDistance; fishData[i].mainCamera = Camera.main; fishData[i].fishState = DrawInstancing.FishData.FishState.normal; fishData[i].fishAlpha = 1f; for (int j = 0; j < fishData[i].chunkLength; j++) { //if (j % 2 == 0) //{ // colors1.Add(fishData[i].color1Main); // colors2.Add(fishData[i].color1Sub); //} //else if (j % 2 == 1) //{ // colors1.Add(fishData[i].color2Main); // colors2.Add(fishData[i].color2Sub); //} //顶点运动随机时间偏移 randomNum.Add(Random.Range(0, 10)); //生成鱼以及鱼中心的位置 Vector3 spawnCenterPos = new Vector3(Random.Range(-fishData[i].centerRect.x, fishData[i].centerRect.x), 0, Random.Range(-fishData[i].centerRect.y, fishData[i].centerRect.y)); Vector3 spawnPos = new Vector3(Random.Range(-fishData[i].fishMaxRadius / 1.414f, fishData[i].fishMaxRadius / 1.414f), Random.Range(-fishData[i].height, fishData[i].height), Random.Range(-fishData[i].fishMaxRadius / 1.414f, fishData[i].fishMaxRadius / 1.414f)); while (spawnPos.x * spawnPos.x + spawnPos.z * spawnPos.z < fishData[i].fishMinRadius * fishData[i].fishMinRadius) { spawnPos = new Vector3(Random.Range(-fishData[i].fishMaxRadius / 1.414f, fishData[i].fishMaxRadius / 1.414f), Random.Range(-fishData[i].height, fishData[i].height), Random.Range(-fishData[i].fishMaxRadius / 1.414f, fishData[i].fishMaxRadius / 1.414f)); } fishData[i].positions.Add(fishData[i].chunkPos + spawnCenterPos + spawnPos); fishData[i].oriPositions.Add(fishData[i].chunkPos + spawnCenterPos + spawnPos); fishData[i].forwards.Add(Vector3.one); fishData[i].startPositions.Add(fishData[i].chunkPos + spawnCenterPos); if (j % 2 == 0) { fishData[i].fishColors1.Add(fishData[i].color1Main); fishData[i].fishColors2.Add(fishData[i].color1Sub); } else if (j % 2 == 1) { fishData[i].fishColors1.Add(fishData[i].color2Main); fishData[i].fishColors2.Add(fishData[i].color2Sub); } colors1.Add(new Vector4(1, 1, 1, 1)); colors2.Add(new Vector4(1, 1, 1, 1)); //每条鱼速度随机系数 speed.Add(Random.Range(0.8f, 1.2f)); fishData[i].fleeFactor = 1f; fishAlphas.Add(1f); } } camTrans = Camera.main.transform; }
//#if UNITY_EDITOR // private void OnValidate() // { // if (colors1!=null&&colors2!=null) // { // colors1.Clear(); // colors2.Clear(); // //colors3.Clear(); // //colors4.Clear(); // for (int i = 0; i < fishData.Count; i++) // { // for (int j = 0; j < fishData[i].chunkLength; j++) // { // if (j % 2 == 0) // { // colors1.Add(fishData[i].color1Main); // colors2.Add(fishData[i].color1Sub); // } // else if (j % 2 == 1) // { // colors1.Add(fishData[i].color2Main); // colors2.Add(fishData[i].color2Sub); // } // } // } // } // } //#endif void Update() { if (!player) { t += Time.deltaTime; if (t > 1f) { player = LsyCommon.FindPlayer(); t = 0f; } } if (!camTrans) { camTrans = Camera.main.transform; } if (!camTrans) { return; } if (!player) { return; } for (int i = 0; i < fishData.Count; i++) { bool shouldDo = true; //摄像机裁剪 float dis = Vector3.Distance(fishData[i].chunkPos, camTrans.position); Vector3 cam2Chunk = fishData[i].chunkPos - camTrans.position; if (Vector3.Dot(cam2Chunk, camTrans.forward) < 0) { shouldDo = dis >= cullDistance * 0.3f ? false : true; } else { shouldDo = dis >= cullDistance ? false : true; } //循环状态 if (fishData[i].fishState == DrawInstancing.FishData.FishState.normal) { if (Vector2.Distance(new Vector2(player.position.x, player.position.z), new Vector2(fishData[i].chunkPos.x, fishData[i].chunkPos.z)) <= fishData[i].fleeDistance) { fishData[i].fishState = DrawInstancing.FishData.FishState.fleeing; } } //逃跑状态 else if (fishData[i].fishState == DrawInstancing.FishData.FishState.fleeing) { fishData[i].chunkTime += Time.deltaTime; fishData[i].fleeFactor -= fishData[i].fadeSpeed * 0.01f * Time.deltaTime; fishData[i].fleeFactor = Mathf.Clamp01(fishData[i].fleeFactor); if (fishData[i].fishAlpha > 0 && fishData[i].chunkTime >= fishData[i].startFadeTime) { fishData[i].fishAlpha -= fishData[i].fadeSpeed * 0.01f * Time.deltaTime; fishData[i].fishAlpha = Mathf.Clamp01(fishData[i].fishAlpha); } if (Vector3.Distance(player.position, fishData[i].chunkPos) >= fishData[i].resetDistance) { for (int j = 0; j < fishData[i].chunkLength; j++) { fishData[i].positions[j] = fishData[i].oriPositions[j]; } fishData[i].fishState = DrawInstancing.FishData.FishState.normal; fishData[i].fishAlpha = 1.0f; fishData[i].fleeFactor = 1.0f; fishData[i].chunkTime = 0f; } } for (int j = 0; j < fishData[i].chunkLength; j++) { Vector3 position = fishData[i].positions[j]; Quaternion quaternion = Quaternion.identity; Vector3 forward = fishData[i].forwards[j]; if (shouldDo && fishData[i].fishAlpha > 0f) { //循环状态 if (fishData[i].fishState == DrawInstancing.FishData.FishState.normal) { Vector3 center2Fish = fishData[i].positions[j] - fishData[i].startPositions[j]; Vector3 newCenter2Fish = Vector3.zero; if (fishData[i].direction == DrawInstancing.FishData.Direction.clockwise) { forward = Vector3.Cross(Vector3.up, new Vector3(center2Fish.x, 0, center2Fish.z)); newCenter2Fish = Quaternion.AngleAxis(Time.deltaTime * speed[j] * fishData[i].swimSpeed, Vector3.up) * center2Fish; } else { forward = -Vector3.Cross(Vector3.up, new Vector3(center2Fish.x, 0, center2Fish.z)); newCenter2Fish = Quaternion.AngleAxis(Time.deltaTime * speed[j] * -fishData[i].swimSpeed, Vector3.up) * center2Fish; } quaternion = Quaternion.LookRotation(forward); position = fishData[i].startPositions[j] + newCenter2Fish; fishData[i].positions[j] = position; fishData[i].forwards[j] = forward; fishAlphas.Add(1.0f); colors1.Add(fishData[i].fishColors1[j]); colors2.Add(fishData[i].fishColors2[j]); } //逃跑状态 else if (fishData[i].fishState == DrawInstancing.FishData.FishState.fleeing) { //Vector3 player2Center = fishData[i].chunkPos - player.position; Vector3 player2Fish = fishData[i].positions[j] - player.position; //float angle = AngleSigned(player.forward, player2Fish, Vector3.up); //angle = Mathf.Clamp(angle, -45f, 45f); //player2Fish = Quaternion.AngleAxis(angle * 2.5f, Vector3.up) * player2Center; player2Fish = new Vector3(player2Fish.x, 0, player2Fish.z); forward = Vector3.Lerp(fishData[i].forwards[j], player2Fish, Mathf.Clamp01((1 - fishData[i].fleeFactor)) * 0.5f); Vector3 right = Quaternion.AngleAxis(90f, Vector3.up) * forward; quaternion = Quaternion.LookRotation(forward); position = fishData[i].positions[j] + forward.normalized * fishData[i].fleeSpeed * speed[j] * Time.deltaTime + right.normalized * Time.deltaTime * Mathf.Sin(Time.timeSinceLevelLoad * 10f + speed[j] * 100f) * 0.5f; fishData[i].positions[j] = position; fishData[i].forwards[j] = forward; fishAlphas.Add(fishData[i].fishAlpha); colors1.Add(fishData[i].fishColors1[j]); colors2.Add(fishData[i].fishColors2[j]); } if (mats.Count < 1023) { mats.Add(Matrix4x4.TRS(position, quaternion, Vector3.one * fishData[i].fishSize)); } } else { position = fishData[i].positions[j]; quaternion = Quaternion.identity; } } } mpb.SetVectorArray(fishColorIndex1, colors1); mpb.SetVectorArray(fishColorIndex2, colors2); //mpb.SetVectorArray(fishColorIndex3, colors3); //mpb.SetVectorArray(fishColorIndex4, colors4); mpb.SetFloatArray(randomNumIndex, randomNum); mpb.SetFloatArray(fishAlphaIndex, fishAlphas); Graphics.DrawMeshInstanced(fishMesh, 0, fishMaterial, mats, mpb); mats.Clear(); fishAlphas.Clear(); colors1.Clear(); colors2.Clear(); }
public override void OnBehaviourPlay(Playable playable, FrameData info) { //timelineObj = GameObject.Find(timelineGameObjName); //if (timelineObj) //{ // timeline = timelineObj.GetComponent<PlayableDirector>(); //} //if (timeline) //{ // var timelineAsset = timeline.playableAsset as TimelineAsset; // foreach (var track in timelineAsset.GetOutputTracks()) // { // var playableTrack = track as PlayableTrack; // if (playableTrack != null) // { // if (playableTrack.GetClips().ToList().Count > clipIndex) // timelineClip = playableTrack.GetClips().ToList()[clipIndex]; // } // } //} player = LsyCommon.FindPlayer(); Transform display; if (part == Part.playerFace) { if (player) { display = player.GetChild(0); for (int i = 0; i < display.childCount; i++) { if (display.GetChild(i).name.StartsWith("face")) { testObj = display.GetChild(i).gameObject; break; } } } } else if (part == Part.npcFace) { GameObject NPC; NPC = GameObject.Find(targetGameObjName); if (NPC) { testObj = NPC; } } if (testObj) { smr = testObj.GetComponent <SkinnedMeshRenderer>(); if (smr) { if (count < 1) { if ((int)materialIndex <= smr.materials.Length - 1) { mat = smr.materials[(int)materialIndex]; count++; } } else { if ((int)materialIndex <= smr.sharedMaterials.Length - 1) { mat = smr.sharedMaterials[(int)materialIndex]; } } } } }