当前位置:   article > 正文

曲面Shader

shader 曲面的平面展开

这是一个能让平面呈现出曲面效果的Shaer。

代码:

  1. Shader "Custom/CurvedWorld"{
  2. Properties {
  3. // Diffuse texture
  4. _MainTex ("Base (RGB)", 2D) = "white" {}
  5. // Degree of curvature
  6. _Curvature ("Curvature", Float) = 0.001
  7. // Axis Around which the curvature is required
  8. _Axis ("Axis", int ) = 2
  9. }
  10. SubShader {
  11. Tags { "RenderType"="Opaque" }
  12. LOD 200
  13. CGPROGRAM
  14. // Surface shader function is called surf, and vertex preprocessor function is called vert
  15. // addshadow used to add shadow collector and caster passes following vertex modification
  16. #pragma surface surf Lambert vertex:vert addshadow
  17. // Access the shaderlab properties
  18. uniform sampler2D _MainTex;
  19. uniform float _Curvature;
  20. uniform int _Axis;
  21. // Basic input structure to the shader function
  22. // requires only a single set of UV texture mapping coordinates
  23. struct Input {
  24. float2 uv_MainTex;
  25. };
  26. // This is where the curvature is applied
  27. void vert( inout appdata_full v)
  28. {
  29. // Transform the vertex coordinates from model space into world space
  30. float4 vv = mul( _Object2World, v.vertex );
  31. // Now adjust the coordinates to be relative to the camera position
  32. vv.xyz -= _WorldSpaceCameraPos.xyz;
  33. // Reduce the y coordinate (i.e. lower the “height”) of each vertex based
  34. // on the square of the distance from the camera in the z axis, multiplied
  35. // by the chosen curvature factor
  36. if(_Axis == 0)
  37. vv = float4( 0.0f, (vv.x * vv.x) *_Curvature, 0.0f, 0.0f );
  38. else if(_Axis == 1)
  39. vv = float4( 0.0f, (vv.y * vv.y) * _Curvature, 0.0f, 0.0f );
  40. else if(_Axis == 2)
  41. vv = float4( 0.0f, (vv.z * vv.z) * _Curvature, 0.0f, 0.0f );
  42. // Now apply the offset back to the vertices in model space
  43. v.vertex += mul(_World2Object, vv);
  44. }
  45. // This is just a default surface shader
  46. void surf (Input IN, inout SurfaceOutput o) {
  47. half4 c = tex2D (_MainTex, IN.uv_MainTex);
  48. o.Albedo = c.rgb;
  49. o.Alpha = c.a;
  50. }
  51. ENDCG
  52. }
  53. // FallBack “Diffuse”
  54. }

  效果图:

不同角度有不同效果:

 

转载于:https://www.cnblogs.com/chimo523/p/5145134.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/107622
推荐阅读
相关标签
  

闽ICP备14008679号