-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree.ShaderMaterial.ts
81 lines (68 loc) · 1.8 KB
/
three.ShaderMaterial.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
74
75
76
77
78
79
80
81
import { Init, Update, Meta, Screen, Time } from 'lucidity'
let time: Time
let uniforms
export const init: Init =
( { context, require, asset, cache, detached } ) => {
const THREE = require ( 'THREE' )
if ( !cache.material ) {
const uniforms: any = cache.uniforms = {}
uniforms.time = { value: 0.0 }
uniforms.screen = { value: new THREE.Vector2 ( 0, 0 ) }
cache.material = new THREE.ShaderMaterial
( { uniforms
, vertexShader: ''
, fragmentShader: ''
}
)
cache.prevmat = context.object3d.material
context.object3d.material = cache.material
}
// Captured for update loop
time = context.time
uniforms = cache.uniforms
const material = cache.material
asset.source ( 'vert.glsl', ( s ) => {
material.vertexShader = s
material.needsUpdate = true
})
asset.source ( 'frag.glsl', ( s ) => {
material.fragmentShader = s
material.needsUpdate = true
})
const screen: Screen = context.screen
uniforms.screen.value = new THREE.Vector2 ( screen.width, screen.height )
if ( detached ) {
context.object3d.material = cache.prevmat
}
}
export const update: Update =
() => {
uniforms.time.value = time.now
}
export const meta: Meta =
{ description: "Set a shader material on the current object3d."
, tags: [ '3D' , 'three.js', 'material', 'shader' ]
, author: 'Gaspard Bucher <[email protected]>'
, origin: 'lucidity.io/three.Material'
, version: '1.0'
, expect:
{ object3d: 'THREE.Object3D'
, time: 'lucidity.Time'
, screen: 'lucidity.Screen'
}
}
const VERT = `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`
const FRAG = `
uniform sampler2D tDiffuse;
varying vec2 vUv;
void main() {
vec4 color = texture2D( tDiffuse, vUv );
gl_FragColor = color;
}
`