Skip to content

Commit e70f5db

Browse files
committed
Use customProgramCacheKey in modified material example
1 parent df64dc6 commit e70f5db

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

examples/webgl_materials_modified.html

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import * as THREE from '../build/three.module.js';
1919

2020
import Stats from './jsm/libs/stats.module.js';
21+
import { GUI } from './jsm/libs/dat.gui.module.js';
2122

2223
import { OrbitControls } from './jsm/controls/OrbitControls.js';
2324
import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
@@ -26,6 +27,12 @@
2627

2728
var materialShader;
2829

30+
var params = {
31+
32+
wavesInYDimension: true
33+
34+
};
35+
2936
init();
3037
animate();
3138

@@ -41,23 +48,31 @@
4148

4249
shader.uniforms.time = { value: 0 };
4350

51+
var dimension = params.wavesInYDimension ? 'y' : 'z';
52+
4453
shader.vertexShader = 'uniform float time;\n' + shader.vertexShader;
4554
shader.vertexShader = shader.vertexShader.replace(
4655
'#include <begin_vertex>',
4756
[
48-
'float theta = sin( time + position.y ) / 2.0;',
49-
'float c = cos( theta );',
50-
'float s = sin( theta );',
51-
'mat3 m = mat3( c, 0, s, 0, 1, 0, -s, 0, c );',
52-
'vec3 transformed = vec3( position ) * m;',
53-
'vNormal = vNormal * m;'
57+
`float theta = sin( time + position.${ dimension } ) / 2.0;
58+
float c = cos( theta );
59+
float s = sin( theta );
60+
mat3 m = mat3( c, 0, s, 0, 1, 0, -s, 0, c );
61+
vec3 transformed = vec3( position ) * m;
62+
vNormal = vNormal * m;`
5463
].join( '\n' )
5564
);
5665

5766
materialShader = shader;
5867

5968
};
6069

70+
material.customProgramCacheKey = function() {
71+
72+
return params.wavesInYDimension ? '1' : '0';
73+
74+
};
75+
6176
var loader = new GLTFLoader();
6277
loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
6378

@@ -79,6 +94,13 @@
7994

8095
stats = new Stats();
8196
document.body.appendChild( stats.dom );
97+
98+
var gui = new GUI();
99+
gui.add( params, 'wavesInYDimension' ).onChange( function() {
100+
101+
material.needsUpdate = true;
102+
103+
});
82104

83105
// EVENTS
84106

0 commit comments

Comments
 (0)