Интернет магазин китайских планшетных компьютеров |
|
Компьютеры - HLSL - Программы для создания шейдеров29 мая 2011Оглавление: 1. HLSL 2. Циклы 3. Входящие и исходящие данные для вершинного и пиксельного шейдеров 4. Программы для создания шейдеров Для облегчения написания шейдеров существует ряд программ, позволяющих составлять шейдеры и тут же просматривать результат
Также пиксельные шейдеры используются визуализаторами, например,
Примерыпростой шейдер «Головокружение»float4x4 view_proj_matrix: register; struct VS_OUTPUT { float4 Pos: POSITION; float2 texCoord: TEXCOORD0; }; VS_OUTPUT VS_Dizzy { VS_OUTPUT Out; Pos.xy = sign; Out.Pos = float4; Out.texCoord = Pos.xy; return Out; } float time_0_X: register; float rings: register; float speed: register; float exponent: register; float4 PS_Dizzy : COLOR { float ang = atan2; float rad = pow, exponent); return 0.5 * ); } шейдер, имитирующий электрический разрядstruct VS_OUTPUT { float4 Pos: POSITION; float2 texCoord: TEXCOORD; }; VS_OUTPUT VS_Electricity { VS_OUTPUT Out; // Clean up inaccuracies Pos.xy = sign; Out.Pos = float4; Out.texCoord = Pos.xy; return Out; } float4 color: register; float glowStrength: register; float height: register; float glowFallOff: register; float speed: register; float sampleDist: register; float ambientGlow: register; float ambientGlowHeightScale: register; float vertNoise: register; float time_0_X: register; sampler Noise: register; float4 PS_Electricity : COLOR { float2 t = float2, speed * time_0_X); // Sample at three positions for some horizontal blur // The shader should blur fine by itself in vertical direction float xs0 = texCoord.x - sampleDist; float xs1 = texCoord.x; float xs2 = texCoord.x + sampleDist; // Noise for the three samples float noise0 = tex3D); float noise1 = tex3D); float noise2 = tex3D); // The position of the flash float mid0 = height * * ; float mid1 = height * * ; float mid2 = height * * ; // Distance to flash float dist0 = abs; float dist1 = abs; float dist2 = abs; // Glow according to distance to flash float glow = 1.0 - pow, glowFallOff); // Add some ambient glow to get some power in the air feeling float ambGlow = ambientGlow * * ); return * color; } пластилиновая модельfloat4x4 view_proj_matrix: register; float4 view_position: register; struct VS_OUTPUT { float4 Pos: POSITION; float3 normal: TEXCOORD0; float3 viewVec: TEXCOORD1; }; VS_OUTPUT VS_Plastic { VS_OUTPUT Out; Out.Pos = mul; Out.normal = normal; Out.viewVec = view_position - Pos; return Out; } float4 color: register; float4 PS_Plastic : COLOR { float v = 0.5 * , normal)); return v * color; } имитация деревянной поверхностиfloat trunk_wobble_frequency; float4x4 view_matrix; float4x4 view_proj_matrix; float4x4 texture_matrix0; float4x4 texture_matrix1; float4x4 texture_matrix2; struct VS_OUTPUT { float4 Pos : POSITION; float3 TCoord0 : TEXCOORD0; float3 TCoord1 : TEXCOORD1; float3 TCoord2 : TEXCOORD2; float3 TCoord3 : TEXCOORD3; float3 TCoord4 : TEXCOORD4; float3 TCoord6 : TEXCOORD6; float3 TCoord7 : TEXCOORD7; }; VS_OUTPUT VS_Wood { VS_OUTPUT Out = 0; float4 TransformedPshade; // Transform position to clip space Out.Pos = mul ; // Transform Pshade and output to pixel shader TransformedPshade = mul ; Out.TCoord0 = TransformedPshade; Out.TCoord1 = mul ; Out.TCoord2 = mul ; // Create two coordinates for sampling noise volume to get wobble Out.TCoord3 = float3; Out.TCoord4 = float3; // Transform position and normal to eye space Out.TCoord6 = mul ; Out.TCoord7 = mul ; return Out; } float4 light_pos; float4 eye_pos; float4 light_wood_color; float4 dark_wood_color; float noise_amplitude; float trunk_wobble_amplitude; float ring_freq; sampler noise_volume; sampler pulse_train; sampler variable_specular; float4 PS_Wood (float3 Pshade0 : TEXCOORD0, float3 Pshade1 : TEXCOORD1, float3 Pshade2 : TEXCOORD2, float3 zWobble0 : TEXCOORD3, float3 zWobble1 : TEXCOORD4, float3 Peye : TEXCOORD6, float3 Neye : TEXCOORD7) : COLOR { float3 coloredNoise; float3 wobble; // Construct colored noise from three samples coloredNoise.x = tex3D ; coloredNoise.y = tex3D ; coloredNoise.z = tex3D ; wobble.x = tex3D ; wobble.y = tex3D ; wobble.z = 0.5f; // Make signed coloredNoise = coloredNoise * 2.0f - 1.0f; wobble = wobble * 2.0f - 1.0f; // Scale noise and add to Pshade float3 noisyWobblyPshade = Pshade0 + coloredNoise * noise_amplitude + wobble * trunk_wobble_amplitude; float scaledDistFromZAxis = sqrt) * ring_freq; // Lookup blend factor from pulse train float4 blendFactor = tex1D ; // Blend wood colors together float4 albedo = lerp ; // Compute normalized vector from vertex to light in eye space float3 Leye = / length; // Normalize interpolated normal Neye = Neye / length; // Compute Veye float3 Veye = -); // Compute half-angle float3 Heye = / length; // Compute N.H float NdotH = clamp, 0.0f, 1.0f); // Scale and bias specular exponent from pulse train into decent range float k = blendFactor.z; // Evaluate^k via dependent read float specular = tex2D (variable_s Просмотров: 5379
|