-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathstate-based-shader.html
172 lines (128 loc) · 4.69 KB
/
state-based-shader.html
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html>
<head>
<title>A-Frame - custom shaders</title>
<meta name="description" content="A-Frame - custom shaders">
<script src="js/aframe-master-v1.3.0.min.js"></script>
<script src="js/aframe-environment-component.js"></script>
</head>
<body>
<script>
AFRAME.registerComponent('life', {
schema: {
updateInterval: {type: 'float', default: 1.0},
},
init: function() {
console.log("life started")
const rtWidth = 512;
const rtHeight = 512;
this.renderTarget0 = new THREE.WebGLRenderTarget(rtWidth, rtHeight);
this.renderTarget0.texture.magFilter = THREE.NearestFilter;
this.renderTarget0.texture.minFilter = THREE.NearestFilter;
this.renderTarget0.texture.generateMipmaps = false;
this.renderTarget1 = new THREE.WebGLRenderTarget(rtWidth, rtHeight);
this.renderTarget1.texture.magFilter = THREE.NearestFilter;
this.renderTarget1.texture.minFilter = THREE.NearestFilter;
this.renderTarget1.texture.generateMipmaps = false;
// ================================================
let self = this;
this.material1 = new THREE.ShaderMaterial( {
uniforms: {
time: { value: 1.0 },
tex: { type: "t", value: self.renderTarget0.texture},
},
vertexShader: `
varying vec2 vUv;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader: `
varying vec2 vUv;
uniform sampler2D tex;
// uniform float timeMsec;
void main()
{
vec4 color = texture2D(tex, vUv);
color = vec4( color.rgb + 0.02, 1.0 );
gl_FragColor = color;
}
`
});
// =========================================================
const texture = new THREE.TextureLoader().load( 'images/color-grid.png' );
this.material0 = new THREE.MeshBasicMaterial({ map: texture }); // after first update, switch to material0.map to this.renderTarget1.texture
// separate scene #0 for texture processing
const geometry0 = new THREE.PlaneGeometry(2, 2);
const plane0 = new THREE.Mesh(geometry0, this.material0);
this.rtScene0 = new THREE.Scene();
this.rtScene0.add(plane0);
// this.material1 is define above
// separate scene #1 for texture processing
const geometry1 = new THREE.PlaneGeometry(2, 2);
const plane1 = new THREE.Mesh(geometry1, this.material1);
this.rtScene1 = new THREE.Scene();
this.rtScene1.add(plane1);
// both scenes can use the same basic camera
this.rtCamera = new THREE.OrthographicCamera();
this.rtCamera.position.z = 0.1;
// only update textures periodically
this.timer = 0;
this.clock = new THREE.Clock();
// initialize during first tick()
this.plane0 = null;
this.plane1 = null;
},
tick: function() {
if (this.plane0 == null)
{
this.plane0 = document.querySelector("#plane0");
this.plane1 = document.querySelector("#plane1");
this.plane0.object3D.children[0].material = this.material0;
this.plane1.object3D.children[0].material = this.material1;
}
let deltaTime = this.clock.getDelta();
this.timer += deltaTime;
if (this.timer > this.data.updateInterval)
{
console.log("tick update")
// reset the timer
this.timer = 0;
// during first update, material0 is using texture data from a separate source
// take a snapshot of scene0, copy it into renderTarget0.
// the texture from renderTarget0 is used in material1,
// so this updates material1 in the main scene.
this.el.sceneEl.renderer.setRenderTarget(this.renderTarget0);
this.el.sceneEl.renderer.render(this.rtScene0, this.rtCamera);
this.el.sceneEl.renderer.setRenderTarget(null);
// connect up the loop: now, redirect material0 to get texture data from renderTarget1
// (this has no effect after first update)
this.material0.map = this.renderTarget1.texture;
// take a snapshot of scene1, copy it into renderTarget1.
// the texture from renderTarget1 is used in material0,
// so this updates material0 in the main scene.
this.el.sceneEl.renderer.setRenderTarget(this.renderTarget1);
this.el.sceneEl.renderer.render(this.rtScene1, this.rtCamera);
this.el.sceneEl.renderer.setRenderTarget(null);
}
},
});
</script>
<a-scene environment>
<a-sky color = "#000337"></a-sky>
<a-entity life></a-entity>
<a-plane
id="plane0"
position = "-1.1 1 -2"
width="2" height="2">
</a-plane>
<a-plane
id="plane1"
position = "1.1 1 -2"
width="2" height="2">
</a-plane>
</a-scene>
</body>
</html>