From a399b420f1dd6b69a7bb54442f64fffe3607740c Mon Sep 17 00:00:00 2001 From: gouldingken Date: Mon, 24 Oct 2016 10:21:30 -0400 Subject: [PATCH] moved glsl code to files referenced on ShaderIndex var --- src/AnimatedPoints.js | 45 ++------------------------ src/glsl/ShaderIndex.js | 7 ++++ src/glsl/animated-points-fragment.glsl | 7 ++++ src/glsl/animated-points-vertex.glsl | 27 ++++++++++++++++ 4 files changed, 44 insertions(+), 42 deletions(-) create mode 100644 src/glsl/ShaderIndex.js create mode 100644 src/glsl/animated-points-fragment.glsl create mode 100644 src/glsl/animated-points-vertex.glsl diff --git a/src/AnimatedPoints.js b/src/AnimatedPoints.js index 7eee572..2fd5812 100644 --- a/src/AnimatedPoints.js +++ b/src/AnimatedPoints.js @@ -1,4 +1,5 @@ 'use strict'; +import { ShaderIndex } from './glsl/ShaderIndex'; export default class AnimatedPoints { constructor(numberOfPoints) { @@ -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)); @@ -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 }); } diff --git a/src/glsl/ShaderIndex.js b/src/glsl/ShaderIndex.js new file mode 100644 index 0000000..58c0595 --- /dev/null +++ b/src/glsl/ShaderIndex.js @@ -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, +}; \ No newline at end of file diff --git a/src/glsl/animated-points-fragment.glsl b/src/glsl/animated-points-fragment.glsl new file mode 100644 index 0000000..c9aaa39 --- /dev/null +++ b/src/glsl/animated-points-fragment.glsl @@ -0,0 +1,7 @@ +#version 120 + +varying vec3 vColor; + +void main() { + gl_FragColor = vec4( vColor, 1.0 ); +} \ No newline at end of file diff --git a/src/glsl/animated-points-vertex.glsl b/src/glsl/animated-points-vertex.glsl new file mode 100644 index 0000000..cfe25d9 --- /dev/null +++ b/src/glsl/animated-points-vertex.glsl @@ -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; +} \ No newline at end of file