作用:1.可以实现一块草皮 2.通过改变颜色和密度可以模拟毛发,3.模拟植物

效果:

Wind.png

使用方法

1.创建一个Material材质球命名Grass,shader选择Grass.shader

2.选中Grass材质球shader属性中Wind风  Wind.png 拖入

2.在场景中创建一个Plane平面作为生成草的面,材质选择Grass材质

运行游戏就可以看到草了

Grass.shader

Shader "Roystan/Grass"
{Properties{[Header(Shading)]_TopColor("Top Color", Color) = (1,1,1,1)_BottomColor("Bottom Color", Color) = (1,1,1,1)_TranslucentGain("Translucent Gain", Range(0,1)) = 0.5[Space]_TessellationUniform ("Tessellation Uniform", Range(1, 64)) = 1[Header(Blades)]_BladeWidth("Blade Width", Float) = 0.05_BladeWidthRandom("Blade Width Random", Float) = 0.02_BladeHeight("Blade Height", Float) = 0.5_BladeHeightRandom("Blade Height Random", Float) = 0.3_BladeForward("Blade Forward Amount", Float) = 0.38_BladeCurve("Blade Curvature Amount", Range(1, 4)) = 2_BendRotationRandom("Bend Rotation Random", Range(0, 1)) = 0.2[Header(Wind)]_WindDistortionMap("Wind Distortion Map", 2D) = "white" {}_WindStrength("Wind Strength", Float) = 1_WindFrequency("Wind Frequency", Vector) = (0.05, 0.05, 0, 0)}CGINCLUDE#include "UnityCG.cginc"#include "Autolight.cginc"#include "Shaders/CustomTessellation.cginc"struct geometryOutput{float4 pos : SV_POSITION;#if UNITY_PASS_FORWARDBASE        float3 normal : NORMAL;float2 uv : TEXCOORD0;// unityShadowCoord4 is defined as a float4 in UnityShadowLibrary.cginc.unityShadowCoord4 _ShadowCoord : TEXCOORD1;#endif};// Simple noise function, sourced from http://answers.unity.com/answers/624136/view.html// Extended discussion on this function can be found at the following link:// https://forum.unity.com/threads/am-i-over-complicating-this-random-function.454887/#post-2949326// Returns a number in the 0...1 range.float rand(float3 co){return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 53.539))) * 43758.5453);}// Construct a rotation matrix that rotates around the provided axis, sourced from:// https://gist.github.com/keijiro/ee439d5e7388f3aafc5296005c8c3f33float3x3 AngleAxis3x3(float angle, float3 axis){float c, s;sincos(angle, s, c);float t = 1 - c;float x = axis.x;float y = axis.y;float z = axis.z;return float3x3(t * x * x + c, t * x * y - s * z, t * x * z + s * y,t * x * y + s * z, t * y * y + c, t * y * z - s * x,t * x * z - s * y, t * y * z + s * x, t * z * z + c);}geometryOutput VertexOutput(float3 pos, float3 normal, float2 uv){geometryOutput o;o.pos = UnityObjectToClipPos(pos);#if UNITY_PASS_FORWARDBASEo.normal = UnityObjectToWorldNormal(normal);o.uv = uv;// Shadows are sampled from a screen-space shadow map texture.o._ShadowCoord = ComputeScreenPos(o.pos);#elif UNITY_PASS_SHADOWCASTER// Applying the bias prevents artifacts from appearing on the surface.o.pos = UnityApplyLinearShadowBias(o.pos);#endifreturn o;}geometryOutput GenerateGrassVertex(float3 vertexPosition, float width, float height, float forward, float2 uv, float3x3 transformMatrix){float3 tangentPoint = float3(width, forward, height);float3 tangentNormal = normalize(float3(0, -1, forward));float3 localPosition = vertexPosition + mul(transformMatrix, tangentPoint);float3 localNormal = mul(transformMatrix, tangentNormal);return VertexOutput(localPosition, localNormal, uv);}float _BladeHeight;float _BladeHeightRandom;float _BladeWidthRandom;float _BladeWidth;float _BladeForward;float _BladeCurve;float _BendRotationRandom;sampler2D _WindDistortionMap;float4 _WindDistortionMap_ST;float _WindStrength;float2 _WindFrequency;#define BLADE_SEGMENTS 3// Geometry program that takes in a single triangle and outputs a blade// of grass at that triangle first vertex position, aligned to the vertex's normal.[maxvertexcount(BLADE_SEGMENTS * 2 + 1)]void geo(triangle vertexOutput IN[3], inout TriangleStream<geometryOutput> triStream){float3 pos = IN[0].vertex.xyz;// Each blade of grass is constructed in tangent space with respect// to the emitting vertex's normal and tangent vectors, where the width// lies along the X axis and the height along Z.// Construct random rotations to point the blade in a direction.float3x3 facingRotationMatrix = AngleAxis3x3(rand(pos) * UNITY_TWO_PI, float3(0, 0, 1));// Matrix to bend the blade in the direction it's facing.float3x3 bendRotationMatrix = AngleAxis3x3(rand(pos.zzx) * _BendRotationRandom * UNITY_PI * 0.5, float3(-1, 0, 0));// Sample the wind distortion map, and construct a normalized vector of its direction.float2 uv = pos.xz * _WindDistortionMap_ST.xy + _WindDistortionMap_ST.zw + _WindFrequency * _Time.y;float2 windSample = (tex2Dlod(_WindDistortionMap, float4(uv, 0, 0)).xy * 2 - 1) * _WindStrength;float3 wind = normalize(float3(windSample.x, windSample.y, 0));float3x3 windRotation = AngleAxis3x3(UNITY_PI * windSample, wind);// Construct a matrix to transform our blade from tangent space// to local space; this is the same process used when sampling normal maps.float3 vNormal = IN[0].normal;float4 vTangent = IN[0].tangent;float3 vBinormal = cross(vNormal, vTangent) * vTangent.w;float3x3 tangentToLocal = float3x3(vTangent.x, vBinormal.x, vNormal.x,vTangent.y, vBinormal.y, vNormal.y,vTangent.z, vBinormal.z, vNormal.z);// Construct full tangent to local matrix, including our rotations.// Construct a second matrix with only the facing rotation; this will be used // for the root of the blade, to ensure it always faces the correct direction.float3x3 transformationMatrix = mul(mul(mul(tangentToLocal, windRotation), facingRotationMatrix), bendRotationMatrix);float3x3 transformationMatrixFacing = mul(tangentToLocal, facingRotationMatrix);float height = (rand(pos.zyx) * 2 - 1) * _BladeHeightRandom + _BladeHeight;float width = (rand(pos.xzy) * 2 - 1) * _BladeWidthRandom + _BladeWidth;float forward = rand(pos.yyz) * _BladeForward;for (int i = 0; i < BLADE_SEGMENTS; i++){float t = i / (float)BLADE_SEGMENTS;float segmentHeight = height * t;float segmentWidth = width * (1 - t);float segmentForward = pow(t, _BladeCurve) * forward;// Select the facing-only transformation matrix for the root of the blade.float3x3 transformMatrix = i == 0 ? transformationMatrixFacing : transformationMatrix;triStream.Append(GenerateGrassVertex(pos, segmentWidth, segmentHeight, segmentForward, float2(0, t), transformMatrix));triStream.Append(GenerateGrassVertex(pos, -segmentWidth, segmentHeight, segmentForward, float2(1, t), transformMatrix));}// Add the final vertex as the tip.triStream.Append(GenerateGrassVertex(pos, 0, height, forward, float2(0.5, 1), transformationMatrix));}ENDCGSubShader{Cull OffPass{Tags{"RenderType" = "Opaque""LightMode" = "ForwardBase"}CGPROGRAM#pragma vertex vert#pragma geometry geo#pragma fragment frag#pragma hull hull#pragma domain domain#pragma target 4.6#pragma multi_compile_fwdbase#include "Lighting.cginc"float4 _TopColor;float4 _BottomColor;float _TranslucentGain;float4 frag (geometryOutput i,  fixed facing : VFACE) : SV_Target{           float3 normal = facing > 0 ? i.normal : -i.normal;float shadow = SHADOW_ATTENUATION(i);float NdotL = saturate(saturate(dot(normal, _WorldSpaceLightPos0)) + _TranslucentGain) * shadow;float3 ambient = ShadeSH9(float4(normal, 1));float4 lightIntensity = NdotL * _LightColor0 + float4(ambient, 1);float4 col = lerp(_BottomColor, _TopColor * lightIntensity, i.uv.y);return col;}ENDCG}Pass{Tags{"LightMode" = "ShadowCaster"}CGPROGRAM#pragma vertex vert#pragma geometry geo#pragma fragment frag#pragma hull hull#pragma domain domain#pragma target 4.6#pragma multi_compile_shadowcasterfloat4 frag(geometryOutput i) : SV_Target{SHADOW_CASTER_FRAGMENT(i)}ENDCG}}
}

By

中文详解

英文原创

U3d 种草 shader实现相关推荐

  1. U3D各向异性Shader

    原文链接:各向异Shader 一.描述: 这个材质提供了一种类似于在拉丝的金属或头发上的各向异高光.这种各项异性高光基于镜面地图specular map的蓝色通道来与Blinn高光进行混合,同时支持漫 ...

  2. 20170916导出fuck 7654导航

    Bookmarks 书签栏 unity Unity游戏开发有哪些让你拍案叫绝的技巧? [Unity3D学习 愤怒的小鸟攻略技巧秘籍 unity3d技巧 unity3d愤怒的小鸟实现 unity3d切换 ...

  3. 技术美术面试问题整理

    这是我前一阵子找实习所准备的有关内容,部分是来自于网上搜索到的别人的面经问题,部分是自己整理的一些比较基础的问题.这里我主要是自己又整理了一遍,结合网上的解释,以自己的理解写了写问题的解答.大家可以参 ...

  4. php获取时间不正确,php date()获取的时间不对解决办法

    因为php默认获取的是格林威治时间,与北京时间相差8小时. 我们要获取到北京时间有两个办法: 1.修改php.ini配置文件: 打开php.ini文件,一般在php配置根目录下,找到其中的 ;date ...

  5. 主流游戏引擎的详细比较和选择分析 - 优选澎湃动力 - 天天飞车游戏引擎选型

    转自:http://gad.qq.com/college/articledetail?cid=486 一.引擎史话 游戏引擎已经成为目前游戏开发必不可少的工具,它所提供的便利性和稳定性在大大降低了游戏 ...

  6. U3D log Flash shader 效果(标题流光效果)

    整理下用到的shader效果,其中用到最多的,或则很大几率在游戏中会用到的,可能是标题的流光效果, 如果用帧动画实现,不仅资源耗费明显,最终效果也没shader来得逼真,废话不多说,直接上代码了(其实 ...

  7. U3D Shader半兰伯特模型

    兰伯特光照模型--在平面某点漫反射光的光强与该反射点的法向量和入射光角度的余弦值成正比.逐像素光照可以得到更加哦平滑的光照效果.但是,即便使用了逐像素漫反射光照,有一个问题仍然存在.在光照无法达到的区 ...

  8. U3D -- 一些知识点和优秀博客收藏

    1. 场景与工程 Project(工程)与Scene(场景)是不同的概念,一个项目工程可包含多个场景,而每个场景是唯一的.例如通关游戏,项目就是整个游戏,场景就是游戏中的各个关卡. 2. 图层的个数 ...

  9. shader 4 杂 一些和函数名词、数据结构

    Normal:  法线 Normao mapping: 法线贴图 Lighting mapping: 光照贴图 Bump mapping:     凹凸贴图:模拟粗糙外表面的技术. FX-Water ...

最新文章

  1. TFS2008自定义过程模板之 Power Tools 工具篇
  2. sas中的sql(2) 行选择 、限制重复、条件运算符、运行前语法检查、feedback、count...
  3. Java并发编程实战_一线大厂架构师整理:java并发编程实践教程
  4. 批处理文件检测windows系统是32位还是64位
  5. SpringBoot中@PropertySource和@ImportResource以及@Bean
  6. Python+Opencv实现实时的条形码检测
  7. 树莓派摄像头 C++ OpenCV YoloV3 实现实时目标检测
  8. 单缸发动机扭矩动力学计算:理论计算virtual.lab motion仿真
  9. C#经典系列-键值对
  10. 明年5G智能手机大爆发!出货量惊人
  11. vs资源视图加载失败
  12. popupWindow的使用心得
  13. 关于@Autowired的使用:推荐使用构造函数进行注入
  14. 高效能人士的七个习惯之一由内而外的全面造就自己读后感
  15. R语言中的K折交叉验证
  16. 【吐血推荐】什么是领域驱动设计?DDD?
  17. oracle数据库 复制粘贴,Oracle数据库复制
  18. SMAA算法详解 - SMAASearchYUp(Down)
  19. 全球及中国轮胎行业发展方向与销售前景状况分析报告2022年
  20. 7.查找——数据结构(严蔚敏 C语言版)

热门文章

  1. 联合国国际能源署MI IC1 罗马会议报道
  2. redis linux下布署
  3. 转载 uboot LCD 进度条 为OMAP L138增加uboot启动画面
  4. 平凉建设信息化智慧城市让数据“跑腿”
  5. 高德地图发布中国高速公路出行大数据报告:2017年假期高速拥堵里程同比2016上升22.5%
  6. C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子
  7. 2020ICPC·小米 网络选拔赛第一场 D.Router Mesh
  8. SOFM 网络的MATLAB实现
  9. 在《我的世界》里从零打造一台计算机有多难?复旦本科生大神花费了一年心血...
  10. nutanix超融合部署案例