source: issm/oecreview/Archive/20545-21336/ISSM-21137-21138.diff@ 21337

Last change on this file since 21337 was 21337, checked in by Mathieu Morlighem, 8 years ago

CHG: added Archive/20545-21336

File size: 16.1 KB
RevLine 
[21337]1Index: ../trunk-jpl/src/m/plot/applyoptions.js
2===================================================================
3--- ../trunk-jpl/src/m/plot/applyoptions.js (revision 21137)
4+++ ../trunk-jpl/src/m/plot/applyoptions.js (revision 21138)
5@@ -330,4 +330,61 @@
6 }
7 canvas.textcanvas = textcanvas;
8 } //}}}
9+ //Atmosphere {{{
10+ if (options.exist('atmosphere')) {
11+ if (options.getfieldvalue('atmosphere')=='on') {
12+ var meshresults = processmesh(md,data,options);
13+ var x = meshresults[0];
14+ var y = meshresults[1];
15+ var z = meshresults[2];
16+ var elements = meshresults[3];
17+ var is2d = meshresults[4];
18+ var isplanet = meshresults[5];
19+
20+ var modelxlim = [ArrayMin(x),ArrayMax(x)];
21+ var modelylim = [ArrayMin(y),ArrayMax(y)];
22+ var modelzlim = [ArrayMin(z),ArrayMax(z)];
23+ var xlim = options.getfieldvalue('xlim',modelxlim);
24+ var ylim = options.getfieldvalue('ylim',modelylim);
25+ var zlim = options.getfieldvalue('zlim',modelzlim);
26+ xmin = xlim[0];
27+ xmax = xlim[1];
28+ ymin = ylim[0];
29+ ymax = ylim[1];
30+ zmin = zlim[0];
31+ zmax = zlim[1];
32+
33+ var scale = 1;
34+ var atmosphereScale = 1.25;
35+
36+ //Atmosphere
37+ var node = Node(gl,options);
38+ canvas.nodes[canvas.nodes.length] = node;
39+ node["shaderName"] = "SkyFromSpace";
40+ node["shader"] = gl["shaders"][node["shaderName"]];
41+ node["drawOrder"] = 1;
42+ node["enableCullFace"] = true;
43+ node["mesh"] = GL.Mesh.icosahedron({size:6371000*atmosphereScale,subdivisions:6});
44+ node["useIndexBuffer"] = false;
45+ node["rotation"] = [0,0,0];
46+ node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
47+ node["scale"] = [scale, scale, scale];
48+ node["modelMatrix"] = recalculateModelMatrix(node);
49+
50+ //Skysphere
51+ var node = Node(gl,options);
52+ canvas.nodes[canvas.nodes.length] = node;
53+ node["shaderName"] = "Textured";
54+ node["shader"] = gl["shaders"][node["shaderName"]];
55+ node["drawOrder"] = 2;
56+ node["enableCullFace"] = true;
57+ node["mesh"] = GL.Mesh.sphere({size:6371000*10});
58+ node["texture"] = initTexture(gl,'../../../js/textures/TychoSkymapII_t4_2k.jpg');
59+ node["useIndexBuffer"] = false;
60+ node["rotation"] = [0,0,0];
61+ node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
62+ node["scale"] = [scale, scale, scale];
63+ node["modelMatrix"] = recalculateModelMatrix(node);
64+ }
65+ } //}}}
66 }
67Index: ../trunk-jpl/src/m/plot/plot_mesh.js
68===================================================================
69--- ../trunk-jpl/src/m/plot/plot_mesh.js (revision 21137)
70+++ ../trunk-jpl/src/m/plot/plot_mesh.js (revision 21138)
71@@ -60,8 +60,8 @@
72 var gl = canvas.gl;
73 var node = Node(gl,options);
74 canvas.nodes[canvas.nodes.length] = node;
75- scale = 1 / (xmax - xmin);
76- node["shaderName"] = "colored";
77+ scale = 1;
78+ node["shaderName"] = "Colored";
79 node["shader"] = gl["shaders"][node["shaderName"]];
80 node["lineWidth"] = options.getfieldvalue('linewidth',1);
81 node["scale"] = [scale, scale, scale * matrixscale];
82Index: ../trunk-jpl/src/m/plot/webgl.js
83===================================================================
84--- ../trunk-jpl/src/m/plot/webgl.js (revision 21137)
85+++ ../trunk-jpl/src/m/plot/webgl.js (revision 21138)
86@@ -1,8 +1,10 @@
87 /*This is where we have all our webgl relevant functionality for the plotting routines: */
88+
89 //{{{ Canvas Initialization
90 function initCanvas(options) {
91 //Initialize open Gl for each canvas, if needed:
92- var canvas=document.getElementById(options.getfieldvalue('canvasid'));
93+ canvas = document.getElementById(options.getfieldvalue('canvasid'));
94+ //var canvas = document.getElementById(options.getfieldvalue('canvasid'));
95 if (!canvas.initialized) {
96 canvas.gl = initWebGL(canvas,options);
97 canvas.nodes = [];
98@@ -29,7 +31,10 @@
99 gl.depthFunc(gl.LEQUAL);
100 // Enable color blending/overlay
101 gl.enable(gl.BLEND);
102-
103+ // Enable face culling
104+ gl.enable(gl.CULL_FACE);
105+ gl.cullFace(gl.FRONT);
106+
107 // Load shaders and store them in gl object
108 gl.shaders = loadShaders(gl);
109
110@@ -40,6 +45,7 @@
111 canvas.zoom = clamp(options.getfieldvalue('zoom',1.0), canvas.zoomBounds[0], canvas.zoomBounds[1]);
112 canvas.zoomLast = canvas.zoom;
113 canvas.cameraMatrix = mat4.create();
114+ canvas.vInverseMatrix = mat4.create();
115 canvas.translation = options.getfieldvalue('origin',[0,0,0.0]);
116 canvas.viewPanning = options.getfieldvalue('viewpanning','off') == 'on';
117 canvas.view = options.getfieldvalue('view',[0,90]); //0 azimuth - up is north, 90 elevation - looking straight down
118@@ -78,7 +84,7 @@
119 function Node(gl,options) { //{{{
120 //Returns a Node object that contains default display states for webgl object
121 return {buffers:[],
122- shader:gl.shaders["colored"],
123+ shader:gl.shaders["Colored"],
124 draw:null,
125 hideOcean:false,
126 level:0,
127@@ -94,7 +100,7 @@
128 rotation:vec3.fromValues(-90, 0, 0),
129 scale:vec3.fromValues(1, 1, 1),
130 modelMatrix:mat4.create(),
131- shaderName:"colored",
132+ shaderName:"Colored",
133 drawOrder:0,
134 maskEnabled:false,
135 maskHeight:150.0,
136@@ -145,90 +151,11 @@
137 //}}}
138 //{{{ Shader Loading
139 function loadShaders(gl) { //{{{
140- var shaderNames = ["colored", "unlit_textured"];
141 shaders = {};
142- shaders["colored"] = {loaded:false, vsh:{}, fsh:{}};
143- //basic phong shader
144- shaders["colored"] = new Shader('\
145- precision highp float;\
146- attribute vec3 a_vertex;\
147- attribute vec4 a_color;\
148- uniform mat4 u_mvp;\
149- uniform float u_alpha;\
150- varying vec4 v_color;\
151- void main() {\
152- gl_PointSize = 3.0;\
153- gl_Position = u_mvp * vec4(a_vertex.xyz, 1.0);\
154- v_color = vec4(a_color.xyz, u_alpha);\
155- }\
156- ', '\
157- precision mediump float;\
158- varying vec4 v_color;\
159- void main() {\
160- gl_FragColor = v_color;\
161- }\
162- ');
163-
164- shaders["unlit_textured"] = new Shader('\
165- precision highp float;\
166- attribute vec3 a_vertex;\
167- attribute vec2 a_coord;\
168- uniform mat4 u_mvp;\
169- varying vec2 v_coord;\
170- varying float v_z;\
171- void main() {\
172- gl_PointSize = 3.0;\
173- gl_Position = u_mvp * vec4(a_vertex.xyz, 1.0);\
174- v_coord = a_coord;\
175- v_z = a_vertex.z;\
176- }\
177- ', '\
178- precision mediump float;\
179- varying vec2 v_coord;\
180- varying float v_z;\
181- uniform sampler2D u_texture;\
182- uniform float u_alpha;\
183- uniform bool u_maskEnabled;\
184- uniform float u_maskHeight;\
185- uniform vec4 u_maskColor;\
186- void main() {\
187- if (u_maskEnabled && (v_z < u_maskHeight)) {\
188- gl_FragColor = vec4(u_maskColor.rgb, u_alpha);\
189- }\
190- else {\
191- gl_FragColor = vec4(texture2D(u_texture, v_coord).rgb, u_alpha);\
192- }\
193- }\
194- ');
195- /*
196- shaders["phong"] = new Shader('\
197- precision highp float;\
198- attribute vec3 a_vertex;\
199- attribute vec3 a_normal;\
200- attribute vec2 a_coord;\
201- varying vec3 v_normal;\
202- varying vec2 v_coord;\
203- uniform mat4 u_mvp;\
204- uniform mat4 u_model;\
205- void main() {\
206- v_coord = a_coord;\
207- v_normal = (u_model * vec4(a_normal,0.0)).xyz;\
208- gl_Position = u_mvp * vec4(a_vertex,1.0);\
209- }\
210- ', '\
211- precision highp float;\
212- varying vec3 v_normal;\
213- varying vec2 v_coord;\
214- uniform vec3 u_lightvector;\
215- uniform vec4 u_color;\
216- uniform sampler2D u_texture;\
217- void main() {\
218- vec3 N = normalize(v_normal);\
219- vec4 color = u_color * texture2D( u_texture, v_coord);\
220- gl_FragColor = color * max(0.0, dot(u_lightvector,N));\
221- }\
222- ');
223- */
224+ shaders["Colored"] = new Shader.fromURL("/js/shaders/Colored.vsh", "/js/shaders/Colored.fsh");
225+ shaders["Textured"] = new Shader.fromURL("/js/shaders/Textured.vsh", "/js/shaders/Textured.fsh");
226+ shaders["SkyFromSpace"] = new Shader.fromURL("/js/shaders/SkyFromSpace.vert", "/js/shaders/SkyFromSpace.frag");
227+ shaders["GroundFromSpace"] = new Shader.fromURL("/js/shaders/GroundFromSpace.vert", "/js/shaders/GroundFromSpace.frag");
228 return shaders;
229 } //}}}
230 //{{{ Interface Functions
231@@ -281,7 +208,7 @@
232 } //}}}
233 function onZoom(ev,canvas,displaylog) { //{{{
234 ev.preventDefault();
235- var delta = clamp(clamp(ev.scale || ev.wheelDelta || -ev.detail, -1, 1) * canvas.controlSensitivity * canvas.zoom / 20, -1.0, 1.0);
236+ var delta = clamp(ev.scale || ev.wheelDelta || -ev.detail, -1, 1) * canvas.controlSensitivity * canvas.zoom / 20;
237 canvas.zoom = clamp(canvas.zoom + delta, canvas.zoomBounds[0], canvas.zoomBounds[1]);
238
239 if (displaylog) console.log(canvas.zoom);
240@@ -299,14 +226,14 @@
241 var aspectRatio = canvas.clientWidth / canvas.clientHeight;
242
243 if (canvas.twod) {
244- mat4.ortho(pMatrix, -aspectRatio/canvas.zoom, aspectRatio/canvas.zoom, -1/canvas.zoom, 1/canvas.zoom, -1.0, 10000.0);
245+ 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);
246 }
247 else {
248- mat4.perspective(pMatrix, 60 * Math.PI / 180, aspectRatio, 0.001, 10000.0);
249+ mat4.perspective(pMatrix, 60 * Math.PI / 180, aspectRatio, 1000, 10000000000.0);
250 }
251
252 //Apply worldspace translation
253- mat4.translate(translateMatrix, translateMatrix, [canvas.translation[0], canvas.translation[2], canvas.translation[1]]);
254+ mat4.translate(translateMatrix, translateMatrix, [6371000.0 * canvas.translation[0], 6371000.0 * canvas.translation[2], 6371000.0 * canvas.translation[1]]);
255 mat4.multiply(vMatrix, translateMatrix, vMatrix);
256
257 //Calculate rotation around camera focal point about worldspace origin
258@@ -323,11 +250,14 @@
259
260 //Apply rotation and scaling transform
261 mat4.multiply(vMatrix, rotationMatrix, vMatrix);
262-
263+
264 //Apply screenspace translation
265 mat4.identity(translateMatrix);
266- mat4.translate(translateMatrix, translateMatrix, [0.0, 0.0, -1/canvas.zoom]);
267+ mat4.translate(translateMatrix, translateMatrix, [0.0, 0.0, -6371000.0/canvas.zoom]);
268 mat4.multiply(vMatrix, translateMatrix, vMatrix);
269+
270+ //Calculate inverse matrices for lighting
271+ mat4.invert(canvas.vInverseMatrix, vMatrix);
272
273 //Apply projection matrix to get camera matrix
274 mat4.multiply(canvas.cameraMatrix, pMatrix, vMatrix);
275@@ -340,30 +270,91 @@
276
277 if (node["texture"]) node["texture"].bind(0);
278 if (node["disableDepthTest"]) gl.disable(gl.DEPTH_TEST);
279-
280+ if (node["enableCullFace"]) gl.enable(gl.CULL_FACE);
281+
282+ gl.cullFace(node["cullFace"]);
283 gl.lineWidth(node["lineWidth"]);
284 gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
285
286+ //Setup for light that originates from camera
287+ var origin = vec3.fromValues(0, 0, 0);
288+ var lightOrigin = vec3.fromValues(0, 0, 0);
289+ var cameraPosition = vec3.create();
290+ vec3.transformMat4(origin, origin, canvas.vInverseMatrix);
291+ vec3.normalize(lightOrigin, lightOrigin);
292+ vec3.sub(cameraPosition, origin, node["translation"]);
293+ cameraHeight = vec3.length(cameraPosition);
294+
295+ var atm = { //Default Values
296+ wavelength_r: 0.65, //0.65 Red wavelength (micrometers)
297+ wavelength_g: 0.57, //0.57 Green wavelength (micrometers)
298+ wavelength_b: 0.475, //0.475 Green wavelength (micrometers)
299+ eSun: 100.0, //20.0 Sun intensity
300+ kRayleigh: 0.0025, //0.0025 Rayleigh scattering amount
301+ kMie: 0.000, //0.01 Mie scattering amount
302+ g: -0.99, //-0.99 Mie phase asymmetry/direction factor
303+ hdr_exposure: 0.8, //0.8 High Dynamic Range Exposure
304+ scale: 1.25, //1.025 Scale of atmosphere. WARNING: Change atmosphereScale in applyoptions.js, and scaling constants.
305+ scaleDepth: 0.25, //0.25 Percentage altitude at which the atmosphere's average density is found
306+ a: -0.00287, //-0.00287 Scaling constant a
307+ b: 0.459, //0.459 Scaling constant b
308+ c: 3.83, //3.83 Scaling constant c
309+ d: -6.80, //-6.80 Scaling constant d
310+ e: 3.6, //5.25 Scaling constant e. Lower when increasing atmosphere scale.
311+ attenuation: 0.5 //0.5 Strength of atmospheric scattering on ground shading.
312+ };
313+
314+ 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)];
315+ var innerRadius = 6371000.0;
316+ var outerRadius = innerRadius*atm.scale;
317+ var scale = 1.0 / (outerRadius - innerRadius);
318+ var scaleDepth = atm.scaleDepth;
319+
320 node["shader"].uniforms({
321- u_mvp: mvpMatrix,
322+ m4MVP: mvpMatrix,
323+ m4Model: node["modelMatrix"],
324 u_texture: 0,
325 u_alpha: node["alpha"],
326 u_maskEnabled: node["maskEnabled"],
327 u_maskHeight: node["maskHeight"],
328- u_maskColor: node["maskColor"]
329- })
330+ u_maskColor: node["maskColor"],
331+ v3CameraPosition: origin,
332+ v3Translate: node["translation"],
333+ v3LightPos: lightOrigin,
334+ v3InvWavelength: inv_wavelength4,
335+ fOuterRadius: outerRadius,
336+ fOuterRadius2: outerRadius * outerRadius,
337+ fInnerRadius: innerRadius,
338+ fInnerRadius2: innerRadius * innerRadius,
339+ fKrESun: atm.kRayleigh * atm.eSun,
340+ fKmESun: atm.kMie * atm.eSun,
341+ fKr4PI: atm.kRayleigh * 4 * Math.PI,
342+ fKm4PI: atm.kMie * 4 * Math.PI,
343+ fScale: scale,
344+ fScaleDepth: scaleDepth,
345+ fScaleOverScaleDepth: scale/scaleDepth,
346+ v3LightPosFrag: lightOrigin,
347+ fHdrExposure: atm.hdr_exposure,
348+ g: atm.g,
349+ g2: atm.g * atm.g,
350+ a: atm.a,
351+ b: atm.b,
352+ c: atm.c,
353+ d: atm.d,
354+ e: atm.e,
355+ attenuation: atm.attenuation
356+ });
357 if (node["useIndexBuffer"] == true) node["shader"].draw(node["mesh"], node["drawMode"], "indices");
358 else node["shader"].draw(node["mesh"], node["drawMode"]);
359
360 gl.enable(gl.DEPTH_TEST);
361+ gl.disable(gl.CULL_FACE);
362 } //}}}
363 function draw(canvas,options) { //{{{
364 // Ensure canvas and gl viewport sizes are the same
365- var displayWidth = canvas.clientWidth;
366- var displayHeight = canvas.clientHeight;
367- if (canvas.width != displayWidth || canvas.height != displayHeight) {
368- canvas.width = displayWidth;
369- canvas.height = displayHeight;
370+ if (canvas.width != canvas.clientWidth || canvas.height != canvas.clientHeight) {
371+ canvas.width = canvas.clientWidth;
372+ canvas.height = canvas.clientHeight;
373 canvas.gl.viewport(0, 0, canvas.width, canvas.height);
374 }
375
376@@ -385,11 +376,9 @@
377 gl.clearColor(canvas.backgroundcolor[0], canvas.backgroundcolor[1], canvas.backgroundcolor[2], canvas.backgroundcolor[3]);
378 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
379
380-
381-
382 updateCameraMatrix(canvas);
383
384- var drawPassNumber = 2;
385+ var drawPassNumber = 3;
386 for (var i = drawPassNumber - 1; i >= 0; i--) {
387 for (var node in nodes) {
388 if (nodes[node]["drawOrder"] == i) drawSceneGraphNode(canvas,nodes[node]);
389Index: ../trunk-jpl/src/m/plot/plot_overlay.js
390===================================================================
391--- ../trunk-jpl/src/m/plot/plot_overlay.js (revision 21137)
392+++ ../trunk-jpl/src/m/plot/plot_overlay.js (revision 21138)
393@@ -60,8 +60,8 @@
394 var gl = canvas.gl;
395 var node = Node(gl,options);
396 canvas.nodes[canvas.nodes.length] = node;
397- scale = 1 / (xmax - xmin);
398- node["shaderName"] = "unlit_textured";
399+ scale = 1;
400+ node["shaderName"] = "GroundFromSpace";
401 node["shader"] = gl["shaders"][node["shaderName"]];
402 node["scale"] = [scale, scale, scale * matrixscale];
403 node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
404Index: ../trunk-jpl/src/m/plot/plot_quiver.js
405===================================================================
406--- ../trunk-jpl/src/m/plot/plot_quiver.js (revision 21137)
407+++ ../trunk-jpl/src/m/plot/plot_quiver.js (revision 21138)
408@@ -62,8 +62,8 @@
409 var gl = canvas.gl;
410 var node = Node(gl,options);
411 canvas.nodes[canvas.nodes.length] = node;
412- scale = 1 / (xmax - xmin);
413- node["shaderName"] = "colored";
414+ scale = 1;
415+ node["shaderName"] = "Colored";
416 node["shader"] = gl["shaders"][node["shaderName"]];
417 node["lineWidth"] = options.getfieldvalue('linewidth',1);
418 node["scale"] = [scale, scale, scale * matrixscale];
419Index: ../trunk-jpl/src/m/plot/plot_unit.js
420===================================================================
421--- ../trunk-jpl/src/m/plot/plot_unit.js (revision 21137)
422+++ ../trunk-jpl/src/m/plot/plot_unit.js (revision 21138)
423@@ -57,13 +57,13 @@
424 zmin = zlim[0];
425 zmax = zlim[1];
426 var caxis;
427-
428+
429 //Compute gl variables:
430 var gl = canvas.gl;
431 var node = Node(gl,options);
432 canvas.nodes[canvas.nodes.length] = node;
433- scale = 1 / (xmax - xmin);
434- node["shaderName"] = "unlit_textured";
435+ scale = 1;
436+ node["shaderName"] = "Textured";
437 node["shader"] = gl["shaders"][node["shaderName"]];
438 node["scale"] = [scale, scale, scale * matrixscale];
439 node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
Note: See TracBrowser for help on using the repository browser.