# <vgl-shader-material>

# Example Usage

<template>
  <div>
    <vgl-renderer antialias>
      <template #scene>
        <vgl-scene>
          <vgl-mesh>
            <template #geometry>
              <vgl-icosahedron-geometry
                :radius="1"
                :detail="5"
              />
            </template>
            <template #material>
              <vgl-shader-material
                :wireframe="wireframe"
                :defines="defines"
                :uniforms="uniforms"
                :vertex-shader="vertexShader"
                :fragment-shader="fragmentShader"
              />
            </template>
          </vgl-mesh>
        </vgl-scene>
      </template>
      <template #camera>
        <vgl-perspective-camera
          position="spherical"
          :position-radius="5"
          :position-phi="0.8"
          :position-theta="0.2"
          rotation="lookAt"
        />
      </template>
    </vgl-renderer>

    <aside class="control-panel">
      <section>
        <h3>Shaders</h3>
        <label>
          Vertex Shader <button @click="vertShaderShown = !vertShaderShown">
            {{ vertShaderShown ? 'Hide' : 'Show' }}
          </button>
          <textarea
            v-show="vertShaderShown"
            v-model="vertexShader"
            rows="14"
            cols="50"
          />
        </label>
        <label>
          Fragment Shader <button @click="fragShaderShown = !fragShaderShown">
            {{ fragShaderShown ? 'Hide' : 'Show' }}
          </button>
          <textarea
            v-show="fragShaderShown"
            v-model="fragmentShader"
            rows="14"
            cols="50"
          />
        </label>
      </section>
      <section>
        <h3>Vert Shader Uniforms</h3>
        <table>
          <tr>
            <td>Offset</td><td>
              <input
                v-model.number="waveOffset"
                type="range"
                max="2"
                step="0.01"
              >
            </td>
          </tr>
          <tr>
            <td>Amplitude</td><td>
              <input
                v-model.number="waveAmp"
                type="range"
                max="2"
                step="0.01"
              >
            </td>
          </tr>
          <tr>
            <td>Frequency</td><td>
              <input
                v-model.number="waveFreq"
                type="range"
                max="50"
              >
            </td>
          </tr>
        </table>
        <label>
          <h3>Frag Shader Uniforms</h3>
          Frag Color
          <select v-model.number="displayColor">
            <option
              value="0"
              selected
            >Cartesian</option>
            <option value="1">Spherical</option>
            <option value="2">Depth</option>
          </select>
        </label>
        <label>
          <h3>Defines</h3>
          Scale
          <input
            v-model.number="scale"
            type="range"
            min="0.2"
            step="0.1"
            max="10"
          >
        </label>
        <label>
          <h3>Options</h3>
          Wireframe
          <input
            v-model="wireframe"
            type="checkbox"
          >
        </label>
      </section>
    </aside>
  </div>
</template>

<script>
import * as components from 'vue-gl';

const fragmentShader = `
  uniform int displayColor;

  varying vec4 vSpherical;
  varying vec4 vCartesian;

  void main() {
    if (displayColor == COLOR_CARTESIAN) {
      gl_FragColor = vCartesian;
    } else if (displayColor == COLOR_SPHERICAL) {
      gl_FragColor = vSpherical;
    } else if (displayColor == COLOR_DEPTH) {
      float depth = gl_FragCoord.z * gl_FragCoord.w;
      gl_FragColor = vec4(vec3(depth), 1.0);
    }
  }
`;

const vertexShader = `
  uniform float waveOffset;
  uniform float waveAmp;
  uniform float waveFreq;

  varying vec4 vCartesian;
  varying vec4 vSpherical;

  void main() {
    float r = length(position);
    float theta = acos(position.y / r); // inclination
    float phi = atan(position.z, position.x); // azimuth

    float finalR = r + sin((theta + waveOffset) * waveFreq) * waveAmp;
    vec4 finalPos = vec4(
      finalR * sin(theta) * cos(phi),
      finalR * cos(theta),
      finalR * sin(theta) * sin(phi),
      1.0 / SCALE
    );

    gl_Position = projectionMatrix * modelViewMatrix * finalPos;

    const float PI = 3.141592654;
    vCartesian = finalPos;
    vSpherical = vec4( // normalize values to [0, 1]
      r,
      theta / PI,
      ((phi / PI) + 1.0) / 2.0,
      1.0
    );
  }
`;

export default {
  components,
  data: () => ({
    scale: 1.0,
    waveOffset: 0.0,
    waveAmp: 0.2,
    waveFreq: 10,
    displayColor: 0,
    vertexShader,
    fragmentShader,
    vertShaderShown: false,
    fragShaderShown: false,
    wireframe: true,
  }),
  computed: {
    defines() {
      return {
        COLOR_CARTESIAN: 0,
        COLOR_SPHERICAL: 1,
        COLOR_DEPTH: 2,
        SCALE: Number.isInteger(this.scale) ? `${this.scale}.0` : this.scale,
      };
    },
    uniforms() {
      return {
        waveOffset: { value: this.waveOffset },
        waveAmp: { value: this.waveAmp },
        waveFreq: { value: this.waveFreq },
        displayColor: { value: this.displayColor },
      };
    },
  },
};
</script>

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

# Props

  • name : string
    An arbitrary name of the instance.
    Defaults to [object Object]
  • side : front | back | double
    Defines which side of faces will be rendered.
    Defaults to [object Object]
  • vertexColors : boolean
    Defines whether vertex coloring is used.
  • defines : object
    Custom defines to be injected into the shader.
    Defaults to [object Object]
  • fog : boolean
    Whether the material color is affected by global fog settings.
  • fragmentShader : string
    The fragment shader GLSL code.
    Defaults to [object Object]
  • uniforms : object
    An uniform definition object. To trigger re-rendering properly, you should pass a new object rather than overwriting the originally passed object.
    Defaults to [object Object]
  • vertexShader : string
    The vertex shader GLSL code.
    Defaults to [object Object]
  • wireframe : boolean
    Whether geometries to be rendered as wireframe or not.