赞
踩
实现:
- //表面皮肤渲染
- Pass
- {
- CGPROGRAM
-
- #pragma vertex vert_surface//点着色器 , 对应引用cginc中的方法
- #pragma fragment frag_surface// 片段着色器 , 对应引用cginc中的方法
- #define FURSTEP 0.0
- #include "FurHelper.cginc" // 引用的shader
-
- ENDCG
-
- }
- // 毛的渲染
- Pass
- {
- CGPROGRAM
-
- #pragma vertex vert_base //点着色器 , 对应引用cginc中的方法
- #pragma fragment frag_base // 片段着色器 , 对应引用cginc中的方法
- #define FURSTEP 0.05 // 皮毛的长度
- #include "FurHelper.cginc" // 引用的shader
-
- ENDCG
-
- }
- v2f vert(a2v v)
- {
- v2f o;
- float3 OffetVertex = v.vertex.xyz + v.normal * _LayerOffset *_FurLength;//顶点外扩
- OffetVertex += mul(unity_WorldToObject, _FurOffset);//顶点受力偏移【加一个方向上的偏移,看起来更自然,例如向下垂】
- }
- fixed3 noise = tex2D(_FurTex, i.uv.zw ).rgb;
- fixed alpha = saturate(noise - (_LayerOffset * _LayerOffset + _LayerOffset * _FurDensity));
- // 生成, material赋值, shader赋值
- void CreateShell()
- {
- CheckParent();
- layers = new GameObject[LayerCount];
- float furOffset = 1.0f/ LayerCount;
- for (int i = 0; i< LayerCount; i++)
- {
- //复制渲染的原模型一遍
- GameObject layer = Instantiate(Target.gameObject, Target.transform.position, Target.transform.rotation);
- layer.transform.parent = _parent;
- layer.GetComponent<Renderer>().sharedMaterials = new Material[1];
- layer.GetComponent<Renderer>().sharedMaterial = ResetSharedMaterials(i, furOffset);
- layers[i] = layer;
- }
- }
-
- // shader 赋值
- Material ResetSharedMaterials(int index, float furOffset)
- {
- Material special = new Material(ShellShader);
- if (special != null)
- {
- special.SetTexture("_MainTex", Target.sharedMaterial.GetTexture("_MainTex"));
- special.SetColor("_Color", FurColor);
- special.SetColor("_RootColor", FurRootColor);
- special.SetColor("_RimColor", FurRimColor);
- special.SetColor("_Specular", FurSpecularColor);
- special.SetFloat("_Shininess", Shininess);
- special.SetFloat("_RimPower", RimPower);
- special.SetFloat("_FurShadow", FurShadow);
- special.SetTexture("_FurTex",FurPattern);
- special.SetFloat("_FurLength", FurLength);
- special.SetFloat("_FurDensity", FurDensity);
- special.SetFloat("_FurThinness", FurThinness);
- special.SetFloat("_LayerOffset", index * furOffset); //不同Shell的偏移参数不一样
- special.SetVector("_FurOffset", FurForce* Mathf.Pow( index*furOffset,FurTenacity));//计算受力、层数和硬度共同影响的Shell偏移
- special.renderQueue = 3000 + index;//由于在单通道渲染半透明材质,进行了深度写入,为了防止被深度剔除,所以要手动更改渲染队列
- }
- return special;
- }
-
-
- // 移动
- void UpdateShellTrans()
- {
- if (layers == null || layers.Length == 0)
- {
- return;
- }
- for (int i = 0; i < layers.Length; i++)
- {
- //位置和旋转Lerp到目标模型
- layers[i].gameObject.transform.position = Vector3.Lerp(layers[i].gameObject.transform.position, Target.transform.position, lerpSpeed * Time.deltaTime *20);
- layers[i].gameObject.transform.rotation = Quaternion.Lerp(layers[i].gameObject.transform.rotation, Target.transform.rotation, lerpSpeed * Time.deltaTime *10);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。