Skip to content

Commit

Permalink
Fix webgl warning
Browse files Browse the repository at this point in the history
  • Loading branch information
agargaro committed Nov 20, 2024
1 parent 08631bf commit 6ae5a32
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
35 changes: 19 additions & 16 deletions src/core/InstancedMesh2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ export class InstancedMesh2<
public raycastOnlyFrustum = false;
public visibilityArray: boolean[];
public infoLOD: LODInfo<TCustomData> = null; // TODO rename
/** @internal */ public _indexArray: Uint16Array | Uint32Array;
/** @internal */ public _matrixArray: Float32Array;
/** @internal */ public _colorArray: Float32Array = null;
/** @internal */ public _count: number;
/** @internal */ public _perObjectFrustumCulled = true;
/** @internal */ public _sortObjects = false;
/** @internal */ public _maxCount: number;
/** @internal */ public _visibilityChanged = false;
/** @internal */ _renderer: WebGLRenderer = null;
/** @internal */ _indexArray: Uint16Array | Uint32Array;
/** @internal */ _matrixArray: Float32Array;
/** @internal */ _colorArray: Float32Array = null;
/** @internal */ _count: number;
/** @internal */ _perObjectFrustumCulled = true;
/** @internal */ _sortObjects = false;
/** @internal */ _maxCount: number;
/** @internal */ _visibilityChanged = false;
/** @internal */ _geometry: TGeometry;
/** @internal */ _material: TMaterial;
/** @internal */ public _parentLOD: InstancedMesh2;
/** @internal */ _parentLOD: InstancedMesh2;
protected _uniformsSetCallback = new Map<string, (id: number, value: UniformValue) => void>();
protected readonly _instancesUseEuler: boolean;
protected readonly _instance: InstancedEntity;
Expand Down Expand Up @@ -107,6 +108,7 @@ export class InstancedMesh2<

super(geometry, material);

this._renderer = renderer;
this._instancesUseEuler = instancesUseEuler;
this._instance = new InstancedEntity(this, -1, instancesUseEuler);
this.instancesCount = count;
Expand All @@ -118,23 +120,24 @@ export class InstancedMesh2<
this.visibilityArray = LOD?.visibilityArray ?? new Array(count).fill(true);

this.initIndexArray();
this.initIndexAttribute(renderer);
this.initIndexAttribute();
this.initMatricesTexture();

this.patchMaterial(this.customDepthMaterial); // TODO check if with LOD can reuse it
this.patchMaterial(this.customDistanceMaterial);
}

public override onBeforeShadow(renderer: WebGLRenderer, scene: Scene, camera: Camera, shadowCamera: Camera, geometry: BufferGeometry, depthMaterial: Material, group: Group): void {
if (this.instanceIndex) this.performFrustumCulling(renderer, shadowCamera, camera);
if (this.instanceIndex) this.performFrustumCulling(shadowCamera, camera);
}

public override onBeforeRender(renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group): void {
if (this.instanceIndex) this.performFrustumCulling(renderer, camera);
if (this.instanceIndex) this.performFrustumCulling(camera);
else this._renderer = renderer;
}

public override onAfterRender(renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group): void {
if (!this.instanceIndex) this.initIndexAttribute(renderer);
if (!this.instanceIndex) this.initIndexAttribute();
}

protected initIndexArray(): void {
Expand All @@ -148,14 +151,14 @@ export class InstancedMesh2<
this._indexArray = array;
}

protected initIndexAttribute(renderer: WebGLRenderer): void {
if (!renderer) {
protected initIndexAttribute(): void {
if (!this._renderer) {
this._count = 0;
return;
}

const array = this._indexArray;
const gl = renderer.getContext() as WebGL2RenderingContext;
const gl = this._renderer.getContext() as WebGL2RenderingContext;

this.instanceIndex = new GLInstancedBufferAttribute(gl, gl.UNSIGNED_INT, 1, 4, array); // UNSIGNED_SHORT usare anche questo se < 65k
this._geometry?.setAttribute('instanceIndex', this.instanceIndex as unknown as BufferAttribute);
Expand Down
8 changes: 4 additions & 4 deletions src/core/feature/FrustumCulling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BVHNode } from 'bvh.js';
import { Camera, Frustum, Material, Matrix4, Sphere, Vector3, WebGLRenderer } from 'three';
import { Camera, Frustum, Material, Matrix4, Sphere, Vector3 } from 'three';
import { getMaxScaleOnAxisAt, getPositionAt } from '../../utils/MatrixUtils.js';
import { sortOpaque, sortTransparent } from '../../utils/SortingUtils.js';
import { InstancedMesh2 } from '../InstancedMesh2.js';
Expand All @@ -10,7 +10,7 @@ import { LODRenderList } from './LOD.js';

declare module '../InstancedMesh2.js' {
interface InstancedMesh2 {
performFrustumCulling(renderer: WebGLRenderer, camera: Camera, cameraLOD?: Camera): void;
performFrustumCulling(camera: Camera, cameraLOD?: Camera): void;

/** @internal */ frustumCulling(camera: Camera): void;
/** @internal */ updateIndexArray(): void;
Expand All @@ -34,7 +34,7 @@ const _cameraLODPos = new Vector3();
const _position = new Vector3();
const _sphere = new Sphere();

InstancedMesh2.prototype.performFrustumCulling = function (renderer: WebGLRenderer, camera: Camera, cameraLOD = camera): void {
InstancedMesh2.prototype.performFrustumCulling = function (camera: Camera, cameraLOD = camera): void {
const info = this.infoLOD;
const isShadowRendering = camera !== cameraLOD;
let renderList: LODRenderList;
Expand All @@ -52,7 +52,7 @@ InstancedMesh2.prototype.performFrustumCulling = function (renderer: WebGLRender
if (renderList?.levels.length > 0) this.frustumCullingLOD(renderList, info.objects, camera, cameraLOD);
else if (!this._parentLOD) this.frustumCulling(camera);

this.instanceIndex.update(renderer, this._count);
this.instanceIndex.update(this._renderer, this._count);
};

InstancedMesh2.prototype.frustumCulling = function (camera: Camera): void {
Expand Down
6 changes: 3 additions & 3 deletions src/core/feature/LOD.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BufferGeometry, Material, RawShaderMaterial } from 'three';
import { BufferGeometry, Material, ShaderMaterial } from 'three';
import { InstancedMesh2 } from '../InstancedMesh2.js';

declare module '../InstancedMesh2.js' {
Expand Down Expand Up @@ -94,6 +94,7 @@ InstancedMesh2.prototype.addShadowLOD = function (geometry: BufferGeometry, dist

const object = this.addLevel(this.infoLOD.shadowRender, geometry, null, distance, hysteresis);
object.castShadow = true;
this.castShadow = true;

return this;
};
Expand All @@ -107,8 +108,7 @@ InstancedMesh2.prototype.addLevel = function (renderList: LODRenderList, geometr

const objIndex = objectsList.findIndex((e) => e.geometry === geometry);
if (objIndex === -1) {
// check if MeshBasicMaterial is better than RawShaderMaterial
object = new InstancedMesh2(undefined, this._maxCount, geometry, material ?? new RawShaderMaterial(), this); // TODO fix renderer param
object = new InstancedMesh2(this._renderer, this._maxCount, geometry, material ?? new ShaderMaterial(), this);
objectsList.push(object);
this.add(object); // TODO handle render order?
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils/GLInstancedBufferAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class GLInstancedBufferAttribute extends GLBufferAttribute {
public isGLInstancedBufferAttribute = true;
public meshPerAttribute: number;
public array: TypedArray;
/** @internal */ public _needsUpdate = false;
/** @internal */ _needsUpdate = false;

constructor(gl: WebGL2RenderingContext, type: GLenum, itemSize: number, elementSize: 1 | 2 | 4, array: TypedArray, meshPerAttribute = 1) {
const buffer = gl.createBuffer();
Expand All @@ -21,7 +21,7 @@ export class GLInstancedBufferAttribute extends GLBufferAttribute {
public update(renderer: WebGLRenderer, count: number): void {
if (!this._needsUpdate) return;

const gl = renderer.getContext();
const gl = renderer.getContext(); // TODO check performance or cache it
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.array, 0, count);

Expand Down

0 comments on commit 6ae5a32

Please sign in to comment.