Skip to content

Commit

Permalink
moved glsl code to files referenced on ShaderIndex var
Browse files Browse the repository at this point in the history
  • Loading branch information
gouldingken committed Oct 24, 2016
1 parent 716398b commit a399b42
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 42 deletions.
45 changes: 3 additions & 42 deletions src/AnimatedPoints.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
import { ShaderIndex } from './glsl/ShaderIndex';
export default class AnimatedPoints {

constructor(numberOfPoints) {
Expand Down Expand Up @@ -41,46 +42,6 @@ export default class AnimatedPoints {
}

getMaterial() {
//another approach to using shader glsl files and importing them into js https://github.com/mrdoob/three.js/blob/master/src/renderers/shaders/ShaderChunk.js
var vertexShader = () => {
return `
attribute float size_from;
attribute float size_to;
attribute float r_from;
attribute float g_from;
attribute float b_from;
attribute float r_to;
attribute float g_to;
attribute float b_to;
attribute vec3 position_to;
varying vec3 vColor;
uniform float animationPos;
void main() {
vColor = vec3(
r_from * (1.0 - animationPos) + r_to * animationPos,
g_from * (1.0 - animationPos) + g_to * animationPos,
b_from * (1.0 - animationPos) + b_to * animationPos
);
gl_Position = projectionMatrix * modelViewMatrix * vec4(position * (1.0 - animationPos) + position_to * animationPos, 1.0);
gl_PointSize = size_from * (1.0 - animationPos) + size_to * animationPos;
}
`
};
var fragmentShader = () => {
return `
varying vec3 vColor;
void main() {
gl_FragColor = vec4( vColor, 1.0 );
}
`
};

//"position" is used internally by ThreeJS so the name cannot change
this.geometry.addAttribute('position', new THREE.BufferAttribute(this.fromPositions, 3));
this.geometry.addAttribute('position_to', new THREE.BufferAttribute(this.toPositions, 3));
Expand All @@ -94,8 +55,8 @@ export default class AnimatedPoints {
uniforms: {
animationPos: {value: this.animationPos}
},
vertexShader: vertexShader(),
fragmentShader: fragmentShader()
vertexShader: ShaderIndex.animated_points_vertex,
fragmentShader: ShaderIndex.animated_points_fragment

});
}
Expand Down
7 changes: 7 additions & 0 deletions src/glsl/ShaderIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import animated_points_vertex from './animated-points-vertex.glsl';
import animated_points_fragment from './animated-points-fragment.glsl';

export var ShaderIndex = {
animated_points_vertex: animated_points_vertex,
animated_points_fragment: animated_points_fragment,
};
7 changes: 7 additions & 0 deletions src/glsl/animated-points-fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#version 120

varying vec3 vColor;

void main() {
gl_FragColor = vec4( vColor, 1.0 );
}
27 changes: 27 additions & 0 deletions src/glsl/animated-points-vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 120

attribute float size_from;
attribute float size_to;

attribute float r_from;
attribute float g_from;
attribute float b_from;

attribute float r_to;
attribute float g_to;
attribute float b_to;

attribute vec3 position_to;

varying vec3 vColor;
uniform float animationPos;

void main() {
vColor = vec3(
r_from * (1.0 - animationPos) + r_to * animationPos,
g_from * (1.0 - animationPos) + g_to * animationPos,
b_from * (1.0 - animationPos) + b_to * animationPos
);
gl_Position = projectionMatrix * modelViewMatrix * vec4(position * (1.0 - animationPos) + position_to * animationPos, 1.0);
gl_PointSize = size_from * (1.0 - animationPos) + size_to * animationPos;
}

0 comments on commit a399b42

Please sign in to comment.