UnityHDRP中雪地留下脚印的Shader 实现思路不复杂,依赖⼀个C#脚本和两个shader,⼀个隐式shader,⼀个显⽰shader。
思路:
1.⽤C#脚本,在玩家的两个脚底向平⾯打射线,得到射线碰撞点UV的位置。
2.得到了UV坐标,在隐式shader中,在该点绘制脚印,返回到C#脚本。
3.C#脚本,将绘制的贴图,给到显⽰shader的⾼度图,即可得到效果,显⽰shader是HDRP⾃带的曲⾯细分。
以下是C#脚本,隐式shader⽤来绘制脚印贴图,显⽰shader是HDRP⾃带的LitTesllation。
using System.Collections;
2011北京理综using System.Collections.Generic;
using UnityEngine;
public class DrawWithMou : MonoBehaviour
{
//public Camera _camera;
public Shader _drawShader;
private RenderTexture _splatmap;
private Material _snowMaterial, _drawMaterial;
private RaycastHit _hit;
一对一辅导语文public Transform footL;
public Transform footR;
public float footCheckDis = 0.1f;
void Start()
{
_drawMaterial = new Material(_drawShader);
_drawMaterial.SetVector("_Color", Color.white);
_snowMaterial = GetComponent<Renderer>().material;男士面部美白
_splatmap = new RenderTexture(1024, 1024, 0, RenderTextureFormat.ARGBFloat);
_snowMaterial.SetTexture("_HeightMap", _splatmap);
_snowMaterial.SetTexture("_BaColorMap", _splatmap);
SetWhite();
stranded//_snowMaterial.SetColor("_EmissionColor",d);
}
void Update()
{
FootDrawSnowTrack();
}
void FootDrawSnowTrack()
{
Ray footLRay = new Ray(footL.position, -Vector3.up);
Ray footRRay = new Ray(footR.position, -Vector3.up);
if (Physics.Raycast(footLRay, out _hit, footCheckDis))
{
DrawAtHit(_hit);
}
if (Physics.Raycast(footRRay, out _hit, footCheckDis))
{
DrawAtHit(_hit);
}
}
//使⽤摄像机绘制贴图的⽅案
// void CameraDrawSnow()
// {
// if (Input.GetKey(KeyCode.Mou0))
// {
// if (Physics.Raycast(_camera.uPosition), out _hit))
// {
/
/ //将射线检测的顶点坐标传递到shader的_Coordinate属性上⾯
// _drawMaterial.SetVector("_Coordinate", new Vector4(_ureCoord.x, _ureCoord.y, 0, 0));
// _drawMaterial.SetVector("_Coordinate", new Vector4(_ureCoord.x, _ureCoord.y, 0, 0));
rear window
// //新建⼀个临时的渲染纹理,尺⼨格式和_splatmap⼀致
// RenderTexture temp = RenderTexture.GetTemporary(_splatmap.width, _splatmap.height, 0, RenderTextureFormat.ARGBFloat); // //public static void Blit(Texturesource,RenderTexture dest,Material mat);
// //Source被设置为⽬标材质的_MainTex,处理之后绘制到dest
// //将_splatmap绘制temp上
// Graphics.Blit(_splatmap, temp);
// //再将temp纹理经过_drawMaterial隐藏材质的处理之后,绘制到_splatmap上
2012年江苏高考语文试卷// //估计不能这样写Graphics.Blit(_splatmap,_splatmap, _drawMaterial);
// Graphics.Blit(temp, _splatmap, _drawMaterial, 0);
// //释放临时渲染纹理
// RenderTexture.ReleaTemporary(temp);
// }
// }
// }
void DrawAtHit(RaycastHit _hit)
{
//将射线检测的顶点坐标传递到shader的_Coordinate属性上⾯
_drawMaterial.SetVector("_Coordinate", new Vector4(_ureCoord.x, _ureCoord.y, 0, 0));
/
/新建⼀个临时的渲染纹理,尺⼨格式和_splatmap⼀致
RenderTexture temp = RenderTexture.GetTemporary(_splatmap.width, _splatmap.height, 0, RenderTextureFormat.ARGBFloat);
//public static void Blit(Texturesource,RenderTexture dest,Material mat);
//Source被设置为⽬标材质的_MainTex,处理之后绘制到dest
//将_splatmap绘制temp上
Graphics.Blit(_splatmap, temp);
//再将temp纹理经过_drawMaterial隐藏材质的处理之后,绘制到_splatmap上
//估计不能这样写Graphics.Blit(_splatmap,_splatmap, _drawMaterial);
Graphics.Blit(temp, _splatmap, _drawMaterial, 0);
//释放临时渲染纹理
RenderTexture.ReleaTemporary(temp);
}
void SetWhite()
{
RenderTexture temp = RenderTexture.GetTemporary(_splatmap.width, _splatmap.height, 0, RenderTextureFormat.ARGBFloat);
Graphics.Blit(_splatmap, temp);
Graphics.Blit(temp, _splatmap, _drawMaterial, 1);
RenderTexture.ReleaTemporary(temp);
}
void OnGUI()
{
//GUI.DrawTexture(new Rect(0, 0, 256, 256), _splatmap, ScaleMode.ScaleToFit, fal, 1);
剑桥雅思词汇
}
}
Shader "WLB/DrawTrack"
{
Properties
{
人工读音_MainTex ("Texture", 2D) = "white" {}
_Coordinate("Coordinate", Vector) = (0,0,0,0)
_Color("Draw Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "inc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Coordinate, _Color;
工作经历英文
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
// fixed4 frag (v2f i) : SV_Target
// {
// // sample the texture
// fixed4 col = tex2D(_MainTex, i.uv);
/
/ //saturate控制变量在0~1之间,相当于Clamp01;
// // pow(x, y): x的y次⽅;
// //下⾯的式⼦可以理解为在UV坐标_处,绘制了⼀个⼩圆点,称尖峰形状 // float draw = pow(saturate(1 - distance(i.uv, _)),200);
// //尖峰处染成红⾊
// fixed4 drawcol = _Color * (draw * 0.1);
// //叠加原本的color和新绘制的红⾊尖峰
// fixed4 output = saturate(col + drawcol);
// return output;
// }
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
//saturate控制变量在0~1之间,相当于Clamp01;
// pow(x, y): x的y次⽅;
//下⾯的式⼦可以理解为在UV坐标_处,绘制了⼀个⼩圆点,称尖峰形状 float draw = pow(saturate((0.5 - distance(i.uv, _))*2),200);
//尖峰处染成红⾊
fixed4 drawcol = _Color * (draw * 0.5);
//叠加原本的color和新绘制的红⾊尖峰
fixed4 output = saturate(col - drawcol);
return output;
}
ENDCG
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "inc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
汉语翻译英文};
sampler2D _MainTex;
float4 _MainTex_ST;
// fixed4 _Coordinate, _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 output = (1,1,1,1);
return output;
}
ENDCG
}
}
}