Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

uber / deck.gl / 13030

26 Aug 2019 - 19:27 coverage decreased (-2.6%) to 80.38%
13030

Pull #3490

travis-ci-com

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
integrate mapbox's near plane fix
Pull Request #3490: [MapboxLayer] integrate mapbox-gl's near plane fix

3369 of 4577 branches covered (73.61%)

Branch coverage included in aggregate %.

6877 of 8170 relevant lines covered (84.17%)

4644.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.3
/modules/mesh-layers/src/simple-mesh-layer/simple-mesh-layer.js
1
// Note: This file will either be moved back to deck.gl or reformatted to web-monorepo standards
15×
2
// Disabling lint temporarily to facilitate copying code in and out of this repo
3
/* eslint-disable */
4

5
// Copyright (c) 2015 Uber Technologies, Inc.
6
//
7
// Permission is hereby granted, free of charge, to any person obtaining a copy
8
// of this software and associated documentation files (the "Software"), to deal
9
// in the Software without restriction, including without limitation the rights
10
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
// copies of the Software, and to permit persons to whom the Software is
12
// furnished to do so, subject to the following conditions:
13
//
14
// The above copyright notice and this permission notice shall be included in
15
// all copies or substantial portions of the Software.
16
//
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
// THE SOFTWARE.
24

25
import {Layer} from '@deck.gl/core';
26
import GL from '@luma.gl/constants';
27
import {Model, Geometry, Texture2D, PhongMaterial, isWebGL2} from '@luma.gl/core';
28

29
import {MATRIX_ATTRIBUTES} from '../utils/matrix';
30

31
// NOTE(Tarek): Should eventually phase out the glsl1 versions.
32
import vs1 from './simple-mesh-layer-vertex.glsl1';
33
import fs1 from './simple-mesh-layer-fragment.glsl1';
34
import vs3 from './simple-mesh-layer-vertex.glsl';
35
import fs3 from './simple-mesh-layer-fragment.glsl';
36

37
// Replacement for the external assert method to reduce bundle size
38
function assert(condition, message) {
39
  if (!condition) {
Branches [[0, 0]] missed. 1×
UNCOV
40
    throw new Error(`deck.gl: ${message}`);
!
41
  }
42
}
43

44
/*
45
 * Convert image data into texture
46
 * @returns {Texture2D} texture
47
 */
48
function getTextureFromData(gl, data, opts) {
UNCOV
49
  if (data instanceof Texture2D) {
Branches [[1, 0], [1, 1]] missed. !
UNCOV
50
    return data;
!
51
  }
UNCOV
52
  return new Texture2D(gl, Object.assign({data}, opts));
!
53
}
54

55
function validateGeometryAttributes(attributes) {
56
  assert(
1×
57
    attributes.positions || attributes.POSITION,
58
    'SimpleMeshLayer requires "postions" or "POSITION" attribute in mesh property.'
59
  );
60
}
61

62
/*
63
 * Convert mesh data into geometry
64
 * @returns {Geometry} geometry
65
 */
66
function getGeometry(data) {
67
  if (data.attributes) {
Branches [[3, 1]] missed. 1×
68
    validateGeometryAttributes(data.attributes);
1×
69
    if (data instanceof Geometry) {
Branches [[4, 1]] missed. 1×
70
      return data;
1×
71
    } else {
UNCOV
72
      return new Geometry(data);
!
73
    }
UNCOV
74
  } else if (data.positions || data.POSITION) {
Branches [[5, 0], [5, 1], [6, 0], [6, 1]] missed. !
UNCOV
75
    validateGeometryAttributes(data);
!
UNCOV
76
    return new Geometry({
!
77
      attributes: data
78
    });
79
  }
UNCOV
80
  throw Error('Invalid mesh');
!
81
}
82

83
const DEFAULT_COLOR = [0, 0, 0, 255];
1×
84
const defaultMaterial = new PhongMaterial();
1×
85

86
const defaultProps = {
1×
87
  mesh: {value: null, type: 'object', async: true},
88
  texture: null,
89
  sizeScale: {type: 'number', value: 1, min: 0},
90
  // TODO - parameters should be merged, not completely overridden
91
  parameters: {
92
    depthTest: true,
93
    depthFunc: GL.LEQUAL
94
  },
95

96
  // NOTE(Tarek): Quick and dirty wireframe. Just draws
97
  // the same mesh with LINE_STRIPS. Won't follow edges
98
  // of the original mesh.
99
  wireframe: false,
100
  // Optional material for 'lighting' shader module
101
  material: defaultMaterial,
UNCOV
102
  getPosition: {type: 'accessor', value: x => x.position},
!
103
  getColor: {type: 'accessor', value: DEFAULT_COLOR},
104

105
  // yaw, pitch and roll are in degrees
106
  // https://en.wikipedia.org/wiki/Euler_angles
107
  // [pitch, yaw, roll]
108
  getOrientation: {type: 'accessor', value: [0, 0, 0]},
109
  getScale: {type: 'accessor', value: [1, 1, 1]},
110
  getTranslation: {type: 'accessor', value: [0, 0, 0]},
111
  // 4x4 matrix
112
  getTransformMatrix: {type: 'accessor', value: []}
113
};
114

115
export default class SimpleMeshLayer extends Layer {
116
  getShaders() {
117
    const gl2 = isWebGL2(this.context.gl);
1×
118
    const vs = gl2 ? vs3 : vs1;
Branches [[7, 0]] missed. 1×
119
    const fs = gl2 ? fs3 : fs1;
Branches [[8, 0]] missed. 1×
120

121
    return super.getShaders({vs, fs, modules: ['project32', 'phong-lighting', 'picking']});
1×
122
  }
123

124
  initializeState() {
125
    const attributeManager = this.getAttributeManager();
1×
126

127
    attributeManager.addInstanced({
1×
128
      instancePositions: {
129
        transition: true,
130
        type: this.use64bitPositions() ? GL.DOUBLE : GL.FLOAT,
Branches [[9, 1]] missed.
131
        size: 3,
132
        accessor: 'getPosition'
133
      },
134
      instanceColors: {
135
        type: GL.UNSIGNED_BYTE,
136
        transition: true,
137
        size: this.props.colorFormat.length,
138
        normalized: true,
139
        accessor: 'getColor',
140
        defaultValue: [0, 0, 0, 255]
141
      },
142
      instanceModelMatrix: MATRIX_ATTRIBUTES
143
    });
144

145
    this.setState({
1×
146
      // Avoid luma.gl's missing uniform warning
147
      // TODO - add feature to luma.gl to specify ignored uniforms?
148
      emptyTexture: new Texture2D(this.context.gl, {
149
        data: new Uint8Array(4),
150
        width: 1,
151
        height: 1
152
      })
153
    });
154
  }
155

156
  updateState({props, oldProps, changeFlags}) {
157
    super.updateState({props, oldProps, changeFlags});
12×
158

159
    if (props.mesh !== oldProps.mesh || changeFlags.extensionsChanged) {
12×
160
      if (this.state.model) {
Branches [[12, 0]] missed. 2×
UNCOV
161
        this.state.model.delete();
!
162
      }
163
      if (props.mesh) {
2×
164
        this.setState({model: this.getModel(props.mesh)});
1×
165

166
        const attributes = props.mesh.attributes || props.mesh;
Branches [[14, 1]] missed. 1×
167
        this.setState({
1×
168
          hasNormals: Boolean(attributes.NORMAL || attributes.normals)
Branches [[15, 1]] missed.
169
        });
170
      }
171
      this.getAttributeManager().invalidateAll();
2×
172
    }
173

174
    if (props.texture !== oldProps.texture) {
12×
175
      this.setTexture(props.texture);
1×
176
    }
177

178
    if (this.state.model) {
12×
179
      this.state.model.setDrawMode(this.props.wireframe ? GL.LINE_STRIP : GL.TRIANGLES);
11×
180
    }
181
  }
182

183
  finalizeState() {
184
    super.finalizeState();
1×
185

186
    this.state.emptyTexture.delete();
1×
187
    if (this.state.texture) {
Branches [[19, 0]] missed. 1×
UNCOV
188
      this.state.texture.delete();
!
189
    }
190
  }
191

192
  draw({uniforms}) {
193
    if (!this.state.model) {
13×
194
      return;
2×
195
    }
196

197
    const {sizeScale} = this.props;
11×
198

199
    this.state.model.draw({
11×
200
      uniforms: Object.assign({}, uniforms, {
201
        sizeScale,
202
        flatShade: !this.state.hasNormals
203
      })
204
    });
205
  }
206

207
  getModel(mesh) {
208
    const model = new Model(
1×
209
      this.context.gl,
210
      Object.assign({}, this.getShaders(), {
211
        id: this.props.id,
212
        geometry: getGeometry(mesh),
213
        isInstanced: true,
214
        shaderCache: this.context.shaderCache
215
      })
216
    );
217

218
    const {texture, emptyTexture} = this.state;
1×
219
    model.setUniforms({
1×
220
      sampler: texture || emptyTexture,
221
      hasTexture: Boolean(texture)
222
    });
223

224
    return model;
1×
225
  }
226

227
  setTexture(image) {
228
    const {gl} = this.context;
1×
229
    const {emptyTexture, model} = this.state;
1×
230

231
    if (this.state.texture) {
Branches [[22, 0]] missed. 1×
UNCOV
232
      this.state.texture.delete();
!
233
    }
234

235
    const texture = image ? getTextureFromData(gl, image) : null;
Branches [[23, 0]] missed. 1×
236
    this.setState({texture});
1×
237

238
    if (model) {
Branches [[24, 0]] missed. 1×
239
      // props.mesh may not be ready at this time.
240
      // The sampler will be set when `getModel` is called
UNCOV
241
      model.setUniforms({
!
242
        sampler: texture || emptyTexture,
Branches [[25, 0], [25, 1]] missed.
243
        hasTexture: Boolean(texture)
244
      });
245
    }
246
  }
247
}
248

249
SimpleMeshLayer.layerName = 'SimpleMeshLayer';
1×
250
SimpleMeshLayer.defaultProps = defaultProps;
1×
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2019 Coveralls, LLC