/
mergeMaterial.ts
73 lines (61 loc) · 1.6 KB
/
mergeMaterial.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @module vim-loader/materials
*/
import * as THREE from 'three'
export class MergeMaterial {
material: THREE.ShaderMaterial
constructor () {
this.material = createMergeMaterial()
}
get color () {
return this.material.uniforms.color.value
}
set color (value: THREE.Color) {
this.material.uniforms.color.value.copy(value)
this.material.uniformsNeedUpdate = true
}
get sourceA () {
return this.material.uniforms.sourceA.value
}
set sourceA (value: THREE.Texture) {
this.material.uniforms.sourceA.value = value
this.material.uniformsNeedUpdate = true
}
get sourceB () {
return this.material.uniforms.sourceB.value
}
set sourceB (value: THREE.Texture) {
this.material.uniforms.sourceB.value = value
this.material.uniformsNeedUpdate = true
}
}
/**
* Material that Merges current fragment with a source texture.
*/
export function createMergeMaterial () {
return new THREE.ShaderMaterial({
uniforms: {
sourceA: { value: null },
sourceB: { value: null },
color: { value: new THREE.Color(1, 1, 1) }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform vec3 color;
uniform sampler2D sourceA;
uniform sampler2D sourceB;
varying vec2 vUv;
void main() {
vec4 A = texture2D(sourceA, vUv);
vec4 B = texture2D(sourceB, vUv);
gl_FragColor = vec4(mix(A.xyz, color, B.x),1.0f);
}
`
})
}