GLShape

Source: GLShape.ts

A GLShape holds custom geometry such as spheres, arrows, meshes, and isosurfaces.
Create it through GLViewer.addShape or a viewer shape helper so it belongs to the
scene. Call viewer.render() after adding geometry or updating styles. Use
GLViewer.removeShape to remove it and release rendering resources.


Constructor

new GLShape(stylespec: ShapeSpec): GLShape

Parameters

  • stylespec (ShapeSpec) — shape style specification

Instance Methods

addArrow(arrowSpec: ArrowSpec)

Creates an arrow shape

Parameters

Example

CODE
$3Dmol.download("pdb:4DM7",viewer,{},function(){
              viewer.setBackgroundColor(0xffffffff);
              viewer.addArrow({
                  start: {x:-10.0, y:0.0, z:0.0},
                  end: {x:0.0, y:-10.0, z:0.0},
                  radius: 1.0,
                  radiusRadio:1.0,
                  mid:1.0,
                  clickable:true,
                  callback:function(){
                      this.color.setHex(0xFF0000FF);
                      viewer.render( );
                  }
              });
              viewer.render();
            });

addBox(boxSpec: BoxSpec)

Creates a box

Parameters

Example

CODE
var shape = viewer.addShape({color:'red'});
     shape.addBox({corner: {x:1,y:2,z:0}, dimensions: {w: 4, h: 2, d: 6}});
     shape.addBox({corner: {x:-5,y:-3,z:0},
                   dimensions: { w: {x:1,y:1,z:0},
                                 h: {x:-1,y:1,z:0},
                                 d: {x:0,y:0,z:1} }});
     viewer.zoomTo();
     viewer.rotate(30);
     viewer.render();

addCurve(curveSpec: CurveSpec)

Creates a curved shape

Parameters

addCustom(customSpec: CustomShapeSpec)

Creates a custom shape from supplied vertex and face arrays

Parameters

addCylinder(cylinderSpec: CylinderSpec)

Creates a cylinder shape

Parameters

Example

CODE
viewer.addCylinder({start:{x:0.0,y:0.0,z:0.0},
                              end:{x:10.0,y:0.0,z:0.0},
                              radius:1.0,
                              fromCap:1,
                              toCap:2,
                              color:'red',
                              hoverable:true,
                              clickable:true,
                              callback:function(){ this.color.setHex(0x00FFFF00);viewer.render( );},
                              hover_callback: function(){ viewer.render( );},
                              unhover_callback: function(){ this.color.setHex(0xFF000000);viewer.render( );}
                             });
          viewer.addCylinder({start:{x:0.0,y:2.0,z:0.0},
                              end:{x:0.0,y:10.0,z:0.0},
                              radius:0.5,
                              fromCap:false,
                              toCap:true,
                              color:'teal'});
          viewer.addCylinder({start:{x:15.0,y:0.0,z:0.0},
                              end:{x:20.0,y:0.0,z:0.0},
                              radius:1.0,
                              color:'black',
                              fromCap:false,
                              toCap:false});
          viewer.render();

addDashedCylinder(cylinderSpec: CylinderSpec)

Creates a dashed cylinder shape

Parameters

addIsosurface( data: VolumeData, volSpec: IsoSurfaceSpec, callback?: function, viewer?: GLViewer, )

Create isosurface from volumetric data.

Parameters

  • data (VolumeData) — volumetric input data
  • volSpec (IsoSurfaceSpec) — volumetric data shape specification
  • callback (function, optional) — Called without arguments after the isosurface is created.
  • viewer (GLViewer, optional) — Viewer used to resolve volSpec.selection when coords are not supplied.

Example

CODE
//the user can specify a selected region for the isosurface 
     $.get('../test_structs/benzene-homo.cube', function(data){
              var voldata = new $3Dmol.VolumeData(data, "cube");
              viewer.addIsosurface(voldata, {isoval: 0.01,
                                             color: "blue",
                                             alpha: 0.5,
                                             smoothness: 10});
              viewer.addIsosurface(voldata, {isoval: -0.01,
                                             color: "red",
                                             smoothness: 5,
                                             opacity:0.5,
                                             wireframe:true,
                                             clickable:true,
                                             callback:
                                             function() {
                                                 this.opacity = 0.0;
                                                 viewer.render( );
                                             }});
              viewer.setStyle({}, {stick:{}});
              viewer.zoomTo();
              viewer.render();
            });

Positive and negative contours of an inline scalar grid

CODE
// An OpenDX grid with z varying fastest and two lobes of opposite sign.
var values = [];
for (var ix = 0; ix < 13; ix++) {
  for (var iy = 0; iy < 13; iy++) {
    for (var iz = 0; iz < 13; iz++) {
      var x = -3 + ix * 0.5, y = -3 + iy * 0.5, z = -3 + iz * 0.5;
      values.push(Math.exp(-((x + 1.4) ** 2 + y * y + z * z))
        - Math.exp(-((x - 1.4) ** 2 + y * y + z * z)));
    }
  }
}
var dx = [
  "object 1 class gridpositions counts 13 13 13", "origin -3 -3 -3",
  "delta 0.5 0 0", "delta 0 0.5 0", "delta 0 0 0.5",
  "object 2 class gridconnections counts 13 13 13",
  "object 3 class array type double rank 0 items 2197 data follows",
  values.join(" ")
].join("\n");
var volume = new $3Dmol.VolumeData(dx, "dx");
viewer.addIsosurface(volume, {isoval: 0.25, color: "blue", smoothness: 1});
viewer.addIsosurface(volume, {isoval: -0.25, color: "red", smoothness: 1});
viewer.addLabel("+0.25", {position: {x: -1.4, y: -1.6, z: 0},
  alignment: "topCenter", fontSize: 16, backgroundColor: "blue"});
viewer.addLabel("-0.25", {position: {x: 1.4, y: -1.6, z: 0},
  alignment: "topCenter", fontSize: 16, backgroundColor: "red"});
viewer.zoomTo();
viewer.zoom(0.8);
viewer.render();

Omitted and empty spatial selections have different meanings

CODE
// Each panel has the same radial density. Only the unrestricted map is drawn.
function radialVolume(centerX) {
  var values = [];
  for (var ix = 0; ix < 7; ix++) {
    for (var iy = 0; iy < 7; iy++) {
      for (var iz = 0; iz < 7; iz++) {
        var x = -1.5 + ix * 0.5, y = -1.5 + iy * 0.5, z = -1.5 + iz * 0.5;
        values.push(Math.exp(-(x * x + y * y + z * z)));
      }
    }
  }
  return new $3Dmol.VolumeData([
    "object 1 class gridpositions counts 7 7 7",
    "origin " + (centerX - 1.5) + " -1.5 -1.5",
    "delta 0.5 0 0", "delta 0 0.5 0", "delta 0 0 0.5",
    "object 2 class gridconnections counts 7 7 7",
    "object 3 class array type double rank 0 items 343 data follows",
    values.join(" ")
  ].join("\n"), "dx");
}
viewer.addModel().addAtoms([-4, 0, 4].map(function(x) {
  return {elem: "C", x: x, y: 0, z: 0};
}));
viewer.setStyle({}, {sphere: {radius: 0.12, color: "gray"}});
viewer.addIsosurface(radialVolume(-4), {isoval: 0.25, color: "seagreen"});
viewer.addIsosurface(radialVolume(0), {isoval: 0.25, color: "red", coords: []});
viewer.addIsosurface(radialVolume(4), {isoval: 0.25, color: "blue",
  selection: {elem: "N"}}); // No nitrogen atoms: the selection is empty.
["unrestricted", "coords: []", "no N atoms"].forEach(function(text, i) {
  viewer.addLabel(text, {position: {x: -4 + i * 4, y: -1.7, z: 0},
    alignment: "topCenter", fontSize: 12, backgroundColor: "black"});
});
viewer.zoomTo();
viewer.zoom(0.8);
viewer.render();

addLine(lineSpec: LineSpec)

Creates a line shape

Parameters

Example

CODE
$3Dmol.download("pdb:2ABJ",viewer,{},function(){
              viewer.addLine({dashed:true,start:{x:0,y:0,z:0},end:{x:100,y:100,z:100}});
              viewer.render(callback);
          });

addSphere(sphereSpec: SphereSpec)

Creates a sphere shape

Parameters

Example

CODE
viewer.addSphere({center:{x:0,y:0,z:0},radius:10.0,color:'red'});
     
     viewer.render();

addVolumetricData( data: string | ArrayBuffer | Uint8Array | Int8Array, fmt: string, volSpec: IsoSurfaceSpec, )

deprecated
GLShape.ts

Use addIsosurface instead Creates custom shape from volumetric data

Parameters

  • data (string | ArrayBuffer | Uint8Array | Int8Array) — Volumetric input data
  • fmt (string) — Input data format (e.g. 'cube' for cube file format)
  • volSpec (IsoSurfaceSpec) — Volumetric data shape specification

getInternalState()

Capture the current geometry and appearance as JSON-safe data, without callbacks.

updateStyle(newspec: ShapeSpec)

Merge supplied style properties into this shape's existing style.
This changes appearance, not geometry dimensions or coordinates. Call
viewer.render() to display the changes.

Parameters

Example

CODE
let sphere = viewer.addSphere({center:{x:0,y:0,z:0},radius:10.0,color:'red'});
        sphere.updateStyle({color:'yellow',opacity:0.5});
        viewer.render();

Static Methods

fromInternalState(state): GLShape

Restore materialized geometry; no shape callbacks or source code are evaluated.

Parameters

  • state

Returns

updateGroupColor()

Write colors in a separate pass, with uniform/direct/mapped paths selected once.

Instance Fields

intersectionShape: IntersectionShapes