Texture Bombing
Добавлено: 28 ноя 2010, 12:32
вот статья источник : http://steps3d.narod.ru/tutorials/textu ... orial.html
Текстурка, котой будем бомбордировать должна быть Clamp.
Рэндом текстурка должны быть без фильтрации и наверно даже без мипмапов + Repeat.
вот код упрошённой версии бомбинга (пришлось циклы развернуть и чтобы избежать артефактов на границах ячеек извращаться с tex2Dlod и выставлять 3ю шейдерную модель как следствие)
Синтаксис:
Используется glsl
Shader "TextureBombing"
{
Properties
{
_MainTex("Texture (RGB)", 2D) = "black" {}
_BombTex("Bomb Texture (RGB)", 2D) = "black" {}
_RandomTex("Random Texture (RGB)", 2D) = "black" {}
_Scale("Scale", Float) = 10
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Cull Back
CGPROGRAM
#pragma surface surf Lambert
#pragma target 3.0
sampler2D _MainTex;
sampler2D _BombTex;
sampler2D _RandomTex;
uniform float _Scale;
struct Input {
float2 uv_MainTex;
float2 uv_BombTex;
float2 uv_RandomTex;
};
void surf (Input IN, inout SurfaceOutput o) {
// get diffuse color
float3 decalColor = tex2D ( _MainTex, IN.uv_MainTex ).xyz;
// get cell no based on texture coordinates
float2 tex = IN.uv_MainTex * _Scale;
float2 cell = floor ( tex );
float2 offs = tex - cell;
float3 rnd;
float4 decCol;
//loop1
float2 curCell = cell + float2 ( -1, -1 );
float2 curOffs = offs - float2 ( -1, -1 );
float2 rndOffs = curCell * float2 ( 0.037, 0.119 );
//sample 1
#if !defined(SHADER_API_OPENGL)
rnd = tex2Dlod ( _RandomTex, float4(rndOffs, 0.0, 0.0) ).xyz;
decCol = tex2Dlod( _BombTex, float4(curOffs.xy - rnd.xy, 0.0, 0.0));
#else
rnd = tex2D ( _RandomTex, rndOffs ).xyz;
decCol = tex2D ( _BombTex, curOffs.xy - rnd.xy );
#endif
decalColor += decCol.xyz*decCol.w;
//loop2
curCell = cell + float2 ( -1, 0 );
curOffs = offs - float2 ( -1, 0 );
rndOffs = curCell * float2 ( 0.037, 0.119 );
//sample 1
#if !defined(SHADER_API_OPENGL)
rnd = tex2Dlod ( _RandomTex, float4(rndOffs, 0.0, 0.0) ).xyz;
decCol = tex2Dlod( _BombTex, float4(curOffs.xy - rnd.xy, 0.0, 0.0));
#else
rnd = tex2D ( _RandomTex, rndOffs ).xyz;
decCol = tex2D ( _BombTex, curOffs.xy - rnd.xy );
#endif
decalColor += decCol.xyz*decCol.w;
//loop3
curCell = cell + float2 ( 0, -1 );
curOffs = offs - float2 ( 0, -1 );
rndOffs = curCell * float2 ( 0.037, 0.119 );
//sample 1
#if !defined(SHADER_API_OPENGL)
rnd = tex2Dlod ( _RandomTex, float4(rndOffs, 0.0, 0.0) ).xyz;
decCol = tex2Dlod( _BombTex, float4(curOffs.xy - rnd.xy, 0.0, 0.0));
#else
rnd = tex2D ( _RandomTex, rndOffs ).xyz;
decCol = tex2D ( _BombTex, curOffs.xy - rnd.xy );
#endif
decalColor += decCol.xyz*decCol.w;
//loop4
curCell = cell;
curOffs = offs;
rndOffs = curCell * float2 ( 0.037, 0.119 );
//sample 1
#if !defined(SHADER_API_OPENGL)
rnd = tex2Dlod ( _RandomTex, float4(rndOffs, 0.0, 0.0) ).xyz;
decCol = tex2Dlod( _BombTex, float4(curOffs.xy - rnd.xy, 0.0, 0.0));
#else
rnd = tex2D ( _RandomTex, rndOffs ).xyz;
decCol = tex2D ( _BombTex, curOffs.xy - rnd.xy );
#endif
decalColor += decCol.xyz*decCol.w;
o.Albedo = decalColor;
}
ENDCG
}
FallBack "Diffuse"
}
{
Properties
{
_MainTex("Texture (RGB)", 2D) = "black" {}
_BombTex("Bomb Texture (RGB)", 2D) = "black" {}
_RandomTex("Random Texture (RGB)", 2D) = "black" {}
_Scale("Scale", Float) = 10
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Cull Back
CGPROGRAM
#pragma surface surf Lambert
#pragma target 3.0
sampler2D _MainTex;
sampler2D _BombTex;
sampler2D _RandomTex;
uniform float _Scale;
struct Input {
float2 uv_MainTex;
float2 uv_BombTex;
float2 uv_RandomTex;
};
void surf (Input IN, inout SurfaceOutput o) {
// get diffuse color
float3 decalColor = tex2D ( _MainTex, IN.uv_MainTex ).xyz;
// get cell no based on texture coordinates
float2 tex = IN.uv_MainTex * _Scale;
float2 cell = floor ( tex );
float2 offs = tex - cell;
float3 rnd;
float4 decCol;
//loop1
float2 curCell = cell + float2 ( -1, -1 );
float2 curOffs = offs - float2 ( -1, -1 );
float2 rndOffs = curCell * float2 ( 0.037, 0.119 );
//sample 1
#if !defined(SHADER_API_OPENGL)
rnd = tex2Dlod ( _RandomTex, float4(rndOffs, 0.0, 0.0) ).xyz;
decCol = tex2Dlod( _BombTex, float4(curOffs.xy - rnd.xy, 0.0, 0.0));
#else
rnd = tex2D ( _RandomTex, rndOffs ).xyz;
decCol = tex2D ( _BombTex, curOffs.xy - rnd.xy );
#endif
decalColor += decCol.xyz*decCol.w;
//loop2
curCell = cell + float2 ( -1, 0 );
curOffs = offs - float2 ( -1, 0 );
rndOffs = curCell * float2 ( 0.037, 0.119 );
//sample 1
#if !defined(SHADER_API_OPENGL)
rnd = tex2Dlod ( _RandomTex, float4(rndOffs, 0.0, 0.0) ).xyz;
decCol = tex2Dlod( _BombTex, float4(curOffs.xy - rnd.xy, 0.0, 0.0));
#else
rnd = tex2D ( _RandomTex, rndOffs ).xyz;
decCol = tex2D ( _BombTex, curOffs.xy - rnd.xy );
#endif
decalColor += decCol.xyz*decCol.w;
//loop3
curCell = cell + float2 ( 0, -1 );
curOffs = offs - float2 ( 0, -1 );
rndOffs = curCell * float2 ( 0.037, 0.119 );
//sample 1
#if !defined(SHADER_API_OPENGL)
rnd = tex2Dlod ( _RandomTex, float4(rndOffs, 0.0, 0.0) ).xyz;
decCol = tex2Dlod( _BombTex, float4(curOffs.xy - rnd.xy, 0.0, 0.0));
#else
rnd = tex2D ( _RandomTex, rndOffs ).xyz;
decCol = tex2D ( _BombTex, curOffs.xy - rnd.xy );
#endif
decalColor += decCol.xyz*decCol.w;
//loop4
curCell = cell;
curOffs = offs;
rndOffs = curCell * float2 ( 0.037, 0.119 );
//sample 1
#if !defined(SHADER_API_OPENGL)
rnd = tex2Dlod ( _RandomTex, float4(rndOffs, 0.0, 0.0) ).xyz;
decCol = tex2Dlod( _BombTex, float4(curOffs.xy - rnd.xy, 0.0, 0.0));
#else
rnd = tex2D ( _RandomTex, rndOffs ).xyz;
decCol = tex2D ( _BombTex, curOffs.xy - rnd.xy );
#endif
decalColor += decCol.xyz*decCol.w;
o.Albedo = decalColor;
}
ENDCG
}
FallBack "Diffuse"
}
Былобы здорово избавиться от 3й шейдерной модели и tex2Dlod как следствие...