Unity Shader学习:GPU Instance
稍微了解下关于gpu instance渲染优化的知识,以下是同屏6400个不同贴图的方块,用柏林噪声改变高度,加了阴影和漫反射。
csharp部分:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PerlinNoiseTest : MonoBehaviour {
private float _seedX,_seedZ;
public GameObject cubePrefab;
private MaterialPropertyBlock props;
private MeshRenderer meshRenderer;
void Start () {
_seedX = Random.value * 80f;
_seedZ = Random.value * 80f;
props = new MaterialPropertyBlock();
//生成80*80的方块
for (int i = 0; i < 80; i++)
{
for (int j= 0; j < 80; j++)
{
GameObject cube = Instantiate(cubePrefab);
cube.transform.localPosition = new Vector3(i, 0, j);
cube.transform.SetParent(transform);
//根据perlin噪声赋值高度
SetY(cube);
}
}
}
void SetY(GameObject cube)
{
float height=0;
//采样的参数
float xSample = (cube.transform.localPosition.x + _seedX)/15.0f;
float zSample = (cube.transform.localPosition.z + _seedZ)/15.0f;
//生成perlin噪声图
float noise = Mathf.PerlinNoise(xSample, zSample);
//根据不同的值设定高度以及贴图
meshRenderer = cube.GetComponent<MeshRenderer>();
if (noise<=0.1)
{
height = 0;
props.SetFloat("_Index1", 1);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 0);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.1 && noise <= 0.2)
{
height = 1;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 1);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 0);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.2 && noise <= 0.3)
{
height = 2;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 1);
props.SetFloat("_Index4", 0);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.3 && noise <= 0.4)
{
height = 3;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 1);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.4 && noise <= 0.5)
{
height = 4;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 1);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.5 && noise <= 0.6)
{
height = 5;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 1);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.6 && noise <= 0.7)
{
height = 6;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 1);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.7 && noise <= 0.8)
{
height = 7;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 1);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.8 && noise <= 0.9)
{
height = 8;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 1);
meshRenderer.SetPropertyBlock(props);
}
else if (noise > 0.9)
{
height = 9;
props.SetFloat("_Index1", 0);
props.SetFloat("_Index2", 0);
props.SetFloat("_Index3", 0);
props.SetFloat("_Index4", 1);
meshRenderer.SetPropertyBlock(props);
}
cube.transform.localPosition = new Vector3(cube.transform.localPosition.x, height, cube.transform.localPosition.z);
}
}
shader部分:
Shader "Unlit/GPUInstanceDiffuse"
{
Properties
{
//四种贴图
_Tex1("Stone",2D) = "white"{}
_Tex2("Water",2D) = "white"{}
_Tex3("Soil",2D) = "white"{}
_Tex4("Grass",2D) = "white"{}
//贴图索引
_Index1("Index1",float) = 1
_Index2("Index2",float) = 1
_Index3("Index3",float) = 1
_Index4("Index4",float) = 1
}
SubShader
{
Tags { "RenderType" = "Opaque" "LightMode" = "ForwardBase"}
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//gpu instance
#pragma multi_compile_instancing
//阴影
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
sampler2D _Tex1;
sampler2D _Tex2;
sampler2D _Tex3;
sampler2D _Tex4;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
float4 pos : SV_POSITION;
float3 worldNormal:TEXCOORD1;
float3 worldPos:TEXCOORD2;
SHADOW_COORDS(3)
};
//不同的属性
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float, _Index1)
UNITY_DEFINE_INSTANCED_PROP(float, _Index2)
UNITY_DEFINE_INSTANCED_PROP(float, _Index3)
UNITY_DEFINE_INSTANCED_PROP(float, _Index4)
UNITY_INSTANCING_BUFFER_END(Props)
v2f vert (appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
o.uv = v.uv;
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
TRANSFER_SHADOW(o);
return o;
}
float4 frag (v2f i) : SV_Target
{
float3 worldNormal = i.worldNormal;
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuse = _LightColor0.rgb*saturate(0.5*dot(worldNormal, lightDir) + 0.5);
float shadow = SHADOW_ATTENUATION(i);
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos.xyz);
float3 mainTex1,mainTex2,mainTex3,mainTex4 =float3(0,0,0);
UNITY_SETUP_INSTANCE_ID(i);
mainTex1 = tex2D(_Tex1, i.uv).xyz;
mainTex2 = tex2D(_Tex2, i.uv).xyz;
mainTex3 = tex2D(_Tex3, i.uv).xyz;
mainTex4 = tex2D(_Tex4, i.uv).xyz;
//根据传来的0和1选择贴图
return float4(((mainTex1 * UNITY_ACCESS_INSTANCED_PROP(Props, _Index1)+
mainTex2 * UNITY_ACCESS_INSTANCED_PROP(Props, _Index2)+
mainTex3 * UNITY_ACCESS_INSTANCED_PROP(Props, _Index3)+
mainTex4 * UNITY_ACCESS_INSTANCED_PROP(Props, _Index4)
)*diffuse+ambient)*shadow,1);
}
ENDCG
}
}
fallback "Diffuse"
}
版权声明:本文为qq_36107199原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。