-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGroundMaterial.ts
50 lines (40 loc) · 1.42 KB
/
GroundMaterial.ts
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
import type { MeshPhongMaterialParameters } from 'three/src/materials/MeshPhongMaterial';
import { MeshPhongMaterial } from 'three/src/materials/MeshPhongMaterial';
import type { Shader } from 'three/src/renderers/shaders/ShaderLib';
import parsVert from '@/shaders/ground/pars.vert';
import mainVert from '@/shaders/ground/main.vert';
import parsFrag from '@/shaders/ground/pars.frag';
import mainFrag from '@/shaders/ground/main.frag';
import { Config } from '@/sandbox/Config';
export default class GroundMaterial extends MeshPhongMaterial
{
private cellSize = { value: Config.Ground.cell };
public constructor (parameters: MeshPhongMaterialParameters = {}) {
super(parameters);
this.setValues(parameters);
}
private updateDefaultVertexShader (shader: Shader): void {
shader.vertexShader = `${parsVert}
${shader.vertexShader.replace(
'void main() {',
`void main() {
${mainVert}`
)}`;
}
private updateDefaultFragmentShader (shader: Shader): void {
shader.fragmentShader = `${parsFrag}
${shader.fragmentShader.replace(
'#include <output_fragment>', `
${mainFrag}`
)}`;
}
public override onBeforeCompile (shader: Shader) {
shader.uniforms.cellSize = this.cellSize;
this.updateDefaultFragmentShader(shader);
this.updateDefaultVertexShader(shader);
this.needsUpdate = true;
}
public set cell (size: number) {
this.cellSize.value = size;
}
}