当前位置:   article > 正文

unity游戏代码_github经典unity游戏源码

github经典unity游戏源码

unity官网:www.unity.cn

1,只狼弹反特效

  1. fixed4 frag (v2f i) : SV_Target
  2. {
  3. //这里是为了让扩散为圆形而不是屏幕的比例,_Aspect是屏幕的宽高比
  4. float2 p=i.uv*float2(_Aspect,1);
  5. float2 dir =normalize(p-_BlurCenter);
  6. dir*=_MainTex_TexelSize.xy;
  7. //对方向上的点采样取平均
  8. fixed4 col =tex2D(_MainTex,i.uv-dir*1) ;
  9. col +=tex2D(_MainTex,i.uv-dir*2) ;
  10. col +=tex2D(_MainTex,i.uv-dir*3) ;
  11. col +=tex2D(_MainTex,i.uv-dir*5) ;
  12. col +=tex2D(_MainTex,i.uv-dir*8) ;
  13. col +=tex2D(_MainTex,i.uv+dir*1) ;
  14. col +=tex2D(_MainTex,i.uv+dir*2) ;
  15. col +=tex2D(_MainTex,i.uv+dir*3) ;
  16. col +=tex2D(_MainTex,i.uv+dir*5) ;
  17. col +=tex2D(_MainTex,i.uv+dir*8) ;
  18. col *=0.1;
  19. return col;
  20. }

 2,场景特效 

  1. Shader "Hidden/RainRippleFX"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. _Rain("Rain", 2D) = "black" {}
  7. _Ripple("Ripple", 2D) = "black" {}
  8. _NoiseTex("Noise Tex", 2D) = "black"{}
  9. _RainForce("RainForce",Range(0,0.5)) = 0
  10. }
  11. SubShader
  12. {
  13. Cull Off ZWrite Off ZTest Always
  14. Fog { Mode Off } Blend Off
  15. Pass
  16. {
  17. CGPROGRAM
  18. #pragma vertex vert
  19. #pragma fragment frag
  20. #pragma target 3.0
  21. #pragma fragmentoption ARB_precision_hint_fastest
  22. #include "UnityCG.cginc"
  23. struct appdata
  24. {
  25. float4 vertex : POSITION;
  26. float2 uv : TEXCOORD0;
  27. };
  28. struct v2f
  29. {
  30. float4 frustumDir : TEXCOORD0;
  31. float2 uv : TEXCOORD1;
  32. float4 vertex : SV_POSITION;
  33. };
  34. sampler2D _MainTex, _NoiseTex;
  35. sampler2D _CameraDepthTexture, _Rain, _Ripple;
  36. float4x4 _FrustumDir;
  37. float3 _CameraForward;
  38. fixed _RainForce;
  39. v2f vert (appdata v)
  40. {
  41. v2f o;
  42. o.vertex = UnityObjectToClipPos(v.vertex);
  43. float2 uv = v.uv;
  44. int ix = (int)uv.x;
  45. int iy = (int)uv.y;
  46. o.frustumDir = _FrustumDir[ix + 2 * iy];
  47. o.uv = uv;
  48. return o;
  49. }
  50. fixed lum(fixed3 c)
  51. {
  52. return c.r * 0.2 + c.g * 0.7 + c.b * 0.1;
  53. }
  54. fixed4 frag (v2f i) : SV_Target
  55. {
  56. fixed4 col = tex2D(_MainTex, i.uv);
  57. float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv));
  58. float linear01Depth = Linear01Depth(depth);
  59. float linearEyeDepth = LinearEyeDepth(depth);
  60. float3 worldPos = _WorldSpaceCameraPos + linearEyeDepth * i.frustumDir.xyz;
  61. float2 fogUV = (worldPos.xz + worldPos.y * 0.5) * 0.0025;
  62. fixed fogNoiseR = tex2D(_NoiseTex, float2(fogUV.x + _Time.x * 0.15, fogUV.y)).r;
  63. fixed fogNoiseG = tex2D(_NoiseTex, float2(fogUV.x , fogUV.y + _Time.x * 0.1)).g;
  64. fixed fogNoiseB = tex2D(_NoiseTex, float2(fogUV.x - _Time.x * 0.05, fogUV.y - _Time.x * 0.3)).b;
  65. fixed3 rippleNoise = tex2D(_Rain, worldPos.xz * 0.005 - _Time.y);
  66. fixed3 ripple = (1-tex2D(_Ripple, worldPos.xz * ((fogNoiseR + fogNoiseG + fogNoiseB + rippleNoise * 0.3) * 0.1 + 0.7))) * step(linear01Depth, 0.99);
  67. ripple *= step(ripple.r, col.r * 0.6 + 0.5);
  68. ripple *= step(col.r * 0.6 + 0.3, ripple.r);
  69. ripple *= (rippleNoise.r * rippleNoise.g * rippleNoise.b);
  70. ripple *= (fogNoiseR + fogNoiseG) * fogNoiseB + 0.5;
  71. fixed2 rainUV = fixed2(i.uv.x , i.uv.y * 0.01 + _Time.x * 1.1);
  72. rainUV.y += i.uv.y * 0.001;
  73. rainUV.x += pow(i.uv.y + (_CameraForward.y + 0.5), _CameraForward.y + 1.15) * (rainUV.x - 0.5) * _CameraForward.y;
  74. fixed3 rain = tex2D(_Rain, rainUV);
  75. col.rgb += ripple * (1 - i.uv.y) * 0.8 * _RainForce * 2;
  76. col.rgb += saturate(rain.r - rain.g * (1 - _RainForce * 0.5) - rain.b * (1 - _RainForce * 0.5)) * 0.15 * (i.uv.y) * _RainForce * 2;
  77. return col;
  78. }
  79. ENDCG
  80. }
  81. }
  82. }

 3,部分插件代码

  1. timeInt = time / (2.0 * interval)
  2. float2 fTime = frac(float2(timeInt, timeInt * .5)
  3. posA = pos.xz - (flowDir/2) * fTime.x * flowDir
  4. posB = pos.xz - (flowDir/2) * fTime.y * flowDir
  5. gridA0 = waveParticles(posA.xz, freq0,scale0)
  6. gridA1 = waveParticles(posA.xz, freq1,scale1)
  7. gridB0 = waveParticles(posB.xz, freq2,scale0)
  8. gridB1 = waveParticles(posB.xz, freq3,scale1)
  9. float3 pos0 = gridA0 + gridA1 + gridA2 + gridA3
  10. float3 pos1 = gridB0 + gridB1 + gridB2 + gridB3
  11. pos = blend(pos0, pos1, abs((2*frac(timeInt)-1)))
  12. pos.y += heightMap.eval(uv)

 

  1. float v = abs(i_view.y);
  2. half towardsSun = pow(max(0., dot(i_lightDir, -i_view)),_SubSurfaceSunFallOff);
  3. half3 subsurface = (_SubSurfaceBase + _SubSurfaceSun * towardsSun) *_SubSurfaceColour.rgb * _LightColor0 * shadow;
  4. subsurface *= (1.0 - v * v) * sssIndensity;
  5. col += subsurface;

4,源码传送门

https://github.com/CRYTEK/CRYENGINE/CryFX/Water.cfx

https:// github.com/crest-ocean/ crest

https:// github.com/Verasl/BoatA ttack

5,《阴阳师》的召唤画符表现

  1. Graphics.SetRenderTarget(destTexture);
  2. GL.PushMatrix();
  3. GL.Clear(true, true, new Color(0,0,0,0f));
  4. GL.PopMatrix();

 

  1. sampler2D _MainTex;
  2. fixed4 frag(v2f IN) : SV_Target
  3. {
  4. float4 col = tex2D(_MainTex, IN.texcoord);
  5. col.rgb *= col.a;
  6. return col;
  7. }

 

  1. using UnityEngine;
  2. using System.Collections;
  3. using DG.Tweening;
  4. using UnityStandardAssets.ImageEffects;
  5. public class Summon : MonoBehaviour
  6. {
  7. private readonly int TEX_SIZE = 512;
  8. private RenderTexture texRender;
  9. public Material _BrushMaterial;
  10. public Material _DrawMaterial;
  11. private Texture brushTypeTexture;
  12. private GameObject _SummonObj = null;
  13. private enum BrushColor
  14. {
  15. red,
  16. green,
  17. blue,
  18. pink,
  19. yellow,
  20. gray,
  21. black,
  22. white,
  23. count,
  24. }
  25. private float brushScale = 0.2f;
  26. private bool m_bShow = true;
  27. private bool m_bMouseDown = false;
  28. private BrushColor brushColorType = BrushColor.black;
  29. private Color[] brushColor = new Color[(int)BrushColor.count] { Color.red, Color.green, Color.blue, new Color(255, 0, 255), Color.yellow, Color.gray, Color.black, Color.white };
  30. private Vector3 startPosition = Vector3.zero;
  31. private Vector3 endPosition = Vector3.zero;
  32. private Rect rcDraw;
  33. void Start()
  34. {
  35. _SummonObj = Instantiate(Resources.Load("Models/summon")) as GameObject;
  36. _SummonObj.transform.position = new Vector3(0, 8f, 15f);
  37. texRender = new RenderTexture(TEX_SIZE, TEX_SIZE, 24, RenderTextureFormat.ARGB32);
  38. brushTypeTexture = _BrushMaterial.mainTexture;
  39. rcDraw = new Rect((Screen.width - TEX_SIZE) * 0.5f, (Screen.height - TEX_SIZE) * 0.5f, TEX_SIZE, TEX_SIZE);
  40. Clear(texRender);
  41. // 预加载资源避免召唤时候卡
  42. UnityEngine.Object bossPrefab = Un2.AssetLoader.LoadPrefab<UnityEngine.Object>(string.Format("Normal/{0}.prefab", "m_01_boss1"));
  43. float timeAfterLoad = Time.realtimeSinceStartup;
  44. if (bossPrefab != null)
  45. {
  46. GameObject bossModel = Instantiate(bossPrefab) as GameObject;
  47. GameObject.DestroyImmediate(bossModel);
  48. }
  49. }
  50. void Update()
  51. {
  52. if (Input.GetMouseButton(0))
  53. {
  54. OnMouseMove(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
  55. }
  56. if (Input.GetMouseButtonUp(0))
  57. {
  58. OnMouseUp();
  59. }
  60. }
  61. void OnMouseUp()
  62. {
  63. startPosition = Vector3.zero;
  64. m_bMouseDown = false;
  65. }
  66. void OnMouseMove(Vector3 pos)
  67. {
  68. if (pos.x < rcDraw.xMin || pos.x > rcDraw.xMax || pos.y < rcDraw.yMin || pos.y > rcDraw.yMax)
  69. {
  70. return;
  71. }
  72. endPosition = pos;
  73. if (!m_bMouseDown)
  74. {
  75. DrawBrush(texRender, (int)endPosition.x, (int)endPosition.y, brushTypeTexture, brushColor[(int)brushColorType], brushScale);
  76. m_bMouseDown = true;
  77. }
  78. if (startPosition.Equals(Vector3.zero))
  79. {
  80. startPosition = endPosition;
  81. return;
  82. }
  83. float distance = Vector3.Distance(startPosition, endPosition);
  84. if (distance > 1)
  85. {
  86. int d = (int)distance;
  87. for (int i = 0; i < d; i++)
  88. {
  89. float difx = endPosition.x - startPosition.x;
  90. float dify = endPosition.y - startPosition.y;
  91. float delta = (float)i / distance;
  92. DrawBrush(texRender, new Vector2(startPosition.x + (difx * delta), startPosition.y + (dify * delta)), brushTypeTexture, brushColor[(int)brushColorType], brushScale);
  93. }
  94. }
  95. startPosition = endPosition;
  96. }
  97. void Clear(RenderTexture destTexture)
  98. {
  99. Graphics.SetRenderTarget(destTexture);
  100. GL.PushMatrix();
  101. GL.Clear(true, true, new Color(0,0,0,0f));
  102. GL.PopMatrix();
  103. Renderer render = _SummonObj.GetComponentInChildren<Renderer>();
  104. render.material.SetTexture("_DecalTex", null);
  105. }
  106. void DrawBrush(RenderTexture destTexture, Vector2 pos, Texture sourceTexture, Color color, float scale)
  107. {
  108. DrawBrush(destTexture, (int)pos.x, (int)pos.y, sourceTexture, color, scale);
  109. }
  110. void DrawBrush(RenderTexture destTexture, int x, int y, Texture sourceTexture, Color color, float scale)
  111. {
  112. DrawBrush(destTexture, new Rect(x, y, sourceTexture.width, sourceTexture.height), sourceTexture, color, scale);
  113. }
  114. void DrawBrush(RenderTexture destTexture, Rect destRect, Texture sourceTexture, Color color, float scale)
  115. {
  116. float left = destRect.left - destRect.width * scale / 2.0f - rcDraw.left;
  117. float right = destRect.left + destRect.width * scale / 2.0f - rcDraw.left;
  118. float top = destRect.top - destRect.height * scale / 2.0f - rcDraw.top;
  119. float bottom = destRect.top + destRect.height * scale / 2.0f - rcDraw.top;
  120. Graphics.SetRenderTarget(destTexture);
  121. GL.PushMatrix();
  122. GL.LoadOrtho();
  123. // mat.SetTexture("_MainTex", sourceTexture); // 在材质里面就已经设置好贴图了
  124. // mat.SetColor("_Color", color); // 取消颜色设置渲染,只用贴图颜色 [2017/5/8 09:05:53 --By aq_1000]
  125. _BrushMaterial.SetPass(0);
  126. GL.Begin(GL.QUADS);
  127. GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(left / TEX_SIZE, top / TEX_SIZE, 0);
  128. GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(right / TEX_SIZE, top / TEX_SIZE, 0);
  129. GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(right / TEX_SIZE, bottom / TEX_SIZE, 0);
  130. GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(left / TEX_SIZE, bottom / TEX_SIZE, 0);
  131. GL.End();
  132. GL.PopMatrix();
  133. }

 

  1. fixed4 frag (v2f i) : SV_Target
  2. {
  3. fixed4 col = tex2D(_MainTex, i.texcoord);
  4. fixed4 decal = 0.0f;
  5. if (i.texcoord.y >= 0.25f && i.texcoord.y <= 0.75f)
  6. {
  7. decal = tex2D(_DecalTex, half2(i.texcoord.x, (i.texcoord.y - 0.25f) * 2.0f));
  8. }
  9. return lerp(col, decal, decal.a);
  10. }
  1. private void _Summon()
  2. {
  3. m_bShow = false;
  4. _SummonObj.GetComponent<Animator>().Play("Summon", 0);
  5. Renderer render = _SummonObj.GetComponentInChildren<Renderer>();
  6. render.material.SetTexture("_DecalTex", texRender);
  7. StartCoroutine(WaitForSummon());
  8. GameObject effect = GameObject.FindGameObjectWithTag("SummonEffect");
  9. effect.GetComponent<Animator>().enabled = true;
  10. }
  11. private IEnumerator WaitForSummon()
  12. {
  13. // suspend execution for 5 seconds
  14. yield return new WaitForSeconds(1.5f);
  15. Tweener tweener = _SummonObj.transform.DOScale(new Vector3(0.001f, 0.001f, 0.001f), 1f);
  16. tweener.SetEase(Ease.Linear);
  17. tweener.OnComplete(() =>
  18. {
  19. _ScaleEnd();
  20. });
  21. }
  22. private void _ScaleEnd()
  23. {
  24. Camera.main.gameObject.GetComponent<MotionBlur>().enabled = true;
  25. Tweener tweener = Camera.main.transform.DOMove(new Vector3(0f, 8f, 14f), 0.75f);
  26. tweener.SetEase(Ease.Linear);
  27. tweener.OnComplete(() =>
  28. {
  29. _CameraMoveEnd();
  30. });
  31. }
  32. private void _CameraMoveEnd()
  33. {
  34. Camera.main.gameObject.GetComponent<MotionBlur>().enabled = false;
  35. // 召唤怪物
  36. Singleton<Un2.BossCtrller>.Instance.CreateBoss("m_01_boss1", new Vector2(0, 0), null);
  37. }

编写人物移动代码

  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent(typeof(Animator))]
  4. public class RunnerController : MonoBehaviour
  5. {
  6. //public Variable
  7. public enum Direction
  8. {
  9. Forward = 90,
  10. Backward = 270,
  11. }
  12. public float maxSpeed;
  13. public float MoveSpeed = 0;
  14. public float Z;
  15. public float JumpForce;
  16. public float Gravity;
  17. public bool PlayerDead;
  18. //private variable
  19. private CharacterController Controller;
  20. private Animator animator;
  21. private Direction direction = Direction.Forward;
  22. private float H;
  23. private float V;
  24. private bool OnGround = true;
  25. private bool Jump;
  26. private float JSpeed;
  27. void Awake()
  28. {
  29. //rigidbody = this.GetComponent<Rigidbody>();
  30. Controller = this.GetComponent<CharacterController>();
  31. animator = this.GetComponent<Animator>();
  32. }
  33. void Update ()
  34. {
  35. if(!PlayerDead)
  36. {
  37. if(this.transform.position.z != Z)
  38. {
  39. Vector3 Position = this.transform.position;
  40. Position.z = Z;
  41. this.transform.position = Position;
  42. }
  43. H = Input.GetAxis("Horizontal");
  44. V = Input.GetAxis("Vertical");
  45. //AniamtorState();
  46. if(Controller.isGrounded)
  47. {
  48. OnGround = true;
  49. Jump = false;
  50. animator.SetBool("Jump",false);
  51. if(Input.GetKeyDown(KeyCode.J))
  52. {
  53. if(V > 0.3)
  54. {
  55. JSpeed = 1.3f * JumpForce;
  56. }
  57. else
  58. {
  59. JSpeed = JumpForce;
  60. }
  61. Jump = true;
  62. animator.SetBool("Jump", true);
  63. }
  64. }
  65. else
  66. {
  67. OnGround = false;
  68. if(!OnGround)
  69. {
  70. Controller.Move(-Vector3.up * Gravity * Time.deltaTime);
  71. }
  72. }
  73. }
  74. else
  75. {
  76. animator.SetBool("Dead",true);
  77. animator.SetFloat("speed", 0);
  78. animator.SetFloat("Slider", 0);
  79. animator.SetBool("Jump", false);
  80. }
  81. }
  82. void FixedUpdate()
  83. {
  84. JumpUp();
  85. Move();
  86. }
  87. void Move()
  88. {
  89. //先计算朝向
  90. if(H >= 0.3)
  91. {
  92. if(MoveSpeed == 0)
  93. {
  94. SetFacingDirection(Direction.Forward);
  95. }
  96. if(direction == Direction.Forward)
  97. {
  98. if(MoveSpeed < maxSpeed)
  99. {
  100. MoveSpeed += 1.0f;
  101. //state = State.Walk;
  102. }
  103. else
  104. {
  105. MoveSpeed = maxSpeed;
  106. //state = State.Run;
  107. }
  108. }
  109. }
  110. else if(H <= -0.3)
  111. {
  112. if(MoveSpeed == 0)
  113. {
  114. SetFacingDirection(Direction.Backward);
  115. }
  116. if(direction == Direction.Backward)
  117. {
  118. if(MoveSpeed < maxSpeed)
  119. {
  120. MoveSpeed += 1.0f;
  121. //state = State.Walk;
  122. }
  123. else
  124. {
  125. MoveSpeed = maxSpeed;
  126. //state = State.Run;
  127. }
  128. }
  129. }
  130. if((direction == Direction.Forward && H < -0.3 && MoveSpeed != 0)
  131. || (direction == Direction.Backward && H > 0.3 && MoveSpeed != 0))
  132. {
  133. MoveSpeed -= 1.0f;
  134. if(MoveSpeed <= 0)
  135. {
  136. MoveSpeed = 0;
  137. //state = State.Idle;
  138. }
  139. }
  140. if(Mathf.Abs(H) < 0.3 && MoveSpeed != 0)
  141. {
  142. MoveSpeed -= 0.5f;
  143. if(MoveSpeed <= 0)
  144. {
  145. MoveSpeed = 0;
  146. //state = State.Run;
  147. }
  148. }
  149. transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
  150. animator.SetFloat("Speed", MoveSpeed);
  151. animator.SetFloat("Slider", V);
  152. }
  153. void JumpUp()
  154. {
  155. if(Jump)
  156. {
  157. JSpeed -= 2 * Gravity * Time.deltaTime;
  158. Controller.Move(Vector3.up * Time.deltaTime * JSpeed);
  159. }
  160. }
  161. void SetFacingDirection(Direction dir)
  162. {
  163. if(direction != dir)
  164. {
  165. transform.Rotate(Vector3.up * (direction - dir));
  166. direction = dir;
  167. }
  168. }
  169. void OnTriggerEnter(Collider _collider)
  170. {
  171. if(_collider.gameObject.tag == "Enemy")
  172. {
  173. PlayerDead = true;
  174. }
  175. }
  176. }

 后面的下个文章在再写

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/104064?site
推荐阅读
相关标签
  

闽ICP备14008679号