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
import {BaseMaterial} from "../core/BaseMaterial";
function Grass(args) {
BaseMaterial.call(this);
this.args = args;
this.uniforms = {
uTime: {value: 0},
uElevation: {value: 0.3},
uFrequency: {value: new THREE.Vector2(1, 3)},
uSpeed: {value: 0.7},
uHeight: {value: 0.6},
uMap: {value: args.mesh.material.map},
};
this.material = null;
this.prepare();
}
Grass.prototype = Object.create(BaseMaterial.prototype);
Grass.prototype.constructor = Grass;
Grass.prototype.prepare = function () {
if (this.args.mesh.name.toLowerCase().includes("grass")) {
this.material = this.copy(this.args.mesh.material);
this.material.transparent = true;
this.material.alphaTest = 0.5;
this.material.depthWrite = true;
this.material.side = THREE.DoubleSide;
this.material.userData.effect = 'Grass';
this.material.userData.args = this.args;
this.material.userData.uniforms = this.uniforms;
this.material.onBeforeCompile = function (shader) {
shader.uniforms.uTime = this.userData.uniforms.uTime;
shader.uniforms.uElevation = this.userData.uniforms.uElevation;
shader.uniforms.uFrequency = this.userData.uniforms.uFrequency;
shader.uniforms.uSpeed = this.userData.uniforms.uSpeed;
shader.uniforms.uHeight = this.userData.uniforms.uHeight;
this.userData.shader = shader;
shader.fragmentShader = require('../glsl/fs/grass_pars.fs.glsl') + shader.fragmentShader;
shader.fragmentShader = shader.fragmentShader.replace('#include <map_fragment>',
require('../glsl/fs/grass.fs.glsl'));
shader.vertexShader = require('../glsl/vs/grass_pars.vs.glsl') + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace('#include <project_vertex>',
require('../glsl/vs/grass.vs.glsl'));
};
}
return this;
};
Grass.prototype.update = function (dt) {
this.uniforms.uTime.value += dt;
};
export {Grass}