aboutsummaryrefslogtreecommitdiffstats
path: root/src/GLSLShaderTest/wood.frag
blob: eecf91fb95e642fa2ab16a8d8f500466fbbc7b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Simple fragment shader for wood
//
// Author: John Kessenich
//
// Copyright (c) 2002-2004 3Dlabs Inc. Ltd.
//
// See 3Dlabs-License.txt for license information
//

//uniform float GrainSizeRecip;
//uniform vec3  DarkColor;
//uniform vec3  spread;
const float GrainSizeRecip = 1.0;
const vec3  DarkColor = vec3 (0.6, 0.3, 0.1);
const vec3  spread = vec3 (0.15, 0.075, 0.0);

varying float lightIntensity; 
varying vec3 Position;

void main (void)
{
    //
    // cheap noise
    //
    vec3 location = Position;
    vec3 floorvec = vec3(floor(10.0 * Position.x), 0.0, floor(10.0 * Position.z));
    vec3 noise = Position * 10.0 - floorvec - 0.5;
    noise *= noise;
    location += noise * 0.12;

    //
    // distance from axis
    //
    float dist = location.x * location.x + location.z * location.z;
    float grain = dist * GrainSizeRecip;

    //
    // grain effects as function of distance
    //
    float brightness = fract(grain);
    if (brightness > 0.5) 
        brightness = (1.0 - brightness);
    vec3 color = DarkColor + brightness * spread;
    
    brightness = fract(grain * 7.0);    
    if (brightness > 0.5) 
        brightness = 1.0 - brightness;
    color -= brightness * spread;

    //
    // also as a function of lines parallel to the axis
    //
    brightness = fract(grain * 47.0) * 0.60;
    float line = fract(Position.z + Position.x);
    float snap = floor(line * 20.0) * (1.0/20.0);
    if (line < snap + 0.006)
        color -= brightness * spread;

    //
    // apply lighting effects from vertex processor
    //
    color = clamp(color * lightIntensity, 0.0, 1.0); 

    gl_FragColor = vec4(color, 1.0);
}