Skip to content

Commit

Permalink
#6 fixed support for webgl2 + added webgl2 test page
Browse files Browse the repository at this point in the history
  • Loading branch information
RonenNess committed Apr 28, 2020
1 parent 66c8033 commit 9bb596a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
14 changes: 12 additions & 2 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ animate();
// init demo
function init() {

// renderer
renderer = new THREE.WebGLRenderer();
// init as webgl2
if (window.webgl2) {
var canvas = document.createElement( 'canvas' );
var context = canvas.getContext( 'webgl2', { alpha: false } );
renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
}
// init as webgl1
else {
renderer = new THREE.WebGLRenderer();
}

// set size and clearcolor
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0x001122, 1 )
document.body.appendChild( renderer.domElement );
Expand Down
26 changes: 26 additions & 0 deletions demo/index_webgl2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Partykals Demo - WebGL 2</title>
<meta name="description" content="A little demo for the Partykals lib.">
<meta name="author" content="Ronen Ness">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<script src="explosion.js"></script>
<script src="three.js"></script>
<script src="stats.js"></script>
<script src="../dist/partykals.js"></script>
<script>
window.webgl2 = true;
</script>
<script src="demo.js"></script>

<p style="color:white; position:fixed; top:10px; right:25px;">
Controls:<br/>
WASD / Arrows: Move camera. <br />
Z / X: Zoom in and out.
</p>
</body>
</html>
10 changes: 5 additions & 5 deletions dist/partykals.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class ParticlesMaterial
}
if (options.map) {
flags += "#define TEXTURE\n";
uniforms.texture = { value: options.map };
uniforms._texture = { value: options.map };
}
if (options.perParticleColor) {
flags += "#define COLORING\n";
Expand Down Expand Up @@ -210,7 +210,7 @@ varying float vAlpha;
// diffuse texture
#ifdef TEXTURE
uniform sampler2D texture;
uniform sampler2D _texture;
#endif
// fragment shader main
Expand All @@ -229,15 +229,15 @@ void main()
float mid = 0.5;
vec2 rotated = vec2(cos(vRotation) * (gl_PointCoord.x - mid) + sin(vRotation) * (gl_PointCoord.y - mid) + mid,
cos(vRotation) * (gl_PointCoord.y - mid) - sin(vRotation) * (gl_PointCoord.x - mid) + mid);
vec4 texture = texture2D(texture, rotated);
vec4 textureCol = texture2D(_texture, rotated);
// no rotation
#else
vec2 coords = vec2((gl_PointCoord.x - 0.5) + 0.5, (gl_PointCoord.y - 0.5) + 0.5);
vec4 texture = texture2D(texture, coords);
vec4 textureCol = texture2D(_texture, coords);
#endif
// get color with texture
gl_FragColor = vec4( globalColor * vColor, vAlpha ) * texture;
gl_FragColor = vec4( globalColor * vColor, vAlpha ) * textureCol;
// no texture (colors only)
#else
Expand Down
2 changes: 1 addition & 1 deletion partykals/material/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ParticlesMaterial
}
if (options.map) {
flags += "#define TEXTURE\n";
uniforms.texture = { value: options.map };
uniforms._texture = { value: options.map };
}
if (options.perParticleColor) {
flags += "#define COLORING\n";
Expand Down
8 changes: 4 additions & 4 deletions partykals/material/shaders/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ varying float vAlpha;
// diffuse texture
#ifdef TEXTURE
uniform sampler2D texture;
uniform sampler2D _texture;
#endif
// fragment shader main
Expand All @@ -41,15 +41,15 @@ void main()
float mid = 0.5;
vec2 rotated = vec2(cos(vRotation) * (gl_PointCoord.x - mid) + sin(vRotation) * (gl_PointCoord.y - mid) + mid,
cos(vRotation) * (gl_PointCoord.y - mid) - sin(vRotation) * (gl_PointCoord.x - mid) + mid);
vec4 texture = texture2D(texture, rotated);
vec4 textureCol = texture2D(_texture, rotated);
// no rotation
#else
vec2 coords = vec2((gl_PointCoord.x - 0.5) + 0.5, (gl_PointCoord.y - 0.5) + 0.5);
vec4 texture = texture2D(texture, coords);
vec4 textureCol = texture2D(_texture, coords);
#endif
// get color with texture
gl_FragColor = vec4( globalColor * vColor, vAlpha ) * texture;
gl_FragColor = vec4( globalColor * vColor, vAlpha ) * textureCol;
// no texture (colors only)
#else
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ For more info, check out the randomizers under `randomizers/` folder, or under `
### 1.0.2 [WIP]

- Fixed `dispose` to also dispose the material.
- Fixed shaders to work with WebGL2 as well.

## License

Expand Down

0 comments on commit 9bb596a

Please sign in to comment.