diff --git a/samples/simple/Gltf2ImporterShadow/main.ts b/samples/simple/Gltf2ImporterShadow/main.ts index 1492d78d1..5be7cbe68 100644 --- a/samples/simple/Gltf2ImporterShadow/main.ts +++ b/samples/simple/Gltf2ImporterShadow/main.ts @@ -179,11 +179,11 @@ declare const Rn: typeof _Rn; ); material.setParameter( Rn.ShaderSemantics.DiffuseColorFactor, - new Rn.Vector4(0.0, 0.0, 0.0, 0.0) + Rn.Vector4.fromCopyArray([0.0, 0.0, 0.0, 0.0]) ); material.setParameter( Rn.ShadowMapDecodeClassicSingleMaterialNode.ShadowColorFactor, - new Rn.Vector4(0.0, 0.0, 0.0, 0.5) + Rn.Vector4.fromCopyArray([0.0, 0.0, 0.0, 0.5]) ); material.alphaMode = Rn.AlphaMode.Translucent; diff --git a/samples/simple/NodeEditor/main.ts b/samples/simple/NodeEditor/main.ts index 199575c24..fa1886cad 100644 --- a/samples/simple/NodeEditor/main.ts +++ b/samples/simple/NodeEditor/main.ts @@ -9,12 +9,18 @@ declare const Rn: typeof _Rn; Rn.CompositionType.Vec4, Rn.ComponentType.Float ); - constant1.setDefaultInputValue('value', new Rn.Vector4(1, 2, 3, 4)); + constant1.setDefaultInputValue( + 'value', + Rn.Vector4.fromCopyArray([1, 2, 3, 4]) + ); const constant2 = new Rn.ConstantVariableShaderNode( Rn.CompositionType.Vec4, Rn.ComponentType.Float ); - constant2.setDefaultInputValue('value', new Rn.Vector4(4, 3, 2, 1)); + constant2.setDefaultInputValue( + 'value', + Rn.Vector4.fromCopyArray([4, 3, 2, 1]) + ); const addShaderNode = new Rn.AddShaderNode( Rn.CompositionType.Vec4, diff --git a/samples/simple/ShadowMapping/main.ts b/samples/simple/ShadowMapping/main.ts index 3b49666a9..8d060da58 100644 --- a/samples/simple/ShadowMapping/main.ts +++ b/samples/simple/ShadowMapping/main.ts @@ -17,10 +17,10 @@ declare const Rn: typeof _Rn; const resolutionDepthCamera = 512; - const diffuseColorFactorSmallBoard = new Rn.Vector4(0.5, 0.1, 0.4, 1); - const diffuseColorFactorLargeBoard = new Rn.Vector4(0.1, 0.7, 0.5, 1); + const diffuseColorFactorSmallBoard = Rn.Vector4.fromCopyArray([0.5, 0.1, 0.4, 1]); + const diffuseColorFactorLargeBoard = Rn.Vector4.fromCopyArray([0.1, 0.7, 0.5, 1]); - const shadowColorFactorLargeBoard = new Rn.Vector4(0.05, 0.35, 0.25, 1); + const shadowColorFactorLargeBoard = Rn.Vector4.fromCopyArray([0.05, 0.35, 0.25, 1]); // ---main algorithm----------------------------------------------------------------------------------------- diff --git a/samples/simple/SkinShader/main.ts b/samples/simple/SkinShader/main.ts index 6a8df126f..5208015cf 100644 --- a/samples/simple/SkinShader/main.ts +++ b/samples/simple/SkinShader/main.ts @@ -19,7 +19,7 @@ const setupRenderPassEntityUidOutput = function (rootGroup: Entity, cameraCompon const framebuffer = Rn.RenderableHelper.createTexturesForRenderTarget(canvas.clientWidth, canvas.clientHeight, 1, {}); renderPass.setFramebuffer(framebuffer); - renderPass.clearColor = new Rn.Vector4(0, 0, 0, 1); + renderPass.clearColor = Rn.Vector4.fromCopyArray([0, 0, 0, 1]); renderPass.toClearColorBuffer = true; renderPass.toClearDepthBuffer = true; diff --git a/samples/simple/VideoTexture/index.html b/samples/simple/VideoTexture/index.html index c95c12fae..12b04071a 100644 --- a/samples/simple/VideoTexture/index.html +++ b/samples/simple/VideoTexture/index.html @@ -26,6 +26,7 @@

Rendering Test Image

+
diff --git a/samples/simple/VideoTexture/main.ts b/samples/simple/VideoTexture/main.ts index 69a955efe..10f014f6c 100644 --- a/samples/simple/VideoTexture/main.ts +++ b/samples/simple/VideoTexture/main.ts @@ -113,3 +113,8 @@ declare const Rn: typeof _Rn; draw(); })(); + +window.exportGltf2 = function () { + const exporter = Rn.Gltf2Exporter.getInstance(); + exporter.export('Rhodonite'); +}; diff --git a/samples/test_e2e/ColorGradingUsingLUTs/main.ts b/samples/test_e2e/ColorGradingUsingLUTs/main.ts index 2b5ab5625..87177684f 100644 --- a/samples/test_e2e/ColorGradingUsingLUTs/main.ts +++ b/samples/test_e2e/ColorGradingUsingLUTs/main.ts @@ -79,7 +79,7 @@ declare const Rn: typeof _Rn; renderPass.cameraComponent = cameraComponent; const entitySmallBoard = createEntityColoredBoard( - new Rn.Vector4(0.5, 0.1, 0.4, 1) + Rn.Vector4.fromCopyArray([0.5, 0.1, 0.4, 1]) ); const transformSmallBoard = entitySmallBoard.getTransform(); transformSmallBoard.scale = new Rn.Vector3(0.2, 0.2, 0.2); @@ -87,7 +87,7 @@ declare const Rn: typeof _Rn; transformSmallBoard.rotate = new Rn.Vector3(Math.PI / 2, 0, 0); const entityLargeBoard = createEntityColoredBoard( - new Rn.Vector4(0.1, 0.7, 0.5, 1) + Rn.Vector4.fromCopyArray([0.1, 0.7, 0.5, 1]) ); const transformLargeBoard = entityLargeBoard.getTransform(); transformLargeBoard.translate = new Rn.Vector3(15, 30, -1.5); diff --git a/samples/test_e2e/GaussianBlur/main.ts b/samples/test_e2e/GaussianBlur/main.ts index 230aef52e..b7350a9d9 100644 --- a/samples/test_e2e/GaussianBlur/main.ts +++ b/samples/test_e2e/GaussianBlur/main.ts @@ -95,7 +95,7 @@ declare const Rn: typeof _Rn; renderPass.cameraComponent = cameraComponent; const entitySmallBoard = createEntityColoredBoard( - new Rn.Vector4(0.5, 0.1, 0.4, 1) + Rn.Vector4.fromCopyArray([0.5, 0.1, 0.4, 1]) ); const transformSmallBoard = entitySmallBoard.getTransform(); transformSmallBoard.scale = new Rn.Vector3(0.2, 0.2, 0.2); @@ -103,7 +103,7 @@ declare const Rn: typeof _Rn; transformSmallBoard.rotate = new Rn.Vector3(Math.PI / 2, 0, 0); const entityLargeBoard = createEntityColoredBoard( - new Rn.Vector4(0.1, 0.7, 0.5, 1) + Rn.Vector4.fromCopyArray([0.1, 0.7, 0.5, 1]) ); const transformLargeBoard = entityLargeBoard.getTransform(); transformLargeBoard.translate = new Rn.Vector3(15, 30, -1.5); diff --git a/samples/test_e2e/Gltf2Importer-GlareEffect/main.ts b/samples/test_e2e/Gltf2Importer-GlareEffect/main.ts index 244c42a85..becf5ad58 100644 --- a/samples/test_e2e/Gltf2Importer-GlareEffect/main.ts +++ b/samples/test_e2e/Gltf2Importer-GlareEffect/main.ts @@ -62,7 +62,7 @@ declare const Rn: typeof _Rn; createAndSetFramebuffer(renderPassHDR, rnCanvasElement.width, 1, { type: Rn.ComponentType.HalfFloat, }); - renderPassHDR.clearColor = new Rn.Vector4(0.0, 0.0, 0.0, 1.0); + renderPassHDR.clearColor = Rn.Vector4.fromCopyArray([0.0, 0.0, 0.0, 1.0]); const materialHighLuminance = Rn.MaterialHelper.createDetectHighLuminanceMaterial( {maxInstancesNumber: 1}, @@ -335,7 +335,7 @@ declare const Rn: typeof _Rn; // need to draw the full viewport size renderPassBlurH.setViewport( - new Rn.Vector4(0, 0, resolutionBlur, resolutionBlur) + Rn.Vector4.fromCopyArray([0, 0, resolutionBlur, resolutionBlur]) ); } renderPassBlurH.cameraComponent = cameraComponentPostEffect; @@ -480,7 +480,7 @@ declare const Rn: typeof _Rn; ) { const renderPass = new Rn.RenderPass(); renderPass.toClearColorBuffer = true; - renderPass.clearColor = new Rn.Vector4(1.0, 1.0, 1.0, 1.0); + renderPass.clearColor = Rn.Vector4.fromCopyArray([1.0, 1.0, 1.0, 1.0]); renderPass.cameraComponent = cameraComponent; renderPass.addEntities([entityGlareTarget]); renderPass.setMaterial(material); diff --git a/samples/test_e2e/GltfImporter-draco-extension/index.html b/samples/test_e2e/GltfImporter-draco-extension/index.html index 0092ec8f3..6b5b62cfb 100644 --- a/samples/test_e2e/GltfImporter-draco-extension/index.html +++ b/samples/test_e2e/GltfImporter-draco-extension/index.html @@ -29,6 +29,7 @@

Rendering Test Image

+
diff --git a/samples/test_e2e/GltfImporter-draco-extension/main.ts b/samples/test_e2e/GltfImporter-draco-extension/main.ts index e61a5b7f6..2a21b27ac 100644 --- a/samples/test_e2e/GltfImporter-draco-extension/main.ts +++ b/samples/test_e2e/GltfImporter-draco-extension/main.ts @@ -1,5 +1,6 @@ import _Rn, {CameraComponent, Material} from '../../../dist/esm/index'; +declare const window: any; declare const Rn: typeof _Rn; const p = document.createElement('p'); document.body.appendChild(p); @@ -183,3 +184,8 @@ function setTextureParameterForMeshComponents( } } } + +window.exportGltf2 = function () { + const exporter = Rn.Gltf2Exporter.getInstance(); + exporter.export('Rhodonite'); +}; diff --git a/samples/test_e2e/GltfImporter-ibl-1/index.html b/samples/test_e2e/GltfImporter-ibl-1/index.html index a303081c2..c1d81a7f3 100644 --- a/samples/test_e2e/GltfImporter-ibl-1/index.html +++ b/samples/test_e2e/GltfImporter-ibl-1/index.html @@ -27,6 +27,10 @@

Rhodonite E2E Test - GltfImporter-ibl-1

Rendering Test Image

+
+ + +

Correct PNG Image

diff --git a/samples/test_e2e/GltfImporter-ibl-1/main.ts b/samples/test_e2e/GltfImporter-ibl-1/main.ts index 109c539da..b66804b9e 100644 --- a/samples/test_e2e/GltfImporter-ibl-1/main.ts +++ b/samples/test_e2e/GltfImporter-ibl-1/main.ts @@ -6,6 +6,7 @@ import _Rn, { } from '../../../dist/esm/index'; declare const Rn: typeof _Rn; +declare const window: any; const p = document.createElement('p'); document.body.appendChild(p); @@ -206,3 +207,8 @@ function setTextureParameterForMeshComponents( } } } + +window.exportGltf2 = function () { + const exporter = Rn.Gltf2Exporter.getInstance(); + exporter.export('Rhodonite'); +}; diff --git a/samples/test_e2e/GltfImporter-orbit-moving/main.ts b/samples/test_e2e/GltfImporter-orbit-moving/main.ts index 74479d506..071b7f667 100644 --- a/samples/test_e2e/GltfImporter-orbit-moving/main.ts +++ b/samples/test_e2e/GltfImporter-orbit-moving/main.ts @@ -80,7 +80,7 @@ document.body.appendChild(p); } mainExpression.renderPasses[0].toClearColorBuffer = true; mainExpression.renderPasses[0].toClearDepthBuffer = true; - mainExpression.renderPasses[0].clearColor = new Rn.Vector4(0, 0, 0, 0); + mainExpression.renderPasses[0].clearColor = Rn.Vector4.fromCopyArray([0, 0, 0, 0]); const postEffectCameraEntity = createPostEffectCameraEntity(); const postEffectCameraComponent = postEffectCameraEntity.getCamera(); diff --git a/samples/test_e2e/GltfImporter-specify-noLight/main.ts b/samples/test_e2e/GltfImporter-specify-noLight/main.ts index dc6db2ec5..de6922d91 100644 --- a/samples/test_e2e/GltfImporter-specify-noLight/main.ts +++ b/samples/test_e2e/GltfImporter-specify-noLight/main.ts @@ -58,7 +58,7 @@ let p = null; setParameterForMeshComponents( meshComponents, Rn.ShaderSemantics.BaseColorFactor, - new Rn.Vector4(0.5, 0.5, 0.5, 1.0) + Rn.Vector4.fromCopyArray([0.5, 0.5, 0.5, 1.0]) ); // cameraController diff --git a/samples/test_e2e/MultiPass/main.ts b/samples/test_e2e/MultiPass/main.ts index bb653be7a..e600e27c5 100644 --- a/samples/test_e2e/MultiPass/main.ts +++ b/samples/test_e2e/MultiPass/main.ts @@ -41,7 +41,7 @@ declare const Stats: any; Promise.all(promises).then(() => { const system = Rn.System.getInstance(); const entityRepository = Rn.EntityRepository.getInstance(); - + const gl = system.setProcessApproachAndCanvas( Rn.ProcessApproach.UniformWebGL1, document.getElementById('world') as HTMLCanvasElement @@ -107,7 +107,7 @@ declare const Stats: any; //primitive.material.setTextureParameter(Rn.ShaderSemantics.DiffuseColorTexture, texture); primitive.material.setParameter( Rn.ShaderSemantics.DiffuseColorFactor, - new Rn.Vector4(1, 0, 1, 1) + Rn.Vector4.fromCopyArray([1, 0, 1, 1]) ); const entities = []; @@ -242,7 +242,7 @@ declare const Stats: any; })(); window.exportGltf2 = function () { - + const exporter = Rn.Gltf2Exporter.getInstance(); exporter.export('Rhodonite'); -}; \ No newline at end of file +}; diff --git a/samples/test_e2e/PixelPickingTest/main.ts b/samples/test_e2e/PixelPickingTest/main.ts index 6d6fac581..adb4f599d 100644 --- a/samples/test_e2e/PixelPickingTest/main.ts +++ b/samples/test_e2e/PixelPickingTest/main.ts @@ -26,7 +26,7 @@ const setupRenderPassEntityUidOutput = function ( {} ); renderPass.setFramebuffer(framebuffer); - renderPass.clearColor = new Rn.Vector4(0, 0, 0, 1); + renderPass.clearColor = Rn.Vector4.fromCopyArray([0, 0, 0, 1]); renderPass.toClearColorBuffer = true; renderPass.toClearDepthBuffer = true; @@ -58,7 +58,7 @@ const pick = function (e: any) { const pickedPixel = renderTargetTexture.getPixelValueAt(x, y); console.log(pickedPixel.toString()); - const bitDec = new Rn.Vector4(1, 255, 65025, 0); + const bitDec = Rn.Vector4.fromCopyArray([1, 255, 65025, 0]); const pickedEntityUID = bitDec.dot(pickedPixel); console.log(pickedEntityUID); @@ -209,7 +209,7 @@ let p: any; })(); window.exportGltf2 = function () { - + const exporter = Rn.Gltf2Exporter.getInstance(); exporter.export('Rhodonite'); }; diff --git a/samples/test_e2e/ShadowMap/main.ts b/samples/test_e2e/ShadowMap/main.ts index 10e748ad6..a69623c21 100644 --- a/samples/test_e2e/ShadowMap/main.ts +++ b/samples/test_e2e/ShadowMap/main.ts @@ -67,17 +67,17 @@ document.body.appendChild(p); setParameterForMeshComponent( meshComponentSmallBoard, Rn.ShaderSemantics.DiffuseColorFactor, - new Rn.Vector4(0.5, 0.1, 0.4, 1) + Rn.Vector4.fromCopyArray([0.5, 0.1, 0.4, 1]) ); setParameterForMeshComponent( meshComponentLargeBoard, Rn.ShaderSemantics.DiffuseColorFactor, - new Rn.Vector4(0.1, 0.7, 0.5, 1) + Rn.Vector4.fromCopyArray([0.1, 0.7, 0.5, 1]) ); setParameterForMeshComponent( meshComponentLargeBoard, Rn.ShadowMapDecodeClassicSingleMaterialNode.ShadowColorFactor, - new Rn.Vector4(0.05, 0.35, 0.25, 1) + Rn.Vector4.fromCopyArray([0.05, 0.35, 0.25, 1]) ); const scaleSmallBoard = new Rn.Vector3(0.2, 0.2, 0.2); diff --git a/samples/test_e2e/ShadowMapDebugMode/main.ts b/samples/test_e2e/ShadowMapDebugMode/main.ts index fa0ebe2d0..951b9114c 100644 --- a/samples/test_e2e/ShadowMapDebugMode/main.ts +++ b/samples/test_e2e/ShadowMapDebugMode/main.ts @@ -67,22 +67,22 @@ let p: any; setParameterForMeshComponent( meshComponentSmallBoard, Rn.ShaderSemantics.DiffuseColorFactor, - new Rn.Vector4(0.5, 0.1, 0.4, 1) + Rn.Vector4.fromCopyArray([0.5, 0.1, 0.4, 1]) ); setParameterForMeshComponent( meshComponentLargeBoard, Rn.ShaderSemantics.DiffuseColorFactor, - new Rn.Vector4(0.1, 0.7, 0.5, 1) + Rn.Vector4.fromCopyArray([0.1, 0.7, 0.5, 1]) ); setParameterForMeshComponent( meshComponentLargeBoard, Rn.ShadowMapDecodeClassicSingleMaterialNode.DebugColorFactor, - new Rn.Vector4(0.85, 0.0, 0.0, 1.0) + Rn.Vector4.fromCopyArray([0.85, 0.0, 0.0, 1.0]) ); setParameterForMeshComponent( meshComponentLargeBoard, Rn.ShadowMapDecodeClassicSingleMaterialNode.ShadowColorFactor, - new Rn.Vector4(0.05, 0.35, 0.25, 1) + Rn.Vector4.fromCopyArray([0.05, 0.35, 0.25, 1]) ); const scaleSmallBoard = new Rn.Vector3(0.2, 0.2, 0.2); diff --git a/samples/test_e2e/ShadowMapVariance/main.ts b/samples/test_e2e/ShadowMapVariance/main.ts index d80fb9da9..da828f165 100644 --- a/samples/test_e2e/ShadowMapVariance/main.ts +++ b/samples/test_e2e/ShadowMapVariance/main.ts @@ -203,11 +203,11 @@ declare const Rn: typeof _Rn; ); materialSphere.setParameter( Rn.ShaderSemantics.DiffuseColorFactor, - new Rn.Vector4(0.5, 0.1, 0.4, 1) + Rn.Vector4.fromCopyArray([0.5, 0.1, 0.4, 1]) ); materialSphere.setParameter( Rn.VarianceShadowMapDecodeClassicSingleMaterialNode.ShadowColor, - new Rn.Vector4(0.25, 0.05, 0.2, 1) + Rn.Vector4.fromCopyArray([0.25, 0.05, 0.2, 1]) ); materialSphere.setParameter( Rn.VarianceShadowMapDecodeClassicSingleMaterialNode.MinimumVariance, @@ -223,11 +223,11 @@ declare const Rn: typeof _Rn; ); materialBoard.setParameter( Rn.ShaderSemantics.DiffuseColorFactor, - new Rn.Vector4(0.1, 0.7, 0.5, 1) + Rn.Vector4.fromCopyArray([0.1, 0.7, 0.5, 1]) ); materialBoard.setParameter( Rn.VarianceShadowMapDecodeClassicSingleMaterialNode.ShadowColor, - new Rn.Vector4(0.05, 0.35, 0.25, 1) + Rn.Vector4.fromCopyArray([0.05, 0.35, 0.25, 1]) ); materialBoard.setParameter( Rn.VarianceShadowMapDecodeClassicSingleMaterialNode.MinimumVariance, diff --git a/samples/test_e2e/VRMAnimation/__image_snapshots__/__diff_output__/test-js-regression-test-vrm-animation-1-diff.png b/samples/test_e2e/VRMAnimation/__image_snapshots__/__diff_output__/test-js-regression-test-vrm-animation-1-diff.png deleted file mode 100644 index ec0965938..000000000 Binary files a/samples/test_e2e/VRMAnimation/__image_snapshots__/__diff_output__/test-js-regression-test-vrm-animation-1-diff.png and /dev/null differ diff --git a/src/effekseer/EffekseerComponent.ts b/src/effekseer/EffekseerComponent.ts index 536c9c719..455eaf7e4 100644 --- a/src/effekseer/EffekseerComponent.ts +++ b/src/effekseer/EffekseerComponent.ts @@ -3,7 +3,6 @@ import EntityRepository from '../foundation/core/EntityRepository'; import SceneGraphComponent from '../foundation/components/SceneGraphComponent'; import {ProcessStage} from '../foundation/definitions/ProcessStage'; import TransformComponent from '../foundation/components/TransformComponent'; -import Vector3 from '../foundation/math/Vector3'; import CameraComponent from '../foundation/components/CameraComponent'; import ComponentRepository from '../foundation/core/ComponentRepository'; import {WellKnownComponentTIDs} from '../foundation/components/WellKnownComponentTIDs'; @@ -17,6 +16,7 @@ import { import Config from '../foundation/core/Config'; import MutableMatrix44 from '../foundation/math/MutableMatrix44'; import {Is} from '../foundation/misc/Is'; +import { IVector3 } from '../foundation/math/IVector'; declare let effekseer: any; @@ -133,7 +133,7 @@ export default class EffekseerComponent extends Component { return true; } - set translate(vec: Vector3) { + set translate(vec: IVector3) { if (this.__handle) { this.__handle.setLocation(vec.x, vec.y, vec.z); } diff --git a/src/foundation/components/MeshComponent.ts b/src/foundation/components/MeshComponent.ts index 111d88f4e..1aa1f07c4 100644 --- a/src/foundation/components/MeshComponent.ts +++ b/src/foundation/components/MeshComponent.ts @@ -122,11 +122,13 @@ export default class MeshComponent extends Component { this.__sceneGraphComponent.worldMatrixInner ); srcPointInLocal = new Vector3( - invWorldMatrix.multiplyVector(new Vector4(srcPointInWorld)) + invWorldMatrix.multiplyVector( + Vector4.fromCopyVector3(srcPointInWorld) + ) ); const distVecInWorld = Vector3.add(srcPointInWorld, directionInWorld); const distVecInLocal = new Vector3( - invWorldMatrix.multiplyVector(new Vector4(distVecInWorld)) + invWorldMatrix.multiplyVector(Vector4.fromCopyVector3(distVecInWorld)) ); directionInLocal = Vector3.normalize( Vector3.subtract(distVecInLocal, srcPointInLocal) @@ -141,7 +143,7 @@ export default class MeshComponent extends Component { if (t >= 0) { intersectPositionInWorld = new Vector3( this.__sceneGraphComponent.worldMatrixInner.multiplyVector( - new Vector4(intersectedPosition!) + Vector4.fromCopyVector3(intersectedPosition!) ) ); } @@ -200,10 +202,11 @@ export default class MeshComponent extends Component { ); let intersectedPositionInWorld = null; if (intersectedPosition != null && t >= 0) { - intersectedPositionInWorld = this.__sceneGraphComponent.worldMatrixInner.multiplyVector3To( - intersectedPosition, - MeshComponent.__returnVector3 - ); + intersectedPositionInWorld = + this.__sceneGraphComponent.worldMatrixInner.multiplyVector3To( + intersectedPosition, + MeshComponent.__returnVector3 + ); } return {t, intersectedPositionInWorld}; diff --git a/src/foundation/exporter/Gltf2Exporter.ts b/src/foundation/exporter/Gltf2Exporter.ts index 666a14de8..6e3d41094 100644 --- a/src/foundation/exporter/Gltf2Exporter.ts +++ b/src/foundation/exporter/Gltf2Exporter.ts @@ -254,8 +254,10 @@ export default class Gltf2Exporter { } } if (!match) { - json.images[countImage++] = { - uri: rnTexture.name ? rnTexture.name : rnTexture.uniqueName, + const imageJson = { + uri: + (rnTexture.name ? rnTexture.name : rnTexture.uniqueName) + + '.png', }; const htmlCanvasElement = rnTexture.htmlCanvasElement; if (htmlCanvasElement) { @@ -281,11 +283,12 @@ export default class Gltf2Exporter { null ); a.href = URL.createObjectURL(blob); - a.download = rnTexture.name; + a.download = imageJson.uri; a.dispatchEvent(e); }, Math.random() * 10000); }); } + json.images[countImage++] = imageJson; } json.textures[countTexture] = { diff --git a/src/foundation/geometry/Mesh.ts b/src/foundation/geometry/Mesh.ts index d3fee32be..cd7059ffd 100644 --- a/src/foundation/geometry/Mesh.ts +++ b/src/foundation/geometry/Mesh.ts @@ -15,6 +15,7 @@ import {Index, CGAPIResourceHandle, MeshUID} from '../../types/CommonTypes'; import MutableVector3 from '../math/MutableVector3'; import {VertexHandles} from '../../webgl/WebGLResourceRepository'; import {Is as is} from '../misc/Is'; +import {IVector3} from '../math/IVector'; /** * The Mesh class. @@ -755,10 +756,11 @@ export default class Mesh { return false; } - const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository(); + const webglResourceRepository = + CGAPIResourceRepository.getWebGLResourceRepository(); if ( - this.__variationVBOUid != + this.__variationVBOUid !== CGAPIResourceRepository.InvalidCGAPIResourceUid ) { webglResourceRepository.deleteVertexBuffer(this.__variationVBOUid); @@ -771,9 +773,8 @@ export default class Mesh { entityUIDs[i + 1] = this.__instances[i]._attachedEntityUID; } - this.__variationVBOUid = webglResourceRepository.createVertexBufferFromTypedArray( - entityUIDs - ); + this.__variationVBOUid = + webglResourceRepository.createVertexBufferFromTypedArray(entityUIDs); this.__instancesDirty = false; @@ -785,7 +786,8 @@ export default class Mesh { if (this.isInstanceMesh()) { return this.__instanceOf!.deleteVariationVBO(); } else { - const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository(); + const webglResourceRepository = + CGAPIResourceRepository.getWebGLResourceRepository(); if ( this.__variationVBOUid !== CGAPIResourceRepository.InvalidCGAPIResourceUid @@ -806,7 +808,8 @@ export default class Mesh { return this.__instanceOf!.updateVAO(); } - const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository(); + const webglResourceRepository = + CGAPIResourceRepository.getWebGLResourceRepository(); // create and update VAO for (let i = 0; i < this.__primitives.length; i++) { @@ -848,7 +851,8 @@ export default class Mesh { return this.__instanceOf!.updateVAO(); } - const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository(); + const webglResourceRepository = + CGAPIResourceRepository.getWebGLResourceRepository(); for (let i = 0; i < this.__vaoUids.length; i++) { webglResourceRepository.deleteVertexArray(this.__vaoUids[i]); this.__vaoUids[i] = CGAPIResourceRepository.InvalidCGAPIResourceUid; @@ -860,24 +864,22 @@ export default class Mesh { } castRay( - srcPointInLocal: Vector3, - directionInLocal: Vector3, + srcPointInLocal: IVector3, + directionInLocal: IVector3, dotThreshold = 0 ) { let finalShortestIntersectedPosVec3: Vector3 | undefined; let finalShortestT = Number.MAX_VALUE; for (const primitive of this.__primitives) { - const { - currentShortestIntersectedPosVec3, - currentShortestT, - } = primitive.castRay( - srcPointInLocal, - directionInLocal, - true, - true, - dotThreshold, - this.__hasFaceNormal - ); + const {currentShortestIntersectedPosVec3, currentShortestT} = + primitive.castRay( + srcPointInLocal, + directionInLocal, + true, + true, + dotThreshold, + this.__hasFaceNormal + ); if (currentShortestT != null && currentShortestT < finalShortestT) { finalShortestT = currentShortestT; finalShortestIntersectedPosVec3 = currentShortestIntersectedPosVec3!; diff --git a/src/foundation/geometry/Primitive.ts b/src/foundation/geometry/Primitive.ts index fff6879b6..f408f1f34 100644 --- a/src/foundation/geometry/Primitive.ts +++ b/src/foundation/geometry/Primitive.ts @@ -22,6 +22,7 @@ import Matrix33 from '../math/Matrix33'; import MutableMatrix33 from '../math/MutableMatrix33'; import MutableVector3 from '../math/MutableVector3'; import {Is as is} from '../misc/Is'; +import {IVector3} from '../math/IVector'; export type Attributes = Map; @@ -381,10 +382,10 @@ export default class Primitive extends RnObject { if (this.__vertexHandles != null) { return false; } - const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository(); - this.__vertexHandles = webglResourceRepository.createVertexBufferAndIndexBuffer( - this - ); + const webglResourceRepository = + CGAPIResourceRepository.getWebGLResourceRepository(); + this.__vertexHandles = + webglResourceRepository.createVertexBufferAndIndexBuffer(this); return true; } @@ -395,7 +396,8 @@ export default class Primitive extends RnObject { return false; } - const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository(); + const webglResourceRepository = + CGAPIResourceRepository.getWebGLResourceRepository(); webglResourceRepository.updateVertexBufferAndIndexBuffer( this, vertexHandles @@ -408,7 +410,8 @@ export default class Primitive extends RnObject { if (this.__vertexHandles == null) { return false; } - const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository(); + const webglResourceRepository = + CGAPIResourceRepository.getWebGLResourceRepository(); webglResourceRepository.deleteVertexDataResources(this.__vertexHandles); this.__vertexHandles = undefined; @@ -420,8 +423,8 @@ export default class Primitive extends RnObject { } castRay( - origVec3: Vector3, - dirVec3: Vector3, + origVec3: IVector3, + dirVec3: IVector3, isFrontFacePickable: boolean, isBackFacePickable: boolean, dotThreshold: number, @@ -505,8 +508,8 @@ export default class Primitive extends RnObject { } private __castRayInner( - origVec3: Vector3, - dirVec3: Vector3, + origVec3: IVector3, + dirVec3: IVector3, i: Index, pos0IndexBase: Index, pos1IndexBase: Index, @@ -534,12 +537,10 @@ export default class Primitive extends RnObject { } const vec3 = Vector3.subtract(origVec3, this.__arenberg3rdPosition[i]); - const convertedOrigVec3 = this.__inverseArenbergMatrix[i].multiplyVector( - vec3 - ); - const convertedDirVec3 = this.__inverseArenbergMatrix[i].multiplyVector( - dirVec3 - ); + const convertedOrigVec3 = + this.__inverseArenbergMatrix[i].multiplyVector(vec3); + const convertedDirVec3 = + this.__inverseArenbergMatrix[i].multiplyVector(dirVec3); if (convertedDirVec3.z >= -1e-6 && convertedDirVec3.z <= 1e-6) { return null; @@ -576,7 +577,7 @@ export default class Primitive extends RnObject { } _calcArenbergInverseMatrices() { - if (this.__inverseArenbergMatrix.length != 0) { + if (this.__inverseArenbergMatrix.length !== 0) { return; } diff --git a/src/foundation/importer/ModelConverter.ts b/src/foundation/importer/ModelConverter.ts index 442cb2ac2..8a7aff7fb 100644 --- a/src/foundation/importer/ModelConverter.ts +++ b/src/foundation/importer/ModelConverter.ts @@ -1805,7 +1805,7 @@ export default class ModelConverter { ); } else { typedDataArray.push( - new Vector4( + Vector4.fromCopyArray([ dataView[dataViewMethod](pos, littleEndian), dataView[dataViewMethod](pos + componentBytes, littleEndian), dataView[dataViewMethod]( @@ -1816,19 +1816,21 @@ export default class ModelConverter { pos + componentBytes * 3, littleEndian ) - ) + ]) ); } break; case 'MAT4': - const matrixComponents = []; - for (let i = 0; i < 16; i++) { - matrixComponents[i] = dataView[dataViewMethod]( - pos + componentBytes * i, - littleEndian - ); + { + const matrixComponents = []; + for (let i = 0; i < 16; i++) { + matrixComponents[i] = dataView[dataViewMethod]( + pos + componentBytes * i, + littleEndian + ); + } + typedDataArray.push(new Matrix44(matrixComponents, true)); } - typedDataArray.push(new Matrix44(matrixComponents, true)); break; } } @@ -2177,7 +2179,7 @@ export default class ModelConverter { textureRotationShaderSemantic: ShaderSemanticsEnum ) { if (textureJson?.extensions?.KHR_texture_transform) { - const transform = new MutableVector4(1.0, 1.0, 0.0, 0.0); + const transform = MutableVector4.fromCopyArray([1.0, 1.0, 0.0, 0.0]); let rotation = 0; const transformJson = textureJson.extensions.KHR_texture_transform; diff --git a/src/foundation/materials/core/ShaderityUtility.ts b/src/foundation/materials/core/ShaderityUtility.ts index 7f4736019..3b57b0c82 100644 --- a/src/foundation/materials/core/ShaderityUtility.ts +++ b/src/foundation/materials/core/ShaderityUtility.ts @@ -42,7 +42,8 @@ export type VertexAttributesLayout = { export default class ShaderityUtility { static __instance: ShaderityUtility; private __shaderity = Shaderity.getInstance(); - private __webglResourceRepository?: WebGLResourceRepository = WebGLResourceRepository.getInstance(); + private __webglResourceRepository?: WebGLResourceRepository = + WebGLResourceRepository.getInstance(); private constructor() { const attributeSemanticsMap = new Map(); @@ -74,8 +75,8 @@ export default class ShaderityUtility { _args.widthOfDataTexture = `const int widthOfDataTexture = ${MemoryManager.bufferWidthLength};`; _args.heightOfDataTexture = `const int heightOfDataTexture = ${MemoryManager.bufferHeightLength};`; const obj = this.__shaderity.fillTemplate(shaderityObject, _args); - const isWebGL2 = this.__webglResourceRepository?.currentWebGLContextWrapper - ?.isWebGL2; + const isWebGL2 = + this.__webglResourceRepository?.currentWebGLContextWrapper?.isWebGL2; const code = this.__shaderity.transformTo( isWebGL2 ? 'WebGL2' : 'WebGL1', obj @@ -91,8 +92,8 @@ export default class ShaderityUtility { _args.heightOfDataTexture = `const int heightOfDataTexture = ${MemoryManager.bufferHeightLength};`; _args.Config = Config; const obj = this.__shaderity.fillTemplate(shaderityObject, _args); - const isWebGL2 = this.__webglResourceRepository?.currentWebGLContextWrapper - ?.isWebGL2; + const isWebGL2 = + this.__webglResourceRepository?.currentWebGLContextWrapper?.isWebGL2; const code = this.__shaderity.transformTo( isWebGL2 ? 'WebGL2' : 'WebGL1', obj @@ -141,7 +142,8 @@ export default class ShaderityUtility { const shaderSemanticsInfoArray = []; for (const row of splitCode) { - const reg = /^(?![\/])[\t ]*uniform[\t ]+(\w+)[\t ]+(\w+);[\t ]*(\/\/)*[\t ]*(.*)/; + const reg = + /^(?![\/])[\t ]*uniform[\t ]+(\w+)[\t ]+(\w+);[\t ]*(\/\/)*[\t ]*(.*)/; const match = row.match(reg); if (match) { @@ -165,9 +167,8 @@ export default class ShaderityUtility { } } - const systemSemantic = ShaderSemantics.fromStringCaseSensitively( - variableName - ); + const systemSemantic = + ShaderSemantics.fromStringCaseSensitively(variableName); shaderSemanticsInfo.semantic = systemSemantic; if (systemSemantic == null) { if (existingShaderInfoMap) { @@ -184,9 +185,8 @@ export default class ShaderityUtility { } } shaderSemanticsInfo.componentType = ComponentType.fromGlslString(type); - shaderSemanticsInfo.compositionType = CompositionType.fromGlslString( - type - ); + shaderSemanticsInfo.compositionType = + CompositionType.fromGlslString(type); const soloDatum = info.match(/soloDatum[\t ]*=[\t ]*(\w+)[,\t ]*/); let bool = false; @@ -272,11 +272,13 @@ export default class ShaderityUtility { break; case 4: checkCompositionNumber(CompositionType.Vec4); - shaderSemanticsInfo.initialValue = new MutableVector4( - parseFloat(split[0]), - parseFloat(split[1]), - parseFloat(split[2]), - parseFloat(split[3]) + shaderSemanticsInfo.initialValue = MutableVector4.fromCopyArray( + [ + parseFloat(split[0]), + parseFloat(split[1]), + parseFloat(split[2]), + parseFloat(split[3]), + ] ); break; case 9: diff --git a/src/foundation/materials/nodes/ConstantVariableShaderNode.test.ts b/src/foundation/materials/nodes/ConstantVariableShaderNode.test.ts index a2e85d640..6e8f3a728 100644 --- a/src/foundation/materials/nodes/ConstantVariableShaderNode.test.ts +++ b/src/foundation/materials/nodes/ConstantVariableShaderNode.test.ts @@ -16,12 +16,12 @@ test('ConstantVariable works correctly 1', async () => { CompositionType.Vec4, ComponentType.Float ); - constant1.setDefaultInputValue('value', new Vector4(1, 2, 3, 4)); + constant1.setDefaultInputValue('value', Vector4.fromCopyArray([1, 2, 3, 4])); const constant2 = new ConstantVariableShaderNode( CompositionType.Vec4, ComponentType.Float ); - constant2.setDefaultInputValue('value', new Vector4(4, 3, 2, 1)); + constant2.setDefaultInputValue('value', Vector4.fromCopyArray([4, 3, 2, 1])); const add = new AddShaderNode(CompositionType.Vec4, ComponentType.Float); add.addInputConnection(constant1, 'outValue', 'lhs'); diff --git a/src/foundation/materials/nodes/VaryingVariableShaderNode.test.ts b/src/foundation/materials/nodes/VaryingVariableShaderNode.test.ts index d65cf8f89..f3cae8d1c 100644 --- a/src/foundation/materials/nodes/VaryingVariableShaderNode.test.ts +++ b/src/foundation/materials/nodes/VaryingVariableShaderNode.test.ts @@ -28,7 +28,7 @@ test('VaryingVariable works correctly 1', async () => { CompositionType.Vec4, ComponentType.Float ); - constant1.setDefaultInputValue('value', new Vector4(4, 3, 2, 1)); + constant1.setDefaultInputValue('value', Vector4.fromCopyArray([4, 3, 2, 1])); const outPositionNode = new OutPositionShaderNode(); const outColorNode = new OutColorShaderNode(); diff --git a/src/foundation/materials/singles/ClassicShadingSingleMaterialNode.ts b/src/foundation/materials/singles/ClassicShadingSingleMaterialNode.ts index 353eb43f1..a3a4ddfc6 100644 --- a/src/foundation/materials/singles/ClassicShadingSingleMaterialNode.ts +++ b/src/foundation/materials/singles/ClassicShadingSingleMaterialNode.ts @@ -74,7 +74,7 @@ export default class ClassicShadingSingleMaterialNode extends AbstractMaterialNo isSystem: false, updateInterval: ShaderVariableUpdateInterval.EveryTime, soloDatum: false, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), min: 0, max: 2, }, diff --git a/src/foundation/materials/singles/EnvConstantSingleMaterialNode.ts b/src/foundation/materials/singles/EnvConstantSingleMaterialNode.ts index 2c16c60ad..96acbcc85 100644 --- a/src/foundation/materials/singles/EnvConstantSingleMaterialNode.ts +++ b/src/foundation/materials/singles/EnvConstantSingleMaterialNode.ts @@ -62,7 +62,7 @@ export default class EnvConstantSingleMaterialNode extends AbstractMaterialNode isSystem: false, updateInterval: ShaderVariableUpdateInterval.EveryTime, soloDatum: false, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), min: 0, max: 2, }, diff --git a/src/foundation/materials/singles/MToonSingleMaterialNode.ts b/src/foundation/materials/singles/MToonSingleMaterialNode.ts index e484064e6..a9290455c 100644 --- a/src/foundation/materials/singles/MToonSingleMaterialNode.ts +++ b/src/foundation/materials/singles/MToonSingleMaterialNode.ts @@ -20,7 +20,7 @@ import {ShaderVariableUpdateInterval} from '../../definitions/ShaderVariableUpda import Vector3 from '../../math/Vector3'; import Vector4 from '../../math/Vector4'; import VectorN from '../../math/VectorN'; -import {Count} from '../../../types/CommonTypes'; +import {Array3, Array4, Count} from '../../../types/CommonTypes'; import WebGLResourceRepository from '../../../webgl/WebGLResourceRepository'; import WebGLContextWrapper from '../../../webgl/WebGLContextWrapper'; import Texture from '../../textures/Texture'; @@ -96,8 +96,12 @@ export default class MToonSingleMaterialNode extends AbstractMaterialNode { static usableBlendEquationModeAlpha?: number; private __OutlineWidthModeIsScreen = false; - private __floatProperties: any = {}; - private __vectorProperties: any = {}; + private __floatProperties: { + [s: string]: number; + } = {}; + private __vectorProperties: { + [s: string]: Array3 | Array4; + } = {}; private __textureProperties: any = {}; constructor( @@ -214,7 +218,9 @@ export default class MToonSingleMaterialNode extends AbstractMaterialNode { isSystem: false, updateInterval: ShaderVariableUpdateInterval.EveryTime, soloDatum: false, - initialValue: new Vector4(this.__vectorProperties._Color), + initialValue: Vector4.fromCopyArray( + this.__vectorProperties._Color as Array4 + ), min: 0, max: 1, }, diff --git a/src/foundation/materials/singles/PbrExtendedShadingSingleMaterialNode.ts b/src/foundation/materials/singles/PbrExtendedShadingSingleMaterialNode.ts index 55d5c7e4a..5c24c6833 100644 --- a/src/foundation/materials/singles/PbrExtendedShadingSingleMaterialNode.ts +++ b/src/foundation/materials/singles/PbrExtendedShadingSingleMaterialNode.ts @@ -89,7 +89,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri min: 0, max: 2, isSystem: false, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), }, { semantic: ShaderSemantics.DiffuseColorTexture, @@ -109,7 +109,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri min: 0, max: 2, isSystem: false, - initialValue: new Vector4(0.0, 0.0, 0.0, 0.0), + initialValue: Vector4.fromCopyArray([0.0, 0.0, 0.0, 0.0]), }, { semantic: ShaderSemantics.SpecularGlossinessTexture, @@ -199,7 +199,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri min: -10, max: 10, isSystem: false, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: PbrExtendedShadingSingleMaterialNode.diffuseTextureRotation, @@ -219,7 +219,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri min: -10, max: 10, isSystem: false, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: PbrExtendedShadingSingleMaterialNode.normalTextureRotation, @@ -240,7 +240,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri min: -10, max: 10, isSystem: false, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: @@ -262,7 +262,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri min: -10, max: 10, isSystem: false, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: @@ -294,7 +294,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri max: Number.MAX_VALUE, isSystem: true, updateInterval: ShaderVariableUpdateInterval.EveryTime, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), }, { semantic: ShaderSemantics.HDRIFormat, @@ -383,7 +383,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri // maxIndex: 4, // isSystem: true, // updateInterval: ShaderVariableUpdateInterval.EveryTime, - // initialValue: new Vector4(0, 0, 0, 1), + // initialValue: Vector4.fromCopyArray([0, 0, 0, 1]), // soloDatum: true, // }); // lights.push( @@ -397,7 +397,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri // index: idx, // maxIndex: 4, // isSystem: true, - // initialValue: new Vector4(0, 1, 0, 1), + // initialValue: Vector4.fromCopyArray([0, 1, 0, 1]), // updateInterval: ShaderVariableUpdateInterval.EveryTime, // soloDatum: true, // }); @@ -412,7 +412,7 @@ export default class PbrExtendedShadingSingleMaterialNode extends AbstractMateri // index: idx, // maxIndex: 4, // isSystem: true, - // initialValue: new Vector4(1, 1, 1, 1), + // initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), // updateInterval: ShaderVariableUpdateInterval.EveryTime, // soloDatum: true, // }); diff --git a/src/foundation/materials/singles/PbrShadingSingleMaterialNode.ts b/src/foundation/materials/singles/PbrShadingSingleMaterialNode.ts index 4a9e6005c..5682c51e5 100644 --- a/src/foundation/materials/singles/PbrShadingSingleMaterialNode.ts +++ b/src/foundation/materials/singles/PbrShadingSingleMaterialNode.ts @@ -110,7 +110,7 @@ export default class PbrShadingSingleMaterialNode extends AbstractMaterialNode { max: 2, isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), }, { semantic: ShaderSemantics.BaseColorTexture, @@ -209,7 +209,7 @@ export default class PbrShadingSingleMaterialNode extends AbstractMaterialNode { max: Number.MAX_VALUE, isSystem: true, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), }, { semantic: ShaderSemantics.HDRIFormat, @@ -253,7 +253,7 @@ export default class PbrShadingSingleMaterialNode extends AbstractMaterialNode { max: 10, isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: PbrShadingSingleMaterialNode.BaseColorTextureRotation, @@ -276,7 +276,7 @@ export default class PbrShadingSingleMaterialNode extends AbstractMaterialNode { max: 10, isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: PbrShadingSingleMaterialNode.MetallicRoughnessTextureRotation, @@ -460,7 +460,7 @@ export default class PbrShadingSingleMaterialNode extends AbstractMaterialNode { max: 10, isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: PbrShadingSingleMaterialNode.NormalTextureRotation, diff --git a/src/foundation/materials/singles/ShadowMapDecodeClassicSingleMaterialNode.ts b/src/foundation/materials/singles/ShadowMapDecodeClassicSingleMaterialNode.ts index f75e13e4b..c211a3a8e 100644 --- a/src/foundation/materials/singles/ShadowMapDecodeClassicSingleMaterialNode.ts +++ b/src/foundation/materials/singles/ShadowMapDecodeClassicSingleMaterialNode.ts @@ -163,7 +163,7 @@ export default class ShadowMapDecodeClassicSingleMaterialNode extends AbstractMa isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, soloDatum: false, - initialValue: new Vector4(0.5, 0.5, 0.5, 1), + initialValue: Vector4.fromCopyArray([0.5, 0.5, 0.5, 1]), min: 0, max: 1, }, @@ -175,7 +175,7 @@ export default class ShadowMapDecodeClassicSingleMaterialNode extends AbstractMa isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, soloDatum: false, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), min: 0, max: 2, }, @@ -356,7 +356,7 @@ export default class ShadowMapDecodeClassicSingleMaterialNode extends AbstractMa isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, soloDatum: false, - initialValue: new Vector4(1, 0, 0, 1), + initialValue: Vector4.fromCopyArray([1, 0, 0, 1]), min: 0, max: 2, }); diff --git a/src/foundation/materials/singles/SkinPbrShadingSingleMaterialNode.ts b/src/foundation/materials/singles/SkinPbrShadingSingleMaterialNode.ts index 2294c5491..8a15342aa 100644 --- a/src/foundation/materials/singles/SkinPbrShadingSingleMaterialNode.ts +++ b/src/foundation/materials/singles/SkinPbrShadingSingleMaterialNode.ts @@ -90,7 +90,7 @@ export default class SkinPbrShadingSingleMaterialNode extends AbstractMaterialNo max: 2, isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), }, { semantic: ShaderSemantics.BaseColorTexture, @@ -215,7 +215,7 @@ export default class SkinPbrShadingSingleMaterialNode extends AbstractMaterialNo min: -Number.MAX_VALUE, max: Number.MAX_VALUE, isSystem: true, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, }, { @@ -272,7 +272,7 @@ export default class SkinPbrShadingSingleMaterialNode extends AbstractMaterialNo max: 10, isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: SkinPbrShadingSingleMaterialNode.baseColorTextureRotation, @@ -294,7 +294,7 @@ export default class SkinPbrShadingSingleMaterialNode extends AbstractMaterialNo max: 10, isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: SkinPbrShadingSingleMaterialNode.normalTextureRotation, @@ -317,7 +317,7 @@ export default class SkinPbrShadingSingleMaterialNode extends AbstractMaterialNo max: 10, isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(1, 1, 0, 0), + initialValue: Vector4.fromCopyArray([1, 1, 0, 0]), }, { semantic: @@ -378,7 +378,7 @@ export default class SkinPbrShadingSingleMaterialNode extends AbstractMaterialNo maxIndex: 4, isSystem: true, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, - initialValue: new Vector4(0, 0, 0, 1), + initialValue: Vector4.fromCopyArray([0, 0, 0, 1]), soloDatum: true }); lights.push( @@ -392,7 +392,7 @@ export default class SkinPbrShadingSingleMaterialNode extends AbstractMaterialNo index: idx, maxIndex: 4, isSystem: true, - initialValue: new Vector4(0, 1, 0, 1), + initialValue: Vector4.fromCopyArray([0, 1, 0, 1]), updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, soloDatum: true }); @@ -407,7 +407,7 @@ export default class SkinPbrShadingSingleMaterialNode extends AbstractMaterialNo index: idx, maxIndex: 4, isSystem: true, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, soloDatum: true }); diff --git a/src/foundation/materials/singles/VarianceShadowMapDecodeClassicSingleMaterialNode.ts b/src/foundation/materials/singles/VarianceShadowMapDecodeClassicSingleMaterialNode.ts index 92275618b..ab3575415 100644 --- a/src/foundation/materials/singles/VarianceShadowMapDecodeClassicSingleMaterialNode.ts +++ b/src/foundation/materials/singles/VarianceShadowMapDecodeClassicSingleMaterialNode.ts @@ -199,7 +199,7 @@ export default class VarianceShadowMapDecodeClassicSingleMaterialNode extends Ab isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, soloDatum: false, - initialValue: new Vector4(0.5, 0.5, 0.5, 1), + initialValue: Vector4.fromCopyArray([0.5, 0.5, 0.5, 1]), min: 0, max: 1, }, @@ -211,7 +211,7 @@ export default class VarianceShadowMapDecodeClassicSingleMaterialNode extends Ab isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, soloDatum: false, - initialValue: new Vector4(1, 1, 1, 1), + initialValue: Vector4.fromCopyArray([1, 1, 1, 1]), min: 0, max: 2, }, @@ -458,7 +458,7 @@ export default class VarianceShadowMapDecodeClassicSingleMaterialNode extends Ab isSystem: false, updateInterval: ShaderVariableUpdateInterval.FirstTimeOnly, soloDatum: false, - initialValue: new Vector4(1, 0, 0, 1), + initialValue: Vector4.fromCopyArray([1, 0, 0, 1]), min: 0, max: 2, }); diff --git a/src/foundation/math/AbstractMathNumber.ts b/src/foundation/math/AbstractMathNumber.ts new file mode 100644 index 000000000..01e7f0bd0 --- /dev/null +++ b/src/foundation/math/AbstractMathNumber.ts @@ -0,0 +1,12 @@ +import {TypedArray} from '../../types/CommonTypes'; +import {IArrayBufferBasedMathNumber} from './IMathNumber'; + +export abstract class AbstractArrayBufferBaseMathNumber + implements IArrayBufferBasedMathNumber +{ + _v: TypedArray = new Float32Array(); + + isTheSourceSame(arrayBuffer: ArrayBuffer): boolean { + return this._v.buffer === arrayBuffer; + } +} diff --git a/src/foundation/math/AbstractMatrix.test.ts b/src/foundation/math/AbstractMatrix.test.ts new file mode 100644 index 000000000..f1da8702f --- /dev/null +++ b/src/foundation/math/AbstractMatrix.test.ts @@ -0,0 +1,14 @@ +import Matrix22 from './Matrix22'; + +test('AbstractMatrix::isTheSourceSame', () => { + const mat_source = new Float32Array([0, 1, 2, 3]); + const mat_source2 = new Float32Array([-1, -1, -1, -1]); + const mat1 = new Matrix22(mat_source, false, true); + const mat2 = new Matrix22(mat_source, false, true); + const matAnother = new Matrix22(mat_source2, false, true); + const matAnother2 = new Matrix22(0, 0, 0, 0); + + expect(mat1.isTheSourceSame(mat2._v.buffer)).toBe(true); + expect(mat1.isTheSourceSame(matAnother._v.buffer)).toBe(false); + expect(mat1.isTheSourceSame(matAnother2._v.buffer)).toBe(false); +}); diff --git a/src/foundation/math/AbstractMatrix.ts b/src/foundation/math/AbstractMatrix.ts index 24f5edec4..8d4061552 100644 --- a/src/foundation/math/AbstractMatrix.ts +++ b/src/foundation/math/AbstractMatrix.ts @@ -27,11 +27,15 @@ export default abstract class AbstractMatrix implements IMatrix { determinant(): number { throw new Error('Method not implemented.'); } - get className() { + get className(): string { return this.constructor.name; } get isIdentityMatrixClass(): boolean { return false; } + + isTheSourceSame(arrayBuffer: ArrayBuffer): boolean { + return this._v.buffer === arrayBuffer; + } } diff --git a/src/foundation/math/AbstractVector.test.ts b/src/foundation/math/AbstractVector.test.ts new file mode 100644 index 000000000..97dd54851 --- /dev/null +++ b/src/foundation/math/AbstractVector.test.ts @@ -0,0 +1,14 @@ +import Vector4 from './Vector4'; + +test('AbstractVector::isTheSourceSame', () => { + const vec_source = new Float32Array([0, 1, 2, 3]); + const vec_source2 = new Float32Array([-1, -1, -1, -1]); + const vec1 = new Vector4(vec_source); + const vec2 = new Vector4(vec_source); + const vecAnother = new Vector4(vec_source2); + const vecAnother2 = Vector4.fromCopyArray([0, 0, 0, 0]); + + expect(vec1.isTheSourceSame(vec2._v.buffer)).toBe(true); + expect(vec1.isTheSourceSame(vecAnother._v.buffer)).toBe(false); + expect(vec1.isTheSourceSame(vecAnother2._v.buffer)).toBe(false); +}); diff --git a/src/foundation/math/AbstractVector.ts b/src/foundation/math/AbstractVector.ts index 662a3a471..28b0ca78c 100644 --- a/src/foundation/math/AbstractVector.ts +++ b/src/foundation/math/AbstractVector.ts @@ -50,7 +50,12 @@ export default abstract class AbstractVector implements IVector { v(i: number): number { return this._v[i]; } - get className() { + + isTheSourceSame(arrayBuffer: ArrayBuffer): boolean { + return this._v.buffer === arrayBuffer; + } + + get className(): string { return this.constructor.name; } } diff --git a/src/foundation/math/ColorRgba.ts b/src/foundation/math/ColorRgba.ts index 06a65f581..36e423fb9 100644 --- a/src/foundation/math/ColorRgba.ts +++ b/src/foundation/math/ColorRgba.ts @@ -1,16 +1,10 @@ import Vector4 from './Vector4'; import {IVector4} from './IVector'; import {IColorRgba} from './IColor'; -import {TypedArray} from '../../types/CommonTypes'; export default class ColorRgba extends Vector4 implements IVector4, IColorRgba { - constructor( - r: number | TypedArray | IVector4 | Array | null, - g?: number, - b?: number, - a?: number - ) { - super(r, g, b, a); + constructor(r: Float32Array) { + super(r); } get x() { diff --git a/src/foundation/math/IMathNumber.ts b/src/foundation/math/IMathNumber.ts new file mode 100644 index 000000000..92c80ba91 --- /dev/null +++ b/src/foundation/math/IMathNumber.ts @@ -0,0 +1,3 @@ +export interface IArrayBufferBasedMathNumber { + isTheSourceSame(arrayBuffer: ArrayBuffer): boolean; +} diff --git a/src/foundation/math/IMatrix.ts b/src/foundation/math/IMatrix.ts index 4e408391c..18afeaaac 100644 --- a/src/foundation/math/IMatrix.ts +++ b/src/foundation/math/IMatrix.ts @@ -21,6 +21,7 @@ export interface IMatrix { v(i: number): number; determinant(): number; readonly isIdentityMatrixClass: boolean; + isTheSourceSame(arrayBuffer: ArrayBuffer): boolean; } export interface IMutableMatrix extends IMatrix { diff --git a/src/foundation/math/IVector.ts b/src/foundation/math/IVector.ts index 43093f011..2272b7a72 100644 --- a/src/foundation/math/IVector.ts +++ b/src/foundation/math/IVector.ts @@ -17,6 +17,7 @@ export interface IVector { lengthSquared(): number; lengthTo(vec: IVector): number; dot(vec: IVector): number; + isTheSourceSame(arrayBuffer: ArrayBuffer): boolean; } export interface IMutableVector extends IVector { diff --git a/src/foundation/math/MathClassUtil.ts b/src/foundation/math/MathClassUtil.ts index bdea346e2..2f8c58b6c 100644 --- a/src/foundation/math/MathClassUtil.ts +++ b/src/foundation/math/MathClassUtil.ts @@ -26,7 +26,12 @@ export default class MathClassUtil { static arrayToVector(element: Array) { if (Array.isArray(element)) { if (typeof element[3] !== 'undefined') { - return new Vector4(element[0], element[1], element[2], element[3]); + return Vector4.fromCopyArray([ + element[0], + element[1], + element[2], + element[3], + ]); } else if (typeof element[2] !== 'undefined') { return new Vector3(element[0], element[1], element[2]); } else { @@ -44,7 +49,12 @@ export default class MathClassUtil { } else if (typeof element[8] !== 'undefined') { return new Matrix33(element); } else if (typeof element[3] !== 'undefined') { - return new Vector4(element[0], element[1], element[2], element[3]); + return Vector4.fromCopyArray([ + element[0], + element[1], + element[2], + element[3], + ]); } else if (typeof element[2] !== 'undefined') { return new Vector3(element[0], element[1], element[2]); } else { @@ -327,7 +337,7 @@ export default class MathClassUtil { } else if (objForDetectType instanceof Vector3) { return new Vector3(val, val, val); } else if (objForDetectType instanceof Vector4) { - return new Vector4(val, val, val, val); + return Vector4.fromCopyArray([val, val, val, val]); } else if (objForDetectType instanceof Quaternion) { return new Quaternion(0, 0, 0, 1); } else if (Array.isArray(objForDetectType)) { @@ -438,7 +448,7 @@ export default class MathClassUtil { let vec; switch (floatArray.length) { case 4: - vec = new Vector4(floatArray); + vec = Vector4.fromFloat32Array(floatArray); break; case 3: vec = new Vector3(floatArray); diff --git a/src/foundation/math/Matrix22.ts b/src/foundation/math/Matrix22.ts index cbd2e39f8..414c2e45d 100644 --- a/src/foundation/math/Matrix22.ts +++ b/src/foundation/math/Matrix22.ts @@ -9,9 +9,7 @@ import {MathUtil} from './MathUtil'; import MutableVector2 from './MutableVector2'; import AbstractMatrix from './AbstractMatrix'; -export default class Matrix22 - extends AbstractMatrix - implements IMatrix, IMatrix22 { +export default class Matrix22 extends AbstractMatrix implements IMatrix22 { constructor(m: null); constructor( m: Float32Array, @@ -39,7 +37,7 @@ export default class Matrix22 ) { super(); const _isColumnMajor = arguments.length === 5 ? isColumnMajor : m1; - const _notCopyFloatArray = arguments.length === 3 ? notCopyFloatArray : m2; + const _notCopyFloatArray = arguments.length === 6 ? notCopyFloatArray : m2; const m = m0; if (m == null) { diff --git a/src/foundation/math/Matrix33.ts b/src/foundation/math/Matrix33.ts index 019bd40e5..5d31a094b 100644 --- a/src/foundation/math/Matrix33.ts +++ b/src/foundation/math/Matrix33.ts @@ -8,6 +8,7 @@ import {MathUtil} from './MathUtil'; import MutableVector3 from './MutableVector3'; import AbstractMatrix from './AbstractMatrix'; import IdentityMatrix33 from './IdentityMatrix33'; +import {IMutableVector3, IVector3} from './IVector'; /* eslint-disable prettier/prettier */ export default class Matrix33 extends AbstractMatrix implements IMatrix, IMatrix33 { @@ -500,14 +501,14 @@ export default class Matrix33 extends AbstractMatrix implements IMatrix, IMatrix - this._v[0] * this._v[5] * this._v[7] - this._v[2] * this._v[4] * this._v[6] - this._v[1] * this._v[3] * this._v[8]; } - multiplyVector(vec: Vector3) { + multiplyVector(vec: IVector3) { const x = this._v[0] * vec._v[0] + this._v[3] * vec._v[1] + this._v[6] * vec._v[2]; const y = this._v[1] * vec._v[0] + this._v[4] * vec._v[1] + this._v[7] * vec._v[2]; const z = this._v[2] * vec._v[0] + this._v[5] * vec._v[1] + this._v[8] * vec._v[2]; return new (vec.constructor as any)(x, y, z); } - multiplyVectorTo(vec: Vector3, outVec: MutableVector3) { + multiplyVectorTo(vec: IVector3, outVec: IMutableVector3) { const x = this._v[0] * vec._v[0] + this._v[3] * vec._v[1] + this._v[6] * vec._v[2]; const y = this._v[1] * vec._v[0] + this._v[4] * vec._v[1] + this._v[7] * vec._v[2]; const z = this._v[2] * vec._v[0] + this._v[5] * vec._v[1] + this._v[8] * vec._v[2]; diff --git a/src/foundation/math/Matrix44.ts b/src/foundation/math/Matrix44.ts index 6d3ebd331..95c666c81 100644 --- a/src/foundation/math/Matrix44.ts +++ b/src/foundation/math/Matrix44.ts @@ -706,7 +706,7 @@ export default class Matrix44 extends AbstractMatrix implements IMatrix, IMatrix const z = this._v[2] * vec._v[0] + this._v[6] * vec._v[1] + this._v[10] * vec._v[2] + this._v[14] * vec._v[3]; const w = this._v[3] * vec._v[0] + this._v[7] * vec._v[1] + this._v[11] * vec._v[2] + this._v[15] * vec._v[3]; - return new Vector4(x, y, z, w); + return Vector4.fromCopyArray([x, y, z, w]); } multiplyVectorTo(vec: Vector4, outVec: MutableVector4) { diff --git a/src/foundation/math/MutableColorRgba.ts b/src/foundation/math/MutableColorRgba.ts index 71e3cb157..8a12bd2a9 100644 --- a/src/foundation/math/MutableColorRgba.ts +++ b/src/foundation/math/MutableColorRgba.ts @@ -1,18 +1,13 @@ import MutableVector4 from './MutableVector4'; import {IVector4, IMutableVector4} from './IVector'; import {IMutableColorRgba} from './IColor'; -import {TypedArray} from '../../types/CommonTypes'; export default class MutableColorRgba extends MutableVector4 - implements IMutableVector4, IMutableColorRgba { - constructor( - r: number | TypedArray | IVector4 | Array | null, - g?: number, - b?: number, - a?: number - ) { - super(r, g, b, a); + implements IMutableVector4, IMutableColorRgba +{ + constructor(r: Float32Array) { + super(r); } get x() { diff --git a/src/foundation/math/MutableVector3.ts b/src/foundation/math/MutableVector3.ts index 8dd346508..30bea23a2 100644 --- a/src/foundation/math/MutableVector3.ts +++ b/src/foundation/math/MutableVector3.ts @@ -5,11 +5,11 @@ import { IMutableVector, IMutableVector3, } from './IVector'; -import {TypedArray, TypedArrayConstructor} from '../../types/CommonTypes'; +import {TypedArray, FloatTypedArrayConstructor} from '../../types/CommonTypes'; import {Vector3_} from './Vector3'; import {IQuaternion} from './IQuaternion'; -export class MutableVector3_ +export class MutableVector3_ extends Vector3_ implements IMutableVector, IMutableVector3 { constructor( diff --git a/src/foundation/math/MutableVector4.test.ts b/src/foundation/math/MutableVector4.test.ts index 0c602a116..08bfd8d45 100644 --- a/src/foundation/math/MutableVector4.test.ts +++ b/src/foundation/math/MutableVector4.test.ts @@ -1,9 +1,9 @@ import MutableVector4 from './MutableVector4'; test('Test isEqual', () => { - const a = new MutableVector4(0.2, 0.2, 0.2, 0.2); - const b = new MutableVector4(0.1, 0.1, 0.1, 0.1); - const c = new MutableVector4(0.3, 0.3, 0.3, 0.3); + const a = MutableVector4.fromCopyArray([0.2, 0.2, 0.2, 0.2]); + const b = MutableVector4.fromCopyArray([0.1, 0.1, 0.1, 0.1]); + const c = MutableVector4.fromCopyArray([0.3, 0.3, 0.3, 0.3]); expect(a.add(b).isEqual(c)).toBe(true); }); diff --git a/src/foundation/math/MutableVector4.ts b/src/foundation/math/MutableVector4.ts index 4d886f5fa..9e64af4ca 100644 --- a/src/foundation/math/MutableVector4.ts +++ b/src/foundation/math/MutableVector4.ts @@ -1,35 +1,22 @@ +import {IVector4, IMutableVector, IMutableVector4} from './IVector'; import { - IVector2, - IVector3, - IVector4, - IMutableVector, - IMutableVector4, -} from './IVector'; -import {TypedArray, TypedArrayConstructor} from '../../types/CommonTypes'; + Array4, + FloatTypedArray, + FloatTypedArrayConstructor, +} from '../../types/CommonTypes'; import {Vector4_} from './Vector4'; -export class MutableVector4_ +export class MutableVector4_ extends Vector4_ - implements IMutableVector, IMutableVector4 { - constructor( - x: - | number - | TypedArray - | IVector2 - | IVector3 - | IVector4 - | Array - | null, - y: number, - z: number, - w: number, - {type}: {type: T} - ) { - super(x, y, z, w, {type}); + implements IMutableVector, IMutableVector4 +{ + constructor(x: FloatTypedArray, {type}: {type: T}) { + super(x, {type}); } set x(x: number) { this._v[0] = x; + this.__updateCount++; } get x(): number { @@ -38,6 +25,7 @@ export class MutableVector4_ set y(y: number) { this._v[1] = y; + this.__updateCount++; } get y(): number { @@ -46,6 +34,7 @@ export class MutableVector4_ set z(z: number) { this._v[2] = z; + this.__updateCount++; } get z(): number { @@ -54,6 +43,7 @@ export class MutableVector4_ set w(w: number) { this._v[3] = w; + this.__updateCount++; } get w(): number { @@ -66,6 +56,7 @@ export class MutableVector4_ setAt(i: number, value: number) { this._v[i] = value; + this.__updateCount++; return this; } @@ -74,6 +65,7 @@ export class MutableVector4_ this._v[1] = y; this._v[2] = z; this._v[3] = w; + this.__updateCount++; return this; } @@ -190,23 +182,21 @@ export class MutableVector4_ } return this; } + + get _updateCount() { + return this.__updateCount; + } + + private __updateCount = 0; } export default class MutableVector4 extends MutableVector4_ { - constructor( - x: - | number - | TypedArray - | IVector2 - | IVector3 - | IVector4 - | Array - | null, - y?: number, - z?: number, - w?: number - ) { - super(x, y!, z!, w!, {type: Float32Array}); + constructor(x: Float32Array) { + super(x, {type: Float32Array}); + } + + static fromCopyArray(array: Array4): MutableVector4 { + return new MutableVector4(new Float32Array(array)); } static zero() { @@ -259,20 +249,8 @@ export default class MutableVector4 extends MutableVector4_ { - constructor( - x: - | number - | TypedArray - | IVector2 - | IVector3 - | IVector4 - | Array - | null, - y?: number, - z?: number, - w?: number - ) { - super(x, y!, z!, w!, {type: Float64Array}); + constructor(x: Float64Array) { + super(x, {type: Float64Array}); } static zero() { @@ -315,6 +293,10 @@ export class MutableVector4d extends MutableVector4_ { return super._divideVector(l_vec, r_vec, Float64Array) as MutableVector4d; } + static fromCopyArray(array: Array4): MutableVector4d { + return new MutableVector4d(new Float64Array(array)); + } + clone() { return super.clone() as MutableVector4d; } diff --git a/src/foundation/math/Vector4.test.ts b/src/foundation/math/Vector4.test.ts index 205dc69fd..1c28c38b9 100644 --- a/src/foundation/math/Vector4.test.ts +++ b/src/foundation/math/Vector4.test.ts @@ -1,7 +1,7 @@ import Vector4 from './Vector4'; test('Vector4 is immutable', () => { - const vec = new Vector4(0, 3, 4, 0); + const vec = Vector4.fromCopyArray([0, 3, 4, 0]); // console.log(vec.w); expect(() => { (vec as any).w = 1; @@ -12,12 +12,12 @@ test('Vector4 is immutable', () => { }); test('Multiplication of two Vector4 works correctly', () => { - const vec = new Vector4(0, 3, 4, 0); + const vec = Vector4.fromCopyArray([0, 3, 4, 0]); // console.log(vec.x, vec.y, vec.z, vec.w); - const vec2 = new Vector4(0, 6, 8, 0); + const vec2 = Vector4.fromCopyArray([0, 6, 8, 0]); const result_vec = Vector4.multiplyVector(vec, vec2); // console.log(result_vec.x, result_vec.y, result_vec.z, result_vec.w); - expect(result_vec.isEqual(new Vector4(0, 18, 32, 0))).toBe(true); + expect(result_vec.isEqual(Vector4.fromCopyArray([0, 18, 32, 0]))).toBe(true); }); diff --git a/src/foundation/math/Vector4.ts b/src/foundation/math/Vector4.ts index dd9c08a65..57d7afad3 100644 --- a/src/foundation/math/Vector4.ts +++ b/src/foundation/math/Vector4.ts @@ -1,74 +1,16 @@ -import { - IVector2, - IVector3, - IVector4, - IVector, - IMutableVector4, -} from './IVector'; -import {TypedArray, TypedArrayConstructor} from '../../types/CommonTypes'; +import {IVector2, IVector3, IVector4, IMutableVector4} from './IVector'; +import {Array4, FloatTypedArray, FloatTypedArrayConstructor} from '../../types/CommonTypes'; import {MathUtil} from './MathUtil'; import {CompositionType} from '../definitions/CompositionType'; import AbstractVector from './AbstractVector'; -export class Vector4_ +export class Vector4_ extends AbstractVector - implements IVector, IVector4 { - constructor( - x: - | number - | TypedArray - | IVector2 - | IVector3 - | IVector4 - | Array - | null, - y: number, - z: number, - w: number, - {type}: {type: T} - ) { + implements IVector4 +{ + protected constructor(v: FloatTypedArray, {type}: {type: T}) { super(); - if (ArrayBuffer.isView(x)) { - this._v = x as TypedArray; - return; - } else if (x == null) { - this._v = new type(0); - return; - } else { - this._v = new type(4); - } - - if (Array.isArray(x)) { - this._v[0] = x[0]; - this._v[1] = x[1]; - this._v[2] = x[2]; - this._v[3] = x[3]; - } else if (typeof x === 'number') { - this._v[0] = x; - this._v[1] = y; - this._v[2] = z; - this._v[3] = w; - } else { - if (typeof x._v[2] === 'undefined') { - // IVector2 - this._v[0] = x._v[0]; - this._v[1] = x._v[1]; - this._v[2] = 0; - this._v[3] = 1; - } else if (typeof x._v[3] === 'undefined') { - // IVector3 - this._v[0] = x._v[0]; - this._v[1] = x._v[1]; - this._v[2] = x._v[2]; - this._v[3] = 1; - } else { - // IVector4 - this._v[0] = x._v[0]; - this._v[1] = x._v[1]; - this._v[2] = x._v[2]; - this._v[3] = x._v[3]; - } - } + this._v = v; } get x(): number { @@ -103,6 +45,42 @@ export class Vector4_ )}, ${Math.floor(this._v[2])}, ${Math.floor(this._v[3])})`; } + static _fromCopyArray4( + array: Array4, + type: FloatTypedArrayConstructor + ) { + return new this(new type(array), {type}); + } + + static _fromCopyArray( + array: Array, + type: FloatTypedArrayConstructor + ) { + return new this(new type(array.slice(0, 4)), {type}); + } + + static _fromCopyVector4(vec4: IVector4, type: FloatTypedArrayConstructor) { + const vec = new this( + new type([vec4._v[0], vec4._v[1], vec4._v[2], vec4._v[3]]), + {type} + ); + return vec; + } + + static _fromCopyVector3(vec3: IVector3, type: FloatTypedArrayConstructor) { + const vec = new this(new type([vec3._v[0], vec3._v[1], vec3._v[2], 1]), { + type, + }); + return vec; + } + + static _fromVector2(vec2: IVector2, type: FloatTypedArrayConstructor) { + const vec = new this(new type([vec2._v[0], vec2._v[1], 0, 1]), { + type, + }); + return vec; + } + static get compositionType() { return CompositionType.Vec4; } @@ -121,22 +99,22 @@ export class Vector4_ /** * Zero Vector */ - static _zero(type: TypedArrayConstructor) { - return new this(0, 0, 0, 0, {type}); + static _zero(type: FloatTypedArrayConstructor) { + return new this(new type([0, 0, 0, 0]), {type}); } - static _one(type: TypedArrayConstructor) { - return new this(1, 1, 1, 1, {type}); + static _one(type: FloatTypedArrayConstructor) { + return new this(new type([1, 1, 1, 1]), {type}); } - static _dummy(type: TypedArrayConstructor) { - return new this(null, 0, 0, 0, {type}); + static _dummy(type: FloatTypedArrayConstructor) { + return new this(new type([]), {type}); } /** * normalize(static version) */ - static _normalize(vec: IVector4, type: TypedArrayConstructor) { + static _normalize(vec: IVector4, type: FloatTypedArrayConstructor) { const length = vec.length(); return this._divide(vec, length, type); } @@ -144,12 +122,16 @@ export class Vector4_ /** * add value(static version) */ - static _add(l_vec: IVector4, r_vec: IVector4, type: TypedArrayConstructor) { + static _add( + l_vec: IVector4, + r_vec: IVector4, + type: FloatTypedArrayConstructor + ) { const x = l_vec._v[0] + r_vec._v[0]; const y = l_vec._v[1] + r_vec._v[1]; const z = l_vec._v[2] + r_vec._v[2]; const w = l_vec._v[3] + r_vec._v[3]; - return new this(x, y, z, w, {type}); + return new this(new type([x, y, z, w]), {type}); } /** @@ -169,13 +151,13 @@ export class Vector4_ static _subtract( l_vec: IVector4, r_vec: IVector4, - type: TypedArrayConstructor + type: FloatTypedArrayConstructor ) { const x = l_vec._v[0] - r_vec._v[0]; const y = l_vec._v[1] - r_vec._v[1]; const z = l_vec._v[2] - r_vec._v[2]; const w = l_vec._v[3] - r_vec._v[3]; - return new this(x, y, z, w, {type}); + return new this(new type([x, y, z, w]), {type}); } /** @@ -192,12 +174,16 @@ export class Vector4_ /** * multiply(static version) */ - static _multiply(vec: IVector4, value: number, type: TypedArrayConstructor) { + static _multiply( + vec: IVector4, + value: number, + type: FloatTypedArrayConstructor + ) { const x = vec._v[0] * value; const y = vec._v[1] * value; const z = vec._v[2] * value; const w = vec._v[3] * value; - return new this(x, y, z, w, {type}); + return new this(new type([x, y, z, w]), {type}); } /** @@ -217,13 +203,13 @@ export class Vector4_ static _multiplyVector( l_vec: IVector4, r_vec: IVector4, - type: TypedArrayConstructor + type: FloatTypedArrayConstructor ) { const x = l_vec._v[0] * r_vec._v[0]; const y = l_vec._v[1] * r_vec._v[1]; const z = l_vec._v[2] * r_vec._v[2]; const w = l_vec._v[3] * r_vec._v[3]; - return new this(x, y, z, w, {type}); + return new this(new type([x, y, z, w]), {type}); } /** @@ -244,7 +230,11 @@ export class Vector4_ /** * divide(static version) */ - static _divide(vec: IVector4, value: number, type: TypedArrayConstructor) { + static _divide( + vec: IVector4, + value: number, + type: FloatTypedArrayConstructor + ) { let x; let y; let z; @@ -261,7 +251,7 @@ export class Vector4_ z = Infinity; w = Infinity; } - return new this(x, y, z, w, {type}); + return new this(new type([x, y, z, w]), {type}); } /** @@ -289,7 +279,7 @@ export class Vector4_ static _divideVector( l_vec: IVector4, r_vec: IVector4, - type: TypedArrayConstructor + type: FloatTypedArrayConstructor ) { let x; let y; @@ -312,7 +302,7 @@ export class Vector4_ z = r_vec._v[2] === 0 ? Infinity : l_vec._v[2] / r_vec._v[2]; w = r_vec._v[3] === 0 ? Infinity : l_vec._v[3] / r_vec._v[3]; } - return new this(x, y, z, w, {type}); + return new this(new type([x, y, z, w]), {type}); } /** @@ -455,29 +445,47 @@ export class Vector4_ clone() { return new (this.constructor as any)( - this._v[0], - this._v[1], - this._v[2], - this._v[3] + new (this._v.constructor as any)([ + this._v[0], + this._v[1], + this._v[2], + this._v[3], + ]) ); } } export default class Vector4 extends Vector4_ { - constructor( - x: - | number - | TypedArray - | IVector2 - | IVector3 - | IVector4 - | Array - | null, - y?: number, - z?: number, - w?: number - ) { - super(x, y!, z!, w!, {type: Float32Array}); + constructor(x: Float32Array) { + super(x, {type: Float32Array}); + } + + static fromCopyArray(array: Array4): Vector4 { + return super._fromCopyArray(array, Float32Array); + } + + static fromCopyArray4(array: Array4): Vector4 { + return super._fromCopyArray4(array, Float32Array); + } + + static fromCopyVector3(vec3: IVector3): Vector4 { + return super._fromCopyVector3(vec3, Float32Array); + } + + static fromCopyVector4(vec4: IVector4): Vector4 { + return super._fromCopyVector4(vec4, Float32Array); + } + + static fromArrayBuffer(arrayBuffer: ArrayBuffer): Vector4 { + return new Vector4(new Float32Array(arrayBuffer)); + } + + static fromFloat32Array(float32Array: Float32Array): Vector4 { + return new Vector4(float32Array); + } + + static fromCopyFloat32Array(float32Array: Float32Array): Vector4 { + return new Vector4(float32Array.slice(0)); } static zero() { @@ -526,20 +534,24 @@ export default class Vector4 extends Vector4_ { } export class Vector4d extends Vector4_ { - constructor( - x: - | number - | TypedArray - | IVector2 - | IVector3 - | IVector4 - | Array - | null, - y?: number, - z?: number, - w?: number - ) { - super(x, y!, z!, w!, {type: Float64Array}); + private constructor(x: Float64Array) { + super(x, {type: Float64Array}); + } + + static fromCopyArray4(array: Array4): Vector4d { + return super._fromCopyArray4(array, Float64Array) as Vector4d; + } + + static fromCopyArray(array: Array4): Vector4d { + return super._fromCopyArray(array, Float64Array) as Vector4d; + } + + static fromArrayBuffer(arrayBuffer: ArrayBuffer): Vector4d { + return new Vector4d(new Float64Array(arrayBuffer)); + } + + static fromFloat64Array(float64Array: Float64Array): Vector4d { + return new Vector4d(float64Array); } static zero() { @@ -588,3 +600,7 @@ export class Vector4d extends Vector4_ { } export type Vector4f = Vector4; + +export const ConstVector4_1_1_1_1 = Vector4.fromCopyArray([1, 1, 1, 1]); +export const ConstVector4_0_0_0_1 = Vector4.fromCopyArray([0, 0, 0, 1]); +export const ConstVector4_0_0_0_0 = Vector4.fromCopyArray([0, 0, 0, 0]); diff --git a/src/foundation/math/raw/raw_extension.test.ts b/src/foundation/math/raw/raw_extension.test.ts new file mode 100644 index 000000000..2ba438973 --- /dev/null +++ b/src/foundation/math/raw/raw_extension.test.ts @@ -0,0 +1,60 @@ +import {add3, add3_offset, add4, add4_offset} from './raw_extension'; + +test('Array extensions', () => { + const a = [0, 0, 0]; + + // add array + const a_add3 = a[add3]([1, 0, 0]); + expect(a.toString()).toEqual([1, 0, 0].toString()); + // return value is the same + expect(a_add3.toString()).toEqual([1, 0, 0].toString()); + + // add Float32Array + a[add3_offset](new Float32Array([0, 1, 0]), 0, 0); + expect(a.toString()).toEqual([1, 1, 0].toString()); + + // add array with offset + const b = [0, 1, 0, 0]; + b[add3_offset]([1, 0, 0, 1], 1, 1); + expect(b.toString()).toEqual([0, 1, 0, 1].toString()); + +}); + +test('Float32Array extensions', () => { + const a = new Float32Array([0, 0, 0]); + + // add Float32Array + const a_add3 = a[add3](new Float32Array([1, 0, 0])); + expect(a.toString()).toEqual([1, 0, 0].toString()); + // return value is the same + expect(a_add3.toString()).toEqual([1, 0, 0].toString()); + + // add array + const a_add3_offset = a[add3_offset]([0, 1, 0], 0, 0); + expect(a_add3_offset.toString()).toEqual([1, 1, 0].toString()); + + // add array with offset + const b = [0, 1, 0, 0]; + b[add3_offset]([1, 0, 0, 1], 1, 1); + expect(b.toString()).toEqual([0, 1, 0, 1].toString()); +}); + +test(`${add4.toString()} operator`, () => { + const a = [0, 0, 0, 0]; + + // add Float32Array + const a_add4 = a[add4]([1, 0, 0, 1]); + expect(a.toString()).toEqual([1, 0, 0, 1].toString()); + // return value is the same + expect(a_add4.toString()).toEqual([1, 0, 0, 1].toString()); +}); + +test(`${add4_offset.toString()} operator`, () => { + const a = [1, 0, 0, 0, 0]; + + // add Float32Array + const ret = a[add4_offset](new Float32Array([1, 0, 0, 0, 1]), 1, 1); + expect(a.toString()).toEqual([1, 0, 0, 0, 1].toString()); + // return value is the same + expect(ret.toString()).toEqual([1, 0, 0, 0, 1].toString()); +}); diff --git a/src/foundation/math/raw/raw_extension.ts b/src/foundation/math/raw/raw_extension.ts new file mode 100644 index 000000000..730f61a4c --- /dev/null +++ b/src/foundation/math/raw/raw_extension.ts @@ -0,0 +1,119 @@ +/* eslint-disable @typescript-eslint/no-empty-interface */ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import type {ArrayType} from '../../../types/CommonTypes'; +export const add2 = Symbol(); +export const add2_offset = Symbol(); +export const add3 = Symbol(); +export const add3_offset = Symbol(); +export const add4 = Symbol(); +export const add4_offset = Symbol(); + +declare global { + interface Extension { + [add2](this: ArrayType, array: ArrayType): ArrayType; + [add2_offset]( + this: ArrayType, + array: ArrayType, + selfOffset: number, + argOffset: number + ): ArrayType; + [add3](this: ArrayType, array: ArrayType): ArrayType; + [add3_offset]( + this: ArrayType, + array: ArrayType, + selfOffset: number, + argOffset: number + ): ArrayType; + [add4](this: ArrayType, array: ArrayType): ArrayType; + [add4_offset]( + this: ArrayType, + array: ArrayType, + selfOffset: number, + argOffset: number + ): ArrayType; + } + + interface Array extends Extension {} + interface ReadonlyArray extends Extension {} + interface Float32Array extends Extension {} +} + +const add2_fn = function (this: ArrayType, array: ArrayType) { + this[0] += array[0]; + this[1] += array[1]; + + return this; +}; + +const add2_offset_fn = function ( + this: ArrayType, + array: ArrayType, + selfOffset: number, + argOffset: number +) { + this[selfOffset] += array[argOffset]; + this[selfOffset + 1] += array[argOffset + 1]; + + return this; +}; + +const add3_fn = function (this: ArrayType, array: ArrayType) { + this[0] += array[0]; + this[1] += array[1]; + this[2] += array[2]; + + return this; +}; + +const add3_offset_fn = function ( + this: ArrayType, + array: ArrayType, + selfOffset: number, + argOffset: number +) { + this[selfOffset] += array[argOffset]; + this[selfOffset + 1] += array[argOffset + 1]; + this[selfOffset + 2] += array[argOffset + 2]; + + return this; +}; + +const add4_fn = function (this: ArrayType, array: ArrayType) { + this[0] += array[0]; + this[1] += array[1]; + this[2] += array[2]; + this[3] += array[3]; + + return this; +}; + +const add4_offset_fn = function ( + this: ArrayType, + array: ArrayType, + selfOffset: number, + argOffset: number +) { + this[selfOffset] += array[argOffset]; + this[selfOffset + 1] += array[argOffset + 1]; + this[selfOffset + 2] += array[argOffset + 2]; + this[selfOffset + 3] += array[argOffset + 3]; + + return this; +}; + +const arrayTypes = [Array, Float32Array, Float64Array]; +const operators = [add2, add2_offset, add3, add3_offset, add4, add4_offset]; +const functions = [ + add2_fn, + add2_offset_fn, + add3_fn, + add3_offset_fn, + add4_fn, + add4_offset_fn, +]; + +for (let i = 0; i < arrayTypes.length; i++) { + for (let j = 0; j < operators.length; j++) { + arrayTypes[i].prototype[operators[j] as unknown as number] = functions[j]; + } +} diff --git a/src/foundation/memory/Accessor.ts b/src/foundation/memory/Accessor.ts index d20152664..4e9a7f8cb 100644 --- a/src/foundation/memory/Accessor.ts +++ b/src/foundation/memory/Accessor.ts @@ -17,9 +17,9 @@ import { Byte, Index, Count, - TypedArrayConstructor, TypedArray, Size, + TypedArrayConstructor, } from '../../types/CommonTypes'; type DataViewGetter = (byteOffset: Byte, littleEndian?: boolean) => number; @@ -39,21 +39,21 @@ export default class Accessor { private __dataView?: DataView; private __typedArray?: TypedArray; private __takenCount: Count = 0; - private __byteStride: Byte = 0; + private __byteStride: Byte = 0; // Accessor has the byteStride. BufferView doesn't. For supporting glTF1, not only glTF2 private __typedArrayClass?: TypedArrayConstructor; private __dataViewGetter: DataViewGetter; private __dataViewSetter: DataViewSetter; - private __max: MutableVector4 = new MutableVector4( + private __max: MutableVector4 = MutableVector4.fromCopyArray([ -Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE, - -Number.MAX_VALUE + -Number.MAX_VALUE] ); - private __min: MutableVector4 = new MutableVector4( + private __min: MutableVector4 = MutableVector4.fromCopyArray([ Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE, - Number.MAX_VALUE + Number.MAX_VALUE] ); private __arrayLength = 1; private __normalized = false; @@ -538,12 +538,12 @@ export default class Accessor { index = indicesAccessor.getScalar(i, {}); } const byteSize = this.componentSizeInBytes; - return new Vector4( + return Vector4.fromCopyArray([ this.__dataViewGetter(this.__byteStride * index, endian), this.__dataViewGetter(this.__byteStride * index + 1 * byteSize, endian), this.__dataViewGetter(this.__byteStride * index + 2 * byteSize, endian), this.__dataViewGetter(this.__byteStride * index + 3 * byteSize, endian) - ); + ]); } getMat3( diff --git a/src/foundation/renderer/RenderPass.ts b/src/foundation/renderer/RenderPass.ts index 0575913b7..812f9c35f 100644 --- a/src/foundation/renderer/RenderPass.ts +++ b/src/foundation/renderer/RenderPass.ts @@ -13,6 +13,7 @@ import ModuleManager from '../system/ModuleManager'; import WebGLResourceRepository from '../../webgl/WebGLResourceRepository'; import Primitive from '../geometry/Primitive'; import MutableVector4 from '../math/MutableVector4'; +import { IVector4 } from '../math/IVector'; /** * A render pass is a collection of the resources which is used in rendering process. @@ -29,7 +30,7 @@ export default class RenderPass extends RnObject { public toClearDepthBuffer = true; public toClearStencilBuffer = false; public isDepthTest = true; - public clearColor = new Vector4(1, 1, 1, 1); + public clearColor = Vector4.fromCopyArray([1, 1, 1, 1]); public clearDepth = 1; public clearStencil = 0; public cameraComponent?: CameraComponent; @@ -172,7 +173,7 @@ export default class RenderPass extends RnObject { this.__frameBuffer = framebuffer; if (framebuffer != null) { this.setViewport( - new Vector4(0, 0, framebuffer.width, framebuffer.height) + Vector4.fromCopyArray([0, 0, framebuffer.width, framebuffer.height]) ); } else { this.__viewport = undefined; @@ -191,11 +192,16 @@ export default class RenderPass extends RnObject { * Sets the viewport of this render pass. * @param vec A Vector4 (Origin of coordinatesX, origin of coordinatesY, width, height). */ - setViewport(vec: Vector4) { + setViewport(vec: IVector4) { if (this.__viewport != null) { this.__viewport.copyComponents(vec); } else { - this.__viewport = new MutableVector4(vec); + this.__viewport = MutableVector4.fromCopyArray([ + vec.x, + vec.y, + vec.z, + vec.w, + ]); } } @@ -308,9 +314,8 @@ export default class RenderPass extends RnObject { const moduleManager = ModuleManager.getInstance(); const moduleName = 'webgl'; const webglModule = moduleManager.getModule(moduleName)! as any; - this.__webglRenderingStrategy = webglModule.getRenderingStrategy( - processApproach - ); + this.__webglRenderingStrategy = + webglModule.getRenderingStrategy(processApproach); } private __getMaterialOf(primitive: Primitive) { diff --git a/src/foundation/textures/AbstractTexture.ts b/src/foundation/textures/AbstractTexture.ts index 3e5ab880d..9c6fc2bb6 100644 --- a/src/foundation/textures/AbstractTexture.ts +++ b/src/foundation/textures/AbstractTexture.ts @@ -22,6 +22,7 @@ import MutableVector3 from '../math/MutableVector3'; import MutableVector4 from '../math/MutableVector4'; import Vector3 from '../math/Vector3'; import Vector4 from '../math/Vector4'; +import { Is } from '../misc/Is'; export default abstract class AbstractTexture extends RnObject { protected __width: Size = 0; @@ -93,17 +94,19 @@ export default abstract class AbstractTexture extends RnObject { } get htmlCanvasElement() { - if (this.__htmlCanvasElement == null) { - const canvas = document.createElement('canvas'); - const ctx = canvas.getContext('2d')!; + const canvas = document.createElement('canvas'); + const ctx = canvas?.getContext('2d'); + if (Is.not.exist(this.__htmlCanvasElement)) { + this.__htmlCanvasElement = canvas; + } + if (Is.exist(ctx) && Is.exist(this.__htmlImageElement)) { ctx.drawImage( - this.__htmlImageElement!, + this.__htmlImageElement, 0, 0, - this.__htmlImageElement!.width, - this.__htmlImageElement!.height + this.__htmlImageElement.width, + this.__htmlImageElement.height ); - this.__htmlCanvasElement = canvas; } return this.__htmlCanvasElement; } diff --git a/src/foundation/textures/RenderTargetTexture.ts b/src/foundation/textures/RenderTargetTexture.ts index 1868bc8d1..d62b3095c 100644 --- a/src/foundation/textures/RenderTargetTexture.ts +++ b/src/foundation/textures/RenderTargetTexture.ts @@ -149,11 +149,11 @@ export default class RenderTargetTexture byteArray = this.getTexturePixelData(); } - const color = new Vector4( + const color = Vector4.fromCopyArray([ byteArray[(y * this.width + x) * 4 + 0], byteArray[(y * this.width + x) * 4 + 1], byteArray[(y * this.width + x) * 4 + 2], - byteArray[(y * this.width + x) * 4 + 3] + byteArray[(y * this.width + x) * 4 + 3]] ); return color; } diff --git a/src/foundation/textures/TextureDataFloat.ts b/src/foundation/textures/TextureDataFloat.ts index ad007641f..e1e18434d 100644 --- a/src/foundation/textures/TextureDataFloat.ts +++ b/src/foundation/textures/TextureDataFloat.ts @@ -1,4 +1,4 @@ -import {Size, Index, TypedArrayConstructor} from '../../types/CommonTypes'; +import {Size, Index, FloatTypedArrayConstructor, TypedArrayConstructor} from '../../types/CommonTypes'; export default class TextureDataFloat { private __data: Float32Array; diff --git a/src/types/CommonTypes.ts b/src/types/CommonTypes.ts index d60909565..98bd6e64b 100644 --- a/src/types/CommonTypes.ts +++ b/src/types/CommonTypes.ts @@ -8,7 +8,8 @@ export type TypedArray = | Uint32Array | Float32Array | Float64Array; -export type ArrayType = TypedArray | Array | Array; +export type FloatTypedArray = Float32Array | Float64Array; +export type ArrayType = TypedArray | Array; export type TypedArrayConstructor = | Int8ArrayConstructor @@ -21,6 +22,12 @@ export type TypedArrayConstructor = | Float32ArrayConstructor | Float64ArrayConstructor; +export type FloatTypedArrayConstructor = + | Float32ArrayConstructor + | Float64ArrayConstructor; + +export type Array4 = [T, T, T, T]; +export type Array3 = [T, T, T]; export type Index = number; export type IndexOf16Bytes = number; export type IndexOf4Bytes = number; diff --git a/src/webgl/WebGLContextWrapper.ts b/src/webgl/WebGLContextWrapper.ts index 7b9bbcaea..2bb914ee7 100644 --- a/src/webgl/WebGLContextWrapper.ts +++ b/src/webgl/WebGLContextWrapper.ts @@ -175,20 +175,20 @@ export default class WebGLContextWrapper { } get viewport() { - return new Vector4( + return Vector4.fromCopyArray([ this.__viewport_left, this.__viewport_top, this.__viewport_width, - this.__viewport_height + this.__viewport_height] ); } get defaultViewport() { - return new Vector4( + return Vector4.fromCopyArray([ this.__default_viewport_left, this.__default_viewport_top, this.__default_viewport_width, - this.__default_viewport_height + this.__default_viewport_height] ); } diff --git a/src/webgl/WebGLResourceRepository.ts b/src/webgl/WebGLResourceRepository.ts index 655ab4e6a..47a22b585 100644 --- a/src/webgl/WebGLResourceRepository.ts +++ b/src/webgl/WebGLResourceRepository.ts @@ -2499,7 +2499,7 @@ vec4 fetchVec4FromVec4Block(int vec4Idx) { this.__glw!.height = height; this.__glw!.canvas.width = width; this.__glw!.canvas.height = height; - this.__glw!.setViewportAsVector4(new Vector4(0, 0, width, height)); + this.__glw!.setViewportAsVector4(Vector4.fromCopyArray([0, 0, width, height])); } switchDepthTest(flag: boolean) { diff --git a/src/xr/WebVRSystem.ts b/src/xr/WebVRSystem.ts index 48a89ec52..935ca015d 100644 --- a/src/xr/WebVRSystem.ts +++ b/src/xr/WebVRSystem.ts @@ -288,21 +288,21 @@ export default class WebVRSystem { } getLeftViewport(originalViewport: Vector4) { - return new Vector4( + return Vector4.fromCopyArray([ 0, 0, this.__canvasWidthForVR / 2, - this.__canvasHeightForVR + this.__canvasHeightForVR] ); } getRightViewport(originalViewport: Vector4) { - return new Vector4( + return Vector4.fromCopyArray([ this.__canvasWidthForVR / 2, 0, this.__canvasWidthForVR / 2, - this.__canvasHeightForVR - ); + this.__canvasHeightForVR, + ]); } getViewMatrixAt(index: Index) { diff --git a/src/xr/WebXRSystem.ts b/src/xr/WebXRSystem.ts index 5e63c0053..64f8edce7 100644 --- a/src/xr/WebXRSystem.ts +++ b/src/xr/WebXRSystem.ts @@ -340,12 +340,12 @@ export default class WebXRSystem { * @returns The viewport vector of left eye */ _getLeftViewport() { - return new Vector4( + return Vector4.fromCopyArray([ 0, 0, this.__canvasWidthForVR / 2, - this.__canvasHeightForVR - ); + this.__canvasHeightForVR, + ]); } /** @@ -354,12 +354,12 @@ export default class WebXRSystem { * @returns The viewport vector of right eye */ _getRightViewport() { - return new Vector4( + return Vector4.fromCopyArray([ this.__canvasWidthForVR / 2, 0, this.__canvasWidthForVR / 2, - this.__canvasHeightForVR - ); + this.__canvasHeightForVR, + ]); } _setValuesToGlobalDataRepository() {