UNPKG

ami-cjs.js

Version:

<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>

106 lines (80 loc) 5.33 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _shaders = require('./interpolation/shaders.interpolation'); var _shaders2 = _interopRequireDefault(_shaders); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Localizer fragment shader */ var _class = function () { /** * */ function _class(uniforms) { _classCallCheck(this, _class); this._uniforms = uniforms; this._functions = {}; this._main = ''; } /** * */ _createClass(_class, [{ key: 'functions', value: function functions() { if (this._main === '') { // if main is empty, functions can not have been computed this.main(); } var content = ''; for (var property in this._functions) { content += this._functions[property] + '\n'; } return content; } /** * */ }, { key: 'uniforms', value: function uniforms() { var content = ''; for (var property in this._uniforms) { var uniform = this._uniforms[property]; content += 'uniform ' + uniform.typeGLSL + ' ' + property; if (uniform && uniform.length) { content += '[' + uniform.length + ']'; } content += ';\n'; } return content; } /** * */ }, { key: 'main', value: function main() { // need to pre-call main to fill up the functions list this._main = '\nvoid intersectionProjection(\n in vec4 plane,\n in vec4 slice,\n out vec3 intersectionProjection){\n\n vec3 intersectionDirection = normalize(cross(plane.xyz, slice.xyz));\n vec3 intersectionPoint = \n cross(intersectionDirection,slice.xyz) * plane.w +\n cross(plane.xyz, intersectionDirection) * slice.w;\n\n intersectionProjection =\n intersectionPoint.xyz +\n (dot(vPos.xyz - intersectionPoint, intersectionDirection)\n * intersectionDirection);\n\n}\n\nvoid main(void) {\n vec4 c1 = vec4(0., 0., 0., 0.);\n vec4 c2 = vec4(0., 0., 0., 0.);\n vec4 c3 = vec4(0., 0., 0., 0.);\n\n // localizer #1\n // must be normalized!\n if(length(uPlane1.xyz) > 0.5) {\n vec3 projection1 = vec3(1.);\n intersectionProjection(\n uPlane1,\n uSlice,\n projection1\n );\n\n vec4 projInter1 = (vProjectionViewMatrix * vec4(projection1, 1.));\n vec3 ndc1 = projInter1.xyz / projInter1.w;\n vec2 screenSpace1 = (ndc1.xy * .5 + .5) * vec2(uCanvasWidth, uCanvasHeight);\n\n float d1 = distance(gl_FragCoord.xy, screenSpace1.xy);\n c1 = vec4(uPlaneColor1, 1. - smoothstep(.5, .7, d1));\n }\n\n // localizer #2\n if(length(uPlane2.xyz) > 0.5) {\n vec3 projection2 = vec3(1.);\n intersectionProjection(\n uPlane2,\n uSlice,\n projection2\n );\n\n vec4 projInter2 = (vProjectionViewMatrix * vec4(projection2, 1.));\n vec3 ndc2 = projInter2.xyz / projInter2.w;\n vec2 screenSpace2 = (ndc2.xy * .5 + .5) * vec2(uCanvasWidth, uCanvasHeight);\n\n float d2 = distance(gl_FragCoord.xy, screenSpace2.xy);\n c2 = vec4(uPlaneColor2, 1. - smoothstep(.5, .7, d2));\n }\n\n // localizer #3\n if(length(uPlane3.xyz) > 0.5) {\n vec3 projection3 = vec3(1.);\n intersectionProjection(\n uPlane3,\n uSlice,\n projection3\n );\n\n vec4 projInter3 = (vProjectionViewMatrix * vec4(projection3, 1.));\n vec3 ndc3 = projInter3.xyz / projInter3.w;\n vec2 screenSpace3 = (ndc3.xy * .5 + .5) * vec2(uCanvasWidth, uCanvasHeight);\n\n float d3 = distance(gl_FragCoord.xy, screenSpace3.xy);\n c3 = vec4(uPlaneColor3, 1. - smoothstep(.5, .7, d3));\n }\n\n vec3 colorMix = c1.xyz*c1.w + c2.xyz*c2.w + c3.xyz*c3.w;\n gl_FragColor = vec4(colorMix, max(max(c1.w, c2.w),c3.w));\n}\n '; } /** * */ }, { key: 'compute', value: function compute() { var shaderInterpolation = ''; // shaderInterpolation.inline(args) //true/false // shaderInterpolation.functions(args) return '\n// uniforms\n' + this.uniforms() + '\n\n// varying (should fetch it from vertex directly)\nvarying vec4 vPos;\nvarying mat4 vProjectionViewMatrix;\n\n// tailored functions\n' + this.functions() + '\n\n// main loop\n' + this._main + '\n '; } }]); return _class; }(); exports.default = _class; module.exports = exports['default'];