From 6ae5a32795d69241a7fa1da5397e283d5657751b Mon Sep 17 00:00:00 2001 From: ANDREA GARGARO Date: Thu, 21 Nov 2024 00:00:46 +0100 Subject: [PATCH] Fix webgl warning --- src/core/InstancedMesh2.ts | 35 +++++++++++--------- src/core/feature/FrustumCulling.ts | 8 ++--- src/core/feature/LOD.ts | 6 ++-- src/core/utils/GLInstancedBufferAttribute.ts | 4 +-- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/core/InstancedMesh2.ts b/src/core/InstancedMesh2.ts index 71f6219..15d9019 100644 --- a/src/core/InstancedMesh2.ts +++ b/src/core/InstancedMesh2.ts @@ -49,17 +49,18 @@ export class InstancedMesh2< public raycastOnlyFrustum = false; public visibilityArray: boolean[]; public infoLOD: LODInfo = 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 void>(); protected readonly _instancesUseEuler: boolean; protected readonly _instance: InstancedEntity; @@ -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; @@ -118,7 +120,7 @@ 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 @@ -126,15 +128,16 @@ export class InstancedMesh2< } 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 { @@ -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); diff --git a/src/core/feature/FrustumCulling.ts b/src/core/feature/FrustumCulling.ts index e74d3f3..1d57892 100644 --- a/src/core/feature/FrustumCulling.ts +++ b/src/core/feature/FrustumCulling.ts @@ -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'; @@ -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; @@ -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; @@ -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 { diff --git a/src/core/feature/LOD.ts b/src/core/feature/LOD.ts index 22ede78..fe256e2 100644 --- a/src/core/feature/LOD.ts +++ b/src/core/feature/LOD.ts @@ -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' { @@ -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; }; @@ -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 { diff --git a/src/core/utils/GLInstancedBufferAttribute.ts b/src/core/utils/GLInstancedBufferAttribute.ts index 33c7361..822d883 100644 --- a/src/core/utils/GLInstancedBufferAttribute.ts +++ b/src/core/utils/GLInstancedBufferAttribute.ts @@ -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(); @@ -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);