Changeset 21138


Ignore:
Timestamp:
08/15/16 17:19:06 (9 years ago)
Author:
dlcheng
Message:

CHG (JS): Adding atmospheric/skysphere rendering.

Location:
issm/trunk-jpl/src/m/plot
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/plot/applyoptions.js

    r20863 r21138  
    331331                canvas.textcanvas = textcanvas;
    332332        } //}}}
     333        //Atmosphere {{{
     334        if (options.exist('atmosphere')) {
     335                if (options.getfieldvalue('atmosphere')=='on') {
     336                        var meshresults = processmesh(md,data,options);
     337                        var x = meshresults[0];
     338                        var y = meshresults[1];
     339                        var z = meshresults[2];
     340                        var elements = meshresults[3];
     341                        var is2d = meshresults[4];
     342                        var isplanet = meshresults[5];
     343                       
     344                        var modelxlim = [ArrayMin(x),ArrayMax(x)];
     345                        var modelylim = [ArrayMin(y),ArrayMax(y)];
     346                        var modelzlim = [ArrayMin(z),ArrayMax(z)];
     347                        var xlim = options.getfieldvalue('xlim',modelxlim);
     348                        var ylim = options.getfieldvalue('ylim',modelylim);
     349                        var zlim = options.getfieldvalue('zlim',modelzlim);
     350                        xmin = xlim[0];
     351                        xmax = xlim[1];
     352                        ymin = ylim[0];
     353                        ymax = ylim[1];
     354                        zmin = zlim[0];
     355                        zmax = zlim[1];
     356
     357                        var scale = 1;
     358                        var atmosphereScale = 1.25;
     359                       
     360                        //Atmosphere
     361                        var node = Node(gl,options);
     362                        canvas.nodes[canvas.nodes.length] = node;
     363                        node["shaderName"] = "SkyFromSpace";
     364                        node["shader"] = gl["shaders"][node["shaderName"]];
     365                        node["drawOrder"] = 1;
     366                        node["enableCullFace"] = true;
     367                        node["mesh"] = GL.Mesh.icosahedron({size:6371000*atmosphereScale,subdivisions:6});
     368                        node["useIndexBuffer"] = false;
     369                        node["rotation"] = [0,0,0];
     370                        node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
     371                        node["scale"] = [scale, scale, scale];
     372                        node["modelMatrix"] = recalculateModelMatrix(node);
     373                       
     374                        //Skysphere
     375                        var node = Node(gl,options);
     376                        canvas.nodes[canvas.nodes.length] = node;
     377                        node["shaderName"] = "Textured";
     378                        node["shader"] = gl["shaders"][node["shaderName"]];
     379                        node["drawOrder"] = 2;
     380                        node["enableCullFace"] = true;
     381                        node["mesh"] = GL.Mesh.sphere({size:6371000*10});
     382                        node["texture"] = initTexture(gl,'../../../js/textures/TychoSkymapII_t4_2k.jpg');
     383                        node["useIndexBuffer"] = false;
     384                        node["rotation"] = [0,0,0];
     385                        node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
     386                        node["scale"] = [scale, scale, scale];
     387                        node["modelMatrix"] = recalculateModelMatrix(node);
     388                }
     389        } //}}}
    333390}
  • issm/trunk-jpl/src/m/plot/plot_mesh.js

    r21094 r21138  
    6161        var node = Node(gl,options);
    6262        canvas.nodes[canvas.nodes.length] = node;
    63         scale = 1 / (xmax - xmin);
    64         node["shaderName"] = "colored";
     63        scale = 1;
     64        node["shaderName"] = "Colored";
    6565        node["shader"] = gl["shaders"][node["shaderName"]];
    6666        node["lineWidth"] = options.getfieldvalue('linewidth',1);
  • issm/trunk-jpl/src/m/plot/plot_overlay.js

    r21094 r21138  
    6161        var node = Node(gl,options);
    6262        canvas.nodes[canvas.nodes.length] = node;
    63         scale = 1 / (xmax - xmin);
    64         node["shaderName"] = "unlit_textured";
     63        scale = 1;
     64        node["shaderName"] = "GroundFromSpace";
    6565        node["shader"] = gl["shaders"][node["shaderName"]];
    6666        node["scale"] = [scale, scale, scale * matrixscale];
  • issm/trunk-jpl/src/m/plot/plot_quiver.js

    r21094 r21138  
    6363        var node = Node(gl,options);
    6464        canvas.nodes[canvas.nodes.length] = node;
    65         scale = 1 / (xmax - xmin);
    66         node["shaderName"] = "colored";
     65        scale = 1;
     66        node["shaderName"] = "Colored";
    6767        node["shader"] = gl["shaders"][node["shaderName"]];
    6868        node["lineWidth"] = options.getfieldvalue('linewidth',1);
  • issm/trunk-jpl/src/m/plot/plot_unit.js

    r21094 r21138  
    5858        zmax = zlim[1];
    5959        var caxis;
    60        
     60
    6161        //Compute gl variables:
    6262        var gl = canvas.gl;
    6363        var node = Node(gl,options);
    6464        canvas.nodes[canvas.nodes.length] = node;
    65         scale = 1 / (xmax - xmin);
    66         node["shaderName"] = "unlit_textured";
     65        scale = 1;
     66        node["shaderName"] = "Textured";
    6767        node["shader"] = gl["shaders"][node["shaderName"]];
    6868        node["scale"] = [scale, scale, scale * matrixscale];
  • issm/trunk-jpl/src/m/plot/webgl.js

    r21094 r21138  
    11/*This is where we have all our webgl relevant functionality for the plotting routines: */
     2       
    23//{{{ Canvas Initialization
    34function initCanvas(options) {
    45        //Initialize open Gl for each canvas, if needed:
    5         var canvas=document.getElementById(options.getfieldvalue('canvasid'));
     6        canvas = document.getElementById(options.getfieldvalue('canvasid'));
     7        //var canvas = document.getElementById(options.getfieldvalue('canvasid'));
    68        if (!canvas.initialized) {
    79                canvas.gl = initWebGL(canvas,options);
     
    3032        // Enable color blending/overlay
    3133        gl.enable(gl.BLEND);
    32 
     34        // Enable face culling
     35        gl.enable(gl.CULL_FACE);
     36        gl.cullFace(gl.FRONT);
     37       
    3338        // Load shaders and store them in gl object
    3439        gl.shaders = loadShaders(gl);
     
    4146        canvas.zoomLast = canvas.zoom;
    4247        canvas.cameraMatrix = mat4.create();
     48        canvas.vInverseMatrix = mat4.create();
    4349        canvas.translation = options.getfieldvalue('origin',[0,0,0.0]);
    4450        canvas.viewPanning = options.getfieldvalue('viewpanning','off') == 'on';
     
    7985        //Returns a Node object that contains default display states for webgl object
    8086        return {buffers:[],
    81                 shader:gl.shaders["colored"],
     87                shader:gl.shaders["Colored"],
    8288                draw:null,
    8389                hideOcean:false,
     
    95101                scale:vec3.fromValues(1, 1, 1),
    96102                modelMatrix:mat4.create(),
    97                 shaderName:"colored",
     103                shaderName:"Colored",
    98104                drawOrder:0,
    99105                maskEnabled:false,
     
    146152//{{{ Shader Loading
    147153function loadShaders(gl) { //{{{
    148         var shaderNames = ["colored", "unlit_textured"];
    149154        shaders = {};
    150         shaders["colored"] = {loaded:false, vsh:{}, fsh:{}};
    151                         //basic phong shader
    152         shaders["colored"] = new Shader('\
    153                 precision highp float;\
    154                 attribute vec3 a_vertex;\
    155                 attribute vec4 a_color;\
    156                 uniform mat4 u_mvp;\
    157                 uniform float u_alpha;\
    158                 varying vec4 v_color;\
    159                 void main() {\
    160                         gl_PointSize = 3.0;\
    161                         gl_Position = u_mvp * vec4(a_vertex.xyz, 1.0);\
    162                         v_color = vec4(a_color.xyz, u_alpha);\
    163                 }\
    164                 ', '\
    165                 precision mediump float;\
    166                 varying vec4 v_color;\
    167                 void main() {\
    168                         gl_FragColor = v_color;\
    169                 }\
    170         ');
    171                        
    172         shaders["unlit_textured"] = new Shader('\
    173                 precision highp float;\
    174                 attribute vec3 a_vertex;\
    175                 attribute vec2 a_coord;\
    176                 uniform mat4 u_mvp;\
    177                 varying vec2 v_coord;\
    178                 varying float v_z;\
    179                 void main() {\
    180                         gl_PointSize = 3.0;\
    181                         gl_Position = u_mvp * vec4(a_vertex.xyz, 1.0);\
    182                         v_coord = a_coord;\
    183                         v_z = a_vertex.z;\
    184                 }\
    185                 ', '\
    186                 precision mediump float;\
    187                 varying vec2 v_coord;\
    188                 varying float v_z;\
    189                 uniform sampler2D u_texture;\
    190                 uniform float u_alpha;\
    191                 uniform bool u_maskEnabled;\
    192                 uniform float u_maskHeight;\
    193                 uniform vec4 u_maskColor;\
    194                 void main() {\
    195                         if (u_maskEnabled && (v_z < u_maskHeight)) {\
    196                                 gl_FragColor = vec4(u_maskColor.rgb, u_alpha);\
    197                         }\
    198                         else {\
    199                                 gl_FragColor = vec4(texture2D(u_texture, v_coord).rgb, u_alpha);\
    200                         }\
    201                 }\
    202         ');
    203         /*
    204         shaders["phong"] = new Shader('\
    205                 precision highp float;\
    206                 attribute vec3 a_vertex;\
    207                 attribute vec3 a_normal;\
    208                 attribute vec2 a_coord;\
    209                 varying vec3 v_normal;\
    210                 varying vec2 v_coord;\
    211                 uniform mat4 u_mvp;\
    212                 uniform mat4 u_model;\
    213                 void main() {\
    214                         v_coord = a_coord;\
    215                         v_normal = (u_model * vec4(a_normal,0.0)).xyz;\
    216                         gl_Position = u_mvp * vec4(a_vertex,1.0);\
    217                 }\
    218                 ', '\
    219                 precision highp float;\
    220                 varying vec3 v_normal;\
    221                 varying vec2 v_coord;\
    222                 uniform vec3 u_lightvector;\
    223                 uniform vec4 u_color;\
    224                 uniform sampler2D u_texture;\
    225                 void main() {\
    226                   vec3 N = normalize(v_normal);\
    227                   vec4 color = u_color * texture2D( u_texture, v_coord);\
    228                   gl_FragColor = color * max(0.0, dot(u_lightvector,N));\
    229                 }\
    230         ');
    231         */
     155        shaders["Colored"] = new Shader.fromURL("/js/shaders/Colored.vsh", "/js/shaders/Colored.fsh");
     156        shaders["Textured"] = new Shader.fromURL("/js/shaders/Textured.vsh", "/js/shaders/Textured.fsh");
     157        shaders["SkyFromSpace"] = new Shader.fromURL("/js/shaders/SkyFromSpace.vert", "/js/shaders/SkyFromSpace.frag");
     158        shaders["GroundFromSpace"] = new Shader.fromURL("/js/shaders/GroundFromSpace.vert", "/js/shaders/GroundFromSpace.frag");
    232159        return shaders;
    233160} //}}}
     
    282209function onZoom(ev,canvas,displaylog) { //{{{
    283210        ev.preventDefault();
    284         var delta = clamp(clamp(ev.scale || ev.wheelDelta || -ev.detail, -1, 1) * canvas.controlSensitivity * canvas.zoom / 20, -1.0, 1.0);
     211        var delta = clamp(ev.scale || ev.wheelDelta || -ev.detail, -1, 1) * canvas.controlSensitivity * canvas.zoom / 20;
    285212        canvas.zoom = clamp(canvas.zoom + delta, canvas.zoomBounds[0], canvas.zoomBounds[1]);
    286213       
     
    300227
    301228        if (canvas.twod) {
    302                 mat4.ortho(pMatrix, -aspectRatio/canvas.zoom, aspectRatio/canvas.zoom, -1/canvas.zoom, 1/canvas.zoom, -1.0, 10000.0);
     229                mat4.ortho(pMatrix, -aspectRatio*6371000.0/canvas.zoom, aspectRatio*6371000.0/canvas.zoom, -6371000.0/canvas.zoom, 6371000.0/canvas.zoom, -1.0, 10000000000.0);
    303230        }
    304231        else {
    305                 mat4.perspective(pMatrix, 60 * Math.PI / 180, aspectRatio, 0.001, 10000.0);
     232                mat4.perspective(pMatrix, 60 * Math.PI / 180, aspectRatio, 1000, 10000000000.0);
    306233        }
    307234       
    308235        //Apply worldspace translation
    309         mat4.translate(translateMatrix, translateMatrix, [canvas.translation[0], canvas.translation[2], canvas.translation[1]]);
     236        mat4.translate(translateMatrix, translateMatrix, [6371000.0 * canvas.translation[0], 6371000.0 * canvas.translation[2], 6371000.0 * canvas.translation[1]]);
    310237        mat4.multiply(vMatrix, translateMatrix, vMatrix);
    311238       
     
    324251        //Apply rotation and scaling transform
    325252        mat4.multiply(vMatrix, rotationMatrix, vMatrix);
    326        
     253
    327254        //Apply screenspace translation
    328255        mat4.identity(translateMatrix);
    329         mat4.translate(translateMatrix, translateMatrix, [0.0, 0.0, -1/canvas.zoom]);
     256        mat4.translate(translateMatrix, translateMatrix, [0.0, 0.0, -6371000.0/canvas.zoom]);
    330257        mat4.multiply(vMatrix, translateMatrix, vMatrix);
     258       
     259        //Calculate inverse matrices for lighting
     260        mat4.invert(canvas.vInverseMatrix, vMatrix);
    331261
    332262        //Apply projection matrix to get camera matrix
     
    341271        if (node["texture"]) node["texture"].bind(0);
    342272        if (node["disableDepthTest"]) gl.disable(gl.DEPTH_TEST);
    343        
     273        if (node["enableCullFace"]) gl.enable(gl.CULL_FACE);
     274
     275        gl.cullFace(node["cullFace"]);
    344276        gl.lineWidth(node["lineWidth"]);
    345277        gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    346278
     279        //Setup for light that originates from camera
     280        var origin = vec3.fromValues(0, 0, 0);
     281        var lightOrigin = vec3.fromValues(0, 0, 0);
     282        var cameraPosition = vec3.create();
     283        vec3.transformMat4(origin, origin, canvas.vInverseMatrix);
     284        vec3.normalize(lightOrigin, lightOrigin);
     285        vec3.sub(cameraPosition, origin, node["translation"]);
     286        cameraHeight = vec3.length(cameraPosition);
     287       
     288        var atm = {                                                     //Default Values
     289                                wavelength_r: 0.65,             //0.65          Red wavelength (micrometers)
     290                                wavelength_g: 0.57,                     //0.57          Green wavelength (micrometers)
     291                                wavelength_b: 0.475,            //0.475         Green wavelength (micrometers)
     292                                eSun: 100.0,                            //20.0          Sun intensity   
     293                                kRayleigh: 0.0025,                      //0.0025        Rayleigh scattering amount
     294                                kMie: 0.000,                            //0.01          Mie scattering amount
     295                                g: -0.99,                                       //-0.99         Mie phase asymmetry/direction factor
     296                                hdr_exposure: 0.8,                      //0.8           High Dynamic Range Exposure
     297                                scale: 1.25,                            //1.025         Scale of atmosphere. WARNING: Change atmosphereScale in applyoptions.js, and scaling constants.
     298                                scaleDepth: 0.25,                       //0.25          Percentage altitude at which the atmosphere's average density is found
     299                                a: -0.00287,                            //-0.00287      Scaling constant a
     300                                b: 0.459,                                       //0.459         Scaling constant b
     301                                c: 3.83,                                        //3.83          Scaling constant c
     302                                d: -6.80,                                       //-6.80         Scaling constant d
     303                                e: 3.6,                                         //5.25          Scaling constant e. Lower when increasing atmosphere scale.
     304                                attenuation: 0.5                        //0.5           Strength of atmospheric scattering on ground shading.
     305        };
     306                       
     307        var inv_wavelength4 = [1.0 / Math.pow(atm.wavelength_r, 4), 1.0 / Math.pow(atm.wavelength_g, 4), 1.0 / Math.pow(atm.wavelength_b, 4)];
     308        var innerRadius = 6371000.0;
     309        var outerRadius = innerRadius*atm.scale;
     310        var scale = 1.0 / (outerRadius - innerRadius);
     311        var scaleDepth = atm.scaleDepth;
     312       
    347313        node["shader"].uniforms({
    348                 u_mvp: mvpMatrix,
     314                m4MVP: mvpMatrix,
     315                m4Model: node["modelMatrix"],
    349316                u_texture: 0,
    350317                u_alpha: node["alpha"],
    351318                u_maskEnabled: node["maskEnabled"],
    352319                u_maskHeight: node["maskHeight"],
    353                 u_maskColor: node["maskColor"]
    354         })
     320                u_maskColor: node["maskColor"],
     321                v3CameraPosition: origin,
     322                v3Translate: node["translation"],
     323                v3LightPos: lightOrigin,
     324                v3InvWavelength: inv_wavelength4,
     325                fOuterRadius: outerRadius,
     326                fOuterRadius2: outerRadius * outerRadius,
     327                fInnerRadius: innerRadius,
     328                fInnerRadius2: innerRadius * innerRadius,
     329                fKrESun: atm.kRayleigh * atm.eSun,
     330                fKmESun: atm.kMie * atm.eSun,
     331                fKr4PI: atm.kRayleigh * 4 * Math.PI,
     332                fKm4PI: atm.kMie * 4 * Math.PI,
     333                fScale: scale,
     334                fScaleDepth: scaleDepth,
     335                fScaleOverScaleDepth: scale/scaleDepth,
     336                v3LightPosFrag: lightOrigin,
     337                fHdrExposure: atm.hdr_exposure,
     338                g: atm.g,                       
     339                g2: atm.g * atm.g,
     340                a: atm.a,
     341                b: atm.b,
     342                c: atm.c,
     343                d: atm.d,               
     344                e: atm.e,
     345                attenuation: atm.attenuation
     346        });
    355347        if (node["useIndexBuffer"] == true) node["shader"].draw(node["mesh"], node["drawMode"], "indices");
    356348        else node["shader"].draw(node["mesh"], node["drawMode"]);
    357349
    358350        gl.enable(gl.DEPTH_TEST);
     351        gl.disable(gl.CULL_FACE);
    359352} //}}}
    360353function draw(canvas,options) { //{{{
    361354        // Ensure canvas and gl viewport sizes are the same
    362         var displayWidth  = canvas.clientWidth;
    363         var displayHeight = canvas.clientHeight;
    364         if (canvas.width  != displayWidth || canvas.height != displayHeight) {
    365                 canvas.width  = displayWidth;
    366                 canvas.height = displayHeight;
     355        if (canvas.width  != canvas.clientWidth || canvas.height != canvas.clientHeight) {
     356                canvas.width  = canvas.clientWidth;
     357                canvas.height = canvas.clientHeight;
    367358                canvas.gl.viewport(0, 0, canvas.width, canvas.height);
    368359        }
     
    386377        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    387378       
    388        
    389        
    390379        updateCameraMatrix(canvas);
    391380       
    392         var drawPassNumber = 2;
     381        var drawPassNumber = 3;
    393382        for (var i = drawPassNumber - 1; i >= 0; i--) {
    394383                for (var node in nodes) {
Note: See TracChangeset for help on using the changeset viewer.