Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate textures holding color and visibility to be big enough to hold the correct number of tree indices. #313

Merged
merged 17 commits into from Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
db0434c
Add utility for computing power-of-two texture size to hold N elements.
larsmoa Mar 21, 2020
1c066b0
Typo.
larsmoa Mar 21, 2020
99a4592
Allocate textures of correct size for holding color and visibility.
larsmoa Mar 21, 2020
07f2653
Add test for createMaterials()
larsmoa Mar 21, 2020
3861987
Fix broken test.
larsmoa Mar 21, 2020
b365db0
Merge refs/heads/master into larsmoa/allocate-correct-texture-size
cognite-bulldozer[bot] Mar 22, 2020
60a71f8
Merge refs/heads/master into larsmoa/allocate-correct-texture-size
cognite-bulldozer[bot] Mar 22, 2020
e5b5554
Merge refs/heads/master into larsmoa/allocate-correct-texture-size
cognite-bulldozer[bot] Mar 23, 2020
14436e0
Fix broken test
larsmoa Mar 23, 2020
488ef93
Merge refs/heads/master into larsmoa/allocate-correct-texture-size
cognite-bulldozer[bot] Mar 23, 2020
a4ed70b
Merge refs/heads/master into larsmoa/allocate-correct-texture-size
cognite-bulldozer[bot] Mar 24, 2020
c02b026
Merge refs/heads/master into larsmoa/allocate-correct-texture-size
cognite-bulldozer[bot] Mar 25, 2020
f97e65d
Merge branch 'master' into larsmoa/allocate-correct-texture-size
larsmoa Mar 26, 2020
b5caea2
Fix broken test.
larsmoa Mar 26, 2020
bc79520
Merge branch 'larsmoa/allocate-correct-texture-size' of github.com:co…
larsmoa Mar 26, 2020
77f5b9f
Merge refs/heads/master into larsmoa/allocate-correct-texture-size
cognite-bulldozer[bot] Mar 26, 2020
0d0663c
Merge remote-tracking branch 'origin/master' into larsmoa/allocate-co…
larsmoa Mar 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,16 @@
/*!
* Copyright 2020 Cognite AS
*/

import { determinePowerOfTwoDimensions } from '../../utils/determinePowerOfTwoDimensions';

test('determinePowerOfTwoDimensions', () => {
expect(determinePowerOfTwoDimensions(0)).toEqual({ width: 1, height: 1 });
expect(determinePowerOfTwoDimensions(1)).toEqual({ width: 1, height: 1 });
expect(determinePowerOfTwoDimensions(2)).toEqual({ width: 2, height: 1 });
expect(determinePowerOfTwoDimensions(3)).toEqual({ width: 2, height: 2 });
expect(determinePowerOfTwoDimensions(1025)).toEqual({ width: 64, height: 32 });
expect(determinePowerOfTwoDimensions(2048)).toEqual({ width: 64, height: 32 });
expect(determinePowerOfTwoDimensions(2049)).toEqual({ width: 64, height: 64 });
expect(determinePowerOfTwoDimensions(4096 * 4096 - 1)).toEqual({ width: 4096, height: 4096 });
});
Expand Up @@ -9,7 +9,7 @@ import { consumeSectorSimple } from '../../../../views/threejs/cad/consumeSector
import { createMaterials } from '../../../../views/threejs/cad/materials';
import 'jest-extended';

const materials = createMaterials();
const materials = createMaterials(64);

describe('consumeSectorDetailed', () => {
const metadata: SectorMetadata = {
Expand Down
14 changes: 14 additions & 0 deletions viewer/src/__tests__/views/threejs/sector/materials.test.ts
@@ -0,0 +1,14 @@
/*!
* Copyright 2020 Cognite AS
*/

import { createMaterials } from '../../../../views/threejs/cad/materials';

describe('createMaterials', () => {
test('Positive treeIndexCount, creates materials', () => {
const materials = createMaterials(32);
for (const entry of Object.entries(materials)) {
expect(entry[1]).not.toBeNull();
}
});
});
3 changes: 1 addition & 2 deletions viewer/src/__tests__/views/threejs/sector/primitives.test.ts
Expand Up @@ -8,9 +8,8 @@ import { PrimitiveAttributes } from '../../../../workers/types/parser.types';
import { createEmptySector } from '../../../models/cad/emptySector';
import { createMaterials } from '../../../../views/threejs/cad/materials';

const materials = createMaterials();

describe('createPrimitives', () => {
const materials = createMaterials(64);
let emptySector: Sector;

beforeEach(() => {
Expand Down
18 changes: 18 additions & 0 deletions viewer/src/utils/determinePowerOfTwoDimensions.ts
@@ -0,0 +1,18 @@
/*!
* Copyright 2020 Cognite AS
*/

/**
* Computes minimal power-of-two width and height that holds at least the number of elements provided.
* This is useful to compute texture sizes.
*/
export function determinePowerOfTwoDimensions(elementCount: number): { width: number; height: number } {
const width = Math.max(1, ceilToPowerOfTwo(Math.sqrt(elementCount)));
const height = Math.max(1, ceilToPowerOfTwo(elementCount / width));
return { width, height };
}

const log2 = Math.log(2);
function ceilToPowerOfTwo(v: number): number {
return Math.pow(2, Math.ceil(Math.log(v) / log2));
}
3 changes: 2 additions & 1 deletion viewer/src/views/threejs/cad/CadNode.ts
Expand Up @@ -67,7 +67,8 @@ export class CadNode extends THREE.Object3D {
super();
this.type = 'CadNode';
this.name = 'Sector model';
this._materialManager = new MaterialManager(options ? options.nodeAppearance : undefined);
const treeIndexCount = model.scene.maxTreeIndex + 1;
this._materialManager = new MaterialManager(treeIndexCount, options ? options.nodeAppearance : undefined);

const rootSector = new RootSectorNode(model, this._materialManager.materials);
this._repository = new CachedRepository(model);
Expand Down
10 changes: 3 additions & 7 deletions viewer/src/views/threejs/cad/MaterialManager.ts
Expand Up @@ -3,11 +3,7 @@
*/

import { createMaterials, Materials } from './materials';
import {
VisibilityDelegate,
ColorDelegate,
NodeAppearance
} from '../../common/cad/NodeAppearance';
import { VisibilityDelegate, ColorDelegate, NodeAppearance } from '../../common/cad/NodeAppearance';

function updateColors(getColor: ColorDelegate, materials: Materials, treeIndices: number[]) {
for (const treeIndex of treeIndices) {
Expand All @@ -29,8 +25,8 @@ export class MaterialManager {
public readonly materials: Materials;
private readonly _options?: NodeAppearance;

constructor(options?: NodeAppearance) {
this.materials = createMaterials();
constructor(treeIndexCount: number, options?: NodeAppearance) {
this.materials = createMaterials(treeIndexCount);
this._options = options;
}

Expand Down
20 changes: 9 additions & 11 deletions viewer/src/views/threejs/cad/materials.ts
Expand Up @@ -5,6 +5,7 @@
import * as THREE from 'three';
import { sectorShaders, shaderDefines } from './shaders';
import { RenderMode } from '../materials';
import { determinePowerOfTwoDimensions } from '../../../utils/determinePowerOfTwoDimensions';

export interface Materials {
// Materials
Expand All @@ -28,18 +29,15 @@ export interface Materials {
overrideVisibilityPerTreeIndex: THREE.DataTexture;
}

export function createMaterials(): Materials {
const pixelCount = 2048;
const colorCount = pixelCount * pixelCount;
const visibilityCount = pixelCount * pixelCount;
export function createMaterials(treeIndexCount: number): Materials {
const textureDims = determinePowerOfTwoDimensions(treeIndexCount);
const textureElementCount = textureDims.width * textureDims.height;

const colors = new Uint8Array(4 * colorCount);
const visibility = new Uint8Array(4 * visibilityCount);
for (let i = 0; i < 4 * visibilityCount; i++) {
visibility[i] = 255;
}
const overrideColorPerTreeIndex = new THREE.DataTexture(colors, pixelCount, pixelCount);
const overrideVisibilityPerTreeIndex = new THREE.DataTexture(visibility, pixelCount, pixelCount);
const colors = new Uint8Array(4 * textureElementCount);
const visibility = new Uint8Array(4 * textureElementCount);
visibility.fill(255);
const overrideColorPerTreeIndex = new THREE.DataTexture(colors, textureDims.width, textureDims.height);
const overrideVisibilityPerTreeIndex = new THREE.DataTexture(visibility, textureDims.width, textureDims.height);

const boxMaterial = new THREE.ShaderMaterial({
name: 'Primitives (Box)',
Expand Down